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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.hzs.common.core.annotation.AccessPermissions;
import com.hzs.common.core.constant.EnumsPrefixConstants;
import com.hzs.common.core.enums.*;
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.domain.member.ext.CuMemberExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.service.ITransactionCommonService;
import com.hzs.member.achieve.service.ICuMemberAchieveService;
import com.hzs.member.base.service.ICuMemberService;
import com.hzs.member.settlecenter.param.PlacementDataParam;
import com.hzs.member.settlecenter.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 结算中心
 *
 * @Description:
 * @Author: ljc
 * @Time: 2023/5/24 16:52
 * @Classname: SettleCenterController
 * @Package_name: com.hzs.member.settlecenter.controller.manage
 */
@RestController
@RequestMapping("/manager/center")
public class SettleCenterController extends BaseController {

    @Autowired
    private ICuMemberAchieveService iCuMemberAchieveService;

    @Autowired
    private ITransactionCommonService iTransactionCommonService;

    @Autowired
    private ICuMemberService cuMemberService;

    /**
     * 查询安置数据
     *
     * @param param 入参
     * @return TableDataInfo
     */
    @AccessPermissions("placement")
    @PostMapping("show-place")
    public TableDataInfo showPlace(@RequestBody PlacementDataParam param) {
        List<PlacementDataVO> placementDataVOList = new ArrayList<>();
        Integer pkSettleCountry = param.getPkSettleCountry();
        if (null == pkSettleCountry) {
            param.setPkSettleCountry(SecurityUtils.getPkCountry());
        }
        String memberCode = param.getMemberCode();
        String startDate = param.getStartDate();
        String endDate = param.getEndDate();
        if (StringUtils.isNotEmpty(memberCode) && StringUtils.isNotEmpty(startDate) && StringUtils.isNotEmpty(endDate)) {
            CuMemberExt cuMember = cuMemberService.getMemberByCode(param.getMemberCode());
            param.setPkMember(cuMember.getPkId());
            startPage();
            placementDataVOList = iCuMemberAchieveService.selectPlacementDataList(param);
        }
        return getDataTable(placementDataVOList);
    }


    /**
     * 查询推荐数据
     *
     * @param param 入参
     * @return TableDataInfo
     */
    @AccessPermissions("recommendData")
    @PostMapping("show-recommend")
    public TableDataInfo showRecommend(@RequestBody PlacementDataParam param) {
        List<PlacementDataVO> placementDataVOList = new ArrayList<>();
        Integer pkSettleCountry = param.getPkSettleCountry();
        String memberCode = param.getMemberCode();
        String startDate = param.getStartDate();
        String endDate = param.getEndDate();
        if (null == pkSettleCountry) {
            param.setPkSettleCountry(SecurityUtils.getPkCountry());
        }

        if (StringUtils.isNotEmpty(memberCode) && StringUtils.isNotEmpty(startDate) && StringUtils.isNotEmpty(endDate)) {
            CuMemberExt member = cuMemberService.getMemberByCode(memberCode);
            if (null == member) {
                return getDataTable(placementDataVOList);
            }
            param.setPkMember(member.getPkId());
            startPage();
            placementDataVOList = iCuMemberAchieveService.selectRecommendDataList(param);
        }
        return getDataTable(placementDataVOList);
    }


