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

import com.hzs.common.core.constant.TableNameConstants;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.domain.member.achieve.CuMemberSettlePeriod;
import com.hzs.common.domain.member.achieve.ext.CuMemberAchieveExt;
import com.hzs.member.achieve.ICuMemberSettlePeriodServiceApi;
import com.hzs.report.member.mapper.MemberMapper;
import com.hzs.report.member.param.CuMemberUmbrellaParam;
import com.hzs.report.member.service.IMemberService;
import com.hzs.report.member.vo.CuMemberUmbrellaBalanceVO;
import com.hzs.report.member.vo.UmbrellaBelowPushVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;

/**
 * 会员查询的服务
 */
@Service
public class MemberServiceImpl implements IMemberService {

    @Autowired
    private MemberMapper memberMapper;

    @DubboReference
    ICuMemberSettlePeriodServiceApi iCuMemberSettlePeriodServiceApi;

    @Override
    public List<CuMemberUmbrellaBalanceVO> selectUmbrellaLowerBalance(CuMemberUmbrellaBalanceVO umbrellaBalanceVO) {
        return memberMapper.selectUmbrellaLowerBalance(umbrellaBalanceVO);
    }

    @Override
    public List<CuMemberAchieveExt> queryPlaceCuMemberAchieveExt(CuMemberUmbrellaParam cuMemberUmbrellaParam) {
        Date settleDate = cuMemberUmbrellaParam.getQueryDate() == null ? DateUtils.currentDate() : cuMemberUmbrellaParam.getQueryDate();
        if (DateUtils.compareDateBefore(settleDate, DateUtils.currentDate())) {
            String date = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, settleDate);
            CuMemberSettlePeriod cuMemberSettlePeriod = iCuMemberSettlePeriodServiceApi.getCuMemberSettlePeriodByDate(date).getData();
            String tableName = TableNameConstants.CU_MEMBER_SETTLE + cuMemberSettlePeriod.getPkId();
            String tableAssName = TableNameConstants.CU_MEMBER_ASSESS + DateUtils.getYear(date);
            Integer period = iCuMemberSettlePeriodServiceApi.getCuMemberSettlePeriodByDate(DateUtils.currentMonthFirstDateStr(settleDate)).getData().getPkId();
            return memberMapper.queryPlaceParentHisCuMemberAchieveExt(tableName, tableAssName, period, cuMemberUmbrellaParam);
        } else {
            settleDate = DateUtils.beforeDate(1, ChronoUnit.DAYS);
            String date = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, settleDate);
            CuMemberSettlePeriod cuMemberSettlePeriod = iCuMemberSettlePeriodServiceApi.getCuMemberSettlePeriodByDate(date).getData();
            String tableAssName = TableNameConstants.CU_MEMBER_ASSESS + DateUtils.getYear(date);
            String tableName = TableNameConstants.CU_MEMBER_SETTLE + cuMemberSettlePeriod.getPkId();
            Integer period = iCuMemberSettlePeriodServiceApi.getCuMemberSettlePeriodByDate(DateUtils.currentMonthFirstDateStr(settleDate)).getData().getPkId();
            return memberMapper.queryPlaceParentCuMemberAchieveExt(tableName, tableAssName, period, cuMemberUmbrellaParam);
        }
    }

    @Override
    public List<CuMemberAchieveExt> queryParentCuMemberAchieveExt(CuMemberUmbrellaParam cuMemberUmbrellaParam) {
        Date settleDate = cuMemberUmbrellaParam.getQueryDate() == null ? DateUtils.currentDate() : cuMemberUmbrellaParam.getQueryDate();
        if (DateUtils.compareDateBefore(settleDate, DateUtils.currentDate())) {
            String date = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, settleDate);
            CuMemberSettlePeriod cuMemberSettlePeriod = iCuMemberSettlePeriodServiceApi.getCuMemberSettlePeriodByDate(date).getData();
            String tableName = TableNameConstants.CU_MEMBER_SETTLE + cuMemberSettlePeriod.getPkId();
            String tableAssName = TableNameConstants.CU_MEMBER_ASSESS + DateUtils.getYear(date);
            Integer period = iCuMemberSettlePeriodServiceApi.getCuMemberSettlePeriodByDate(DateUtils.currentMonthFirstDateStr(settleDate)).getData().getPkId();
            return memberMapper.queryParentParentHisCuMemberAchieveExt(tableName, tableAssName, period, cuMemberUmbrellaParam);
        } else {
            settleDate = DateUtils.beforeDate(1, ChronoUnit.DAYS);
            String date = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, settleDate);
            String tableAssName = TableNameConstants.CU_MEMBER_ASSESS + DateUtils.getYear(date);
            Integer period = iCuMemberSettlePeriodServiceApi.getCuMemberSettlePeriodByDate(DateUtils.currentMonthFirstDateStr(settleDate)).getData().getPkId();
            return memberMapper.queryParentParentCuMemberAchieveExt(tableAssName, period, cuMemberUmbrellaParam);
        }
    }

    @Override
    public List<UmbrellaBelowPushVO> queryUmbrellaBelowPush(UmbrellaBelowPushVO umbrellaBelowPushVO) {
        return memberMapper.queryUmbrellaBelowPush(umbrellaBelowPushVO);
    }

    @Override
    public List<UmbrellaBelowPushVO> queryOrderExpend(UmbrellaBelowPushVO umbrellaBelowPushVO) {
        return memberMapper.queryOrderExpend(umbrellaBelowPushVO);
    }

}
