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

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.utils.BusinessException;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sunshine.settleSet.dao.RewardsPunishWayDao;
import com.sifang.modules.sunshine.settleSet.dao.RewardsPunishWayMsgDao;
import com.sifang.modules.sunshine.settleSet.entity.CrmRewardsPunishWay;
import com.sifang.modules.sunshine.settleSet.entity.CrmRewardsPunishWayMsg;
import com.sifang.modules.sunshine.settleSet.vo.RewardWayVo;
import com.sifang.modules.sunshine.settleSet.vo.SaveRewardVo;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.common.service.BaseService;
import com.sifang.modules.sys.common.utils.DateUtils;
import com.sifang.modules.sys.util.LayuiPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class RewardsPunishWayService extends BaseService {

    @Autowired
    private RewardsPunishWayMsgDao rewardsPunishWayMsgDao;

    @Autowired
    private RewardsPunishWayDao rewardsPunishWayDao;

    public CrmRewardsPunishWay getById(String id){
           return rewardsPunishWayDao.get(id);
    }

    public void rewardsPunishWayListDate(Map<String,String> map,LayuiPage<Record> page){
       rewardsPunishWayDao.rewardsPunishWayListDate(map,page);
    }

    public List<Record> punishWayListDate(){
        List<Record> list=rewardsPunishWayDao.punishWayListDate();
        return list;
    }

    //保存惩罚方案
    @Transactional(rollbackFor = Exception.class)
    public void savePunishWay(SaveRewardVo vo){
        User user= UserUtils.getUser();
        CrmRewardsPunishWay punishWay=rewardsPunishWayDao.get(vo.getId());
        punishWay.setLowData(vo.getLowData());
        punishWay.setHighData(vo.getHighData());
        punishWay.setLowTime(vo.getLowTime());
        punishWay.setHighTime(vo.getHighTime());
        punishWay.setRatioData(vo.getRatioData());
        punishWay.setUpdateTime(new Date());
        punishWay.setUpdateBy(user.getUserName());
        rewardsPunishWayDao.save(punishWay);
        rewardsPunishWayMsgDao.saveMsg(punishWay,user);
    }

    //保存奖励方案
    @Transactional(rollbackFor = Exception.class)
    public void saveRewardsWay(RewardWayVo vo){
        User user= UserUtils.getUser();
        CrmRewardsPunishWay punishWay;
        Date date=new Date();
        if(StringUtil.isBlank(vo.getTraderIds()))vo.setTraderIds("-1");
        String str=rewardsPunishWayDao.checkIfHave(vo.getId(),vo.getTraderIds());
        if(StringUtil.isNotBlank(str)){
            throw new BusinessException(str);
        }
        if(StringUtil.isBlank(vo.getId())){
           punishWay=new CrmRewardsPunishWay();
           punishWay.setType("1");
           punishWay.setNumber(vo.getNumber());
           Long maxVersion=rewardsPunishWayDao.getMaxVersion();
           punishWay.setVersion(maxVersion+1);
           punishWay.setCreateBy(user.getId());
           punishWay.setCreateTime(date);
        }else{
            punishWay=rewardsPunishWayDao.get(vo.getId());
        }
        punishWay.setName(vo.getName());
        punishWay.setLowData(vo.getLowData());
        punishWay.setHighData(vo.getHighData());
        punishWay.setMaxHighData(vo.getMaxHighData());
        punishWay.setRatioData(vo.getRatioData());
        punishWay.setMaxHighDataRadio(vo.getMaxHighDataRadio());
        /*int cj=vo.getLowTime().compareTo(date);
        if(cj>0){  //LowTime晚于data
            punishWay.setStatus("0");
        }else if(cj==0){
            punishWay.setStatus("1");
        }else if(cj<0){ //LowTime早于date
            if(vo.getHighTime().compareTo(date)>=0){
                punishWay.setStatus("1");
            }else{
                punishWay.setStatus("2");
            }
        }*/
        punishWay.setStatus(vo.getStatus());
        punishWay.setLowTime(vo.getLowTime());
        punishWay.setHighTime(vo.getHighTime());
        punishWay.setSettlementNodes("每月1号");
        if(StringUtil.isNotBlank(vo.getTraderIds())){
            punishWay.setTraderIds(vo.getTraderIds());
        }else {
            punishWay.setTraderIds("-1");
        }
        punishWay.setUpdateBy(user.getId());
        punishWay.setUpdateTime(date);
        rewardsPunishWayDao.save(punishWay);
        rewardsPunishWayMsgDao.saveMsg(punishWay,user);
    }

    public List<Record> getChangeRecord(String id){
       return rewardsPunishWayMsgDao.getChangeRecord(id);
    }


    //获取奖励政策的方案编号
    public String createNumber(){
        Long maxVersion=rewardsPunishWayDao.getMaxVersion();
        String number=addZero(maxVersion+1);
        return number;
    }


    public String addZero(Long version){
        String s = String.format("%03d", version);
        return "JL"+s;
    }

}
