package com.hzs.system.config.controller.manage;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.enums.*;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.domain.system.config.*;
import com.hzs.common.domain.system.config.ext.BdBonusDifferenceExt;
import com.hzs.common.domain.system.config.ext.BdBonusDirectLoopExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.config.service.*;
import com.hzs.system.config.vo.BdBonusDirectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 直推循环配置 前端控制器
 *
 * @author zhangjing
 * @since 2022-11-03
 */
@RestController
@RequestMapping("/manage/bd-bonus-direct-loop")
public class BdBonusDirectLoopController extends BaseController {

    @Autowired
    private IBdBonusDirectLoopService bonusDirectLoopService;
    @Autowired
    private IBdLoopDetailsService loopDetailsService;

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

    /**
     * @description: 查询直推循环配置列表
     * @author: zhang jing
     * @date: 2022/8/30 10:20
     * @param: [bdBonusType]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @Log(module = EOperationModule.BD_BONUS_DIRECT_LOOP, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public AjaxResult list(BdBonusDirectLoopExt bonusDirectLoopExt) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        BdBonusDirectVo bbdVo = new BdBonusDirectVo();
        //直推循环配置集合
        bonusDirectLoopExt.setPkCountry(pkCountry);
        QueryWrapper<BdBonusDirectLoop> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PK_COUNTRY", pkCountry);
        List<BdBonusDirectLoop> bonusDirectLoop = bonusDirectLoopService.list(queryWrapper);
        List<BdBonusDirectLoopExt> bonusDirectLoopExtList = new ArrayList<>();
        bonusDirectLoop.forEach(item -> bonusDirectLoopExtList.add(BeanUtil.copyProperties(item, BdBonusDirectLoopExt.class)));
        if (CollectionUtil.isNotEmpty(bonusDirectLoopExtList)) {
            for (BdBonusDirectLoopExt bbdExt : bonusDirectLoopExtList) {
                QueryWrapper<BdLoopDetails> queryWrapperLDetails = new QueryWrapper<>();
                queryWrapperLDetails.eq("PK_BONUS_DIRECT", bbdExt.getPkId());
                List<BdLoopDetails> lodList = loopDetailsService.list(queryWrapperLDetails);
                List<Integer> loopDetailsList = new ArrayList<>();
                if (CollectionUtil.isNotEmpty(lodList)) {
                    for (BdLoopDetails bld : lodList) {
                        loopDetailsList.add(bld.getPkGrade());
                    }
                    bbdExt.setLoopDetailsList(loopDetailsList);
                } else {
                    loopDetailsList = new ArrayList<>();
                    bbdExt.setLoopDetailsList(loopDetailsList);
                }
            }
        }
        bbdVo.setBonusDirectLoopExtsList(bonusDirectLoopExtList);
        //直推奖励配置集合
        QueryWrapper<BdBonusDirect> bbdiQueryWrapper = new QueryWrapper<>();
        bbdiQueryWrapper.eq("PK_COUNTRY", pkCountry);
        bbdiQueryWrapper.orderByAsc("CREATION_TIME");
        List<BdBonusDirect> bonusDirectList = bonusDirectService.list(bbdiQueryWrapper);
        if (CollectionUtil.isNotEmpty(bonusDirectList)) {
            bbdVo.setBonusDirectList(bonusDirectList);
        } else {
            bonusDirectList = new ArrayList<>();
            bbdVo.setBonusDirectList(bonusDirectList);
        }
        //嗨粉直推奖励配置
        QueryWrapper<BdBonusHiPink> bbhpQueryWrapper = new QueryWrapper<>();
        bbhpQueryWrapper.eq("PK_COUNTRY", pkCountry);
        bbhpQueryWrapper.orderByAsc("RANGE_TYPE", "CAL_TYPE", "BONUS_VALUE", "CREATION_TIME");
        List<BdBonusHiPink> bonusHiPinkList = bonusHiPinkService.list(bbhpQueryWrapper);
        if (CollectionUtil.isNotEmpty(bonusHiPinkList)) {
            bbdVo.setBonusHiPinkList(bonusHiPinkList);
        } else {
            bonusHiPinkList = new ArrayList<>();
            bbdVo.setBonusHiPinkList(bonusHiPinkList);
        }
        //一次性补差升级配置
        QueryWrapper<BdBonusDifference> queryWrapperDiff = new QueryWrapper<>();
        queryWrapperDiff.eq("PK_COUNTRY", SecurityUtils.getPkCountry());
        queryWrapperDiff.orderByAsc("CREATION_TIME");
        List<BdBonusDifference> list = bdBonusDifferenceService.list(queryWrapperDiff);
        BdBonusDifferenceExt bonusDifferenceExt = new BdBonusDifferenceExt();
        bonusDifferenceExt.setBonusDifferencesList(list);
        //复购直推配置
        QueryWrapper<BdRepoDirectPush> queryRepoDirectPush = new QueryWrapper<>();
        queryRepoDirectPush.eq("PK_COUNTRY", SecurityUtils.getPkCountry());
        queryRepoDirectPush.orderByAsc("ALGEBRA");
        List<BdRepoDirectPush> drpList = iBdRepoDirectPushService.list(queryRepoDirectPush);
        if (CollectionUtil.isEmpty(drpList)) {
            for (EAlgebra value : EAlgebra.values()) {
                BdRepoDirectPush rd = new BdRepoDirectPush();
                rd.setAlgebra(value.getValue());
                drpList.add(rd);
            }
        }
        bbdVo.setRepoDirectPushList(drpList);
        bbdVo.setBonusDifferenceExt(bonusDifferenceExt);
        return AjaxResult.success(bbdVo);
    }


    /**
     * @description: 新增一次性补差升级配置配置
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [bdBonusType]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.BD_BONUS_DIRECT_LOOP, method = EOperationMethod.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody BdBonusDirectVo bonusDirectVo) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        Long userId = SecurityUtils.getUserId();
        Date date = DateUtils.currentDate();
        bonusDirectVo.setPkCreator(userId);
        bonusDirectVo.setCreationTime(date);
        /**
         * 复购直推配置集合
         */
        List<BdRepoDirectPush> repoDirectPushList = bonusDirectVo.getRepoDirectPushList();
        String addAttribute = "0";
        if (CollectionUtil.isNotEmpty(repoDirectPushList)) {
            for (BdRepoDirectPush rd : repoDirectPushList) {
                if (null == rd.getPushValue()) {
                    addAttribute = "1";
                    break;
                }
                rd.setPkCountry(pkCountry);
                rd.setPkCreator(userId);
                rd.setCreationTime(date);
            }
        }
        return AjaxResult.success(bonusDirectLoopService.saveBonusDirectVo(bonusDirectVo, pkCountry, addAttribute));
    }

}
