package com.hzs.member.achieve.controller.api;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.hzs.common.core.constant.CountryConstants;
import com.hzs.common.core.constant.TableNameConstants;
import com.hzs.common.core.constant.msg.ConfigMsgConstants;
import com.hzs.common.core.constant.msg.FinanceMsgConstants;
import com.hzs.common.core.enums.EAchievementType;
import com.hzs.common.core.enums.EOrderType;
import com.hzs.common.core.utils.ComputeUtil;
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.core.web.page.TableDataInfo;
import com.hzs.common.domain.member.achieve.CuMemberSettlePeriod;
import com.hzs.common.domain.member.achieve.ext.CuMemberAchieveExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.core.utils.TransactionUtils;
import com.hzs.member.achieve.controller.vo.*;
import com.hzs.member.achieve.param.MemberAchieveDisParam;
import com.hzs.member.achieve.param.MonthAchieveParam;
import com.hzs.member.achieve.service.ICuMemberAchieveService;
import com.hzs.member.achieve.service.ICuMemberMonthAchieveService;
import com.hzs.member.achieve.service.ICuMemberSettlePeriodService;
import com.hzs.member.achieve.service.ICuMemberTreeService;
import com.hzs.member.base.service.ICuMemberService;
import com.hzs.system.base.ICurrencyServiceApi;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;

/**
 * description: 会员端查询市场动态的接口
 *
 * @author: sui q
 * @time: 2023/4/6 16:11
 * @classname: CuMemberAchieveDisController
 * @package_name: com.hzs.member.achieve.controller.api
 * version 1.0.0
 */
@Slf4j
@RestController
@RequestMapping("/api/achieve")
public class ApiCuMemberAchieveDisController extends BaseController {
    @Autowired
    private ICuMemberTreeService memberTreeService;
    @DubboReference
    private ICurrencyServiceApi currencyServiceApi;
    @Autowired
    private ICuMemberService memberService;
    @Autowired
    private ICuMemberSettlePeriodService memberSettlePeriodService;
    private ICuMemberAchieveService cuMemberAchieveService;

    private ICuMemberMonthAchieveService cuMemberMonthAchieveService;

    BigDecimal ten = new BigDecimal(10000);

    @Autowired
    public void setCuMemberMonthAchieveService(ICuMemberMonthAchieveService cuMemberMonthAchieveService) {
        this.cuMemberMonthAchieveService = cuMemberMonthAchieveService;
    }

    @Autowired
    public void setCuMemberAchieveService(ICuMemberAchieveService cuMemberAchieveService) {
        this.cuMemberAchieveService = cuMemberAchieveService;
    }

    /**
     * @description: 用于查询会员市场动态
     * @return: AjaxResult
     * @Author: sui q
     * @Date: 2023/2/4 14:48
     */
    @GetMapping("/query-place-tree")
    public AjaxResult queryPlaceParentTree() {
        // 查询架构树
        MemberAchieveDisParam memberAchieveDisParam = new MemberAchieveDisParam();
        memberAchieveDisParam.setPkMember(SecurityUtils.getUserId());
        return AjaxResult.success(cuMemberAchieveService.queryCuMemberAchieveDis(memberAchieveDisParam));
    }

    /**
     * @param memberAchieveDisParam 查询参数
     * @description: 用于查询会员市场动态
     * @return: AjaxResult
     * @Author: sui q
     * @Date: 2023/2/4 14:48
     */
    @PostMapping("/query-place-tree-detail")
    public AjaxResult queryPlaceParentTreeDetail(@RequestBody MemberAchieveDisParam memberAchieveDisParam) {
        // 查询架构树
        if (StringUtils.isEmpty(memberAchieveDisParam.getMemberCode())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.MEMBER_CODE_NOT_NULL));
        }
        memberAchieveDisParam.setPkCountry(SecurityUtils.getPkCountry());
        return AjaxResult.success(cuMemberAchieveService.queryCuMemberDis(memberAchieveDisParam));
    }

    /**
     * @param memberAchieveDisParam 查询参数
     * @description: 用于查询会员最近的业绩
     * @return: AjaxResult
     * @Author: sui q
     * @Date: 2023/2/4 14:48
     */
    @PostMapping("/query-last-achieve")
    public AjaxResult queryLastAchieve(@RequestBody MemberAchieveDisParam memberAchieveDisParam) {
        // 业绩分布 近7日业绩 左区、右区新增人数 血缘业绩  左区、右区新增业绩
        memberAchieveDisParam.setPkMember(SecurityUtils.getUserId());
        return AjaxResult.success(cuMemberAchieveService.queryCuMemberUnderBloodAchieveByPkMember(memberAchieveDisParam));
    }

    /**
     * @param memberAchieveDisParam 查询参数
     * @description: 用于查询会员真实业绩
     * @return: AjaxResult
     * @Author: sui q
     * @Date: 2023/2/4 14:48
     */
    @PostMapping("/query-real-achieve")
    public AjaxResult queryRealAchieve(@RequestBody MemberAchieveDisParam memberAchieveDisParam) {
        // 业绩分布 近7日业绩 左区、右区新增人数 血缘业绩  左区、右区新增业绩
        memberAchieveDisParam.setPkMember(SecurityUtils.getUserId());
        return AjaxResult.success(cuMemberAchieveService.queryCuMemberUnderBloodAchieveByPkMember(memberAchieveDisParam));
    }

    /**
     * 查询会员真实业绩
     */
    @GetMapping("/query-real-performance")
    public AjaxResult queryRealPerformance(Integer month) {
        RealPerformanceVo realPerformanceVo = cuMemberAchieveService.queryRealPerformance(SecurityUtils.getUserId(), SecurityUtils.getPkCountry(), month);
        return AjaxResult.success(realPerformanceVo);
    }

