package com.hzs.member.achieve.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.enums.HonorWallType;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.domain.member.achieve.CuMemberMonthAchieve;
import com.hzs.common.domain.member.achieve.CuMemberSettlePeriod;
import com.hzs.common.domain.member.ext.CuMemberMonthAchieveExt;
import com.hzs.member.achieve.controller.vo.ParentAchieveVO;
import com.hzs.member.achieve.controller.vo.RealPerformanceVo;
import com.hzs.member.achieve.mapper.CuMemberMonthAchieveMapper;
import com.hzs.member.achieve.service.ICuMemberMonthAchieveService;
import com.hzs.member.achieve.service.ICuMemberSettlePeriodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 会员月数据 服务实现类
 */
@Service
public class CuMemberMonthAchieveServiceImpl extends ServiceImpl<CuMemberMonthAchieveMapper, CuMemberMonthAchieve> implements ICuMemberMonthAchieveService {

    @Autowired
    private ICuMemberSettlePeriodService iCuMemberSettlePeriodService;

    @Override
    public List<CuMemberMonthAchieveExt> queryCuMemberMonthAchieveRecommend(ParentAchieveVO parentAchieveVO) {
        packageParamPeriod(parentAchieveVO);
        return baseMapper.queryCuMemberMonthAchieveRecommend(parentAchieveVO);
    }

    private void packageParamPeriod(ParentAchieveVO parentAchieveVO) {
        Date settleDate = DateUtils.currentDate();
        if (Objects.equals(HonorWallType.RECOMMEND_MONTH.getValue(), parentAchieveVO.getWallType()) ||
                Objects.equals(HonorWallType.ANTIFADING_MONTH.getValue(), parentAchieveVO.getWallType())) {
            //1号取上月的
            // 月
            Date date = DateUtils.currentMonthFirstDate(settleDate);
            if (DateUtils.compareDateEqual(settleDate, date)) {
                // 取上月第一天
                date = DateUtils.beforeMonthFirstDate(settleDate);
            }
            String currentMonthDate = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, date);
            CuMemberSettlePeriod cuMemberSettlePeriod = iCuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(currentMonthDate);
            parentAchieveVO.setStartPeriod(cuMemberSettlePeriod.getPkId());
            parentAchieveVO.setEndPeriod(cuMemberSettlePeriod.getPkId());
        } else {
            // 季度
            // 上月期间，作为结束期间
            String beforeFirstMonthDate = DateUtils.beforeMonthFirstDateStr(settleDate);
            CuMemberSettlePeriod beforePeriod = iCuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(beforeFirstMonthDate);
            parentAchieveVO.setEndPeriod(beforePeriod.getPkId());
            if (Objects.equals(HonorWallType.RECOMMEND_QUARTER.getValue(), parentAchieveVO.getWallType()) ||
                    Objects.equals(HonorWallType.ANTIFADING_QUARTER.getValue(), parentAchieveVO.getWallType())) {
                // 3个月期间，作为开始期间
                String quarterFirstMonthDate = DateUtils.beforeMonthFirstDateStr(3, settleDate);
                CuMemberSettlePeriod quarterPeriod = iCuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(quarterFirstMonthDate);
                parentAchieveVO.setStartPeriod(quarterPeriod.getPkId());
            } else {
                String yearFirstMonthDate = DateUtils.beforeMonthFirstDateStr(12, settleDate);
                CuMemberSettlePeriod yearPeriod = iCuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(yearFirstMonthDate);
                parentAchieveVO.setStartPeriod(yearPeriod.getPkId());
            }
        }
    }

    @Override
    public RealPerformanceVo queryCuMemberMonthAchieveInfo(Long pkMember, String achieveTable) {
        return baseMapper.queryCuMemberMonthAchieveInfo(pkMember, achieveTable);
    }
}
