package com.sifang.modules.sunshine.settleSet.dao;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sunshine.settleSet.entity.CrmRewardsPunishWay;
import com.sifang.modules.sys.common.persistence.BaseDao;
import com.sifang.modules.sys.common.persistence.Parameter;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.util.SqlBuilder;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Repository
public class RewardsPunishWayDao extends BaseDao<CrmRewardsPunishWay> {


    public void rewardsPunishWayListDate(Map<String,String> map, LayuiPage page){
        SqlBuilder sb=new SqlBuilder();
        sb.line(" SELECT w.*,DATE_FORMAT(create_time,'%Y-%m-%d') as createTime FROM `crm_rewards_punish_way` w ");
        sb.line(" where w.type= '1' ");
        getCondition(map,sb);
        sb.line("  ORDER BY w.create_time desc ");
        if (page != null) {
            sb.line(" limit " + page.getLimit() + " offset " + (page.getPage() - 1) * page.getLimit());
            Long count = getCount(map);
            page.setCount(count);
        }
        List<Record> list = Db.find(sb.build(),sb.params());
        page.setData(list);
    }

    public Long getCount(Map<String,String> map){
        SqlBuilder sb=new SqlBuilder();
        sb.line(" SELECT count(w.id) FROM `crm_rewards_punish_way` w ");
        sb.line(" where w.type= '1' ");
        getCondition(map,sb);
        return Db.queryLong(sb.build(),sb.params());
    }

    public void getCondition(Map<String,String> map,SqlBuilder sb){
        if(StringUtil.isNotBlank(map.get("status"))){
            sb.line(" and w.status= ? ",map.get("status"));
        }
        if(StringUtil.isNotBlank(map.get("traderId"))){
            sb.line(" and FIND_IN_SET(?,w.trader_ids) ",map.get("traderId"));
        }
        if(StringUtil.isNotBlank(map.get("name"))){
            sb.like(" and w.name like ?",map.get("name"));
        }
    }

    public List<Record> punishWayListDate(){
        StringBuffer sb=new StringBuffer();
        sb.append(" SELECT w.*,DATE_FORMAT(create_time,'%Y-%m-%d') as createTime FROM `crm_rewards_punish_way` w ");
        sb.append(" where w.type in('2','3') ");
        sb.append("  ORDER BY w.type asc ");
        return Db.find(sb.toString());
    }

    public CrmRewardsPunishWay getRewardsPunishWay(String type){
        return getByHql(" from CrmRewardsPunishWay  where type=:p1 and status='1' ",new Parameter(type));
    }

    //获取奖励政策
    public Record getRewardsWayByTraderId(String traderId){
        StringBuffer sb=new StringBuffer();
        sb.append(" SELECT w.id,w.number,w.low_data,w.high_data,w.max_high_data,w.ratio_data ");
        sb.append(" ,w.max_high_data_radio,DATE_FORMAT(w.low_time,'%Y-%m-%d') AS lowTime,DATE_FORMAT(w.high_time,'%Y-%m-%d') AS highTime ");
        sb.append(" FROM `crm_rewards_punish_way` w WHERE w.type='1' AND w.status='1' ");
        sb.append(" AND FIND_IN_SET(?,w.trader_ids) LIMIT 1 ");
        Record record=Db.findFirst(sb.toString(),traderId);
        return record;
    }

    //获取通用的奖励政策
    public Record getRewardsWayTy(){
        StringBuffer sb=new StringBuffer();
        sb.append(" SELECT w.id,w.number,w.low_data,w.high_data,w.max_high_data,w.ratio_data ");
        sb.append(" ,w.max_high_data_radio,DATE_FORMAT(w.low_time,'%Y-%m-%d') AS lowTime,DATE_FORMAT(w.high_time,'%Y-%m-%d') AS highTime ");
        sb.append(" FROM `crm_rewards_punish_way` w WHERE w.type='1' AND w.status='1' ");
        sb.append(" AND w.trader_ids='-1' LIMIT 1 ");
        Record record=Db.findFirst(sb.toString());
        return record;
    }

    public Long getMaxVersion(){
        return Db.queryLong(" SELECT IFNULL(MAX(w.version),0) FROM `crm_rewards_punish_way` w WHERE w.type='1' ");
    }

    public String checkIfHave(String id,String traderIds){
        String str="";
        if(StringUtil.isBlank(id)){
            id="-1";
        }
        //先看通用的
        if("-1".equals(traderIds)){
            long count= Db.queryLong("  SELECT COUNT(w.id) FROM `crm_rewards_punish_way` w WHERE w.type='1' AND w.status='1' AND w.trader_ids='-1' and id<>? ",id);
            if(count>0){
               str="通用的奖励方案已存在，请勿重复添加！";
                return str;
            }
        }else{
            //一个一个判断是否已存在奖励方案
            int i=1;
           for(String traderId:traderIds.split(",")){
               long count1=Db.queryLong(" SELECT COUNT(w.id) FROM `crm_rewards_punish_way` w WHERE w.type='1' AND w.status='1' AND  FIND_IN_SET(?,w.trader_ids) AND id<>? ",traderId,id);
               if(count1>0){
                   str="第"+i+"个代理商的奖励方案已存在，请勿重复添加！";
                   return str;
               }
               i++;
           }
        }
        return str;
    }

}
