package com.hzs.member.statis.controller.manager;

import cn.hutool.core.collection.CollectionUtil;
import com.hzs.common.core.annotation.AccessPermissions;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.enums.EOperationBusiness;
import com.hzs.common.core.enums.EOperationMethod;
import com.hzs.common.core.enums.EOperationModule;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.utils.poi.ExcelUtil;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.member.statis.service.ICuBonusVertexStatisService;
import com.hzs.member.statis.vo.CuBonusVertexStatisTotalVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;

/**
 * 奖金拨比零线汇总统计 前端控制器
 */
@Slf4j
@RestController
@RequestMapping("/manage/bonus-vertex-total-statis")
public class CuBonusVertexStatisTotalController extends BaseController {

    @Autowired
    private ICuBonusVertexStatisService iCuBonusVertexStatisService;

    /**
     * 奖金拨比列表
     **/
    @AccessPermissions("TopPerformanceTotal")
    @GetMapping("/list")
    @Log(module = EOperationModule.BONUS_DIAL_RATIO, business = EOperationBusiness.BONUS_DIAL_VERTEX_RATIO_TOTAL, method = EOperationMethod.SELECT)
    public TableDataInfo list(CuBonusVertexStatisTotalVO cuBonusVertexStatisTotalVO) {
        cuBonusVertexStatisTotalVO.setPkCountry(SecurityUtils.getPkCountry());

        //默认查询当前日期的前一天
        if (StringUtils.isEmpty(cuBonusVertexStatisTotalVO.getStartDate()) || StringUtils.isEmpty(cuBonusVertexStatisTotalVO.getEndDate())) {
            Date date = DateUtils.currentDate();
            cuBonusVertexStatisTotalVO.setStartDate(DateUtils.beforeDateStr(1, ChronoUnit.DAYS, DateUtils.YYYY_MM_DD, date));
            cuBonusVertexStatisTotalVO.setEndDate(DateUtils.beforeDateStr(1, ChronoUnit.DAYS, DateUtils.YYYY_MM_DD, date));
        }

        startPage();
        //查询顶点奖金拨比汇总
        List<CuBonusVertexStatisTotalVO> slList = iCuBonusVertexStatisService.queryCuBonusStatisTotal(cuBonusVertexStatisTotalVO);
        if (CollectionUtil.isNotEmpty(slList)) {
            BigDecimal rate = new BigDecimal(100);
            slList.forEach(vo -> {
                this.handleStatisVO(vo, rate);
            });

            //查询顶点奖金拨比汇总总和（最后一行）
            CuBonusVertexStatisTotalVO voSum = iCuBonusVertexStatisService.queryCuBonusStatisSum(cuBonusVertexStatisTotalVO);
            if (null != voSum) {
                this.handleStatisVO(voSum, rate);
                voSum.setMemberName("汇总");
                slList.add(voSum);
            }
        }
        return getDataTable(slList);
    }

    /**
     * 导出奖金拨比列表
     **/
    @Log(module = EOperationModule.BONUS_DIAL_RATIO, business = EOperationBusiness.BONUS_DIAL_VERTEX_RATIO_TOTAL, method = EOperationMethod.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CuBonusVertexStatisTotalVO cuBonusVertexStatisTotalVO) {
        cuBonusVertexStatisTotalVO.setPkCountry(SecurityUtils.getPkCountry());

        //默认查询当前日期的前一天
        if (StringUtils.isEmpty(cuBonusVertexStatisTotalVO.getStartDate()) || StringUtils.isEmpty(cuBonusVertexStatisTotalVO.getEndDate())) {
            Date date = DateUtils.currentDate();
            cuBonusVertexStatisTotalVO.setStartDate(DateUtils.beforeDateStr(1, ChronoUnit.DAYS, DateUtils.YYYY_MM_DD, date));
            cuBonusVertexStatisTotalVO.setEndDate(DateUtils.beforeDateStr(1, ChronoUnit.DAYS, DateUtils.YYYY_MM_DD, date));
        }

        //查询顶点奖金拨比汇总
        List<CuBonusVertexStatisTotalVO> slList = iCuBonusVertexStatisService.queryCuBonusStatisTotal(cuBonusVertexStatisTotalVO);
        if (CollectionUtil.isNotEmpty(slList)) {
            BigDecimal rate = new BigDecimal(100);
            slList.forEach(vo -> {
                this.handleStatisVO(vo, rate);
            });

            //查询顶点奖金拨比汇总总和（最后一行）
            CuBonusVertexStatisTotalVO voSum = iCuBonusVertexStatisService.queryCuBonusStatisSum(cuBonusVertexStatisTotalVO);
            if (null != voSum) {
                this.handleStatisVO(voSum, rate);
                voSum.setMemberName("汇总");
                slList.add(voSum);
            }
        }
        ExcelUtil<CuBonusVertexStatisTotalVO> util = new ExcelUtil<>(CuBonusVertexStatisTotalVO.class);
        util.exportExcel(response, slList, "拨比列表");
    }


    /**
     * 处理统计返回VO
     */
    private void handleStatisVO(CuBonusVertexStatisTotalVO vo, BigDecimal rate) {
        // 精品专区金额 = 精品 + 甄选
        vo.setMainRegAmount(vo.getMainRegAmount().add(vo.getMainUpAmount()));
        // 精品专区奖金
        vo.setMainBonus(vo.getIntroduceBonus().add(vo.getStoreIncome()).add(vo.getRangeIncome41()).add(vo.getSameLevelIncome41()));
        // 精品专区拨比
        vo.setMainBonusRate(vo.getMainBonus().divide(vo.getMainRegAmount(), 4, RoundingMode.HALF_UP).multiply(rate));

        // 商城专区奖金
        vo.setMainRepurBonus(vo.getGlobalPoints().add(vo.getRangeIncome43()).add(vo.getSameLevelIncome43()));
        // 商城专区拨比
        vo.setMainRepurBonusRate(vo.getMainRepurBonus().divide(vo.getMainRepurAmount(), 4, RoundingMode.HALF_UP).multiply(rate));

        // 五折专区奖金
        vo.setRetailConsumeBonus(vo.getRangeIncome47());
        // 五折专区拨比
        vo.setRetailConsumeBonusRate(vo.getRetailConsumeBonus().divide(vo.getRetailConsumeAmount(), 4, RoundingMode.HALF_UP).multiply(rate));

        // 总金额
        vo.setTotalAmount(vo.getMainRegAmount().add(vo.getMainRepurAmount().add(vo.getRetailConsumeAmount())));
        // 总拨出金额
        vo.setTotalBonus(vo.getIntroduceBonus().add(vo.getStoreIncome()).add(vo.getRetailRangeIncome()).add(vo.getRetailSameLevelIncome()).add(vo.getGlobalPoints()));
        // 总拨出比例
        vo.setTotalBonusRate(vo.getTotalBonus().divide(vo.getTotalAmount(), 4, RoundingMode.HALF_UP).multiply(rate));
    }

}
