package com.hzs.bonus.bonus.controller.api;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.hzs.bonus.achieve.service.ICuMemberAchieveService;
import com.hzs.bonus.bonus.param.BonusParam;
import com.hzs.bonus.bonus.service.ICuMemberBonusService;
import com.hzs.bonus.bonus.vo.*;
import com.hzs.common.core.enums.EGrantStatus;
import com.hzs.common.core.utils.ComputeUtil;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.domain.member.ext.CuMemberBonusExt;
import com.hzs.common.domain.member.ext.CuMemberSettleCountryExt;
import com.hzs.common.security.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @Author: sui q
 * @Time: 2023/1/13 9:39
 * @Classname: ApiCuMemberBonusController
 * @PackageName: com.hzs.member.bonus.controller.api
 */
@RestController
@RequestMapping("/api/bonus")
@Slf4j
public class ApiCuMemberBonusController extends BaseController {

    private ICuMemberBonusService cuMemberBonusService;

    private ICuMemberAchieveService cuMemberAchieveService;

    @Autowired
    public void setCuMemberAchieveService(ICuMemberAchieveService cuMemberAchieveService) {
        this.cuMemberAchieveService = cuMemberAchieveService;
    }

    @Autowired
    public void setCuMemberBonusService(ICuMemberBonusService cuMemberBonusService) {
        this.cuMemberBonusService = cuMemberBonusService;
    }

    /**
     * 查询会员某个时间段内的奖金，默认查询15天内,查询直推
     */
    @PostMapping("/query-bonus-first")
    public TableDataInfo listFirstRepurchaseBonus(@RequestBody BonusParam bonusParam) {
        startPage();
        setBonusParam(bonusParam);
        List<CuMemberBonusExt> cuMemberBonusList = cuMemberBonusService.queryMemberBonusVoByCondition(bonusParam);
        List<MemberBonusFirstPurchaseVO> firstPurchaseVOList = new ArrayList<>();
        cuMemberBonusList.forEach(cuMemberBonus -> {
            MemberBonusFirstPurchaseVO memberBonusFirstPurchaseVO = BeanUtil.copyProperties(cuMemberBonus, MemberBonusFirstPurchaseVO.class, "settleDate");
            packageSettleDate(cuMemberBonus, memberBonusFirstPurchaseVO);
            BigDecimal serviceSpend = ComputeUtil.computeSubtract(ComputeUtil.computeAddNum(memberBonusFirstPurchaseVO.getDirectIncome(), memberBonusFirstPurchaseVO.getExpandIncome(),
                            memberBonusFirstPurchaseVO.getCoachIncome(), memberBonusFirstPurchaseVO.getSeePointIncome(), memberBonusFirstPurchaseVO.getShareIncome(),
                            memberBonusFirstPurchaseVO.getStoreIncome(), memberBonusFirstPurchaseVO.getStoreRangeIncome()),
                    memberBonusFirstPurchaseVO.getPurRealSubtotal());
            memberBonusFirstPurchaseVO.setBackPoints(serviceSpend);
            firstPurchaseVOList.add(memberBonusFirstPurchaseVO);
        });
        return getDataTable(firstPurchaseVOList);
    }

    /**
     * 查询会员实时奖金，当日的奖金
     */
    @PostMapping("/query-current-bonus")
    public TableDataInfo listCurrentBonus(@RequestBody BonusParam bonusParam) {
        bonusParam.setPkCountry(SecurityUtils.getPkCountry());
        bonusParam.setPkMember(SecurityUtils.getUserId());
        bonusParam.setStartDate(DateUtils.currentDate());
        bonusParam.setEndDate(DateUtils.afterDate(1, ChronoUnit.DAYS, DateUtils.currentDate()));
        bonusParam.setGrantStatus(EGrantStatus.SETTLED.getValue());
        List<CuMemberBonusExt> cuMemberBonusList = cuMemberBonusService.queryMemberSecondBonusVoByCondition(bonusParam);
        List<MemberBonusVO> memberBonusVOList = new ArrayList<>();
        cuMemberBonusList.forEach(cuMemberBonus -> {
            if (ComputeUtil.compareValue(cuMemberBonus.getPurRealSubtotal())) {
                MemberBonusVO memberBonusVO = BeanUtil.copyProperties(cuMemberBonus, MemberBonusVO.class, "settleDate");
                // 首购
                BigDecimal serviceSpend = ComputeUtil.computeSubtract(ComputeUtil.computeAddNum(memberBonusVO.getDirectIncome(), memberBonusVO.getExpandIncome(),
                                memberBonusVO.getCoachIncome(), memberBonusVO.getServiceIncome(), memberBonusVO.getShareIncome()),
                        memberBonusVO.getPurRealSubtotal());
                memberBonusVO.setBackPoints(serviceSpend);
//                memberBonusVO.setServiceSpend(ComputeUtil.computeMultiplyHalf(serviceSpend));
                // 复购
                BigDecimal repServiceSpend = ComputeUtil.computeSubtract(ComputeUtil.computeAddNum(memberBonusVO.getRepurPushIncome(), memberBonusVO.getRepurExpandIncome(), memberBonusVO.getRepurRangeIncome()),
                        memberBonusVO.getRepurRealSubtotal());
                memberBonusVO.setRepBackPoints(repServiceSpend);

                // 海粉
                memberBonusVOList.add(memberBonusVO);
            }
        });
        return getDataTable(memberBonusVOList);
    }

