package com.hzs.member.census.controller.manage;

import cn.hutool.core.bean.BeanUtil;
import com.hzs.common.core.constant.TableNameConstants;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.utils.DateUtils;
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.domain.member.achieve.CuMemberSettlePeriod;
import com.hzs.common.domain.system.config.ext.BdAreaCurrencyExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.member.achieve.service.ICuMemberSettlePeriodService;
import com.hzs.member.achieve.service.ICuMemberTreeService;
import com.hzs.member.base.IMemberServiceApi;
import com.hzs.member.base.service.ICuMemberService;
import com.hzs.member.census.param.CensusSummaryParam;
import com.hzs.member.census.vo.CensusSummaryCollectVo;
import com.hzs.member.census.vo.CensusSummaryCopyVo;
import com.hzs.member.census.vo.CensusSummaryVo;
import com.hzs.system.base.ICurrencyServiceApi;
import com.hzs.system.base.dto.CurrencyDTO;
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.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @Author: yuhui
 * @Time: 2023/2/6 11:50
 * @Classname: censusSummaryController
 * @PackageName: com.hzs.member.censusSummary
 */
@RestController
@RequestMapping("/manage/member-structure")
public class CensusSummaryController extends BaseController {


    @Autowired
    private ICuMemberSettlePeriodService memberSettlePeriodService;
    @Autowired
    private ICuMemberService memberService;
    @Autowired
    private ICuMemberTreeService memberTreeService;
    @DubboReference
    private ICurrencyServiceApi currencyServiceApi;

    /**
     * 业绩汇总
     *
     * @param censusSummaryParam
     * @return
     */
    @GetMapping("list")
    public AjaxResult list(CensusSummaryParam censusSummaryParam) {
        if (censusSummaryParam.getStartDate() == null || censusSummaryParam.getEndDate() == null){
            return AjaxResult.success(new ArrayList<>());
        }

        // 当天查询秒结
        Date date = new Date();
        // 查询当天
        String thisTime = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, date);

        // 查询当前时间范围
        List<String> days = new ArrayList();
        if (censusSummaryParam.getStartDate() != null && censusSummaryParam.getEndDate() != null) {
            days = DateUtils.findDaysStr(DateUtils.parseDateTimeToStr(DateUtils.YYYY_MM_DD, censusSummaryParam.getStartDate()), DateUtils.parseDateTimeToStr(DateUtils.YYYY_MM_DD, censusSummaryParam.getEndDate()));
        } else {
            // 默认查询当天
            days.add(thisTime);
        }

        List<CensusSummaryVo> resultList = memberService.selectCensusSummary(censusSummaryParam,days);

