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 com.hzs.report.report.vo.DeptMemberNumberVO;
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;

/**
 * @description: 会员
 * @author: sui q
 * @time: 2023/8/26 09:37
 * @classname: MemberServiceImpl
 * @package_name: com.hzs.report.member.service.impl
 * version 1.0.0
 */
@Service
public class MemberServiceImpl implements IMemberService {

    @Autowired
    private MemberMapper memberMapper;

    @DubboReference
    private ICuMemberSettlePeriodServiceApi cuMemberSettlePeriodServiceApi;

    /**
     * @description: 查询伞下账户余额
     * @author: zhang jing
     * @date: 2023/8/25 15:29
     * @param: [umbrellaBalanceVO]
     * @return: java.util.List<com.hzs.member.base.vo.CuMemberUmbrellaBalanceVO>
     **/
    @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 = cuMemberSettlePeriodServiceApi.getCuMemberSettlePeriodByDate(date).getData();
            String tableName = TableNameConstants.CU_MEMBER_SETTLE + cuMemberSettlePeriod.getPkId();
            Integer period = cuMemberSettlePeriodServiceApi.getCuMemberSettlePeriodByDate(DateUtils.currentMonthFirstDateStr(settleDate)).getData().getPkId();
            return memberMapper.queryPlaceParentHisCuMemberAchieveExt(tableName, period, cuMemberUmbrellaParam);
        } else {
            settleDate = DateUtils.beforeDate(1, ChronoUnit.DAYS);
            String date = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, settleDate);
            CuMemberSettlePeriod cuMemberSettlePeriod = cuMemberSettlePeriodServiceApi.getCuMemberSettlePeriodByDate(date).getData();
            String tableName = TableNameConstants.CU_MEMBER_SETTLE + cuMemberSettlePeriod.getPkId();
            Integer period = cuMemberSettlePeriodServiceApi.getCuMemberSettlePeriodByDate(DateUtils.currentMonthFirstDateStr(settleDate)).getData().getPkId();
            return memberMapper.queryPlaceParentCuMemberAchieveExt(tableName, 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 = cuMemberSettlePeriodServiceApi.getCuMemberSettlePeriodByDate(date).getData();
            String tableName = TableNameConstants.CU_MEMBER_SETTLE + cuMemberSettlePeriod.getPkId();
            Integer period = cuMemberSettlePeriodServiceApi.getCuMemberSettlePeriodByDate(DateUtils.currentMonthFirstDateStr(settleDate)).getData().getPkId();
            return memberMapper.queryParentParentHisCuMemberAchieveExt(tableName, period, cuMemberUmbrellaParam);
        } else {
            settleDate = DateUtils.beforeDate(1, ChronoUnit.DAYS);
            Integer period = cuMemberSettlePeriodServiceApi.getCuMemberSettlePeriodByDate(DateUtils.currentMonthFirstDateStr(settleDate)).getData().getPkId();
            return memberMapper.queryParentParentCuMemberAchieveExt(period, cuMemberUmbrellaParam);
        }
    }

    /**
     * @description: 统计分析-伞下直推
     * @author: zhang jing
     * @date: 2023/6/13 13:54
     * @param: [memberId]
     * @return: java.util.List<com.hzs.member.statis.vo.UmbrellaBelowPushVO>
     **/
    @Override
    public List<UmbrellaBelowPushVO> queryUmbrellaBelowPush(UmbrellaBelowPushVO umbrellaBelowPushVO) {
        return memberMapper.queryUmbrellaBelowPush(umbrellaBelowPushVO);
    }

    /**
     * @description: 查询会员订单消费业绩
     * @author: zhang jing
     * @date: 2023/9/12 10:41
     * @param: [umbrellaBelowPushVO]
     * @return: java.util.List<com.hzs.report.member.vo.UmbrellaBelowPushVO>
     **/
    @Override
    public List<UmbrellaBelowPushVO> queryOrderExpend(UmbrellaBelowPushVO umbrellaBelowPushVO) {
        return memberMapper.queryOrderExpend(umbrellaBelowPushVO);
    }

    /**
     * @description: 统计分析-伞下直推会员信息
     * @author: zhang jing
     * @date: 2023/6/13 13:44
     * @param: [memberId]
     * @return: java.util.List<com.hzs.member.statis.vo.UmbrellaBelowPushVO>
     **/
    @Override
    public UmbrellaBelowPushVO queryUmbrellaBelowInfo(Long pkId) {
        return memberMapper.queryUmbrellaBelowInfo(pkId);
    }


    /**
     * 查询会员左右区人数
     *
     * @param pkMember
     * @return
     */
    @Override
    public List<DeptMemberNumberVO> selectByDeptNumberPlace(Long pkMember) {
        return memberMapper.selectByDeptNumberPlace(pkMember);
    }

}
