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

import cn.hutool.core.bean.BeanUtil;
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.constant.msg.FinanceMsgConstants;
import com.hzs.common.core.domain.R;
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.domain.AjaxResult;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.util.TransactionUtils;
import com.hzs.member.statis.service.ICuBonusVertexStatisService;
import com.hzs.member.statis.vo.*;
import com.hzs.system.base.ICurrencyServiceApi;
import com.hzs.system.base.dto.CurrencyDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
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.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

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

    @Autowired
    private ICuBonusVertexStatisService iCuBonusVertexStatisService;

    @DubboReference
    ICurrencyServiceApi currencyServiceApi;

    /**
     * 查询所有顶点会员
     **/
    @GetMapping("/vertexList")
    public TableDataInfo vertexList() {
        List<CuBonusVertexStatisVO> vertexList = iCuBonusVertexStatisService.vertexList(null);
        return getDataTable(vertexList);
    }

    /**
     * 奖金拨比列表
     **/
    @AccessPermissions("TopPerformance")
    @GetMapping("/list")
    @Log(module = EOperationModule.BONUS_DIAL_RATIO, business = EOperationBusiness.BONUS_DIAL_VERTEX_RATIO_LIST, method = EOperationMethod.SELECT)
    public TableDataInfo list(CuBonusVertexStatisVO cuBonusVertexStatisVO) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        List<CuBonusVertexStatisVO> slList;
        R<CurrencyDTO> currency = currencyServiceApi.getCurrency(pkCountry);
        BigDecimal exchangeRate = currency.getData().getInExchangeRate() == null ? BigDecimal.ONE : currency.getData().getInExchangeRate();
        cuBonusVertexStatisVO.setRate(exchangeRate);
        BigDecimal cent = new BigDecimal(100);
        cuBonusVertexStatisVO.setPerCent(exchangeRate.multiply(cent));
        cuBonusVertexStatisVO.setPkCountry(pkCountry);
        //默认查询当前日期的前一天
        Date date = DateUtils.currentDate();
        if (StringUtils.isEmpty(cuBonusVertexStatisVO.getStartDate()) ||
                StringUtils.isEmpty(cuBonusVertexStatisVO.getEndDate())) {
            cuBonusVertexStatisVO.setStartDate(DateUtils.beforeDateStr(1, ChronoUnit.DAYS, DateUtils.YYYY_MM_DD, date));
            cuBonusVertexStatisVO.setEndDate(DateUtils.beforeDateStr(1, ChronoUnit.DAYS, DateUtils.YYYY_MM_DD, date));
        }
        startPage();
        //查询中国奖金拨比列表
        slList = iCuBonusVertexStatisService.queryCuBonusStatis(cuBonusVertexStatisVO);
        return getDataTable(slList);
    }

    /**
     * 奖金拨比列表(PV)
     **/
    @AccessPermissions("TopPerformance")
    @GetMapping("/list-pv")
    @Log(module = EOperationModule.BONUS_DIAL_RATIO, business = EOperationBusiness.BONUS_DIAL_VERTEX_RATIO_LIST, method = EOperationMethod.SELECT)
    public TableDataInfo listPv(CuBonusVertexStatisPvVO cuBonusVertexStatisVO) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        List<CuBonusVertexStatisPvVO> slList;
        R<CurrencyDTO> currency = currencyServiceApi.getCurrency(pkCountry);
        BigDecimal exchangeRate = currency.getData().getInExchangeRate() == null ? BigDecimal.ONE : currency.getData().getInExchangeRate();
        cuBonusVertexStatisVO.setRate(exchangeRate);
        cuBonusVertexStatisVO.setPkCountry(pkCountry);
        //默认查询当前日期的前一天
        Date date = DateUtils.currentDate();
        if (StringUtils.isEmpty(cuBonusVertexStatisVO.getStartDate()) ||
                StringUtils.isEmpty(cuBonusVertexStatisVO.getEndDate())) {
            cuBonusVertexStatisVO.setStartDate(DateUtils.beforeDateStr(1, ChronoUnit.DAYS, DateUtils.YYYY_MM_DD, date));
            cuBonusVertexStatisVO.setEndDate(DateUtils.beforeDateStr(1, ChronoUnit.DAYS, DateUtils.YYYY_MM_DD, date));
        }
        startPage();
        //查询奖金拨比列表（PV）
        slList = iCuBonusVertexStatisService.queryCuBonusPvStatis(cuBonusVertexStatisVO);
        return getDataTable(slList);
    }

    /**
     * 导出奖金拨比列表
     **/
    @Log(module = EOperationModule.BONUS_DIAL_RATIO, business = EOperationBusiness.BONUS_DIAL_RATIO_LIST, method = EOperationMethod.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CuBonusVertexStatisVO cuBonusVertexStatisVO) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        R<CurrencyDTO> currency = currencyServiceApi.getCurrency(pkCountry);
        BigDecimal exchangeRate = currency.getData().getInExchangeRate() == null ? BigDecimal.ONE : currency.getData().getInExchangeRate();
        cuBonusVertexStatisVO.setRate(exchangeRate);
        BigDecimal cent = new BigDecimal(100);
        cuBonusVertexStatisVO.setPerCent(exchangeRate.multiply(cent));
        cuBonusVertexStatisVO.setPkCountry(pkCountry);
        //默认查询当前日期的前一天
        Date date = DateUtils.currentDate();
        if (StringUtils.isEmpty(cuBonusVertexStatisVO.getStartDate())
                || StringUtils.isEmpty(cuBonusVertexStatisVO.getEndDate())) {
            cuBonusVertexStatisVO.setStartDate(DateUtils.beforeDateStr(1, ChronoUnit.DAYS, DateUtils.YYYY_MM_DD, date));
            cuBonusVertexStatisVO.setEndDate(DateUtils.beforeDateStr(1, ChronoUnit.DAYS, DateUtils.YYYY_MM_DD, date));
        }

        List<CuBonusVertexStatisVO> slList = iCuBonusVertexStatisService.queryCuBonusStatis(cuBonusVertexStatisVO);
        //合计
        CuBonusVertexStatisVO subtotal = iCuBonusVertexStatisService.queryCuBonusSumStatis(cuBonusVertexStatisVO);
        subtotal.setMainRegAmount(subtotal.getMainRegAmountSum());
        subtotal.setMainUpAmount(subtotal.getMainUpAmountSum());
        subtotal.setFirstPurchaseAll(subtotal.getFirstPurchaseAllSum());
        subtotal.setMainRepurAmount(subtotal.getMainRepurAmountSum());
        subtotal.setRetailConsumeAmount(subtotal.getRetailConsumeAmountSum());
        subtotal.setTotalAmount(subtotal.getTotalAmountSum());
        subtotal.setRetailRangeIncome(subtotal.getRetailRangeIncomeSum());
        subtotal.setRetailRangeIncomeRate(subtotal.getRetailRangeIncomeRateSum());
        subtotal.setRetailSameLevelIncomeV1V4(subtotal.getRetailSameLevelIncomeV1V4Sum());
        subtotal.setRetailSameLevelIncomeV1V4Rate(subtotal.getSameLevelIncomeV1V4RateSum());
        subtotal.setRetailSameLevelIncomeV5(subtotal.getRetailSameLevelIncomeV5Sum());
        subtotal.setRetailSameLevelIncomeV5Rate(subtotal.getSameLevelIncomeV5RateSum());
        subtotal.setRetailAreaIncomeRate(subtotal.getRetailRangeIncomeRateSum());
        subtotal.setRetailAreaIncome(subtotal.getRetailAreaIncomeSum());
        subtotal.setRetailAreaIncomeRate(subtotal.getRetailAreaIncomeRateSum());
        subtotal.setRetailBenefitAvgIncome(subtotal.getRetailBenefitAvgIncomeSum());
        subtotal.setRetailBenefitAvgIncomeRate(subtotal.getRetailBenefitAvgIncomeRateSum());
        subtotal.setRetailBenefitIncome(subtotal.getRetailBenefitIncomeSum());
        subtotal.setRetailBenefitIncomeRate(subtotal.getRetailBenefitIncomeRateSum());
        subtotal.setRetailBenefitRangeIncome(subtotal.getRetailBenefitRangeIncomeSum());
        subtotal.setRetailBenefitRangeIncomeRate(subtotal.getBenefitRangeIncomeRateSum());
        subtotal.setTotalBonus(subtotal.getTotalBonusSum());
        subtotal.setTotalBonusRate(subtotal.getTotalBonusRateSum());
        subtotal = new CuBonusVertexStatisVO();
        subtotal.setMemberCode("合计");
        slList.add(subtotal);
        ExcelUtil<CuBonusVertexStatisVO> util = new ExcelUtil<>(CuBonusVertexStatisVO.class);
        util.exportExcel(response, slList, "拨比列表");
    }

    /**
     * 导出奖金拨比列表(PV)
     **/
    @Log(module = EOperationModule.BONUS_DIAL_RATIO, business = EOperationBusiness.BONUS_DIAL_RATIO_LIST, method = EOperationMethod.EXPORT)
    @PostMapping("/export-pv")
    public void exportPv(HttpServletResponse response, CuBonusVertexStatisPvVO cuBonusVertexStatisPvVO) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        R<CurrencyDTO> currency = currencyServiceApi.getCurrency(pkCountry);
        BigDecimal exchangeRate = currency.getData().getInExchangeRate() == null ? BigDecimal.ONE : currency.getData().getInExchangeRate();
        cuBonusVertexStatisPvVO.setRate(exchangeRate);
        BigDecimal cent = new BigDecimal(100);
        cuBonusVertexStatisPvVO.setPerCent(exchangeRate.multiply(cent));
        cuBonusVertexStatisPvVO.setPkCountry(pkCountry);
        //默认查询当前日期的前一天
        Date date = DateUtils.currentDate();
        if (StringUtils.isEmpty(cuBonusVertexStatisPvVO.getStartDate())
                || StringUtils.isEmpty(cuBonusVertexStatisPvVO.getEndDate())) {
            cuBonusVertexStatisPvVO.setStartDate(DateUtils.beforeDateStr(1, ChronoUnit.DAYS, DateUtils.YYYY_MM_DD, date));
            cuBonusVertexStatisPvVO.setEndDate(DateUtils.beforeDateStr(1, ChronoUnit.DAYS, DateUtils.YYYY_MM_DD, date));
        }
        List<CuBonusVertexStatisPvVO> slList = iCuBonusVertexStatisService.queryCuBonusPvStatis(cuBonusVertexStatisPvVO);
        //合计
        CuBonusVertexStatisPvVO subtotal = iCuBonusVertexStatisService.queryCuBonusPvSumStatis(cuBonusVertexStatisPvVO);
        subtotal.setMainRegPv(subtotal.getMainRegPvSum());
        subtotal.setMainUpPv(subtotal.getMainUpPvSum());
        subtotal.setFirstPurchaseAll(subtotal.getFirstPurchaseAllSum());
        subtotal.setMainRepurPv(subtotal.getMainRepurPvSum());
        subtotal.setTotalAchieve(subtotal.getTotalAchieveSum());
        subtotal.setRetailRangeIncomeRate(subtotal.getRetailRangeIncomeRateSum());
        subtotal.setRetailSameLevelIncomeV1V4(subtotal.getRetailSameLevelIncomeV1V4Sum());
        subtotal.setRetailSameLevelIncomeV1V4Rate(subtotal.getSameLevelIncomeV1V4RateSum());
        subtotal.setRetailSameLevelIncomeV5(subtotal.getRetailSameLevelIncomeV5Sum());
        subtotal.setRetailSameLevelIncomeV5Rate(subtotal.getSameLevelIncomeV5RateSum());
        subtotal.setRetailConsumePv(subtotal.getRetailConsumePvSum());
        subtotal.setRetailRangeIncome(subtotal.getRetailRangeIncomeSum());
        subtotal.setRetailAreaIncomeRate(subtotal.getRetailRangeIncomeRateSum());
        subtotal.setRetailAreaIncome(subtotal.getRetailAreaIncomeSum());
        subtotal.setRetailAreaIncomeRate(subtotal.getRetailAreaIncomeRateSum());
        subtotal.setRetailBenefitAvgIncome(subtotal.getRetailBenefitAvgIncomeSum());
        subtotal.setRetailBenefitAvgIncomeRate(subtotal.getRetailBenefitAvgIncomeRateSum());
        subtotal.setRetailBenefitIncome(subtotal.getRetailBenefitIncomeSum());
        subtotal.setRetailBenefitIncomeRate(subtotal.getRetailBenefitIncomeRateSum());
        subtotal.setRetailBenefitRangeIncome(subtotal.getRetailBenefitRangeIncomeSum());
        subtotal.setRetailBenefitRangeIncomeRate(subtotal.getBenefitRangeIncomeRateSum());
        subtotal.setTotalBonus(subtotal.getTotalBonusSum());
        subtotal.setTotalBonusRate(subtotal.getTotalBonusRateSum());
        subtotal = new CuBonusVertexStatisPvVO();
        subtotal.setMemberCode("合计");
        slList.add(subtotal);
        ExcelUtil<CuBonusVertexStatisPvVO> util = new ExcelUtil<>(CuBonusVertexStatisPvVO.class);
        util.exportExcel(response, slList, "PV拨比列表");
    }

    /**
     * 奖金拨比列表合计
     **/
    @AccessPermissions("TopPerformance")
    @GetMapping("/subtotal")
    @Log(module = EOperationModule.BONUS_DIAL_RATIO, business = EOperationBusiness.BONUS_DIAL_RATIO_LIST, method = EOperationMethod.SELECT)
    public AjaxResult subtotal(CuBonusVertexStatisVO cuBonusVertexStatisVO) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        CuBonusVertexStatisVO subtotal;
        R<CurrencyDTO> currency = currencyServiceApi.getCurrency(pkCountry);
        BigDecimal exchangeRate = currency.getData().getInExchangeRate() == null ? BigDecimal.ONE : currency.getData().getInExchangeRate();
        cuBonusVertexStatisVO.setRate(exchangeRate);
        BigDecimal cent = new BigDecimal(100);
        cuBonusVertexStatisVO.setPerCent(exchangeRate.multiply(cent));
        cuBonusVertexStatisVO.setPkCountry(pkCountry);
        Date date = DateUtils.currentDate();
        //默认查询当前日期的前一天
        if (StringUtils.isEmpty(cuBonusVertexStatisVO.getStartDate())
                || StringUtils.isEmpty(cuBonusVertexStatisVO.getEndDate())) {
            cuBonusVertexStatisVO.setStartDate(DateUtils.beforeDateStr(1, ChronoUnit.DAYS, DateUtils.YYYY_MM_DD, date));
            cuBonusVertexStatisVO.setEndDate(DateUtils.beforeDateStr(1, ChronoUnit.DAYS, DateUtils.YYYY_MM_DD, date));
        }
        //查询合计
        subtotal = iCuBonusVertexStatisService.queryCuBonusSumStatis(cuBonusVertexStatisVO);
        CuBonusVertexStatisSumVO cbvSum = BeanUtil.copyProperties(subtotal, CuBonusVertexStatisSumVO.class);
        return AjaxResult.success(cbvSum);
    }

    /**
     * 奖金拨比列表合计(PV)
     **/
    @AccessPermissions("TopPerformance")
    @GetMapping("/subtotal-pv")
    @Log(module = EOperationModule.BONUS_DIAL_RATIO, business = EOperationBusiness.BONUS_DIAL_RATIO_LIST, method = EOperationMethod.SELECT)
    public AjaxResult subtotalPv(CuBonusVertexStatisPvVO cuBonusVertexStatisPvVO) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        CuBonusVertexStatisPvVO subtotal;
        R<CurrencyDTO> currency = currencyServiceApi.getCurrency(pkCountry);
        BigDecimal exchangeRate = currency.getData().getInExchangeRate() == null ? BigDecimal.ONE : currency.getData().getInExchangeRate();
        cuBonusVertexStatisPvVO.setRate(exchangeRate);
        BigDecimal cent = new BigDecimal(100);
        cuBonusVertexStatisPvVO.setPerCent(exchangeRate.multiply(cent));
        cuBonusVertexStatisPvVO.setPkCountry(pkCountry);
        Date date = DateUtils.currentDate();
        //默认查询当前日期的前一天
        if (StringUtils.isEmpty(cuBonusVertexStatisPvVO.getStartDate())
                || StringUtils.isEmpty(cuBonusVertexStatisPvVO.getEndDate())) {
            cuBonusVertexStatisPvVO.setStartDate(DateUtils.beforeDateStr(1, ChronoUnit.DAYS, DateUtils.YYYY_MM_DD, date));
            cuBonusVertexStatisPvVO.setEndDate(DateUtils.beforeDateStr(1, ChronoUnit.DAYS, DateUtils.YYYY_MM_DD, date));
        }
        //查询合计
        subtotal = iCuBonusVertexStatisService.queryCuBonusPvSumStatis(cuBonusVertexStatisPvVO);
        CuBonusVertexStatisSumPvVO cbvSum = BeanUtil.copyProperties(subtotal, CuBonusVertexStatisSumPvVO.class);
        return AjaxResult.success(cbvSum);
    }

    /**
     * 统计历史奖金拨比数据, 手动统计某一天
     **/
    @GetMapping("/statis-history")
    public AjaxResult statisHistoryBonus(String startDate, String endDate) {
        if (StringUtils.isEmpty(startDate) || StringUtils.isEmpty(endDate)) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.SELECT_DATE_NOT_NULL));
        }
        List<CuBonusVertexStatisVO> vertexList = iCuBonusVertexStatisService.vertexList(null);
        List<Integer> pkVertexList = vertexList.stream().map(CuBonusVertexStatisVO::getPkVertex).collect(Collectors.toList());
        //删除数据
        if (CollectionUtil.isNotEmpty(pkVertexList)) {
            iCuBonusVertexStatisService.delStatis(startDate, endDate, pkVertexList);
        }
        //重新统计
        Boolean b = iCuBonusVertexStatisService.anewStatisHistoryBonus(startDate, endDate);
        return AjaxResult.success(b);
    }

}