    /**
     * 查询安置数据导出
     *
     * @param response 响应
     * @param param    查询入参
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, PlacementDataParam param) {
        Integer pkSettleCountry = param.getPkSettleCountry();
        if (null == pkSettleCountry) {
            param.setPkSettleCountry(SecurityUtils.getPkCountry());
        }

        List<PlacementExportDataVO> dataVOList = new ArrayList<>();
        String memberCode = param.getMemberCode();
        String startDate = param.getStartDate();
        String endDate = param.getEndDate();
        if (StringUtils.isNotEmpty(memberCode) && StringUtils.isNotEmpty(startDate) && StringUtils.isNotEmpty(endDate)) {
            CuMemberExt cuMember = cuMemberService.getMemberByCode(param.getMemberCode());
            param.setPkMember(cuMember.getPkId());
            List<PlacementDataVO> placementDataVOList = iCuMemberAchieveService.selectPlacementDataList(param);
            if (CollUtil.isNotEmpty(placementDataVOList)) {
                //枚举翻译
                Map<String, String> transactionMap = iTransactionCommonService.exportEnumTransaction(EDadaType.values());
                //数据库翻译
                Map<Integer, String> dataBaseMap = iTransactionCommonService.exportEnumTransactionByDB(EnumsPrefixConstants.KEY_AWARD);
                PlacementExportDataVO dataVO;
                for (PlacementDataVO placementDataVO : placementDataVOList) {
                    Integer pkAwards = placementDataVO.getPkAwards();
                    String pkAwardsVal = dataBaseMap.get(pkAwards);

                    dataVO = BeanUtil.copyProperties(placementDataVO, PlacementExportDataVO.class);
                    dataVO.setPkAwardsVal(pkAwardsVal);
                    List<PalaceAchieveDetailVO> detailVOList = placementDataVO.getDetailVOList();
                    if (CollUtil.isNotEmpty(detailVOList)) {
                        for (PalaceAchieveDetailVO detailVO : detailVOList) {
                            Integer dataType = detailVO.getDataType();
                            if (dataType.equals(EDadaType.FIRST_PURCHASE.getValue())) {
                                dataVO.setLeftNewAchieve(detailVO.getLeftNewAchieve());
                                dataVO.setRightNewAchieve(detailVO.getRightNewAchieve());
                                dataVO.setLeftNewAmount(detailVO.getLeftNewAmount());
                                dataVO.setRightNewAmount(detailVO.getRightNewAmount());
                                dataVO.setLeftSumAchieve(detailVO.getLeftSumAchieve());
                                dataVO.setRightSumAchieve(detailVO.getRightSumAchieve());
                                dataVO.setLeftBalance(detailVO.getLeftBalance());
                                dataVO.setRightBalance(detailVO.getRightBalance());
                                dataVO.setDataTypeVal(transactionMap.get(EnumsPrefixConstants.DADA_TYPE + EDadaType.FIRST_PURCHASE.getValue()));

                                dataVO.setSettleLeftNewAchieve(detailVO.getSettleLeftNewAchieve());
                                dataVO.setSettleRightNewAchieve(detailVO.getSettleRightNewAchieve());
                                dataVO.setSettleLeftNewAmount(detailVO.getSettleLeftNewAmount());
                                dataVO.setSettleRightNewAmount(detailVO.getSettleRightNewAmount());
                                dataVO.setSettleLeftSumAchieve(detailVO.getSettleLeftSumAchieve());
                                dataVO.setSettleRightSumAchieve(detailVO.getSettleRightSumAchieve());
                                dataVO.setSettleLeftBalance(detailVO.getSettleLeftBalance());
                                dataVO.setSettleRightBalance(detailVO.getSettleRightBalance());
                            }

                            if (dataType.equals(EDadaType.REPURCHASE.getValue())) {
                                dataVO.setRepLeftNewAchieve(detailVO.getLeftNewAchieve());
                                dataVO.setRepRightNewAchieve(detailVO.getRightNewAchieve());
                                dataVO.setRepLeftNewAmount(detailVO.getLeftNewAmount());
                                dataVO.setRepRightNewAmount(detailVO.getRightNewAmount());
                                dataVO.setRepLeftSumAchieve(detailVO.getLeftSumAchieve());
                                dataVO.setRepRightSumAchieve(detailVO.getRightSumAchieve());
                                dataVO.setRepLeftBalance(detailVO.getLeftBalance());
                                dataVO.setRepRightBalance(detailVO.getRightBalance());

                                dataVO.setRepSettleLeftNewAchieve(detailVO.getSettleLeftNewAchieve());
                                dataVO.setRepSettleRightNewAchieve(detailVO.getSettleRightNewAchieve());
                                dataVO.setRepSettleLeftNewAmount(detailVO.getSettleLeftNewAmount());
                                dataVO.setRepSettleRightNewAmount(detailVO.getSettleRightNewAmount());
                                dataVO.setRepSettleLeftSumAchieve(detailVO.getSettleLeftSumAchieve());
                                dataVO.setRepSettleRightSumAchieve(detailVO.getSettleRightSumAchieve());
                                dataVO.setRepSettleLeftBalance(detailVO.getSettleLeftBalance());
                                dataVO.setRepSettleRightBalance(detailVO.getSettleRightBalance());
                                dataVO.setDataTypeVal(transactionMap.get(EnumsPrefixConstants.DADA_TYPE + EDadaType.REPURCHASE.getValue()));
                            }

                            if (dataType.equals(EDadaType.REAL.getValue())) {
                                dataVO.setRealLeftNewAchieve(detailVO.getLeftNewAchieve());
                                dataVO.setRealRightNewAchieve(detailVO.getRightNewAchieve());
                                dataVO.setRealLeftNewAmount(detailVO.getLeftNewAmount());
                                dataVO.setRealRightNewAmount(detailVO.getRightNewAmount());
                                dataVO.setRealLeftSumAchieve(detailVO.getLeftSumAchieve());
                                dataVO.setRealRightSumAchieve(detailVO.getRightSumAchieve());
                                dataVO.setRealLeftBalance(detailVO.getLeftBalance());
                                dataVO.setRealRightBalance(detailVO.getRightBalance());

                                dataVO.setRealSettleLeftNewAchieve(detailVO.getSettleLeftNewAchieve());
                                dataVO.setRealSettleRightNewAchieve(detailVO.getSettleRightNewAchieve());
                                dataVO.setRealSettleLeftNewAmount(detailVO.getSettleLeftNewAmount());
                                dataVO.setRealSettleRightNewAmount(detailVO.getSettleRightNewAmount());
                                dataVO.setRealSettleLeftSumAchieve(detailVO.getSettleLeftSumAchieve());
                                dataVO.setRealSettleRightSumAchieve(detailVO.getSettleRightSumAchieve());
                                dataVO.setRealSettleLeftBalance(detailVO.getSettleLeftBalance());
                                dataVO.setRealSettleRightBalance(detailVO.getSettleRightBalance());
                                dataVO.setDataTypeVal(transactionMap.get(EnumsPrefixConstants.DADA_TYPE + EDadaType.REPURCHASE.getValue()));
                            }

                        }
                    }
                    dataVOList.add(dataVO);
                }
            }
        }
        ExcelUtil<PlacementExportDataVO> util = new ExcelUtil<>(PlacementExportDataVO.class);
        util.exportExcel(response, dataVOList, "安置数据导出");
    }


    /**
     * 推荐数据导出
     *
     * @param response 响应
     * @param param    查询入参
     */
    @PostMapping("/recommend-export")
    public void recommendExport(HttpServletResponse response, PlacementDataParam param) {
        Integer pkSettleCountry = param.getPkSettleCountry();
        if (null == pkSettleCountry) {
            param.setPkSettleCountry(SecurityUtils.getPkCountry());
        }
        List<RecommendExportDataVO> dataVOList = new ArrayList<>();
        String memberCode = param.getMemberCode();
        String startDate = param.getStartDate();
        String endDate = param.getEndDate();
        if (StringUtils.isNotEmpty(memberCode) && StringUtils.isNotEmpty(startDate) && StringUtils.isNotEmpty(endDate)) {
            CuMemberExt member = cuMemberService.getMemberByCode(memberCode);
            param.setPkMember(member.getPkId());
            List<PlacementDataVO> placementDataVOList = iCuMemberAchieveService.selectRecommendDataList(param);
            if (CollUtil.isNotEmpty(placementDataVOList)) {
                //枚举翻译
                Map<String, String> transactionMap = iTransactionCommonService.exportEnumTransaction(ERecommendDadaType.values());
                //数据库翻译
                Map<Integer, String> dataBaseMap = iTransactionCommonService.exportEnumTransactionByDB(EnumsPrefixConstants.KEY_AWARD);
                RecommendExportDataVO exportDataVO;
                for (PlacementDataVO placementDataVO : placementDataVOList) {
                    exportDataVO = BeanUtil.copyProperties(placementDataVO, RecommendExportDataVO.class);
                    //奖衔
                    Integer pkAwards = placementDataVO.getPkAwards();
                    exportDataVO.setPkAwardsVal(dataBaseMap.get(pkAwards));

                    List<RecommendDataDetailVO> recommendDetailList = placementDataVO.getRecommendDetailList();
                    if (CollUtil.isNotEmpty(recommendDetailList)) {
                        for (RecommendDataDetailVO detailVO : recommendDetailList) {
                            Integer dataType = detailVO.getDataType();
                            if (dataType.equals(ERecommendDadaType.TEAM.getValue())) {
                                exportDataVO.setDataTypeVal(transactionMap.get(EnumsPrefixConstants.RECOMMEND_DADA_TYPE + ERecommendDadaType.TEAM.getValue()));
                                convertTeam(detailVO, exportDataVO);
                            }
                            if (dataType.equals(ERecommendDadaType.DEDUPLICATION.getValue())) {
                                exportDataVO.setReplyDataTypeVal(transactionMap.get(EnumsPrefixConstants.RECOMMEND_DADA_TYPE + ERecommendDadaType.DEDUPLICATION.getValue()));
                                convertReply(detailVO, exportDataVO);
                            }
                        }
                    }
                    dataVOList.add(exportDataVO);
                }

            }
        }
        ExcelUtil<RecommendExportDataVO> util = new ExcelUtil<>(RecommendExportDataVO.class);
        util.exportExcel(response, dataVOList, "推荐数据导出");
    }