    /**
     * 查询会员昨天奖金的值
     */
    @PostMapping("/query-bonus-total")
    public TableDataInfo lisBonusTotal(@RequestBody BonusParam bonusParam) {
        bonusParam.setPkCountry(SecurityUtils.getPkCountry());
        bonusParam.setPkMember(SecurityUtils.getUserId());
        bonusParam.setStartDate(DateUtils.beforeDate(1, ChronoUnit.DAYS));
        bonusParam.setEndDate(DateUtils.currentDate());
        List<CuMemberBonusExt> cuMemberBonusList = cuMemberBonusService.queryMemberBonusTotalByCondition(bonusParam);
        MemberRealIncomeVO memberRealIncomeVO = new MemberRealIncomeVO();
        if (CollectionUtil.isEmpty(cuMemberBonusList)) {
            memberRealIncomeVO.setRealIncomeTotal(BigDecimal.ZERO);
        } else {
            memberRealIncomeVO.setRealIncomeTotal(cuMemberBonusList.get(0).getPurRealSubtotal());
        }
        return getDataTable(Collections.singletonList(memberRealIncomeVO));
    }

    /**
     * 查询会员某个时间段内的奖金，默认查询15天内,查询复购
     */
    @PostMapping("/query-bonus-repurchase")
    public TableDataInfo listRepurchaseBonus(@RequestBody BonusParam bonusParam) {
        setBonusParam(bonusParam);
        List<CuMemberBonusExt> cuMemberBonusList = cuMemberBonusService.queryMemberBonusVoByCondition(bonusParam);
        List<MemberBonusPurchaseVO> memberBonusPurchaseVoList = new ArrayList<>();
        cuMemberBonusList.forEach(cuMemberBonus -> {
            MemberBonusPurchaseVO memberBonusPurchaseVO = BeanUtil.copyProperties(cuMemberBonus, MemberBonusPurchaseVO.class, "settleDate");
            packageSettleDate(cuMemberBonus, memberBonusPurchaseVO);
            BigDecimal serviceSpend = ComputeUtil.computeSubtract(ComputeUtil.computeAddNum(memberBonusPurchaseVO.getRepurRangeIncome(), memberBonusPurchaseVO.getSameLevelIncome(),
                            memberBonusPurchaseVO.getPointShareIncome()),
                    memberBonusPurchaseVO.getRepurRealSubtotal());
            memberBonusPurchaseVO.setBackPoints(serviceSpend);
            memberBonusPurchaseVoList.add(memberBonusPurchaseVO);
        });
        return getDataTable(memberBonusPurchaseVoList);
    }

    /**
     * 查询会员某个时间段内的奖金，默认查询15天内,查询复购
     */
    @PostMapping("/query-bonus-hi-fun")
    public TableDataInfo listHiFunBonus(@RequestBody BonusParam bonusParam) {
        setBonusParam(bonusParam);
        List<CuMemberBonusExt> cuMemberBonusList = cuMemberBonusService.queryMemberBonusVoByCondition(bonusParam);
        List<MemberBonusHiFunVO> bonusHiFunVoList = new ArrayList<>();
        return getDataTable(bonusHiFunVoList);
    }