//    /**
//     * 安置业绩分布
//     */
//    @GetMapping("az-achievement")
//    public AjaxResult azAchievement() {
//        int month = DateUtil.month(DateUtils.currentDate()) + 1;
//        // 查询安置伞下
////        List<CuMemberExt> cuMemberExts = memberService.queryPlaceSponsorUnderUmbrella(SecurityUtils.getUserId());
////        List<Long> memberIds = cuMemberExts.stream().map(a -> {
////            return a.getPkId();
////        }).collect(Collectors.toList());
//        AchievementVo achievementVo = cuMemberAchieveService.azAchievement(SecurityUtils.getUserId(), SecurityUtils.getPkCountry(), month);
//
//        return AjaxResult.success(achievementVo);
//    }


    /**
     * 会员真实业绩分布
     *
     * @return
     */
    @GetMapping("/query-real-performance-pic")
    public AjaxResult queryRealPerformancePic() {

        int month = DateUtil.month(DateUtils.currentDate()) + 1;
        AzAchievementPie achievementPie = cuMemberAchieveService.queryRealPerformancePic(SecurityUtils.getUserId(), SecurityUtils.getPkCountry(), month);

        return AjaxResult.success(achievementPie);
    }

//    /**
//     * 首购业绩分布
//     *
//     * @return
//     */
//    @GetMapping("/query-first-performance-pic")
//    public AjaxResult queryFirstPerformancePic() {
//        Integer pkCountry = SecurityUtils.getPkCountry();
//        R<CurrencyDTO> currencyDto = currencyServiceApi.getCurrency(pkCountry);
//        CurrencyDTO currency = currencyDto.getData();
//        String time = null;
//        Long userId = SecurityUtils.getUserId();
//        CuMemberSettleExt memberSettleExt = null;
//        Integer period = null;
//        String settleTableName = null;
//        CuMemberSettlePeriod csPeriod = null;
//        Long pkMember = SecurityUtils.getUserId();
//
//        DateTime dateTime = DateUtil.offsetDay(DateUtils.currentDate(), -1);
//        time = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, dateTime);
//        csPeriod = memberSettlePeriodService.getCuMemberSettlePeriodByDate(time);
//        settleTableName = TableNameConstants.CU_MEMBER_SETTLE + csPeriod.getPkId();
//        memberSettleExt = memberTreeService.queryCuMemberTreeByPkMemberTable(settleTableName, pkMember);
//        // 左区业绩
//        BigDecimal leftAchieve = BigDecimal.ZERO;
//        // 右区业绩
//        BigDecimal rightAchieve = BigDecimal.ZERO;
//        if (memberSettleExt != null && memberSettleExt.getASumPv() != null) {
//            leftAchieve = memberSettleExt.getASumPv();
//        }
//        if (memberSettleExt != null && memberSettleExt.getBSumPv() != null){
//            rightAchieve = memberSettleExt.getBSumPv();
//        }
//        //
//        AchievementVo achievementVo = new AchievementVo();
//        if (pkCountry.equals(CountryConstants.CHINA_COUNTRY)) {
//            achievementVo.setLeftAchieve(leftAchieve.divide(ten).multiply(currency.getInExchangeRate()));
//            achievementVo.setRightAchieve(rightAchieve.divide(ten).multiply(currency.getInExchangeRate()));
//        } else {
//            achievementVo.setLeftAchieve(leftAchieve.divide(ten));
//            achievementVo.setRightAchieve(rightAchieve.divide(ten));
//        }
//
//        // 左区人数
//    //    Long leftNumber = 0L;
//        // 右区人数
//   //     Long rightNumber = 0L;
//
//      //  leftNumber = memberService.selectByDeptNumberCount(userId, EPlaceDept.LEFT_DEPT.getValue());
//     //   rightNumber = memberService.selectByDeptNumberCount(userId, EPlaceDept.RIGHT_DEPT.getValue());
//
//        // 查询会员左右区人数
//        List<DeptMemberNumberVo> deptMemberNumberVoList = memberService.selectByDeptNumberPlace(pkMember);
//        Map<Integer, Long> memberNumberMap = deptMemberNumberVoList.stream().collect(Collectors.toMap(DeptMemberNumberVo::getPlaceDept, DeptMemberNumberVo::getQuantity));
//
//        achievementVo.setLeftPeopleNumber(memberNumberMap.get(EPlaceDept.LEFT_DEPT.getValue()));
//        achievementVo.setRightPeopleNumber(memberNumberMap.get(EPlaceDept.RIGHT_DEPT.getValue()));
//        return AjaxResult.success(achievementVo);
//
//    }