    /**
     * 查询安置图表
     *
     * @param param 入参
     * @return AjaxResult
     */
    @PostMapping("show-place-image")
    public AjaxResult selectPlaceImage(@RequestBody PlacementDataParam param) {
        param.setPkSettleCountry(SecurityUtils.getPkCountry());
        return AjaxResult.success(iCuMemberAchieveService.selectPlaceImage(param));
    }

    /**
     * 查询推荐体系图表
     *
     * @param param 入参
     * @return TableDataInfo
     */
    @PostMapping("show-vertex-image")
    public AjaxResult showRecommendImage(@RequestBody PlacementDataParam param) {
        param.setPkSettleCountry(SecurityUtils.getPkCountry());
        return AjaxResult.success(iCuMemberAchieveService.selectRecommendVertexImage(param));
    }


    /**
     * 查询推荐团队图表
     *
     * @param param 入参
     * @return TableDataInfo
     */
    @PostMapping("show-team-image")
    public AjaxResult showRecommendTeamImage(@RequestBody PlacementDataParam param) {
        param.setPkSettleCountry(SecurityUtils.getPkCountry());
        return AjaxResult.success(iCuMemberAchieveService.selectRecommendTeamImage(param));
    }


    /**
     * 转化团队数据
     *
     * @param detailVO     详情VO
     * @param exportDataVO 导出数据VO
     * @return RecommendExportDataVO
     */
    private RecommendExportDataVO convertTeam(RecommendDataDetailVO detailVO, RecommendExportDataVO exportDataVO) {
        exportDataVO.setRealAmount(detailVO.getRealAmount());
        exportDataVO.setRealAchieve(detailVO.getRealAchieve());
        exportDataVO.setFirstAmount(detailVO.getFirstAmount());
        exportDataVO.setFirstAchieve(detailVO.getFirstAchieve());
        exportDataVO.setRepeatAmount(detailVO.getRepeatAmount());
        exportDataVO.setRepeatAchieve(detailVO.getRepeatAchieve());
        exportDataVO.setHiFunAmount(detailVO.getHiFunAmount());
        exportDataVO.setHiFunAchieve(detailVO.getHiFunAchieve());
        exportDataVO.setSystemRatio(detailVO.getSystemRatio());
        exportDataVO.setMarketRatio(detailVO.getMarketRatio());

        exportDataVO.setSettleRealAmount(detailVO.getSettleRealAmount());
        exportDataVO.setSettleRealAchieve(detailVO.getSettleRealAchieve());
        exportDataVO.setSettleFirstAmount(detailVO.getSettleFirstAmount());
        exportDataVO.setSettleFirstAchieve(detailVO.getSettleFirstAchieve());
        exportDataVO.setSettleRepeatAmount(detailVO.getSettleRepeatAmount());
        exportDataVO.setSettleRepeatAchieve(detailVO.getSettleRepeatAchieve());
        exportDataVO.setSettleHiFunAmount(detailVO.getSettleHiFunAmount());
        exportDataVO.setSettleHiFunAchieve(detailVO.getSettleHiFunAchieve());
        exportDataVO.setSettleSystemRatio(detailVO.getSettleSystemRatio());
        exportDataVO.setSettleMarketRatio(detailVO.getSettleMarketRatio());
        return exportDataVO;
    }


