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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
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.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.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;

@RestController
@RequestMapping("/api/bonus")
@Slf4j
public class ApiCuMemberBonusController extends BaseController {

    @Autowired
    private ICuMemberBonusService iCuMemberBonusService;

    /**
     * 新零售实时奖金：查询会员奖金列表
     */
    @PostMapping("/query-bonus-first")
    public TableDataInfo listFirstRepurchaseBonus(@RequestBody BonusParam bonusParam) {
        setBonusParam(bonusParam);

        // 当前日期的天
        int dateDD = Integer.parseInt(DateUtils.parseDateTimeToStr(DateUtils.DD, bonusParam.getEndDate()));
        if (dateDD < 15) {
            // 今天日期小于15号，查询上月23号之前的
            bonusParam.setEndDate(DateUtils.setDays(DateUtils.addMonths(bonusParam.getEndDate(), -1), 23));
        } else {
            // 今天日期大于等于15号，查询本月7号之前的
            bonusParam.setEndDate(DateUtils.setDays(bonusParam.getEndDate(), 7));
        }

        // 新查询奖金明细
        List<CuMemberBonusExt> cuMemberBonusList = iCuMemberBonusService.listMemberBonus(bonusParam);

        List<MemberBonusFirstPurchaseVO> firstPurchaseVOList = new ArrayList<>();
        cuMemberBonusList.forEach(cuMemberBonus -> {
            MemberBonusFirstPurchaseVO memberBonusFirstPurchaseVO = BeanUtil.copyProperties(cuMemberBonus, MemberBonusFirstPurchaseVO.class, "settleDate");
            packageSettleDate(cuMemberBonus, memberBonusFirstPurchaseVO);
            firstPurchaseVOList.add(memberBonusFirstPurchaseVO);
        });
        return getDataTable(firstPurchaseVOList);
    }

    /**
     * 新零售实时奖金：查询会员昨日奖金汇总
     */
    @PostMapping("/query-bonus-total")
    public TableDataInfo lisBonusTotal(@RequestBody BonusParam bonusParam) {
        setBonusParam(bonusParam);

        // 新查询奖金明细
        List<CuMemberBonusExt> cuMemberBonusList = iCuMemberBonusService.listMemberBonus(bonusParam);

        MemberRealIncomeVO memberRealIncomeVO = new MemberRealIncomeVO();
        if (CollectionUtil.isEmpty(cuMemberBonusList)) {
            memberRealIncomeVO.setRealIncomeTotal(BigDecimal.ZERO);
        } else {
            memberRealIncomeVO.setRealIncomeTotal(cuMemberBonusList.get(0).getRealIncomeTotal());
        }
        return getDataTable(Collections.singletonList(memberRealIncomeVO));
    }


    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()));
        }
//        pubMemberBonusVO.setServiceSpend(BigDecimal.ZERO);
    }

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

        // 默认查询90天之内的
        Date startDate = DateUtils.beforeDate(60, ChronoUnit.DAYS, DateUtils.currentDate());
        if (bonusParam.getStartDate() == null) {
            bonusParam.setStartDate(startDate);
        } 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();
        return iCuMemberBonusService.queryMemberBonusDetailVoByCondition(bonusParam);
    }

}