//    /**
//     * 复购业绩分布
//     *
//     * @return
//     */
//    @GetMapping("/query-repurchase-performance-pic")
//    public AjaxResult queryRepurchasePerformancePic() {
//        int month = DateUtil.month(DateUtils.currentDate()) + 1;
//        AchievementVo achievementVo = cuMemberAchieveService.queryRepurchasePerformancePic(SecurityUtils.getUserId(), SecurityUtils.getPkCountry(), month);
//
//        return AjaxResult.success(achievementVo);
//    }

//    /**
//     * 推荐小区分布
//     *
//     * @return
//     */
//    @GetMapping("recommend-community")
//    public AjaxResult recommendCommunity() {
//        int month = DateUtil.month(DateUtils.currentDate()) + 1;
//        AchievementVo achievementVo = cuMemberAchieveService.recommendAchievement(SecurityUtils.getUserId(), SecurityUtils.getPkCountry(), month);
//        return AjaxResult.success(achievementVo);
//    }

    /**
     * 月度新增业绩对比
     *
     * @param month 月份
     * @return AjaxResult
     */
    @GetMapping("/new-add-achieve-contrast")
    public AjaxResult queryNewAddAchieveContrast(Integer month) {
        return AjaxResult.success(cuMemberAchieveService.selectNewAddAchieveContrast(SecurityUtils.getUserId(), SecurityUtils.getPkCountry(), month));
    }