    /**
     * 转化去重数据
     *
     * @param detailVO     详情VO
     * @param exportDataVO 导出数据VO
     * @return RecommendExportDataVO
     */
    private RecommendExportDataVO convertReply(RecommendDataDetailVO detailVO, RecommendExportDataVO exportDataVO) {
        exportDataVO.setReplyRealAmount(detailVO.getRealAmount());
        exportDataVO.setReplyRealAchieve(detailVO.getRealAchieve());
        exportDataVO.setReplyFirstAmount(detailVO.getFirstAmount());
        exportDataVO.setReplyFirstAchieve(detailVO.getFirstAchieve());
        exportDataVO.setReplyRepeatAmount(detailVO.getRepeatAmount());
        exportDataVO.setReplyRepeatAchieve(detailVO.getRepeatAchieve());
        exportDataVO.setReplyHiFunAmount(detailVO.getHiFunAmount());
        exportDataVO.setReplyHiFunAchieve(detailVO.getHiFunAchieve());
        exportDataVO.setReplySystemRatio(detailVO.getSystemRatio());
        exportDataVO.setReplyMarketRatio(detailVO.getMarketRatio());

        exportDataVO.setReplySettleRealAmount(detailVO.getSettleRealAmount());
        exportDataVO.setReplySettleRealAchieve(detailVO.getSettleRealAchieve());
        exportDataVO.setReplySettleFirstAmount(detailVO.getSettleFirstAmount());
        exportDataVO.setReplySettleFirstAchieve(detailVO.getSettleFirstAchieve());
        exportDataVO.setReplySettleRepeatAmount(detailVO.getSettleRepeatAmount());
        exportDataVO.setReplySettleRepeatAchieve(detailVO.getSettleRepeatAchieve());
        exportDataVO.setReplySettleHiFunAmount(detailVO.getSettleHiFunAmount());
        exportDataVO.setReplySettleHiFunAchieve(detailVO.getSettleHiFunAchieve());
        exportDataVO.setReplySettleSystemRatio(detailVO.getSettleSystemRatio());
        exportDataVO.setReplySettleMarketRatio(detailVO.getSettleMarketRatio());
        return exportDataVO;
    }
}
