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

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.EOrderType;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.domain.member.achieve.CuActiveMemberCensus;
import com.hzs.common.domain.sale.order.SaOrder;
import com.hzs.member.achieve.controller.vo.CuActiveMemberCensusVO;
import com.hzs.member.achieve.mapper.CuActiveMemberCensusMapper;
import com.hzs.member.achieve.service.ICuActiveMemberCensusService;
import com.hzs.member.achieve.service.ICuMemberSettlePeriodService;
import com.hzs.member.bonus.service.ICuMemberBonusService;
import com.hzs.sale.order.ISaOrderServiceApi;
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.util.List;

/**
 * 统计活跃会员 服务实现类
 *
 * @author zhangjing
 * @since 2023-05-17
 */
@Service
public class CuActiveMemberCensusServiceImpl extends ServiceImpl<CuActiveMemberCensusMapper, CuActiveMemberCensus> implements ICuActiveMemberCensusService {

    @Autowired
    private ICuMemberBonusService cuMemberBonusService;
    @Autowired
    ICuMemberSettlePeriodService cuMemberSettlePeriodService;
    @DubboReference
    ISaOrderServiceApi orderServiceApi;

    /**
     * @description: 手动补充数据
     * @author: zhang jing
     * @date: 2023/5/18 15:57
     * @param: []
     * @return: java.lang.Boolean
     **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean supplementData(String payTime) {
        R<List<SaOrder>> registerMember = orderServiceApi.querySaOrderByOrderType(payTime);
        if (CollectionUtil.isNotEmpty(registerMember.getData())) {
            for (SaOrder so : registerMember.getData()) {
                CuActiveMemberCensusVO cam = new CuActiveMemberCensusVO();
                cam.setPkMember(so.getPkMember());
                cam.setPayTime(payTime);
                CuActiveMemberCensusVO camc = checkActiveMember(cam);
                if (StringUtils.isNotNull(camc)) {
                    UpdateWrapper<CuActiveMemberCensus> updateWrapper = new UpdateWrapper();
                    updateWrapper.eq("PK_ID", camc.getPkId());
                    if (so.getOrderType() == EOrderType.REGISTER_ORDER.getValue()) {
                        updateWrapper.set("REGISTER", camc.getRegister() + 1);
                    }
                    if (so.getOrderType() == EOrderType.UPGRADE_ORDER.getValue()) {
                        updateWrapper.set("UPGRADE", camc.getUpgrade() + 1);
                    }
                    if (so.getOrderType() == EOrderType.REPURCHASE_ORDER.getValue()) {
                        updateWrapper.set("REPURCHASE", camc.getRepurchase() + 1);
                    }
                    baseMapper.update(null, updateWrapper);
                } else {
                    CuActiveMemberCensus mc = new CuActiveMemberCensus();
                    mc.setPkMember(so.getPkMember());
                    mc.setCreationTime(DateUtils.parseStringToDate(payTime));
                    if (so.getOrderType() == EOrderType.REGISTER_ORDER.getValue()) {
                        mc.setRegister(1);
                    }
                    if (so.getOrderType() == EOrderType.UPGRADE_ORDER.getValue()) {
                        mc.setUpgrade(1);
                    }
                    if (so.getOrderType() == EOrderType.REPURCHASE_ORDER.getValue()) {
                        mc.setRepurchase(1);
                    }
                    baseMapper.insert(mc);
                }
            }
        }
        int period = cuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(payTime).getPkId();
        List<Long> villageMember = cuMemberBonusService.queryMemberBonusByTime(period);
        if (CollectionUtil.isNotEmpty(villageMember)) {
            for (Long l : villageMember) {
                CuActiveMemberCensusVO cam = new CuActiveMemberCensusVO();
                cam.setPkMember(l);
                cam.setPayTime(payTime);
                CuActiveMemberCensusVO camc = checkActiveMember(cam);
                if (StringUtils.isNotNull(camc)) {
                    UpdateWrapper<CuActiveMemberCensus> updateWrapper = new UpdateWrapper();
                    updateWrapper.eq("PK_MEMBER", l);
                    updateWrapper.set("NEWLY_VILLAGE", camc.getNewlyVillage() + 1);
                    baseMapper.update(null, updateWrapper);
                } else {
                    CuActiveMemberCensus mc = new CuActiveMemberCensus();
                    mc.setPkMember(l);
                    mc.setCreationTime(DateUtils.parseStringToDate(payTime));
                    mc.setNewlyVillage(1);
                    baseMapper.insert(mc);
                }
            }
        }
        return true;
    }

    /**
     * @description: 查询伞下活跃用户
     * @author: zhang jing
     * @date: 2023/5/18 10:30
     * @param: [cvo]
     * @return: java.util.List<com.hzs.member.achieve.controller.vo.CuActiveMemberCensusVO>
     **/
    @Override
    public List<CuActiveMemberCensusVO> queryActiveMember(CuActiveMemberCensusVO cvo) {
        return baseMapper.queryActiveMember(cvo);
    }

    /**
     * @description: 查询会员当天是否活跃
     * @author: zhang jing
     * @date: 2023/5/18 14:33
     * @param: [cam]
     * @return: java.util.List<com.hzs.common.domain.member.achieve.CuActiveMemberCensus>
     **/
    @Override
    public CuActiveMemberCensusVO checkActiveMember(CuActiveMemberCensusVO cam) {
        return baseMapper.checkActiveMember(cam);
    }

}
