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.constant.CountryConstants;
import com.hzs.common.core.constant.msg.FinanceMsgConstants;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.*;
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.ICuBonusLenjoyStatisService;
import com.hzs.member.statis.vo.CuBonusLenjoyStatisVO;
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.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

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

    @Autowired
    private ICuBonusLenjoyStatisService iCuBonusLenjoyStatisService;
    @DubboReference
    ICurrencyServiceApi currencyServiceApi;


    /**
     * 奖金拨比列表
     **/
    @AccessPermissions("LenTopPerformance")
    @GetMapping("/list")
    @Log(module = EOperationModule.BONUS_DIAL_RATIO, business = EOperationBusiness.BONUS_DIAL_VERTEX_RATIO_LIST, method = EOperationMethod.SELECT)
    public TableDataInfo list(CuBonusLenjoyStatisVO cuBonusLenjoyStatisVO) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        List<CuBonusLenjoyStatisVO> slList;
        R<CurrencyDTO> currency = currencyServiceApi.getCurrency(pkCountry);
        BigDecimal exchangeRate = currency.getData().getInExchangeRate() == null ? BigDecimal.ONE : currency.getData().getInExchangeRate();
        cuBonusLenjoyStatisVO.setRate(exchangeRate);
        BigDecimal cent = new BigDecimal(100);
        cuBonusLenjoyStatisVO.setPerCent(exchangeRate.multiply(cent));
        cuBonusLenjoyStatisVO.setPkCountry(pkCountry);
        //默认查询当前日期的前一天
        Date date = DateUtils.currentDate();
        if (StringUtils.isEmpty(cuBonusLenjoyStatisVO.getStartDate()) ||
                StringUtils.isEmpty(cuBonusLenjoyStatisVO.getEndDate())) {
            cuBonusLenjoyStatisVO.setStartDate(DateUtils.beforeDateStr(1, ChronoUnit.DAYS, DateUtils.YYYY_MM_DD, date));
            cuBonusLenjoyStatisVO.setEndDate(DateUtils.beforeDateStr(1, ChronoUnit.DAYS, DateUtils.YYYY_MM_DD, date));
        }
        //查询顶点会员
        startPage();
        //查询乐享顶点奖金拨比列表
        slList = iCuBonusLenjoyStatisService.queryCuBonusStatis(cuBonusLenjoyStatisVO);
        if (CollectionUtil.isNotEmpty(slList)) {
            slList.forEach(itm -> {
                itm.setCloudRealSubtotal(itm.getCloudRealSubtotal().multiply(exchangeRate));
            });
        }
        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, CuBonusLenjoyStatisVO cuBonusLenjoyStatisVO) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        List<CuBonusLenjoyStatisVO> slList = null;
        CuBonusLenjoyStatisVO subtotal = null;
        R<CurrencyDTO> currency = currencyServiceApi.getCurrency(pkCountry);
        BigDecimal exchangeRate = currency.getData().getInExchangeRate() == null ? BigDecimal.ONE : currency.getData().getInExchangeRate();
        cuBonusLenjoyStatisVO.setRate(exchangeRate);
        BigDecimal cent = new BigDecimal(100);
        cuBonusLenjoyStatisVO.setPerCent(exchangeRate.multiply(cent));
        cuBonusLenjoyStatisVO.setPkCountry(pkCountry);
        //默认查询当前日期的前一天
        Date date = DateUtils.currentDate();
        if (StringUtils.isEmpty(cuBonusLenjoyStatisVO.getStartDate()) ||
                StringUtils.isEmpty(cuBonusLenjoyStatisVO.getEndDate())) {
            cuBonusLenjoyStatisVO.setStartDate(DateUtils.beforeDateStr(1, ChronoUnit.DAYS, DateUtils.YYYY_MM_DD, date));
            cuBonusLenjoyStatisVO.setEndDate(DateUtils.beforeDateStr(1, ChronoUnit.DAYS, DateUtils.YYYY_MM_DD, date));
        }
        startPage();
        if (pkCountry.equals(CountryConstants.CHINA_COUNTRY)) {
            slList = iCuBonusLenjoyStatisService.queryCuBonusStatis(cuBonusLenjoyStatisVO);
            if (CollectionUtil.isNotEmpty(slList)) {
                slList.forEach(itm -> {
                    itm.setCloudRealSubtotal(itm.getCloudRealSubtotal().multiply(exchangeRate));
                });
            }
            //合计
            subtotal = iCuBonusLenjoyStatisService.queryCuBonusSumStatis(cuBonusLenjoyStatisVO);
        }
        //合计
        if (subtotal == null) {
            subtotal = new CuBonusLenjoyStatisVO();
        }
        subtotal.setMemberCode("合计");
        slList.add(subtotal);
        ExcelUtil<CuBonusLenjoyStatisVO> util = new ExcelUtil<>(CuBonusLenjoyStatisVO.class);
        util.exportExcel(response, slList, "拨比列表");
    }

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

    /**
     * 统计历史奖金拨比数据, 手动统计某一天
     **/
    @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<CuBonusLenjoyStatisVO> vertexList = iCuBonusLenjoyStatisService.vertexList(null);
        List<Integer> pkVertexList = new ArrayList<>();
        pkVertexList.addAll(vertexList.stream().map(CuBonusLenjoyStatisVO::getPkVertex).collect(Collectors.toList()));
        //删除数据
        if (CollectionUtil.isNotEmpty(pkVertexList)) {
            iCuBonusLenjoyStatisService.delStatis(startDate, endDate, pkVertexList);
        }
        //重新统计
        Boolean b = iCuBonusLenjoyStatisService.anewStatisHistoryBonus(startDate, endDate);
        return AjaxResult.success(b);
    }

}
