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

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.constant.TableNameConstants;
import com.hzs.common.core.enums.*;
import com.hzs.common.core.utils.ComputeUtil;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.domain.member.achieve.CuMemberSettlePeriod;
import com.hzs.common.domain.member.detail.CuMemberAssess;
import com.hzs.common.domain.member.detail.CuMemberAssessDetail;
import com.hzs.common.domain.member.ext.CuMemberAssessExt;
import com.hzs.common.domain.member.ext.CuMemberSettleExt;
import com.hzs.common.domain.sale.order.SaOrder;
import com.hzs.common.domain.system.config.BdAwards;
import com.hzs.member.achieve.service.ICuMemberSettlePeriodService;
import com.hzs.member.achieve.service.ICuMemberTreeService;
import com.hzs.member.achieve.service.impl.CuMemberBonusSettle;
import com.hzs.member.detail.mapper.CuMemberAssessMapper;
import com.hzs.member.detail.service.ICuMemberAssessDetailService;
import com.hzs.member.detail.service.ICuMemberAssessService;
import com.hzs.member.detail.vo.CuMemberAssessVO;
import com.hzs.sale.order.ISaOrderServiceApi;
import com.hzs.system.config.IAwardsServiceApi;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.temporal.ChronoUnit;
import java.util.*;

/**
 * <p>
 * 会员信息-会员考核记录 服务实现类
 * </p>
 *
 * @author hzs
 * @since 2022-08-31
 */
@Service
public class CuMemberAssessServiceImpl extends ServiceImpl<CuMemberAssessMapper, CuMemberAssess> implements ICuMemberAssessService {


    private ICuMemberSettlePeriodService cuMemberSettlePeriodService;

    @Autowired
    public void setCuMemberSettlePeriodService(ICuMemberSettlePeriodService cuMemberSettlePeriodService) {
        this.cuMemberSettlePeriodService = cuMemberSettlePeriodService;
    }

    /**
     * @description: 会员-考核明细列表
     * @author: zhang jing
     * @date: 2023/1/23 10:16
     * @param: [cuMemberAssessVO]
     * @return: java.util.List<com.hzs.member.detail.vo.CuMemberAssessVO>
     **/
    @Override
    public List<CuMemberAssessVO> selectAssessList(CuMemberAssessVO cuMemberAssessVO,List<Integer> orderTypeList) {
        Integer year;
        if (StringUtils.isNotNull(cuMemberAssessVO) && StringUtils.isNotNull(cuMemberAssessVO.getStartDate())) {
            year = DateUtils.currentYear(cuMemberAssessVO.getStartDate());
        } else {
            year = DateUtils.currentYear(new Date());
        }
        String tableName = TableNameConstants.CU_MEMBER_ASSESS + year;
        if(StringUtils.isNotEmpty(tableName)){
            return baseMapper.selectAssessList(tableName,cuMemberAssessVO.getPkMember(),
                    cuMemberAssessVO.getPkCountry(),cuMemberAssessVO.getAssessStatus(),
                    cuMemberAssessVO.getAssessType(),cuMemberAssessVO.getChangeType(),cuMemberAssessVO.getStartDate(),
                    cuMemberAssessVO.getEndDate(), orderTypeList
            );
        }
        return null;
    }

    /**
     * @description: 查询会员月度考核(查询列表用)
     * @author: zhang jing
     * @date: 2023/3/14 14:46
     * @param: [cuMemberAssessVO]
     * @return: java.util.List<com.hzs.member.detail.vo.CuMemberAssessVO>
     **/
    @Override
    public List<CuMemberAssessVO> getMemberAssessMonthList(CuMemberAssessVO cuMemberAssessVO) {
        Integer year;
        //获取期数
        Date date=new Date();
        if (StringUtils.isNotNull(cuMemberAssessVO) && StringUtils.isNotNull(cuMemberAssessVO.getStartDate())) {
            year = DateUtils.currentYear(cuMemberAssessVO.getStartDate());
        } else {
            year = DateUtils.currentYear(date);
        }
        String tableName = TableNameConstants.CU_MEMBER_ASSESS + year;
        if(StringUtils.isNotEmpty(tableName)){
            return baseMapper.getMemberAssessMonthList(tableName,cuMemberAssessVO.getPkMember(),cuMemberAssessVO.getAssessType(),
                    cuMemberAssessVO.getPkCountry(),null);
        }
        return null;
    }
    /**
     * @description: 查询会员月度考核(查询积分用)
     * @author: zhang jing
     * @date: 2023/7/2 8:28
     * @param: [cuMemberAssessVO]
     * @return: java.util.List<com.hzs.member.detail.vo.CuMemberAssessVO>
     **/
    @Override
    public List<CuMemberAssessVO> getMemberAssessMonth(CuMemberAssessVO cuMemberAssessVO) {
        Integer year;
        //获取期数
        Date date=new Date();
        int period;
        if (StringUtils.isNotNull(cuMemberAssessVO) && StringUtils.isNotNull(cuMemberAssessVO.getStartDate())) {
            year = DateUtils.currentYear(cuMemberAssessVO.getStartDate());
            if(DateUtils.compareDateBefore(date,cuMemberAssessVO.getStartDate()) || DateUtils.compareDateEqual(date,cuMemberAssessVO.getStartDate())){
                //获取当前月第一天
                Date date1= DateUtils.currentMonthFirstDate(DateUtils.beforeDate(1, ChronoUnit.DAYS, date));
                period = cuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(
                        DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,date1)).getPkId();
            }else{
                Date date1= DateUtils.currentMonthFirstDate(cuMemberAssessVO.getStartDate());
                period = cuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(
                        DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,date1)).getPkId();
            }
        } else {
            year = DateUtils.currentYear(date);
            Date date1= DateUtils.currentMonthFirstDate(DateUtils.beforeDate(1, ChronoUnit.DAYS, date));
            period = cuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(
                    DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,date1)).getPkId();
        }
        String tableName = TableNameConstants.CU_MEMBER_ASSESS + year;
        if(StringUtils.isNotEmpty(tableName)){
            return baseMapper.getMemberAssessMonthList(tableName,cuMemberAssessVO.getPkMember(),cuMemberAssessVO.getAssessType(),
                    cuMemberAssessVO.getPkCountry(),period);
        }
        return null;
    }


    /**
     * @description: 查询会员订单考核积分
     * @author: zhang jing
     * @date: 2023/3/14 17:52
     * @param: [cuMemberAssessVO]
     * @return: java.util.List<com.hzs.member.detail.vo.CuMemberAssessVO>
     **/
    @Override
    public CuMemberAssessVO getOrderAssessList(CuMemberAssessVO cuMemberAssessVO) {
        return baseMapper.getOrderAssessList(cuMemberAssessVO);
    }

}
