package com.hzs.system.config.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.enums.EDelFlag;
import com.hzs.common.core.enums.ERangeType;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.domain.system.config.*;
import com.hzs.common.domain.system.config.ext.BdBonusDirectLoopExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.config.mapper.BdBonusDirectLoopMapper;
import com.hzs.system.config.service.*;
import com.hzs.system.config.vo.BdBonusDirectVo;
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;


/**
 * <p>
 * 直推循环配置 服务实现类
 * </p>
 *
 * @author zhangjing
 * @since 2022-11-03
 */
@Service
public class BdBonusDirectLoopServiceImpl extends ServiceImpl<BdBonusDirectLoopMapper, BdBonusDirectLoop> implements IBdBonusDirectLoopService {

    @Autowired
    private IBdLoopDetailsService bdLoopDetailsService;
    @Autowired
    private IBdBonusDirectService bonusDirectService;
    @Autowired
    private IBdBonusHiPinkService bonusHiPinkService;
    @Autowired
    private IBdBonusDifferenceService bdBonusDifferenceService;
    @Autowired
    private IBdRepoDirectPushService iBdRepoDirectPushService;


    /**
     * @description: 新增直推奖励循环配置、直推奖励配置、嗨粉直推配置奖励、一次性补差升级配置
     * @author: zhang jing
     * @date: 2022/11/3 19:17
     * @param: [bonusDirectLoopExtsList]
     * @return: boolean
     **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveBonusDirectVo(BdBonusDirectVo bonusDirectVo, Integer pkCountry,String addAttribute) {
        for (BdBonusDirectLoopExt bdl : bonusDirectVo.getBonusDirectLoopExtsList()) {
            if (StringUtils.isNotNull(bdl.getPkId())) {
                baseMapper.updateById(bdl);
                UpdateWrapper<BdLoopDetails> updateWrapperLod = new UpdateWrapper();
                updateWrapperLod.set("DEL_FLAG", EDelFlag.DELETE.getValue());
                updateWrapperLod.eq("PK_BONUS_DIRECT", bdl.getPkId());
                updateWrapperLod.eq("PK_COUNTRY", pkCountry);
                bdLoopDetailsService.update(updateWrapperLod);
                for (Integer bld : bdl.getLoopDetailsList()) {
                    if (StringUtils.isNotNull(bld)) {
                        BdLoopDetails loopDetails = new BdLoopDetails();
                        loopDetails.setPkBonusDirect(bdl.getPkId());
                        loopDetails.setPkGrade(bld);
                        loopDetails.setPkCountry(pkCountry);
                        loopDetails.setPkCreator(bonusDirectVo.getPkCreator());
                        loopDetails.setCreationTime(new Date());
                        if(null!=bld&&null!=loopDetails.getPkBonusDirect()){
                            bdLoopDetailsService.save(loopDetails);
                        }
                    }
                }
            } else {
                if(null!=bdl.getFirstOrder()&&
                null!=bdl.getSecondOrder()&&
                null!=bdl.getThirdOrder()&&
                null!=bdl.getTakeValueType()){
                    bdl.setPkCountry(pkCountry);
                    bdl.setPkCreator(bonusDirectVo.getPkCreator());
                    bdl.setCreationTime(bonusDirectVo.getCreationTime());
                    baseMapper.insert(bdl);
                }
                for (Integer bld : bdl.getLoopDetailsList()) {
                    BdLoopDetails loopDetails = new BdLoopDetails();
                    loopDetails.setPkBonusDirect(bdl.getPkId());
                    loopDetails.setPkGrade(bld);
                    loopDetails.setPkCountry(pkCountry);
                    loopDetails.setPkCreator(bonusDirectVo.getPkCreator());
                    loopDetails.setCreationTime(new Date());
                    if(null!=bld&&null!=loopDetails.getPkBonusDirect()){
                        bdLoopDetailsService.save(loopDetails);
                    }
                }
            }
        }
        UpdateWrapper<BdBonusDirect> updateWrapperbbd = new UpdateWrapper();
        updateWrapperbbd.set("DEL_FLAG", EDelFlag.DELETE.getValue());
        updateWrapperbbd.eq("PK_COUNTRY", pkCountry);
        updateWrapperbbd.eq("DEL_FLAG", EDelFlag.UN_DELETE.getValue());
        bonusDirectService.update(updateWrapperbbd);
        for (BdBonusDirect bbd : bonusDirectVo.getBonusDirectList()) {
            if(null!=bbd.getCalType()&&null!=bbd.getPkLevel()&&
            null!=bbd.getBonusValue()){
                bbd.setRangeType(ERangeType.GRADE.getValue());
                bbd.setPkCountry(pkCountry);
                bbd.setCreationTime(bonusDirectVo.getCreationTime());
                bbd.setPkCreator(bonusDirectVo.getPkCreator());
                bonusDirectService.save(bbd);
            }
        }
        UpdateWrapper<BdBonusHiPink> updateWrapperbbhp = new UpdateWrapper();
        updateWrapperbbhp.set("DEL_FLAG", EDelFlag.DELETE.getValue());
        updateWrapperbbhp.eq("PK_COUNTRY", pkCountry);
        updateWrapperbbhp.eq("DEL_FLAG", EDelFlag.UN_DELETE.getValue());
        bonusHiPinkService.update(updateWrapperbbhp);
        for (BdBonusHiPink bbhp : bonusDirectVo.getBonusHiPinkList()) {
            if(null!=bbhp.getRangeType()&&
            null!=bbhp.getPkLevel()&&null!=bbhp.getBonusValue()){
                bbhp.setPkCountry(pkCountry);
                bbhp.setCreationTime(bonusDirectVo.getCreationTime());
                bbhp.setPkCreator(bonusDirectVo.getPkCreator());
                bonusHiPinkService.save(bbhp);
            }
        }

        UpdateWrapper<BdBonusDifference> updateWrapperbbf = new UpdateWrapper();
        updateWrapperbbf.set("DEL_FLAG", EDelFlag.DELETE.getValue());
        updateWrapperbbf.eq("PK_COUNTRY", pkCountry);
        updateWrapperbbf.eq("DEL_FLAG", EDelFlag.UN_DELETE.getValue());
        bdBonusDifferenceService.update(updateWrapperbbf);
        for (BdBonusDifference bbd : bonusDirectVo.getBonusDifferenceExt().getBonusDifferencesList()) {
            if(null!=bbd.getRewardValue()&&
            null!=bbd.getUpgradeType()){
                bbd.setPkCountry(pkCountry);
                bbd.setCreationTime(bonusDirectVo.getCreationTime());
                bbd.setPkCreator(bonusDirectVo.getPkCreator());
                bdBonusDifferenceService.save(bbd);
            }
        }
        //复购直推配置(不是所有国家有该配置)
        if(addAttribute.equals("0")){
            UpdateWrapper<BdRepoDirectPush> updateWrapperrd = new UpdateWrapper();
            updateWrapperrd.set("DEL_FLAG", EDelFlag.DELETE.getValue());
            updateWrapperrd.eq("PK_COUNTRY", pkCountry);
            updateWrapperrd.eq("DEL_FLAG", EDelFlag.UN_DELETE.getValue());
            iBdRepoDirectPushService.update(updateWrapperrd);
            for(BdRepoDirectPush rd:bonusDirectVo.getRepoDirectPushList()){
                iBdRepoDirectPushService.save(rd);
            }
        }
        return true;
    }

    @Override
    public List<BdBonusDirectLoopExt> queryBonusDirectLoop() {
        return baseMapper.queryBonusDirectLoop();
    }
}