//    /**
//     * 根据天数查询新增业绩
//     *
//     * @return AjaxResult
//     */
//    @GetMapping("/new-add-achieve-day")
//    public AjaxResult selectNewAddAchieveByDay() {
//        CuMemberNewAddAchieveVO newAddAchieveVO = CuMemberNewAddAchieveVO.builder().days(30L)
//                .pkMember(SecurityUtils.getUserId()).pkCountry(SecurityUtils.getPkCountry()).build();
//        return AjaxResult.success(cuMemberAchieveService.selectNewAddAchieveByDay(newAddAchieveVO));
//    }

    /**
     * @description: 月度业绩-新增业绩统计
     * @author: zhang jing
     * @date: 2023/5/19 9:48
     * @param:
     * @return:
     **/
    @GetMapping("/place-new-add-achieve-stat")
    public AjaxResult placeNewAddAchieveStat(PlaceNewAddAchieveVO pna) {
        pna.setPkMember(SecurityUtils.getUserId());
        pna.setPkCountry(SecurityUtils.getPkCountry());
        if (null == pna.getAchievementType()) {
            pna.setAchievementType(EAchievementType.WHOLE.getValue());
        }
        pna = cuMemberAchieveService.placeNewAddAchieveStat(pna);

        return AjaxResult.success(pna);
    }

    /**
     * @description: 月度业绩-详情
     * @author: zhang jing
     * @date: 2023/9/28 11:00
     * @param: [pna]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @GetMapping("/place-new-add-achieve-details")
    public TableDataInfo placeNewAddAchieveDetails(PlaceNewAddAchieveVO pna) {
        pna.setPkCountry(SecurityUtils.getPkCountry());
        if (null == pna.getAchievementType()) {
            pna.setAchievementType(EAchievementType.WHOLE.getValue());
        }
       List<PlaceNewAddAchieveVO> pnaList = cuMemberAchieveService.placeNewAddAchieveDetails(pna);
        return getDataTable(pnaList);
    }

    /**
     * @description: 导出
     * @author: zhang jing
     * @date: 2023/6/6 15:42
     * @param: [response, pna]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @PostMapping("/export")
    public void export(HttpServletResponse response, PlaceNewAddAchieveVO pna) {
        pna.setPkMember(SecurityUtils.getUserId());
        pna.setPkCountry(SecurityUtils.getPkCountry());
        if (null == pna.getAchievementType()) {
            pna.setAchievementType(EAchievementType.WHOLE.getValue());
        }
        pna = cuMemberAchieveService.placeNewAddAchieveStat(pna);
        List<PlaceNewAddAchieveVO> list = new ArrayList<>();
        list.add(pna);
        ExcelUtil<PlaceNewAddAchieveVO> util = new ExcelUtil<>(PlaceNewAddAchieveVO.class);
        util.exportExcel(response, list, "推荐业绩");
    }


    /**
     * @description: 业绩分布-直推业绩统计（右图）
     * @author: zhang jing
     * @date: 2023/5/19 9:48
     * @param:
     * @return:
     **/
    @PostMapping("/parent-achieve-right")
    public AjaxResult parentAchieveRight(@RequestBody ParentAchieveVO pa) {
        if (StringUtils.isEmpty(pa.getMonth())) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.SELECT_DATE_NOT_NULL));
        }
        Date d = DateUtils.parseStringToDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, DateUtils.currentDate()));
        pa.setEndDate(lastDayByMonth(pa.getMonth()));
        Date enddate = DateUtils.parseStringToDate(pa.getEndDate());
        if (DateUtils.compareDateBefore(d, enddate) || DateUtils.compareDateEqual(d, enddate)) {
            pa.setEndDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, DateUtils.beforeDate(1, ChronoUnit.DAYS, d)));
        }
        pa.setPkMember(SecurityUtils.getUserId());
        pa.setPkCountry(SecurityUtils.getPkCountry());
        pa = cuMemberAchieveService.parentAchieveRight(pa);
        return AjaxResult.success(pa);
    }

    /**
     * @description: 业绩分布-直推业绩统计（左图）
     * @author: zhang jing
     * @date: 2023/5/23 20:03
     * @param: [pa]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @PostMapping("/parent-achieve-left")
    public AjaxResult parentAchieveLeft(@RequestBody ParentAchieveVO pa) {
        if (StringUtils.isEmpty(pa.getMonth())) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.SELECT_DATE_NOT_NULL));
        }
        Date d = DateUtils.parseStringToDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, DateUtils.currentDate()));
        pa.setEndDate(lastDayByMonth(pa.getMonth()));
        Date enddate = DateUtils.parseStringToDate(pa.getEndDate());
        if (DateUtils.compareDateBefore(d, enddate) || DateUtils.compareDateEqual(d, enddate)) {
            pa.setEndDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, DateUtils.beforeDate(1, ChronoUnit.DAYS, d)));
        }
        pa.setPkMember(SecurityUtils.getUserId());
        pa.setPkCountry(SecurityUtils.getPkCountry());
        List<ParentAchieveVO> paList = cuMemberAchieveService.parentAchieveLeft(pa);
        return AjaxResult.success(paList);
    }

    /**
     * @description: 根据月查询该月的最后一天
     * @author: zhang jing
     * @date: 2023/9/5 19:40
     * @param: []
     * @return:
     **/
    public String lastDayByMonth(String endDate) {
        // 将月份字符串转换为 LocalDate 对象
        LocalDate date = LocalDate.parse(endDate + "-01");
        // 获取该月的最后一天
        int lastDay = date.lengthOfMonth();
        // 构建具体的日期和时间
        LocalDateTime lastDayDateTime = LocalDateTime.of(date.withDayOfMonth(lastDay), LocalTime.MAX);
        // 定义日期时间格式化模式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        // 格式化日期时间为字符串
        endDate = lastDayDateTime.format(formatter);
        return endDate;
    }

    /**
     * @description: 推荐达人
     * @author: zhang jing
     * @date: 2023/5/23 20:03
     * @param: [pa]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @PostMapping("/recommend")
    public TableDataInfo recommendExperts(@RequestBody ParentAchieveVO parentAchieveVO) {
        return new TableDataInfo();
//        if (parentAchieveVO.getWallType() == null) {
//            return new TableDataInfo();
//        }
//        parentAchieveVO.setPkCountry(SecurityUtils.getPkCountry());
//        List<CuMemberMonthAchieveExt> cuMemberMonthAchieveExtList = cuMemberMonthAchieveService.queryCuMemberMonthAchieveRecommend(parentAchieveVO);
//        cuMemberMonthAchieveExtList.forEach(cuMemberMonthAchieveExt -> {
//            String memberName = cuMemberMonthAchieveExt.getMemberName();
//            if (memberName.length() > 2) {
//                cuMemberMonthAchieveExt.setMemberName(memberName.replaceFirst(memberName.substring(1, memberName.length() - 1), "*"));
//            } else {
//                cuMemberMonthAchieveExt.setMemberName(memberName.replaceFirst(memberName.substring(1), "*"));
//            }
//        });
//        return getDataTable(cuMemberMonthAchieveExtList);
    }

    /**
     * @description: 抗衰达人
     * @author: zhang jing
     * @date: 2023/5/23 20:03
     * @param: [pa]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @PostMapping("/repurchase")
    public TableDataInfo repurchaseExperts(@RequestBody ParentAchieveVO parentAchieveVO) {
        return new TableDataInfo();
//        if (parentAchieveVO.getWallType() == null) {
//            return new TableDataInfo();
//        }
//        parentAchieveVO.setPkCountry(SecurityUtils.getPkCountry());
//        List<CuMemberMonthAchieveExt> cuMemberMonthAchieveExtList = cuMemberMonthAchieveService.queryCuMemberMonthAchieveRepurchase(parentAchieveVO);
//        cuMemberMonthAchieveExtList.forEach(cuMemberMonthAchieveExt -> {
//            String memberName = cuMemberMonthAchieveExt.getMemberName();
//            if (memberName.length() > 2) {
//                cuMemberMonthAchieveExt.setMemberName(memberName.replaceFirst(memberName.substring(1, memberName.length() - 1), "*"));
//            } else {
//                cuMemberMonthAchieveExt.setMemberName(memberName.replaceFirst(memberName.substring(1), "*"));
//            }
//        });
//        return getDataTable(cuMemberMonthAchieveExtList);
    }

    /**
     * @description: 抗衰达人
     * @author: zhang jing
     * @date: 2023/5/23 20:03
     * @param: [pa]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @PostMapping("/month-achieve")
    public TableDataInfo memberMonthAchieve(@RequestBody MonthAchieveParam monthAchieveParam) {
        List<MemberMonthAchieveVO> memberMonthAchieveList = new ArrayList<>();
        List<CuMemberAchieveExt> cuMemberAchieveExtList = cuMemberAchieveService.queryMemberMonthAchieve(SecurityUtils.getUserId(), monthAchieveParam.getMonth());
        for (CuMemberAchieveExt cuMemberAchieveExt : cuMemberAchieveExtList) {
            MemberMonthAchieveVO memberMonthAchieveVO = BeanUtil.copyProperties(cuMemberAchieveExt, MemberMonthAchieveVO.class);
            setAchieve(cuMemberAchieveExt, memberMonthAchieveVO);
            if(StringUtils.isNotEmpty(memberMonthAchieveVO.getMonth())){
                String month = memberMonthAchieveVO.getMonth();
                memberMonthAchieveVO.setMonth(month.substring(0,4) + "-" + month.substring(4));
            }
            memberMonthAchieveList.add(memberMonthAchieveVO);
        }
        return getDataTable(memberMonthAchieveList);
    }

    private void setAchieve(CuMemberAchieveExt cuMemberAchieveExt, MemberMonthAchieveVO memberMonthAchieveVO) {
        if(Objects.equals(CountryConstants.CHINA_COUNTRY, cuMemberAchieveExt.getPkSettleCountry())){
            memberMonthAchieveVO.setASumPv(ComputeUtil.computeMultiply(memberMonthAchieveVO.getASumPv(), cuMemberAchieveExt.getOutExchangeRate()));
            memberMonthAchieveVO.setBSumPv(ComputeUtil.computeMultiply(memberMonthAchieveVO.getBSumPv(), cuMemberAchieveExt.getOutExchangeRate()));
            memberMonthAchieveVO.setRepASumPv(ComputeUtil.computeMultiply(memberMonthAchieveVO.getRepASumPv(), cuMemberAchieveExt.getOutExchangeRate()));
            memberMonthAchieveVO.setRepBSumPv(ComputeUtil.computeMultiply(memberMonthAchieveVO.getRepBSumPv(), cuMemberAchieveExt.getOutExchangeRate()));
            memberMonthAchieveVO.setFirstAchieve(ComputeUtil.computeMultiply(memberMonthAchieveVO.getFirstAchieve(), cuMemberAchieveExt.getOutExchangeRate()));
            memberMonthAchieveVO.setRepurchaseAchieve(ComputeUtil.computeMultiply(memberMonthAchieveVO.getRepurchaseAchieve(), cuMemberAchieveExt.getOutExchangeRate()));
        }
    }

    /**
     * @description: 抗衰达人
     * @author: zhang jing
     * @date: 2023/5/23 20:03
     * @param: [pa]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @PostMapping("/month-achieve-detail")
    public TableDataInfo memberMonthAchieveDetail(@RequestBody MonthAchieveParam monthAchieveParam) {
        List<MemberMonthAchieveVO> memberMonthAchieveList = new ArrayList<>();
        if(StringUtils.isNotEmpty(monthAchieveParam.getMonth()) && monthAchieveParam.getPlaceDept() != null) {
            startPage();
            Date startDate = DateUtils.parseStringToDate(monthAchieveParam.getMonth() + "-01");
            Date endDate = DateUtils.currentMonthLastDate(startDate);
            monthAchieveParam.setStartDate(startDate);
            monthAchieveParam.setEndDate(endDate);
            monthAchieveParam.setPkMember(SecurityUtils.getUserId());
            List<CuMemberAchieveExt> cuMemberAchieveExtList;
            monthAchieveParam.setMonth(monthAchieveParam.getMonth().replace("-", ""));
            if (monthAchieveParam.getPlaceDept() == 0) {
                cuMemberAchieveExtList = cuMemberAchieveService.queryParentMemberMonthAchieve(monthAchieveParam);
            } else {
                cuMemberAchieveExtList = cuMemberAchieveService.queryPlaceParentMemberMonthAchieve(monthAchieveParam);
            }
            if (CollectionUtil.isNotEmpty(cuMemberAchieveExtList)) {
                for (CuMemberAchieveExt cuMemberAchieveExt : cuMemberAchieveExtList) {
                    MemberMonthAchieveVO memberMonthAchieveVO = BeanUtil.copyProperties(cuMemberAchieveExt, MemberMonthAchieveVO.class);
                    memberMonthAchieveVO.setFirstAchieve(BigDecimal.ZERO);
                    memberMonthAchieveVO.setRepurchaseAchieve(BigDecimal.ZERO);
                    if (EOrderType.REGISTER_ORDER.getValue() == cuMemberAchieveExt.getOrderType() ||
                            EOrderType.UPGRADE_ORDER.getValue() == cuMemberAchieveExt.getOrderType()) {
                        if (Objects.equals(CountryConstants.CHINA_COUNTRY, cuMemberAchieveExt.getPkSettleCountry())) {
                            memberMonthAchieveVO.setFirstAchieve(ComputeUtil.computeMultiply(cuMemberAchieveExt.getOrderAchieve(), cuMemberAchieveExt.getOutExchangeRate()));
                        } else {
                            memberMonthAchieveVO.setFirstAchieve(cuMemberAchieveExt.getOrderAchieve());
                        }
                    } else {
                        if (Objects.equals(CountryConstants.CHINA_COUNTRY, cuMemberAchieveExt.getPkSettleCountry())) {
                            memberMonthAchieveVO.setRepurchaseAchieve(ComputeUtil.computeMultiply(cuMemberAchieveExt.getOrderAchieve(), cuMemberAchieveExt.getOutExchangeRate()));
                        } else {
                            memberMonthAchieveVO.setRepurchaseAchieve(cuMemberAchieveExt.getOrderAchieve());
                        }
                    }
                    memberMonthAchieveList.add(memberMonthAchieveVO);
                }
                TableDataInfo tableDataInfo = getDataTable(cuMemberAchieveExtList);
                tableDataInfo.setRows(memberMonthAchieveList);
                return tableDataInfo;
            }
        }
        return getDataTable(memberMonthAchieveList);
    }

    /**
     * 查询当时时间如果每月是第一天则不展示本月
     *
     * @return
     */
    @GetMapping("get-data-info")
    public AjaxResult getDataInfo() {

        Date date = DateUtils.beforeDate(1, ChronoUnit.DAYS, DateUtils.currentDate());
        DateUtils.parseDateToFormat(DateUtils.YYYY_MM_DD, date);
        return AjaxResult.success();

    }

    public static void main(String[] args) {
        String firstTime = DateUtils.currentMonthFirstDateStr(DateUtils.currentDate());
        String thisTime = DateUtils.parseDateToFormat(DateUtils.YYYY_MM_DD, DateUtils.currentDate());
        Date date = DateUtils.beforeDate(1, ChronoUnit.DAYS, DateUtils.currentDate());
        System.out.println(DateUtils.parseDateToFormat("M", date));
    }

    /**
     * @description: 安置数据查询
     * @author: zhang jing
     * @date: 2024/7/11 15:52
     * @param: [pna]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @GetMapping("/place-frame-query")
    public TableDataInfo placeFrameQuery(PlaceFrameQueryVO pfq) {
        if(StringUtils.isEmpty(pfq.getMemberCode())){
            pfq.setPkMember(SecurityUtils.getUserId());
        }else{
            if(memberService.getMemberByCode(pfq.getMemberCode())==null){
                return getDataTable(new ArrayList<>());
            }
            pfq.setPkMember(memberService.getMemberByCode(pfq.getMemberCode()).getPkId());
            if(pfq.getPkMember()==null){
                return getDataTable(new ArrayList<>());
            }
        }
        if(StringUtils.isEmpty(pfq.getStartDate())||StringUtils.isEmpty(pfq.getEndDate())){
            return getDataTable(new ArrayList<>());
        }
        //当前时间
        Date date=DateUtils.currentDate();
        String todayDate=DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,date);
        pfq.setTodayDate(todayDate);
        String currentDate=DateUtils.beforeDateStr(1,ChronoUnit.DAYS, DateUtils.YYYY_MM_DD,date);
        pfq.setCurrentDate(currentDate);
        Integer curPeriod = memberSettlePeriodService.getCuMemberSettlePeriodByDate(pfq.getCurrentDate()).getPkId();
        String curSettleTableName = TableNameConstants.CU_MEMBER_SETTLE + curPeriod;
        String curAchieveTableName = TableNameConstants.CU_MEMBER_SETTLE_D + curPeriod;
        pfq.setCurSettleTableName(curSettleTableName);
        pfq.setCurAchieveTableName(curAchieveTableName);

        //查询期数
        Integer staPeriod = 0;
        Integer endPeriod = 0;
        pfq.setStartDate(DateUtils.beforeDateStr(1,ChronoUnit.DAYS, DateUtils.YYYY_MM_DD,pfq.getStartDate()));
        if(memberSettlePeriodService.getCuMemberSettlePeriodByDate(pfq.getStartDate())!=null&&
                memberSettlePeriodService.getCuMemberSettlePeriodByDate(pfq.getStartDate())!=null
        ){
            staPeriod = memberSettlePeriodService.getCuMemberSettlePeriodByDate(pfq.getStartDate()).getPkId();
            endPeriod = memberSettlePeriodService.getCuMemberSettlePeriodByDate(pfq.getEndDate()).getPkId();
        }
        //前一天的一期
        Integer beforePeriod = memberSettlePeriodService.getCuMemberSettlePeriodByDate(DateUtils.beforeDateStr(1,ChronoUnit.DAYS, DateUtils.YYYY_MM_DD,pfq.getCurrentDate())).getPkId();
        pfq.setStaPeriod(staPeriod);
        pfq.setEndPeriod(endPeriod);
        pfq.setBeforePeriod(beforePeriod);
        String endAchieveTableName = TableNameConstants.CU_MEMBER_SETTLE_D + endPeriod;
        String endSettleTableName = TableNameConstants.CU_MEMBER_SETTLE + endPeriod;
        String beforeSettleTableName = TableNameConstants.CU_MEMBER_SETTLE + beforePeriod;
        String beforeAchieveTableName = TableNameConstants.CU_MEMBER_SETTLE_D + beforePeriod;
        pfq.setEndAchieveTableName(endAchieveTableName);
        pfq.setEndSettleTableName(endSettleTableName);
        pfq.setBeforeAchieveTableName(beforeAchieveTableName);
        pfq.setBeforeSettleTableName(beforeSettleTableName);
        //校验表是否存在
        Integer endAchieveTable = memberTreeService.queryExistOracleTable(endAchieveTableName);
        Integer endSettleTabl=memberTreeService.queryExistOracleTable(endSettleTableName);
        Integer curSettleTabl = memberTreeService.queryExistOracleTable(curSettleTableName);
        pfq.setEndAchieveTable(endAchieveTable);
        pfq.setEndSettleTabl(endSettleTabl);
        //校验会员是否在某会员安置伞下
        pfq.setPkPlaceParent(SecurityUtils.getUserId());
        if(!pfq.getPkMember().equals(pfq.getPkPlaceParent())){
            if(cuMemberAchieveService.checkPlaceMemberQuery(pfq)==null){
                return getDataTable(new ArrayList<>());
            }
        }
        //查询会员安置伞下第一层会员(本期)
        if(curSettleTabl!=null){
            List<PlaceFrameQueryVO> memberList=cuMemberAchieveService.placeBefMemberQuery(pfq);
            if(CollectionUtil.isNotEmpty(memberList)){
                pfq.setBefMemberList(memberList);
                pfq.setLev(memberList.get(0).getLev());
            }
        }
        List<PlaceFrameQueryVO> beflist=new ArrayList<>();
        if(CollectionUtil.isNotEmpty(pfq.getBefMemberList())){
             beflist= cuMemberAchieveService.placeBefFrameQuery(pfq);
        }

        //查询会员安置伞下第一层会员
        if(endSettleTabl!=null){
            List<PlaceFrameQueryVO> memberList=cuMemberAchieveService.placeMemberQuery(pfq);
            if(CollectionUtil.isNotEmpty(memberList)){
                pfq.setMemberList(memberList);
                pfq.setLev(memberList.get(0).getLev());
            }
        }

        List<PlaceFrameQueryVO> list= cuMemberAchieveService.placeFrameQuery(pfq);
        for(PlaceFrameQueryVO pq:list){
            for(PlaceFrameQueryVO pf:beflist){
                if(pq.getPkMember().equals(pf.getPkMember())){
                    pq.setNewCount(pf.getNewCount());
                    pq.setNewPv(pf.getNewPv());
                }
            }
        }
        return  getDataTable(list);
    }

    /**
     * @description: 推荐数据查询
     * @author: zhang jing
     * @date: 2024/7/12 13:33
     * @param: [pna]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @GetMapping("/parent-frame-query")
    public TableDataInfo parentFrameQuery(PlaceFrameQueryVO pfq) {
        if(StringUtils.isEmpty(pfq.getMemberCode())){
            pfq.setPkMember(SecurityUtils.getUserId());
        }else{
            if(memberService.getMemberByCode(pfq.getMemberCode())==null){
                return getDataTable(new ArrayList<>());
            }
            pfq.setPkMember(memberService.getMemberByCode(pfq.getMemberCode()).getPkId());
            if(pfq.getPkMember()==null){
                return getDataTable(new ArrayList<>());
            }
        }
        if(StringUtils.isEmpty(pfq.getStartDate())||StringUtils.isEmpty(pfq.getEndDate())){
            return getDataTable(new ArrayList<>());
        }
        //当前时间
        Date date=DateUtils.currentDate();
        String todayDate=DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,date);
        pfq.setTodayDate(todayDate);
        String currentDate=DateUtils.beforeDateStr(1,ChronoUnit.DAYS, DateUtils.YYYY_MM_DD,date);
        pfq.setCurrentDate(currentDate);
        Integer curPeriod = memberSettlePeriodService.getCuMemberSettlePeriodByDate(pfq.getCurrentDate()).getPkId();
        String curSettleTableName = TableNameConstants.CU_MEMBER_SETTLE + curPeriod;
        pfq.setCurSettleTableName(curSettleTableName);
        //查询期数
        Integer staPeriod = 0;
        Integer endPeriod = 0;
        pfq.setStartDate(DateUtils.beforeDateStr(1,ChronoUnit.DAYS, DateUtils.YYYY_MM_DD,pfq.getStartDate()));
        if(memberSettlePeriodService.getCuMemberSettlePeriodByDate(pfq.getStartDate())!=null&&
                memberSettlePeriodService.getCuMemberSettlePeriodByDate(pfq.getStartDate())!=null
        ){
            staPeriod = memberSettlePeriodService.getCuMemberSettlePeriodByDate(pfq.getStartDate()).getPkId();
            endPeriod = memberSettlePeriodService.getCuMemberSettlePeriodByDate(pfq.getEndDate()).getPkId();
        }

        //前一天的一期
        Integer beforePeriod = memberSettlePeriodService.getCuMemberSettlePeriodByDate(DateUtils.beforeDateStr(1,ChronoUnit.DAYS, DateUtils.YYYY_MM_DD,pfq.getCurrentDate())).getPkId();
        pfq.setStaPeriod(staPeriod);
        pfq.setEndPeriod(endPeriod);
        pfq.setBeforePeriod(beforePeriod);
        String staSettleTableName = TableNameConstants.CU_MEMBER_SETTLE + staPeriod;
        String endSettleTableName = TableNameConstants.CU_MEMBER_SETTLE + endPeriod;
        String beforeSettleTableName = TableNameConstants.CU_MEMBER_SETTLE + beforePeriod;
        pfq.setStaSettleTableName(staSettleTableName);
        pfq.setEndSettleTableName(endSettleTableName);
        pfq.setBeforeSettleTableName(beforeSettleTableName);
        //校验表是否存在
        Integer staSettleTabl = memberTreeService.queryExistOracleTable(staSettleTableName);
        Integer endSettleTabl=memberTreeService.queryExistOracleTable(endSettleTableName);
        Integer curSettleTabl = memberTreeService.queryExistOracleTable(curSettleTableName);
        pfq.setStaSettleTabl(staSettleTabl);
        pfq.setEndSettleTabl(endSettleTabl);
        //校验会员是否在某会员推荐线
        pfq.setPkParent(SecurityUtils.getUserId());
        if(!pfq.getPkMember().equals(pfq.getPkParent())){
            if(cuMemberAchieveService.checkParentMemberQuery(pfq)==null){
                return getDataTable(new ArrayList<>());
            }
        }
        //查询会员推荐伞下第一层会员(本期)
        if(curSettleTabl!=null){
            List<PlaceFrameQueryVO> memberList=cuMemberAchieveService.placeBefMemberQuery(pfq);
            if(CollectionUtil.isNotEmpty(memberList)){
                pfq.setBefMemberList(memberList);
                pfq.setLev(memberList.get(0).getLev());
            }
        }
        //查询本期业绩和新增人数
        List<PlaceFrameQueryVO> beflist=new ArrayList<>();
        if(CollectionUtil.isNotEmpty(pfq.getBefMemberList())){
            beflist= cuMemberAchieveService.parentBefFrameQuery(pfq);
        }
        //查询会员推荐伞下第一层会员
        if(endSettleTabl!=null){
            List<PlaceFrameQueryVO> memberList=cuMemberAchieveService.parentBefMemberQuery(pfq);
            if(CollectionUtil.isNotEmpty(memberList)){
                pfq.setMemberList(memberList);
                pfq.setLev(memberList.get(0).getLev());
            }
        }
        List<PlaceFrameQueryVO> list= cuMemberAchieveService.parentFrameQuery(pfq);
        for(PlaceFrameQueryVO pq:list){
            for(PlaceFrameQueryVO pf:beflist){
                if(pq.getPkMember().equals(pf.getPkMember())){
                    pq.setNewCount(pf.getNewCount());
                    pq.setNewPv(pf.getNewPv());
                }
            }
        }
        return  getDataTable(list);
    }


}
