package com.hzs.bonus.member.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.hzs.bonus.achieve.service.ICuMemberSettlePeriodService;
import com.hzs.bonus.member.param.MemberActivityParam;
import com.hzs.bonus.member.service.ICuMemberActivity;
import com.hzs.bonus.member.vo.MemberActivityDetailVO;
import com.hzs.bonus.member.vo.MemberActivityExportVO;
import com.hzs.bonus.member.vo.MemberActivityVO;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.HttpStatus;
import com.hzs.common.core.constant.TableNameConstants;
import com.hzs.common.core.constant.msg.MemberMsgConstants;
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.enums.EPlaceDept;
import com.hzs.common.core.utils.ComputeUtil;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.core.utils.TransactionUtils;
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.domain.scm.ic.ext.IcOnHandNumExt;
import com.hzs.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.temporal.ChronoUnit;
import java.util.*;

/**
 * @description: 会员活动数据查询
 * @author: sui q
 * @time: 2024/7/8 10:24
 * @classname: CuMemberActivityController
 * @package_name: com.hzs.report.member.controller.manage
 * version 1.0.0
 */
@RestController
@RequestMapping("/manager/member-awards")
public class CuMemberActivityController extends BaseController {

    @Autowired
    private ICuMemberSettlePeriodService cuMemberSettlePeriodService;

    @Autowired
    private ICuMemberActivity memberActivity;