    private static void packageSettleDate(CuMemberBonusExt cuMemberBonus, PubMemberBonusVO pubMemberBonusVO) {
        if (cuMemberBonus.getEndSettleDate() == null) {
            pubMemberBonusVO.setSettleDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, cuMemberBonus.getSettleDate()));
        } else {
            pubMemberBonusVO.setSettleDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, cuMemberBonus.getSettleDate()) + "-" +
                    DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, cuMemberBonus.getEndSettleDate()));
        }
    }

    /**
     * 查询会员某个时间段内的奖金，默认查询15天内,查询复购
     */
    @PostMapping("/query-bonus-cloud")
    public TableDataInfo listCloudAgentBonus(@RequestBody BonusParam bonusParam) {
        setBonusParam(bonusParam);
        List<CuMemberBonusExt> cuMemberBonusList = cuMemberBonusService.queryMemberBonusVoByCondition(bonusParam);
        List<MemberBonusCloudAgentVO> memberBonusCloudAgentVoArrayList = new ArrayList<>();
        return getDataTable(memberBonusCloudAgentVoArrayList);
    }

    /**
     * 查询会员某个时间段内的奖金，默认查询15天内,查询复购
     */
    @PostMapping("/query-bonus-coupon")
    public TableDataInfo listRepurchaseCouponBonus(@RequestBody BonusParam bonusParam) {
        setBonusParam(bonusParam);
        List<CuMemberBonusExt> cuMemberBonusList = cuMemberBonusService.queryMemberBonusVoByCondition(bonusParam);
        List<MemberBonusRepurchaseCouponVO> memberBonusRepurchaseCouponList = new ArrayList<>();
        cuMemberBonusList.forEach(cuMemberBonus -> {
            MemberBonusRepurchaseCouponVO repurchaseCouponVO = BeanUtil.copyProperties(cuMemberBonus, MemberBonusRepurchaseCouponVO.class, "settleDate");
            packageSettleDate(cuMemberBonus, repurchaseCouponVO);
            BigDecimal serviceSpend = ComputeUtil.computeSubtract(ComputeUtil.computeAdd(repurchaseCouponVO.getRepurCoupon(),
                    repurchaseCouponVO.getRepurCouponShare()), repurchaseCouponVO.getRepurCouponSubtotal());
            memberBonusRepurchaseCouponList.add(repurchaseCouponVO);
        });
        return getDataTable(memberBonusRepurchaseCouponList);
    }

    /**
     * 查询会员某个时间段内的奖金，默认查询15天内,查询复购
     */
    @PostMapping("/query-bonus-maker")
    public TableDataInfo listMakerSpaceBonus(@RequestBody BonusParam bonusParam) {
        setBonusParam(bonusParam);
        List<CuMemberBonusExt> cuMemberBonusList = cuMemberBonusService.queryMemberBonusVoByCondition(bonusParam);
        List<MemberBonusMakerSpaceVO> memberBonusMakerSpaceVoList = new ArrayList<>();
        cuMemberBonusList.forEach(cuMemberBonus -> {
            MemberBonusMakerSpaceVO bonusMakerSpaceVO = BeanUtil.copyProperties(cuMemberBonus, MemberBonusMakerSpaceVO.class, "settleDate");
            packageSettleDate(cuMemberBonus, bonusMakerSpaceVO);
            BigDecimal serviceSpend = ComputeUtil.computeSubtract(ComputeUtil.computeAdd(bonusMakerSpaceVO.getMakerDirectIncome(),
                    bonusMakerSpaceVO.getMakerShareIncome()), bonusMakerSpaceVO.getMakerRealSubtotal());
            memberBonusMakerSpaceVoList.add(bonusMakerSpaceVO);
        });
        return getDataTable(memberBonusMakerSpaceVoList);
    }


    /**
     * 查询会员某个时间段内的奖金，默认查询15天内,查询复购
     */
    @PostMapping("/query-bonus-hai-fun")
    public TableDataInfo listHaiFunBonus(@RequestBody BonusParam bonusParam) {
        setBonusParam(bonusParam);
        List<CuMemberBonusExt> cuMemberBonusList = cuMemberBonusService.queryMemberBonusVoByCondition(bonusParam);
        List<MemberBonusHaiFunVO> bonusHaiFunVoList = new ArrayList<>();
        return getDataTable(bonusHaiFunVoList);
    }


    private void setBonusParam(@RequestBody BonusParam bonusParam) {
        bonusParam.setPkCountry(SecurityUtils.getPkCountry());
        bonusParam.setPkMember(SecurityUtils.getUserId());

//        Date startDate = DateUtils.beforeDate(15, ChronoUnit.DAYS, DateUtils.currentDate());
        if (bonusParam.getStartDate() == null) {
            bonusParam.setStartDate(DateUtils.currentMonthFirstDate());
        }
//        else if (DateUtils.compareDateBefore(bonusParam.getStartDate(), startDate)) {
//            bonusParam.setStartDate(startDate);
//        }
        if (bonusParam.getEndDate() == null) {
            bonusParam.setEndDate(DateUtils.currentDate());
        } else {
            bonusParam.setEndDate(DateUtils.afterDate(1, ChronoUnit.DAYS, bonusParam.getEndDate()));
        }
    }

    /**
     * 查询会员某个时间段内的奖金来源
     */
    @PostMapping("/query-bonus-detail")
    public TableDataInfo listBonusSource(@RequestBody BonusParam bonusParam) {
        setBonusParam(bonusParam);
        startPage();
//        List<MemberBonusVO> memberBonusVoList = new ArrayList<>();
        bonusParam.setPkMember(SecurityUtils.getUserId());
        TableDataInfo tableDataInfo = cuMemberBonusService.queryMemberBonusDetailVoByCondition(bonusParam);
//        cuMemberBonusList.forEach(cuMemberBonus -> {
//            MemberBonusVO memberBonusVO = BeanUtil.copyProperties(cuMemberBonus, MemberBonusVO.class);
//            memberBonusVoList.add(memberBonusVO);
//        });
//        TableDataInfo dataTable = getDataTable(cuMemberBonusList);
//        dataTable.setRows(memberBonusVoList);
        return tableDataInfo;
    }

    /**
     * 查询会员某个时间段内的奖金，默认查询15天内,查询复购
     */
    @PostMapping("/query-country-achieve")
    public TableDataInfo listCountryAchieve(@RequestBody BonusParam bonusParam) {
        bonusParam.setPkMember(SecurityUtils.getUserId());
        return getDataTable(cuMemberAchieveService.queryCuMemberSettleCountry(bonusParam));
    }
}