        return AjaxResult.success(resultList);
    }

    /**
     * 导出（导出）
     */
    @PostMapping("export")
    public void export(CensusSummaryParam censusSummaryParam, HttpServletResponse response){
        if (censusSummaryParam.getStartDate() == null || censusSummaryParam.getEndDate() == null){
            ExcelUtil<CensusSummaryVo> util = new ExcelUtil<>(CensusSummaryVo.class);
            util.exportExcel(response, new ArrayList<>(), "业绩汇总信息");
        }

        // TODO 业绩汇总 暂无
        Integer period = null;
        // 当天查询秒结
        Date date = new Date();
        // 查询当天
        String thisTime = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, date);
        CuMemberSettlePeriod csPeriod = null;

        if (censusSummaryParam.getMemberSettlePeriodId() != null) {
            csPeriod = memberSettlePeriodService.getById(censusSummaryParam.getMemberSettlePeriodId());
            period = csPeriod.getPkId();
        } else {
            csPeriod = memberSettlePeriodService.getCuMemberSettlePeriodByDate(thisTime);
            period = csPeriod.getPkId();
        }
        List<CensusSummaryVo> resultList = new ArrayList<>();

        // 查询当前时间范围
        List<String> days = new ArrayList();
        if (censusSummaryParam.getStartDate() != null && censusSummaryParam.getEndDate() != null) {
            days = DateUtils.findDaysStr(DateUtils.parseDateTimeToStr(DateUtils.YYYY_MM_DD, censusSummaryParam.getStartDate()), DateUtils.parseDateTimeToStr(DateUtils.YYYY_MM_DD, censusSummaryParam.getEndDate()));
        } else {
            // 默认查询当天
            days.add(thisTime);
        }

        resultList = memberService.selectCensusSummary(censusSummaryParam,days);

        //  CensusSummaryCollectVo censusSummaryCollectVo = new CensusSummaryCollectVo();
        List<CensusSummaryCopyVo> rsList = new ArrayList<>();
       // 注册金额(￥)
        BigDecimal registerAmountTotal = BigDecimal.ZERO;
        // 注册金额($)
        BigDecimal registerAmountDollarTotal = BigDecimal.ZERO;
        // 注册金额
        BigDecimal registerAmountLocalityTotal = BigDecimal.ZERO;
        // 注册业绩(￥)
        BigDecimal registerPvTotal = BigDecimal.ZERO;
        // 注册业绩($)
        BigDecimal registerPvDollarTotal = BigDecimal.ZERO;
        // 注册业绩
        BigDecimal registerPvLocalityTotal = BigDecimal.ZERO;
        // 升级金额(￥)
        BigDecimal upgradeAmountTotal = BigDecimal.ZERO;
        // 升级金额($)
        BigDecimal upgradeAmountDollarTotal = BigDecimal.ZERO;
        // 升级金额
        BigDecimal upgradeAmountLocalityTotal = BigDecimal.ZERO;
        // 升级业绩(￥)
        BigDecimal upgradePvTotal = BigDecimal.ZERO;
        // 升级业绩($)
        BigDecimal upgradePvDollar = BigDecimal.ZERO;
        // 升级业绩
        BigDecimal upgradePvLocalityTotal = BigDecimal.ZERO;
        // 首购金额(￥)
        BigDecimal firstPurchaseAmountTotal = BigDecimal.ZERO;
        // 首购金额($)
        BigDecimal firstPurchaseAmountDollarTotal = BigDecimal.ZERO;
        // 首购金额
        BigDecimal firstPurchaseAmountLocalityTotal = BigDecimal.ZERO;
        // 首购业绩
        BigDecimal firstPurchasePvTotal = BigDecimal.ZERO;
        // 首购业绩($)
        BigDecimal firstPurchasePvDollarTotal = BigDecimal.ZERO;
        // 首购业绩
        BigDecimal firstPurchasePvLocalityTotal = BigDecimal.ZERO;
        // 复购金额(￥)
        BigDecimal repurchaseAmountTotal = BigDecimal.ZERO;
        // 复购金额($)
        BigDecimal repurchaseAmountDollarTotal = BigDecimal.ZERO;
        // 复购金额
        BigDecimal repurchaseAmountLocalityTotal = BigDecimal.ZERO;
        // 复购业绩(￥)
        BigDecimal repurchasePvTotal = BigDecimal.ZERO;
        // 复购业绩($)
        BigDecimal repurchasePvDollarTotal = BigDecimal.ZERO;
        // 复购业绩
        BigDecimal repurchasePvLocalityTotal = BigDecimal.ZERO;
        // 金额总计(￥)
        BigDecimal amountTotalTotal = BigDecimal.ZERO;
        // 金额总计($)
        BigDecimal amountTotalDollarTotal = BigDecimal.ZERO;
        // 金额总计
        BigDecimal amountTotalLocalityTotal = BigDecimal.ZERO;
        // 业绩汇总(￥)
        BigDecimal pvTotalTotal = BigDecimal.ZERO;
        // 业绩汇总($)
        BigDecimal pvTotalDollarTotal = BigDecimal.ZERO;
        // 业绩汇总
        BigDecimal pvTotalLocalityTotal = BigDecimal.ZERO;
        // 直供金额(￥)
        BigDecimal directSupplyAmountTotal = BigDecimal.ZERO;
        // 直供金额($)
        BigDecimal directSupplyAmountDollarTotal = BigDecimal.ZERO;
        // 直供金额
        BigDecimal directSupplyAmountLocalityTotal = BigDecimal.ZERO;
        // 直供业绩(￥)
        BigDecimal directSupplyPvTotal = BigDecimal.ZERO;
        // 直供业绩($)
        BigDecimal directSupplyPvDollarTotal = BigDecimal.ZERO;
        // 直供业绩
        BigDecimal directSupplyPvLocalityTotal = BigDecimal.ZERO;
        for (CensusSummaryVo censusSummaryVo : resultList) {
            CensusSummaryCopyVo censusSummaryCopyVo = BeanUtil.copyProperties(censusSummaryVo, CensusSummaryCopyVo.class);
            rsList.add(censusSummaryCopyVo);
            // 注册金额(￥)
            registerAmountTotal = registerAmountTotal.add(censusSummaryVo.getRegisterAmount());
            // 注册金额($)
            registerAmountDollarTotal = registerAmountDollarTotal.add(censusSummaryVo.getRegisterAmountDollar());
            // 注册金额
            registerAmountLocalityTotal = registerAmountLocalityTotal.add(censusSummaryVo.getRegisterAmountLocality());
            // 注册业绩(￥)
            registerPvTotal = registerPvTotal.add(censusSummaryVo.getRegisterPv());
            // 注册业绩($)
            registerPvDollarTotal = registerPvDollarTotal.add(censusSummaryVo.getRegisterPvDollar());
            // 注册业绩
            registerPvLocalityTotal = registerPvLocalityTotal.add(censusSummaryVo.getRegisterPvLocality());
            // 升级金额(￥)
            upgradeAmountTotal = upgradeAmountTotal.add(censusSummaryVo.getUpgradeAmount());
            // 升级金额($)
            upgradeAmountDollarTotal = upgradeAmountDollarTotal.add(censusSummaryVo.getUpgradeAmountDollar());
            // 升级金额
            upgradeAmountLocalityTotal = upgradeAmountLocalityTotal.add(censusSummaryVo.getUpgradeAmountLocality());
            // 升级业绩(￥)
            upgradePvTotal = upgradePvTotal.add(censusSummaryVo.getUpgradePv());
            // 升级业绩($)
            upgradePvDollar = upgradePvDollar.add(censusSummaryVo.getUpgradePvDollar());
            // 升级业绩
            upgradePvLocalityTotal = upgradePvLocalityTotal.add(censusSummaryVo.getUpgradePvLocality());
            // 首购金额(￥)
            firstPurchaseAmountTotal = firstPurchaseAmountTotal.add(censusSummaryVo.getFirstPurchaseAmount());
            // 首购金额($)
            firstPurchaseAmountDollarTotal = firstPurchaseAmountDollarTotal.add(censusSummaryVo.getFirstPurchaseAmountDollar());
            // 首购金额
            firstPurchaseAmountLocalityTotal = firstPurchaseAmountLocalityTotal.add(censusSummaryVo.getFirstPurchaseAmountLocality());
            // 首购业绩
            firstPurchasePvTotal = firstPurchasePvTotal.add(censusSummaryVo.getFirstPurchasePv());
            //  首购业绩($)
            firstPurchasePvDollarTotal = firstPurchasePvDollarTotal.add(censusSummaryVo.getFirstPurchasePvDollar());
            // 首购业绩
            firstPurchasePvLocalityTotal = firstPurchasePvLocalityTotal.add(censusSummaryVo.getFirstPurchasePvLocality());
            // 复购金额(￥)
            repurchaseAmountTotal = repurchaseAmountTotal.add(censusSummaryVo.getRepurchaseAmount());
            // 复购金额($)
            repurchaseAmountDollarTotal = repurchaseAmountDollarTotal.add(censusSummaryVo.getRepurchaseAmountDollar());
            // 复购金额
            repurchaseAmountLocalityTotal = repurchaseAmountLocalityTotal.add(censusSummaryVo.getRepurchaseAmountLocality());
            //  复购业绩(￥)
            repurchasePvTotal = repurchasePvTotal.add(censusSummaryVo.getRepurchasePv());
            // 复购业绩($)
            repurchasePvDollarTotal = repurchasePvDollarTotal.add(censusSummaryVo.getRepurchasePvDollar());
            // 复购业绩
            repurchasePvLocalityTotal = repurchasePvLocalityTotal.add(censusSummaryVo.getRepurchasePvLocality());
            // 金额总计(￥)
            amountTotalTotal = amountTotalTotal.add(censusSummaryVo.getAmountTotal());
            // 金额总计($)
            amountTotalDollarTotal = amountTotalDollarTotal.add(censusSummaryVo.getAmountTotalDollar());
            // 金额总计
            amountTotalLocalityTotal = amountTotalLocalityTotal.add(censusSummaryVo.getAmountTotalLocality());
            // 业绩汇总(￥)
            pvTotalTotal = pvTotalTotal.add(censusSummaryVo.getPvTotal());
            //业绩汇总($)
            pvTotalDollarTotal = pvTotalDollarTotal.add(censusSummaryVo.getPvTotalDollar());
            // 业绩汇总
            pvTotalLocalityTotal = pvTotalLocalityTotal.add(censusSummaryVo.getPvTotalLocality());
            // 直供金额(￥)
            directSupplyAmountTotal = directSupplyAmountTotal.add(censusSummaryVo.getDirectSupplyAmount());
            // 直供金额($)
            directSupplyAmountDollarTotal = directSupplyAmountDollarTotal.add(censusSummaryVo.getDirectSupplyAmountDollar());
           //直供金额
            directSupplyAmountLocalityTotal = directSupplyAmountLocalityTotal.add(censusSummaryVo.getDirectSupplyAmountLocality());
           //直供业绩(￥)
            directSupplyPvTotal = directSupplyPvTotal.add(censusSummaryVo.getDirectSupplyPv());
            // 直供业绩($)
            directSupplyPvDollarTotal = directSupplyPvDollarTotal.add(censusSummaryVo.getDirectSupplyPvDollar());
            //直供业绩
            directSupplyPvLocalityTotal = directSupplyPvLocalityTotal.add(censusSummaryVo.getDirectSupplyPvLocality());
        }
        // 汇总
        CensusSummaryCopyVo censusSummaryCopyVo = new CensusSummaryCopyVo();
        censusSummaryCopyVo.setRegisterAmount(registerAmountTotal);
        censusSummaryCopyVo.setRegisterAmountDollar(registerAmountDollarTotal);
        censusSummaryCopyVo.setRegisterAmountLocality(registerAmountLocalityTotal);
        censusSummaryCopyVo.setRegisterPv(registerPvTotal);
        censusSummaryCopyVo.setRegisterPvDollar(registerPvDollarTotal);
        censusSummaryCopyVo.setRegisterPvLocality(registerPvLocalityTotal);
        censusSummaryCopyVo.setUpgradeAmount(upgradeAmountTotal);
        censusSummaryCopyVo.setUpgradeAmountDollar(upgradeAmountDollarTotal);
        censusSummaryCopyVo.setUpgradeAmountLocality(upgradeAmountLocalityTotal);
        censusSummaryCopyVo.setUpgradePv(upgradePvTotal);
        censusSummaryCopyVo.setUpgradePvDollar(upgradePvDollar);
        censusSummaryCopyVo.setUpgradePvLocality(upgradePvLocalityTotal);
        censusSummaryCopyVo.setFirstPurchaseAmount(firstPurchaseAmountTotal);
        censusSummaryCopyVo.setFirstPurchaseAmountDollar(firstPurchaseAmountDollarTotal);
        censusSummaryCopyVo.setFirstPurchaseAmountLocality(firstPurchaseAmountLocalityTotal);
        censusSummaryCopyVo.setFirstPurchasePv(firstPurchasePvTotal);
        censusSummaryCopyVo.setFirstPurchasePvDollar(firstPurchasePvDollarTotal);
        censusSummaryCopyVo.setFirstPurchasePvLocality(firstPurchasePvLocalityTotal);
        censusSummaryCopyVo.setRepurchaseAmount(repurchaseAmountTotal);
        censusSummaryCopyVo.setRepurchaseAmountDollar(repurchaseAmountDollarTotal);
        censusSummaryCopyVo.setRepurchaseAmountLocality(repurchaseAmountLocalityTotal);
        censusSummaryCopyVo.setRepurchasePv(repurchasePvTotal);
        censusSummaryCopyVo.setRepurchasePvDollar(repurchasePvDollarTotal);
        censusSummaryCopyVo.setRepurchasePvLocality(repurchasePvLocalityTotal);
        censusSummaryCopyVo.setAmountTotal(amountTotalTotal);
        censusSummaryCopyVo.setAmountTotalDollar(amountTotalDollarTotal);
        censusSummaryCopyVo.setAmountTotalLocality(amountTotalLocalityTotal);
        censusSummaryCopyVo.setPvTotal(pvTotalTotal);
        censusSummaryCopyVo.setPvTotalDollar(pvTotalDollarTotal);
        censusSummaryCopyVo.setPvTotalLocality(pvTotalLocalityTotal);
        censusSummaryCopyVo.setDirectSupplyAmountDollar(directSupplyAmountDollarTotal);
        censusSummaryCopyVo.setDirectSupplyAmountLocality(directSupplyAmountLocalityTotal);
        censusSummaryCopyVo.setDirectSupplyPv(directSupplyPvTotal);
        censusSummaryCopyVo.setDirectSupplyPvDollar(directSupplyPvDollarTotal);
        censusSummaryCopyVo.setDirectSupplyPvLocality(directSupplyPvLocalityTotal);
        rsList.add(censusSummaryCopyVo);


        ExcelUtil<CensusSummaryCopyVo> util = new ExcelUtil<>(CensusSummaryCopyVo.class);
        util.exportExcel(response, rsList, "业绩汇总信息");
    }

//    /**
//     * 测试
//     * @return
//     */
//    @GetMapping("test")
//    public AjaxResult test(){
//        // 用户推荐平均用时计算
//        R<Boolean> r = memberServiceApi.averageTimeCalculation();
//
//        return toAjax(true);
//    }
}