    /*
     * @description: 活动数据查询
     * @author: sui q
     * @date: 2024/7/8 10:31
     * @param: null null
     **/
    @Log(module = EOperationModule.ACTIVITY_DATA, business = EOperationBusiness.ACTIVITY_DATA, method = EOperationMethod.SELECT)
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody MemberActivityParam memberActivityParam) {
        // 查询是否是当天，当天查询昨天的数据
        TableDataInfo tableDataInfo = getTableDataInfo(memberActivityParam);
        if (tableDataInfo != null) {
            return tableDataInfo;
        }
        // 判断国家，国家为null时说明是所有业绩，查询CU_MEMBER_SETTLE_D，安置业绩有国家时查询 CU_MEMBER_SETTLE_C
        Date endDate = memberActivityParam.getEndDate();
        if(DateUtils.compareDateAfter(memberActivityParam.getEndDate(), DateUtils.beforeDate(1, ChronoUnit.DAYS))){
            endDate = DateUtils.beforeDate(1, ChronoUnit.DAYS);
            memberActivityParam.setEndDate(DateUtils.currentDate());
        }
        Integer period = cuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, endDate)).getPkId();
        String currentSettleTableName = TableNameConstants.CU_MEMBER_SETTLE + period;
        String currentTableNameDetail = TableNameConstants.CU_MEMBER_SETTLE_D + period;
        String currentCountryTableNameDetail = TableNameConstants.CU_MEMBER_SETTLE_C + period;

        Integer beforePeriod = cuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, memberActivityParam.getStartDate())).getPkId();
        String beforeTableNameDetail = TableNameConstants.CU_MEMBER_SETTLE_D + beforePeriod;
        String beforeCountryTableNameDetail = TableNameConstants.CU_MEMBER_SETTLE_C + beforePeriod;
        // 先过滤推荐,从订单表中过滤
        List<MemberActivityVO> memberActivityVoList;
        if(memberActivityParam.getPkRecGrade() != null || (memberActivityParam.getRecNum() != null && memberActivityParam.getRecNum() >0)
                || (memberActivityParam.getRecAchieve() != null && ComputeUtil.compareValue(memberActivityParam.getRecAchieve()))){
            memberActivityVoList = memberActivity.queryRecCuMemberActivityVO(currentSettleTableName, memberActivityParam);
        }else {
            memberActivityVoList = memberActivity.queryCuMemberActivityVO(currentSettleTableName, memberActivityParam);
        }
        List<MemberActivityVO> returnMemberActivityVoList = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(memberActivityVoList)){
            // 过滤安置，安置过滤分为全部国家业绩和某个国家业绩，当需要查询国家时，从 CU_MEMBER_SETTLE_C 查询
            Map<Long, List<MemberActivityDetailVO>> memberActivityDetailMap;
            if(memberActivityParam.getPkCountry() == null){
                memberActivityDetailMap = memberActivity.queryCuMemberActivityDetailVO(beforeTableNameDetail, currentTableNameDetail, memberActivityVoList, memberActivityParam);
            }else{
                memberActivityDetailMap = memberActivity.queryCouCuMemberActivityDetailVO(beforeCountryTableNameDetail, currentCountryTableNameDetail, memberActivityVoList, memberActivityParam);
            }
            if(memberActivityDetailMap != null && memberActivityDetailMap.size() > 0) {
                for (MemberActivityVO memberActivityVO : memberActivityVoList) {
                    if(memberActivityDetailMap.containsKey(memberActivityVO.getPkMember())){
                        memberActivityVO.setMemberActivityDetailVOList(memberActivityDetailMap.get(memberActivityVO.getPkMember()));
                        returnMemberActivityVoList.add(memberActivityVO);
                    }
                }
            }
        }

        return getDataTable(returnMemberActivityVoList);
    }

    private TableDataInfo getTableDataInfo(MemberActivityParam memberActivityParam) {
        if(memberActivityParam.getPkGrade() == null || memberActivityParam.getPkAwards() == null ||
       memberActivityParam.getStartDate() == null || memberActivityParam.getEndDate() == null ||
         memberActivityParam.getPlaceDeptAchieve1() == null || memberActivityParam.getPlaceDeptAchieve2() == null ||
         memberActivityParam.getPlaceDeptAchieve3() == null || memberActivityParam.getPlaceDeptAchieve4() == null ||
         memberActivityParam.getPlaceDeptAchieve5() == null){
            return getTableDataInfo();
        }
        if(memberActivityParam.getPkRecGrade() != null || memberActivityParam.getRecNum() != null || memberActivityParam.getRecAchieve() != null){
            if(memberActivityParam.getPkRecGrade() == null || memberActivityParam.getRecNum() == null || memberActivityParam.getRecAchieve() == null){
                return getTableDataInfo();
            }
        }
        return null;
    }

    /**
     * 分页查询会员某个时间段内的奖金汇总
     */
    @PostMapping("/export")
    @Log(module = EOperationModule.ACTIVITY_DATA, business = EOperationBusiness.ACTIVITY_DATA, method = EOperationMethod.EXPORT)
    public void exportListOnHandNum(HttpServletResponse response, MemberActivityParam memberActivityParam){
        // 查询是否是当天，当天查询昨天的数据
        TableDataInfo tableDataInfo = getTableDataInfo(memberActivityParam);
        if (tableDataInfo != null) {
            return;
        }
        // 判断国家，国家为null时说明是所有业绩，查询CU_MEMBER_SETTLE_D，安置业绩有国家时查询 CU_MEMBER_SETTLE_C
        Date endDate = memberActivityParam.getEndDate();
        if(DateUtils.compareDateAfter(memberActivityParam.getEndDate(), DateUtils.beforeDate(1, ChronoUnit.DAYS))){
            endDate = DateUtils.beforeDate(1, ChronoUnit.DAYS);
            memberActivityParam.setEndDate(DateUtils.currentDate());
        }
        Integer period = cuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, endDate)).getPkId();
        String currentSettleTableName = TableNameConstants.CU_MEMBER_SETTLE + period;
        String currentTableNameDetail = TableNameConstants.CU_MEMBER_SETTLE_D + period;
        String currentCountryTableNameDetail = TableNameConstants.CU_MEMBER_SETTLE_C + period;

        Integer beforePeriod = cuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, memberActivityParam.getStartDate())).getPkId();
        String beforeTableNameDetail = TableNameConstants.CU_MEMBER_SETTLE_D + beforePeriod;
        String beforeCountryTableNameDetail = TableNameConstants.CU_MEMBER_SETTLE_C + beforePeriod;
        // 先过滤推荐,从订单表中过滤
        List<MemberActivityVO> memberActivityVoList;
        if(memberActivityParam.getPkRecGrade() != null || (memberActivityParam.getRecNum() != null && memberActivityParam.getRecNum() >0)
                || (memberActivityParam.getRecAchieve() != null && ComputeUtil.compareValue(memberActivityParam.getRecAchieve()))){
            memberActivityVoList = memberActivity.queryRecCuMemberActivityVO(currentSettleTableName, memberActivityParam);
        }else {
            memberActivityVoList = memberActivity.queryCuMemberActivityVO(currentSettleTableName, memberActivityParam);
        }
        List<MemberActivityExportVO> memberActivityExportVoList = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(memberActivityVoList)){
            // 过滤安置，安置过滤分为全部国家业绩和某个国家业绩，当需要查询国家时，从 CU_MEMBER_SETTLE_C 查询
            Map<Long, List<MemberActivityDetailVO>> memberActivityDetailMap;
            if(memberActivityParam.getPkCountry() == null){
                memberActivityDetailMap = memberActivity.queryCuMemberActivityDetailVO(beforeTableNameDetail, currentTableNameDetail, memberActivityVoList, memberActivityParam);
            }else{
                memberActivityDetailMap = memberActivity.queryCouCuMemberActivityDetailVO(beforeCountryTableNameDetail, currentCountryTableNameDetail, memberActivityVoList, memberActivityParam);
            }
            if(memberActivityDetailMap != null && memberActivityDetailMap.size() > 0) {
                for (MemberActivityVO memberActivityVO : memberActivityVoList) {
                    if(memberActivityDetailMap.containsKey(memberActivityVO.getPkMember())){
                        memberActivityVO.setMemberActivityDetailVOList(memberActivityDetailMap.get(memberActivityVO.getPkMember()));
                        for (MemberActivityDetailVO memberActivityDetailVO : memberActivityVO.getMemberActivityDetailVOList()) {
                            MemberActivityExportVO memberActivityExportVO = BeanUtil.copyProperties(memberActivityVO, MemberActivityExportVO.class);
                            for(EPlaceDept placeDept:EPlaceDept.values()){
                                if(memberActivityDetailVO.getPlaceDept().equals(placeDept.getValue())){
                                    memberActivityExportVO.setPlaceDeptVal(placeDept.getLabel());
                                    break;
                                }
                            }
                            memberActivityExportVO.setSumPv(memberActivityDetailVO.getSumPv());
                            memberActivityExportVoList.add(memberActivityExportVO);
                        }
                    }
                }
            }
        }
        ExcelUtil<MemberActivityExportVO> util = new ExcelUtil<>(MemberActivityExportVO.class);
        util.exportExcel(response, memberActivityExportVoList, "活动数据导出");
    }

    private static TableDataInfo getTableDataInfo() {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.ERROR);
        rspData.setMsg(TransactionUtils.getContent(MemberMsgConstants.REQUIRED_NOT_EMPTY));
        return rspData;
    }

}
