package com.hzs.activity.recommend.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.activity.base.param.ActivityParam;
import com.hzs.activity.base.service.*;
import com.hzs.activity.base.vo.AcGiftConfigVO;
import com.hzs.activity.base.vo.BaseConfigVo;
import com.hzs.activity.dto.AcRecommendUpgradeDTO;
import com.hzs.activity.people.vo.AcBaseConfigVO;
import com.hzs.activity.recommend.mapper.AcRepurCouponsRuleConfigMapper;
import com.hzs.activity.recommend.param.AcDirectPushUpgradeParam;
import com.hzs.activity.recommend.param.AcRecommendParam;
import com.hzs.activity.recommend.service.*;
import com.hzs.activity.recommend.vo.*;
import com.hzs.activity.wares.service.IAcGiftConfigService;
import com.hzs.common.core.constant.EnumsPrefixConstants;
import com.hzs.common.core.constant.RabbitMqConstants;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.*;
import com.hzs.common.core.exception.ServiceException;
import com.hzs.common.core.utils.CommonUtil;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.domain.activity.base.*;
import com.hzs.common.domain.activity.base.ext.AcGiftConfigExt;
import com.hzs.common.domain.activity.recommend.*;
import com.hzs.common.domain.member.base.CuMember;
import com.hzs.common.domain.sale.ext.SaOrderExt;
import com.hzs.common.domain.sale.order.SaOrder;
import com.hzs.common.service.ITransactionCommonService;
import com.hzs.member.base.IMemberServiceApi;
import com.hzs.sale.product.IProductServiceApi;
import com.hzs.system.sys.dto.ApprovalBusinessResultDTO;
import com.hzs.system.sys.dto.BusinessLogDTO;
import com.hzs.system.sys.dto.LoginUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 直推配置 服务实现类
 */
@Slf4j
@Service
public class AcRepurCouponsRuleConfigServiceImpl extends ServiceImpl<AcRepurCouponsRuleConfigMapper, AcRepurCouponsRuleConfig> implements IAcRepurCouponsRuleConfigService {

    @Autowired
    private IActivityService iActivityService;
    @Autowired
    private IAcBaseConfigService iAcBaseConfigService;
    @Autowired
    private IAcRecomUpgradeRuleConfigService iAcRecomUpgradeRuleConfigService;
    @Autowired
    private IAcRecomUpgradeRecordService iAcRecomUpgradeRecordService;

    @Autowired
    private IAcGiftConfigService giftConfigService;
    @Autowired
    private IAcApprovalLogService acApprovalLogService;
    @Autowired
    private IAcGoalMemberConfigService acGoalMemberConfigService;
    @Autowired
    private IAcRepurSharOrderTypeService acRepurSharOrderTypeService;
    @Autowired
    private IAcRepurSharRuleConfigService acRepurSharRuleConfigService;
    @Autowired
    private IAcBaseAuthorityService iAcBaseAuthorityService;

    @DubboReference
    IMemberServiceApi iMemberServiceApi;
    @DubboReference
    IProductServiceApi iProductServiceApi;

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private ITransactionCommonService iTransactionCommonService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public String onLinePetition(AcRecommendParam recommendParam) {
        log.info("直推参数{}", JSONUtil.toJsonStr(recommendParam));
        EApprovalBusiness approvalBusiness = EApprovalBusiness.DIRECT_PUSH_GIFT;
        //根据产品ID查询产品名称
        List<AcRecommendUpgradeVO> recommendUpgradeList = recommendParam.getRecommendUpgradeList();
        if (CollUtil.isNotEmpty(recommendUpgradeList)) {
            for (AcRecommendUpgradeVO recommendUpgradeVO : recommendUpgradeList) {
                List<AcGiftConfigVO> giftConfigList = recommendUpgradeVO.getGiftConfigList();
                if (CollUtil.isNotEmpty(giftConfigList)) {
                    for (AcGiftConfigVO giftConfigVO : giftConfigList) {
                        Integer pkProduct = giftConfigVO.getPkProduct();
                        if (null != pkProduct) {
                            giftConfigVO.setProductName(iProductServiceApi.getProduct(pkProduct).getData().getProductName());
                        }
                    }
                }
            }
        }
        Integer controlType = recommendParam.getControlType();
        if (controlType.equals(EControlType.SAVE.getValue())) {
            return saveRecommendData(recommendParam);
        } else if (controlType.equals(EControlType.MODIFY.getValue())) {
            deleteRecommendData(recommendParam);
            return saveRecommendData(recommendParam);
        } else {
            deleteRecommendData(recommendParam);
        }
        //业务单号
        String businessCode = CommonUtil.createSerialNumber(EOrderPrefix.ACTIVITY_CODE.getValue());
        //日志
        packageRecommendOperationLog(recommendParam, approvalBusiness, businessCode);
        return null;
    }


    /**
     * 直推赠送操作日志
     *
     * @param param            入参
     * @param approvalBusiness 业务类型
     * @param businessCode     业务代码
     */
    private void packageRecommendOperationLog(AcRecommendParam param, EApprovalBusiness approvalBusiness, String businessCode) {
        //活动的基本信息
        ActivityParam activityParam = param.getActivityParam();
        //推荐升级
        List<AcRecommendUpgradeVO> recommendUpgradeList = param.getRecommendUpgradeList();
        //直推复购券
        List<AcRepurchaseCouponsShowVO> repurchaseCouponsShowList = param.getRepurchaseCouponsShowList();
        //复购均分
        AcRecommendSharVO acRecommendSharVO = param.getAcRecommendSharVO();
        Map<String, String> transactionMap = iTransactionCommonService.exportEnumTransaction(EControlType.values());
        Integer controlType = param.getControlType();
        controlType = null == controlType ? EControlType.SAVE.getValue() : controlType;
        StringBuilder sb = new StringBuilder();
        sb.append("活动名称：").append(activityParam.getActName())
                .append(" 活动说明：").append(activityParam.getActExplain())
                .append(" 活动时间：").append(DateUtils.parseDateTimeToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, activityParam.getActStartDate())).append(" 至 ")
                .append(DateUtils.parseDateTimeToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, activityParam.getActEndDate()))
                .append(" 业务类型：");
        if (CollUtil.isNotEmpty(recommendUpgradeList)) {
            sb.append(EApprovalBusiness.DIRECT_PUSH_UPGRADE.getLabel()).append(" ");
        }
        if (CollUtil.isNotEmpty(repurchaseCouponsShowList)) {
            sb.append(EApprovalBusiness.REPURCHASE_COUPON.getLabel()).append(" ");
        }
        if (null != acRecommendSharVO && CollUtil.isNotEmpty(acRecommendSharVO.getOrderTypeList())) {
            sb.append(EApprovalBusiness.REPURCHASE_SHAR.getLabel());
        }
        sb.append(" 操作类型：").append(transactionMap.get(EnumsPrefixConstants.CONTROL_TYPE + controlType));
        BusinessLogDTO businessLog = BusinessLogDTO.builder()
                .businessType(approvalBusiness.getValue())
                .businessCode(businessCode)
                .content(sb.toString()).pkCreator(param.getPkMember())
                .pkCountry(param.getPkCountry())
                .freeSignFlag(false).build();
        rabbitTemplate.convertAndSend(RabbitMqConstants.BUSINESS_LOG_EXCHANGE, RabbitMqConstants.BUSINESS_LOG_KEY, Collections.singleton(businessLog));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String approvalAgree(ApprovalBusinessResultDTO businessResult) {
        EApprovalBusiness eApprovalBusiness = businessResult.getEApprovalBusiness();
        String businessCode = businessResult.getBusinessCode();
        LoginUser loginUser = businessResult.getLoginUser();
        AcApprovalLog approvalLog = acApprovalLogService.queryApprovalLog(eApprovalBusiness, businessCode, loginUser.getDataCountry());
        if (null == approvalLog) {
            return "审批数据不存在！";
        }
        Integer controlType = approvalLog.getControlType();
        String data = approvalLog.getAfterData();
        if (StringUtils.isNotEmpty(data)) {
            AcRecommendParam recommendParam = JSONUtil.toBean(data, AcRecommendParam.class);
            if (controlType.equals(EControlType.SAVE.getValue())) {
                return saveRecommendData(recommendParam);
            } else if (controlType.equals(EControlType.MODIFY.getValue())) {
                deleteRecommendData(recommendParam);
                return saveRecommendData(recommendParam);
            } else {
                deleteRecommendData(recommendParam);
            }
        }
        //更新审批状态
        acApprovalLogService.updateApprovalLog(eApprovalBusiness, businessCode, loginUser.getDataCountry(),
                loginUser.getUserId(), EApproveRechargeStatus.AGREE, businessResult.getApproveRemark());
        return null;
    }

    /**
     * 保存直推赠送数据
     *
     * @param recommendParam 入参
     * @return String
     */
    private String saveRecommendData(AcRecommendParam recommendParam) {
        //保存活动基本信息
        String message = saveBaseConfig(recommendParam);
        //保存复购券配置信息
        saveRepurchaseCouponsInfo(recommendParam);
        //保存直推升级配置信息
        saveRecommendUpgradeRuleInfo(recommendParam);
        //保存复购均分
        saveRepurchaseSharInfo(recommendParam);
        // 保存活动权限
        this.saveAuthority(recommendParam);
        return message;
    }

    /**
     * 删除直推赠送数据
     *
     * @param recommendParam 入参
     * @return String
     */
    public String deleteRecommendData(AcRecommendParam recommendParam) {
        Long pkBaseId = recommendParam.getActivityParam().getPkId();
        Long pkModifyId = recommendParam.getPkMember();
        //删除活动基本信息
        iAcBaseConfigService.deleteBaseConfigById(pkBaseId, pkModifyId);
        // 删除活动权限配置
        iAcBaseAuthorityService.update(Wrappers.<AcBaseAuthority>lambdaUpdate()
                .eq(AcBaseAuthority::getPkBaseId, pkBaseId)
                .eq(AcBaseAuthority::getDelFlag, EDelFlag.UN_DELETE.getValue())
                .set(AcBaseAuthority::getDelFlag, EDelFlag.DELETE.getValue())
        );
        acGoalMemberConfigService.deleteGoalMemberConfig(pkBaseId, pkModifyId);
        //删除复购券配置信息
        List<AcRepurchaseCouponsShowVO> repurchaseCouponsShowList = recommendParam.getRepurchaseCouponsShowList();
        if (CollUtil.isNotEmpty(repurchaseCouponsShowList)) {
            deleteCouponsRuleConfigByBaseId(pkBaseId, pkModifyId);
        }
        //删除直推升级配置信息
        List<AcRecommendUpgradeVO> recommendUpgradeList = recommendParam.getRecommendUpgradeList();
        if (CollUtil.isNotEmpty(recommendUpgradeList)) {
            iAcRecomUpgradeRuleConfigService.deleteUpgradeRuleConfigByBaseId(pkBaseId, pkModifyId);
        }
        //删除复购均分
        AcRecommendSharVO acRecommendSharVO = recommendParam.getAcRecommendSharVO();
        if (null != acRecommendSharVO && CollUtil.isNotEmpty(acRecommendSharVO.getOrderTypeList())) {
            AcRepurSharRuleConfig repurSharRuleConfig = acRepurSharRuleConfigService.selectAcRepurSharRuleConfigByBaseId(pkBaseId);
            if (null == repurSharRuleConfig) {
                return "复购均分规则配置不存在！";
            }
            acRepurSharRuleConfigService.deleteSharRuleConfigByActivityId(pkBaseId, pkModifyId);
            acRepurSharOrderTypeService.deleteSharOrderTypeByRuleId(repurSharRuleConfig.getPkId(), pkModifyId);
        }
        return null;
    }


    /**
     * 保存活动基本信息
     *
     * @param recommendParam 入参
     * @return
     */
    private String saveBaseConfig(AcRecommendParam recommendParam) {
        ActivityParam activityParam = recommendParam.getActivityParam();
        if (null == activityParam) {
            return "没有活动的基本信息！";
        }
        List<AcRecommendUpgradeVO> recommendUpgradeList = recommendParam.getRecommendUpgradeList();
        if (CollUtil.isNotEmpty(recommendUpgradeList)) {
            activityParam.setAtcModule(EActModule.DIRECT_PUSH_UPGRADE.getValue());
        }
        activityParam.setActType(EActType.DIRECT_PUSH_GIFT.getValue());
        //保存基础配置
        activityParam.setPkCreator(recommendParam.getPkMember());
        activityParam.setPkCountry(recommendParam.getPkCountry());
        AcBaseConfig acBaseConfig = iActivityService.saveBaseConfig(activityParam);
        recommendParam.setPkBaseId(acBaseConfig.getPkId());
        iActivityService.saveGoalMemberConfig(activityParam, acBaseConfig);

        return null;
    }


    /**
     * 保存复购券配置信息
     *
     * @param recommendParam 入参
     * @return Boolean
     */
    private Boolean saveRepurchaseCouponsInfo(AcRecommendParam recommendParam) {
        List<AcRepurCouponsRuleConfig> repurCouponsRuleConfigList = new ArrayList<>();
        List<AcRepurchaseCouponsShowVO> repurchaseCouponsShowList = recommendParam.getRepurchaseCouponsShowList();
        if (CollUtil.isNotEmpty(repurchaseCouponsShowList)) {
            for (AcRepurchaseCouponsShowVO repurchaseCouponsShowVO : repurchaseCouponsShowList) {
                repurCouponsRuleConfigList.addAll(getRepurchaseCouponsRuleConfig(recommendParam, repurchaseCouponsShowVO));
            }

        }
        return saveBatch(repurCouponsRuleConfigList);
    }


    /**
     * 保存复购均分配置信息
     *
     * @param recommendParam 入参
     * @return Boolean
     */
    private Boolean saveRepurchaseSharInfo(AcRecommendParam recommendParam) {
        AcRecommendSharVO acRecommendSharVO = recommendParam.getAcRecommendSharVO();
        Long pkBaseId = recommendParam.getPkBaseId();
        if (null != acRecommendSharVO) {
            List<Integer> orderTypeList = acRecommendSharVO.getOrderTypeList();
            if (CollUtil.isNotEmpty(orderTypeList)) {
                AcRepurSharRuleConfig repurSharRuleConfig = AcRepurSharRuleConfig.builder().pkBaseId(pkBaseId)
                        .orderAmount(acRecommendSharVO.getOrderAmount()).orderAchieve(acRecommendSharVO.getOrderAchieve())
                        .ratioValue(acRecommendSharVO.getRatioValue()).build();
                repurSharRuleConfig.setPkCreator(recommendParam.getPkMember());
                repurSharRuleConfig.setPkCountry(recommendParam.getPkCountry());
                //复购券均分-规则配置信息
                acRepurSharRuleConfigService.save(repurSharRuleConfig);

                AcRepurSharOrderType repurSharOrderType;
                List<AcRepurSharOrderType> sharOrderTypes = new ArrayList<>();
                for (Integer orderType : orderTypeList) {
                    repurSharOrderType = AcRepurSharOrderType.builder().pkRuleId(repurSharRuleConfig.getPkId())
                            .orderType(orderType).build();
                    repurSharOrderType.setPkCreator(recommendParam.getPkMember());
                    repurSharOrderType.setPkCountry(recommendParam.getPkCountry());
                    sharOrderTypes.add(repurSharOrderType);
                }
                //复购券均分-订单类型
                acRepurSharOrderTypeService.saveBatch(sharOrderTypes);
            }
        }
        return true;
    }

    /**
     * 保存活动权限
     *
     * @param recommendParam
     */
    private void saveAuthority(AcRecommendParam recommendParam) {
        List<AcBaseAuthority> authorityList = recommendParam.getAuthorityList();
        if (CollectionUtil.isNotEmpty(authorityList)) {
            for (AcBaseAuthority acBaseAuthority : authorityList) {
                acBaseAuthority.setPkBaseId(recommendParam.getPkBaseId());
            }
            iAcBaseAuthorityService.saveBatch(authorityList);
        }
    }

    /**
     * 每个专区复购券规则配置
     *
     * @param recommendParam          直推赠送
     * @param repurchaseCouponsShowVO 专区复购券规则
     * @return List<AcRepurCouponsRuleConfig>
     */
    private List<AcRepurCouponsRuleConfig> getRepurchaseCouponsRuleConfig(AcRecommendParam recommendParam, AcRepurchaseCouponsShowVO repurchaseCouponsShowVO) {
        AcRepurCouponsRuleConfig repurCouponsRuleConfig;
        List<AcRepurCouponsRuleConfig> repurCouponsRuleConfigList = new ArrayList<>();
        //专区
        Integer orderType = repurchaseCouponsShowVO.getOrderType();
        List<AcRepurchaseCouponsVO> repurchaseCouponsList = repurchaseCouponsShowVO.getRepurchaseCouponsList();
        if (CollUtil.isNotEmpty(repurchaseCouponsList)) {
            //循环每个专区
            for (AcRepurchaseCouponsVO repurchaseCouponsVO : repurchaseCouponsList) {
                repurCouponsRuleConfig = AcRepurCouponsRuleConfig.builder().pkBaseId(recommendParam.getPkBaseId())
                        .orderType(orderType).orderAmount(repurchaseCouponsVO.getOrderAmount())
                        .orderAchieve(repurchaseCouponsVO.getOrderAchieve()).ratioType(repurchaseCouponsVO.getRatioType())
                        .ratioValue(repurchaseCouponsVO.getRatioValue()).build();
                repurCouponsRuleConfig.setPkCreator(recommendParam.getPkMember());
                repurCouponsRuleConfig.setPkCountry(recommendParam.getPkCountry());
                repurCouponsRuleConfigList.add(repurCouponsRuleConfig);
            }
        }
        return repurCouponsRuleConfigList;
    }


    /**
     * 保存直推升级配置信息
     *
     * @param recommendParam 直推信息
     * @return Boolean
     */
    private Boolean saveRecommendUpgradeRuleInfo(AcRecommendParam recommendParam) {
        AcRecomUpgradeRuleConfig recomUpgradeRuleConfig;
        //直推升级规则配置
        List<AcRecommendUpgradeVO> recommendUpgradeList = recommendParam.getRecommendUpgradeList();
        if (CollUtil.isNotEmpty(recommendUpgradeList)) {
            for (AcRecommendUpgradeVO recommendUpgradeVO : recommendUpgradeList) {
                recommendUpgradeVO.setPkCreator(recommendParam.getPkMember());
                recommendUpgradeVO.setPkCountry(recommendParam.getPkCountry());
                recomUpgradeRuleConfig = BeanUtil.copyProperties(recommendUpgradeVO, AcRecomUpgradeRuleConfig.class);
                recomUpgradeRuleConfig.setPkBaseId(recommendParam.getPkBaseId());
                iAcRecomUpgradeRuleConfigService.save(recomUpgradeRuleConfig);
                List<AcGiftConfig> giftConfigs = saveRecommendUpgradeGift(recommendUpgradeVO, recomUpgradeRuleConfig.getPkId());
                giftConfigService.saveBatch(giftConfigs);
            }
        }
        return true;
    }

    /**
     * 保存活动审批日志信息
     *
     * @param recommendParam   直推赠送参数
     * @param approvalBusiness 审批业务
     * @return boolean
     */
    public AcApprovalLog saveApprovalLog(AcRecommendParam recommendParam, EApprovalBusiness approvalBusiness, String businessCode) {
        String detailJson = null;
        Integer controlType = recommendParam.getControlType();
        //查询变更前的数据
        Long pkBaseId = recommendParam.getActivityParam().getPkId();
        if (null != pkBaseId) {
            String detail = JSONUtil.toJsonStr(showDetailInfo(pkBaseId, recommendParam.getPkCountry()));
            detailJson = null != detail ? JSONUtil.toJsonStr(detail) : null;
        }
        String afterData = JSONUtil.toJsonStr(recommendParam);
        String beforeData = controlType.equals(EControlType.MODIFY.getValue()) ? detailJson : null;
        AcApprovalLog approvalLog = AcApprovalLog.builder().approvalType(approvalBusiness.getValue())
                .beforeData(beforeData).businessCode(businessCode).controlType(controlType)
                .approvalStatus(EApproveRechargeStatus.WAIT.getValue()).afterData(afterData)
                .build();
        approvalLog.setPkCreator(recommendParam.getPkMember());
        approvalLog.setPkCountry(recommendParam.getPkCountry());
        acApprovalLogService.save(approvalLog);
        return approvalLog;
    }

    /**
     * 直推升级赠品
     *
     * @param recommendUpgradeVO 直推升级赠品VO
     * @param pkRuleId           规则ID
     * @return List<AcGiftConfig>
     */
    public List<AcGiftConfig> saveRecommendUpgradeGift(AcRecommendUpgradeVO recommendUpgradeVO, Long pkRuleId) {
        AcGiftConfig giftConfig;
        List<AcGiftConfig> giftConfigList = new ArrayList<>();
        List<AcGiftConfigVO> giftConfigVOList = recommendUpgradeVO.getGiftConfigList();
        if (CollUtil.isNotEmpty(giftConfigVOList)) {
            for (AcGiftConfigVO giftConfigVO : giftConfigVOList) {
                if (null == giftConfigVO.getPkProduct()) {
                    continue;
                }
                giftConfig = BeanUtil.copyProperties(giftConfigVO, AcGiftConfig.class);
                giftConfig.setPkCreator(recommendUpgradeVO.getPkCreator());
                giftConfig.setPkCountry(recommendUpgradeVO.getPkCountry());
                giftConfig.setPkRuleId(pkRuleId);
                giftConfig.setActType(EActType.DIRECT_PUSH_GIFT.getValue());
                giftConfigList.add(giftConfig);
            }
        }
        return giftConfigList;
    }

    @Override
    public AcRecommendVO showDetailInfo(Long activityId, Integer pkCountry) {
        AcRecommendVO recommendVO = new AcRecommendVO();
        recommendVO.setPkBaseId(activityId);
        AcBaseConfig baseConfig = iAcBaseConfigService.getById(activityId);
        //查询目标用户群体
        List<AcGoalMemberConfig> acGoalMemberConfigs = acGoalMemberConfigService.selectMemberConfig(activityId);
        BaseConfigVo baseConfigVo = BeanUtil.copyProperties(baseConfig, BaseConfigVo.class);
        baseConfigVo.setGoalMemberConfigList(acGoalMemberConfigs);

        recommendVO.setPkBaseId(baseConfig.getPkId());
        recommendVO.setActivityParam(baseConfigVo);

        // 查询活动权限
        recommendVO.setAuthorityList(iAcBaseAuthorityService.listAuthority(activityId));
        //查询复购券规则
        List<AcRepurCouponsRuleConfig> repurCouponsRuleConfigList = selectRuleConfigByActivityId(activityId);
        recommendVO.setRepurchaseCouponsShowList(conversionAcRepurchaseCouponsShowVO(repurCouponsRuleConfigList));
        //查询直推升级规则
        List<AcRecomUpgradeRuleConfig> recomUpgradeRuleConfigList = iAcRecomUpgradeRuleConfigService.selectByActivityId(activityId);
        recommendVO.setRecommendUpgradeList(conversionRecommendUpgradeVO(recomUpgradeRuleConfigList, pkCountry));
        //复购券均分
        AcRepurSharRuleConfig repurSharRuleConfig = acRepurSharRuleConfigService.selectAcRepurSharRuleConfigByBaseId(activityId);
        if (null != repurSharRuleConfig) {
            List<AcRepurSharOrderType> repurSharOrderTypes = acRepurSharOrderTypeService.selectSharOrderByRuleId(repurSharRuleConfig.getPkId());
            //订单类型
            List<Integer> orderTypeList = repurSharOrderTypes.stream().map(AcRepurSharOrderType::getOrderType).collect(Collectors.toList());

            AcRecommendSharVO recommendSharVO = BeanUtil.copyProperties(repurSharRuleConfig, AcRecommendSharVO.class);
            recommendSharVO.setOrderTypeList(orderTypeList);
            recommendVO.setAcRecommendSharVO(recommendSharVO);
        }

        return recommendVO;
    }

    /**
     * 转化推荐升级
     *
     * @param recomUpgradeRuleConfigList 推荐升级
     * @param pkCountry                  国家ID
     * @return List<AcRecommendUpgradeVO>
     */
    private List<AcRecommendUpgradeVO> conversionRecommendUpgradeVO(List<AcRecomUpgradeRuleConfig> recomUpgradeRuleConfigList, Integer pkCountry) {
        List<AcRecommendUpgradeVO> recommendUpgradeVOList = new ArrayList<>();
        if (CollUtil.isNotEmpty(recomUpgradeRuleConfigList)) {
            AcRecommendUpgradeVO acRecommendUpgradeVO;
            List<Long> ruleId = recomUpgradeRuleConfigList.stream().map(AcRecomUpgradeRuleConfig::getPkId).collect(Collectors.toList());
            Map<Long, List<AcGiftConfigExt>> listMap = giftConfigService.queryListByRuleIdList(ruleId, pkCountry);
            for (AcRecomUpgradeRuleConfig recomUpgradeRuleConfig : recomUpgradeRuleConfigList) {
                acRecommendUpgradeVO = BeanUtil.copyProperties(recomUpgradeRuleConfig, AcRecommendUpgradeVO.class);
                //赠品
                List<AcGiftConfigVO> giftConfigList = BeanUtil.copyToList(listMap.get(recomUpgradeRuleConfig.getPkId()), AcGiftConfigVO.class);
                acRecommendUpgradeVO.setGiftConfigList(giftConfigList);
                recommendUpgradeVOList.add(acRecommendUpgradeVO);
            }
        }
        return recommendUpgradeVOList;
    }

    /**
     * 转化直推复购券VO
     *
     * @param repurCouponsRuleConfigList 直推复购券
     * @return List<AcRepurchaseCouponsShowVO>
     */
    private List<AcRepurchaseCouponsShowVO> conversionAcRepurchaseCouponsShowVO(List<AcRepurCouponsRuleConfig> repurCouponsRuleConfigList) {
        List<AcRepurchaseCouponsShowVO> showVOList = new ArrayList<>();
        AcRepurchaseCouponsShowVO repurchaseCouponsShowVO;
        if (CollUtil.isNotEmpty(repurCouponsRuleConfigList)) {
            AcRepurchaseCouponsVO acRepurchaseCouponsVO;
            List<AcRepurchaseCouponsVO> repurchaseCouponsVOList;
            //转换每个专区的配置
            Map<Integer, List<AcRepurCouponsRuleConfig>> map = repurCouponsRuleConfigList.stream().collect(Collectors.groupingBy(AcRepurCouponsRuleConfig::getOrderType));
            //封装AcRepurchaseCouponsShowVO
            for (Map.Entry<Integer, List<AcRepurCouponsRuleConfig>> entry : map.entrySet()) {
                repurchaseCouponsShowVO = new AcRepurchaseCouponsShowVO();
                repurchaseCouponsVOList = new ArrayList<>();
                //循环每个分区
                repurchaseCouponsShowVO.setOrderType(entry.getKey());
                for (AcRepurCouponsRuleConfig repurCouponsRuleConfig : entry.getValue()) {
                    acRepurchaseCouponsVO = BeanUtil.copyProperties(repurCouponsRuleConfig, AcRepurchaseCouponsVO.class);
                    repurchaseCouponsVOList.add(acRepurchaseCouponsVO);
                }
                repurchaseCouponsShowVO.setRepurchaseCouponsList(repurchaseCouponsVOList);
                showVOList.add(repurchaseCouponsShowVO);
            }
        }
        return showVOList;
    }

    public List<AcRepurCouponsRuleConfig> selectRuleConfigByActivityId(Long activityId) {
        LambdaQueryWrapper<AcRepurCouponsRuleConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AcRepurCouponsRuleConfig::getPkBaseId, activityId);
        return baseMapper.selectList(queryWrapper);
    }


    @Override
    public Boolean deleteRecommendConfig(Long activityId) {
        return null;
    }

    @Override
    public Boolean updateRecommendConfig(AcRecommendVO recommendVO) {
        return null;
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean invokeRecommendUpgrade(AcRecommendUpgradeDTO acRecommendUpgrade) {
        if (EOrderOperateStatus.NORMAL.equals(acRecommendUpgrade.getOrderOperateStatus())) {
            // 正向处理
            AcBaseConfigVO baseConfigVO = AcBaseConfigVO.builder()
                    .actType(EActType.RECOMMEND_UPGRADE.getValue())
                    .pkCountry(acRecommendUpgrade.getSaOrderExt().getPkCountry())
                    .build();
            // 查询订单时内的直推活动
            List<AcBaseConfig> acBaseConfigs = iAcBaseConfigService.selectBaseConfigByType(baseConfigVO);
            if (CollUtil.isNotEmpty(acBaseConfigs)) {
                for (AcBaseConfig baseConfig : acBaseConfigs) {
                    handleRecommendUpgrade(acRecommendUpgrade, baseConfig);
                }
            }
        } else {
            // 撤单处理
            handleCancelRecommendUpgrade(acRecommendUpgrade);
        }
        return true;
    }

    /**
     * 下单处理
     */
    private void handleRecommendUpgrade(AcRecommendUpgradeDTO acRecommendUpgrade, AcBaseConfig baseConfig) {
        Date nowDate = new Date();
        // 订单信息
        SaOrderExt saOrderExt = acRecommendUpgrade.getSaOrderExt();
        // 活动ID
        Long pkBaseId = baseConfig.getPkId();
        // 推荐人
        Long pkMember = saOrderExt.getPkReference();
        // 当前时间年月
        String upgradeTime = DateUtils.parseDateToFormat(DateUtils.YYYYMM, nowDate);
        // 校验推荐人本月是否已经触发过
        if (iAcRecomUpgradeRecordService.count(Wrappers.<AcRecomUpgradeRecord>lambdaQuery()
                .eq(AcRecomUpgradeRecord::getPkBaseId, pkBaseId)
                .eq(AcRecomUpgradeRecord::getPkMember, pkMember)
                .eq(AcRecomUpgradeRecord::getUpgradeTime, upgradeTime)
        ) > 0) {
            return;
        }

        // 推荐人等级ID
        CuMember referenceMember = iMemberServiceApi.getMember(pkMember).getData();
        Integer pkSettleGrade = referenceMember.getPkSettleGrade();
        // 国家
        Integer pkCountry = saOrderExt.getPkCountry();
        AcRecommendUpgradeVO recommendUpgradeVO = AcRecommendUpgradeVO.builder()
                .pkBaseId(pkBaseId)
                .pkSettleGrade(pkSettleGrade)
                .pkCountry(pkCountry)
                .memberType(1)
                .build();
        //查询规则
        List<AcRecomUpgradeRuleConfig> upgradeRuleConfigList = iAcRecomUpgradeRuleConfigService.listRuleConfig(recommendUpgradeVO);
        if (CollUtil.isNotEmpty(upgradeRuleConfigList)) {
            // 升级规则
            AcRecomUpgradeRuleConfig upgradeRuleConfig = upgradeRuleConfigList.get(0);
//            // 查询活动权限
//            if (!this.checkAuthority(baseConfig, saOrderExt.getPkReference())) {
//                return;
//            }
//            log.info("直推升级权限校验，pkBaseId: {}，orderCode: {}", pkBaseId, saOrderExt.getOrderCode());

            // 查询订单时间范围
            // 默认活动开始时间
            Date startTime = baseConfig.getActStartDate();
            // 默认订单时间
            Date endTime = saOrderExt.getPayTime();
            // 当月第一天
            Date monthFirst = DateUtils.getFirstDayOfMonth(nowDate);
            if (monthFirst.compareTo(startTime) > 0) {
                startTime = monthFirst;
            }
            // TODO 需要校验，只处理上次升级之前的订单
            List<SaOrder> recomOrderList = iAcRecomUpgradeRecordService.listRecomOrder(pkMember, upgradeRuleConfig.getPkRecommendGrade(), upgradeRuleConfig.getRecommendNumber(), startTime, endTime);
            // 校验推荐人是否可以升级
            if (CollectionUtil.isEmpty(recomOrderList)
                    || recomOrderList.size() != upgradeRuleConfig.getRecommendNumber()) {
                return;
            }
            // 封装规则升级记录
            AcRecomUpgradeRecord recomUpgradeRecord = AcRecomUpgradeRecord.builder()
                    .pkBaseId(pkBaseId)
                    .pkRuleId(upgradeRuleConfig.getPkId())
                    .pkMember(pkMember)
                    .pkSettleGrade(pkSettleGrade)
                    .pkRecommendGrade(upgradeRuleConfig.getPkUpgradeGrade())
                    .pkOrderId(saOrderExt.getPkId())
                    .upgradeTime(upgradeTime)
                    .build();
            recomUpgradeRecord.setPkCreator(referenceMember.getPkId());
            recomUpgradeRecord.setPkCountry(referenceMember.getPkSettleCountry());
            iAcRecomUpgradeRecordService.save(recomUpgradeRecord);

            // 更新用户信息
            CuMember cuMember = CuMember.builder()
                    .pkId(pkMember)
                    .pkSettleGrade(upgradeRuleConfig.getPkUpgradeGrade())
                    .pkSettleCountry(referenceMember.getPkCountry())
                    .build();
            cuMember.setPkModified(pkMember);
            cuMember.setModifiedTime(nowDate);
            SaOrder saOrder = SaOrder.builder()
                    .pkId(saOrderExt.getPkId())
                    .payTime(nowDate)
                    .build();
            //调用升级接口
            R<?> resultBool = iMemberServiceApi.updateMemberLevel(cuMember, saOrder, pkSettleGrade);
            if (!resultBool.isSuccess()) {
                log.error("直推活动会员升级升级失败. {}", resultBool.getMsg());
                throw new ServiceException(resultBool.getMsg());
            }
        }
    }

    /**
     * 撤单处理
     */
    private void handleCancelRecommendUpgrade(AcRecommendUpgradeDTO acRecommendUpgrade) {
        Date nowDate = new Date();
        // 订单信息
        SaOrderExt saOrderExt = acRecommendUpgrade.getSaOrderExt();
        // 推荐人
        Long pkMember = saOrderExt.getPkReference();
        // 订单年月
        String upgradeTime = DateUtils.parseDateToFormat(DateUtils.YYYYMM, saOrderExt.getPayTime());

        List<AcRecomUpgradeRecord> upgradeRecordList = iAcRecomUpgradeRecordService.list(Wrappers.<AcRecomUpgradeRecord>lambdaQuery()
                .eq(AcRecomUpgradeRecord::getPkMember, pkMember)
                .eq(AcRecomUpgradeRecord::getUpgradeTime, upgradeTime)
                .orderByDesc(AcRecomUpgradeRecord::getUpgradeTime)
        );
        if (CollectionUtil.isNotEmpty(upgradeRecordList)) {
            // 会员活动升级记录
            AcRecomUpgradeRecord recomUpgradeRecord = upgradeRecordList.get(0);
            // 删除活动升级记录
            iAcRecomUpgradeRecordService.update(Wrappers.<AcRecomUpgradeRecord>lambdaUpdate()
                    .eq(AcRecomUpgradeRecord::getPkId, recomUpgradeRecord.getPkId())
                    .set(AcRecomUpgradeRecord::getDelFlag, EDelFlag.DELETE.getValue())
                    .set(AcRecomUpgradeRecord::getModifiedTime, nowDate)
                    .set(AcRecomUpgradeRecord::getPkModified, saOrderExt.getPkMember())
            );

            // 推荐会员信息
            CuMember referenceMember = iMemberServiceApi.getMember(pkMember).getData();
            if (!referenceMember.getPkSettleGrade().equals(recomUpgradeRecord.getPkRecommendGrade())) {
                // 会员目前等于与这次活动升级不一致，只需要删掉升级记录
                CuMember cuMember = CuMember.builder()
                        .pkId(pkMember)
                        .pkSettleGrade(referenceMember.getPkSettleGrade())
                        .build();
                iMemberServiceApi.reduceMemberLevel(cuMember, recomUpgradeRecord.getPkOrderId());
                return;
            }
            // 活动基础配置
            AcBaseConfig baseConfig = iAcBaseConfigService.getById(recomUpgradeRecord.getPkBaseId());
            // 活动基础配置
            AcRecomUpgradeRuleConfig upgradeRuleConfig = iAcRecomUpgradeRuleConfigService.getById(recomUpgradeRecord.getPkRuleId());
            // 查询订单时间范围
            // 默认活动开始时间
            Date startTime = baseConfig.getActStartDate();
            // 默认活动结束时间
            Date endTime = baseConfig.getActEndDate();
            // 当月第一天
            Date monthFirst = DateUtils.getFirstDayOfMonth(nowDate);
            if (monthFirst.compareTo(startTime) > 0) {
                startTime = monthFirst;
            }
            // 当月最后一天
            Date monthLast = DateUtils.getLastDayOfMonth(nowDate);
            if (monthLast.compareTo(endTime) < 0) {
                endTime = monthLast;
            }
            List<SaOrder> recomOrderList = iAcRecomUpgradeRecordService.listRecomOrder(pkMember, upgradeRuleConfig.getPkRecommendGrade(), upgradeRuleConfig.getRecommendNumber(), startTime, endTime);
            // 校验推荐人是否可以升级
            if (CollectionUtil.isEmpty(recomOrderList)
                    || recomOrderList.size() != upgradeRuleConfig.getRecommendNumber()) {
                // 订单撤单后，目前订单不够升级了，需要降级
                CuMember cuMember = CuMember.builder()
                        .pkId(pkMember)
                        .pkSettleGrade(recomUpgradeRecord.getPkSettleGrade())
                        .build();
                iMemberServiceApi.reduceMemberLevel(cuMember, recomUpgradeRecord.getPkOrderId());
                return;
            }

            // 足够升级，更新升级记录时间
            SaOrder newOrder = recomOrderList.get(recomOrderList.size() - 1);

            // 封装规则升级记录
            AcRecomUpgradeRecord newUpgradeRecord = AcRecomUpgradeRecord.builder()
                    .pkBaseId(baseConfig.getPkId())
                    .pkRuleId(upgradeRuleConfig.getPkId())
                    .pkMember(pkMember)
                    .pkSettleGrade(recomUpgradeRecord.getPkSettleGrade())
                    .pkRecommendGrade(recomUpgradeRecord.getPkRecommendGrade())
                    .pkOrderId(newOrder.getPkId())
                    .upgradeTime(upgradeTime)
                    .build();
            newUpgradeRecord.setPkCreator(referenceMember.getPkId());
            newUpgradeRecord.setPkCountry(referenceMember.getPkSettleCountry());
            iAcRecomUpgradeRecordService.save(newUpgradeRecord);

            saOrderExt.setPkId(recomUpgradeRecord.getPkOrderId());
            iMemberServiceApi.changeMemberLevel(saOrderExt, newOrder);
        }
    }


    /**
     * 校验活动权限（true：进行处理，false：不进行处理）
     *
     * @param baseConfig 活动配置
     * @param pkMember   会员ID
     * @return
     */
    private boolean checkAuthority(AcBaseConfig baseConfig, Long pkMember) {
        List<AcBaseAuthority> authList = iAcBaseAuthorityService.list(Wrappers.<AcBaseAuthority>lambdaQuery()
                .eq(AcBaseAuthority::getPkBaseId, baseConfig.getPkId())
                .orderByDesc(AcBaseAuthority::getAuthorityType)
        );
        if (CollectionUtil.isNotEmpty(authList)) {
            Map<Integer, List<String>> authMap = new HashMap<>();
            for (AcBaseAuthority authority : authList) {
                authMap.computeIfAbsent(authority.getAuthorityType(), k -> new ArrayList<>());
                authMap.get(authority.getAuthorityType()).add(authority.getMemberCode());
            }

            for (Integer key : authMap.keySet()) {
                List<String> memberCodeList = authMap.get(key);
                switch (EWaresPermissionType.getEnumByValue(key)) {
                    case AUTH_ORITY_TYPE:
                        // 全部
                        return true;
                    case PLACE_STOP:
                        // 安置禁止
                        return iAcBaseAuthorityService.checkPlaceParent(pkMember, memberCodeList) <= 0;
                    case BLOOD_STOP:
                        // 血缘禁止
                        return iAcBaseAuthorityService.checkParent(pkMember, memberCodeList) <= 0;
                    case PLACE_ALLWO:
                        // 安置可见
                        return iAcBaseAuthorityService.checkPlaceParent(pkMember, memberCodeList) > 0;
                    case BLOOD_ALLOW:
                        // 血缘可见
                        return iAcBaseAuthorityService.checkParent(pkMember, memberCodeList) > 0;
                    default:
                }
            }
        }
        return true;
    }

    @Override
    public List<AcRepurCouponsRuleConfig> queryAcRepurchaseCouponsRule(Date settleDate) {
        return baseMapper.queryAcRepurchaseCouponsRule(settleDate);
    }

    @Override
    public Boolean deleteCouponsRuleConfigByBaseId(Long pkBaseId, Long pkModifyId) {
        LambdaUpdateWrapper<AcRepurCouponsRuleConfig> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(AcRepurCouponsRuleConfig::getDelFlag, EDelFlag.DELETE.getValue());
        updateWrapper.set(AcRepurCouponsRuleConfig::getPkModified, pkModifyId);
        updateWrapper.set(AcRepurCouponsRuleConfig::getModifiedTime, new Date());
        updateWrapper.eq(AcRepurCouponsRuleConfig::getPkBaseId, pkBaseId);
        return update(updateWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String directPushUpgradeSubmit(AcDirectPushUpgradeParam acDirectPushUpgradeParam) {
        log.info("直推参数{}", JSONUtil.toJsonStr(acDirectPushUpgradeParam));
        EApprovalBusiness approvalBusiness = EApprovalBusiness.DIRECT_PUSH_UPGRADE;
        //根据产品ID查询产品名称
        List<AcRecommendUpgradeVO> recommendUpgradeList = acDirectPushUpgradeParam.getRecommendUpgradeList();
        if (CollUtil.isNotEmpty(recommendUpgradeList)) {
            for (AcRecommendUpgradeVO recommendUpgradeVO : recommendUpgradeList) {
                List<AcGiftConfigVO> giftConfigList = recommendUpgradeVO.getGiftConfigList();
                if (CollUtil.isNotEmpty(giftConfigList)) {
                    for (AcGiftConfigVO giftConfigVO : giftConfigList) {
                        Integer pkProduct = giftConfigVO.getPkProduct();
                        if (null != pkProduct) {
                            giftConfigVO.setProductName(iProductServiceApi.getProduct(pkProduct).getData().getProductName());
                        }
                    }
                }
            }
        }
        Integer controlType = acDirectPushUpgradeParam.getControlType();
        if (controlType.equals(EControlType.SAVE.getValue())) {
            log.info("直推升级新增{}", JSONUtil.toJsonStr(acDirectPushUpgradeParam));
            return saveDirectPushUpgradeData(acDirectPushUpgradeParam);
        } else if (controlType.equals(EControlType.MODIFY.getValue())) {
            log.info("直推升级修改{}", JSONUtil.toJsonStr(acDirectPushUpgradeParam));
            deleteDirectPushUpgradeData(acDirectPushUpgradeParam);
            return saveDirectPushUpgradeData(acDirectPushUpgradeParam);
        } else {
            log.info("直推升级刪除{}", JSONUtil.toJsonStr(acDirectPushUpgradeParam));
            deleteDirectPushUpgradeData(acDirectPushUpgradeParam);
        }
        //业务单号
        String businessCode = CommonUtil.createSerialNumber(EOrderPrefix.ACTIVITY_CODE.getValue());
        //日志
        packageDirectPushOperationLog(acDirectPushUpgradeParam, approvalBusiness, businessCode);
        return null;
    }

    private void packageDirectPushOperationLog(AcDirectPushUpgradeParam param, EApprovalBusiness approvalBusiness, String businessCode) {
        //活动的基本信息
        ActivityParam activityParam = param.getActivityParam();
        //推荐升级
        List<AcRecommendUpgradeVO> recommendUpgradeList = param.getRecommendUpgradeList();
        Map<String, String> transactionMap = iTransactionCommonService.exportEnumTransaction(EControlType.values());
        Integer controlType = param.getControlType();
        controlType = null == controlType ? EControlType.SAVE.getValue() : controlType;
        StringBuilder sb = new StringBuilder();
        sb.append("活动名称：").append(activityParam.getActName())
                .append(" 活动说明：").append(activityParam.getActExplain())
                .append(" 活动时间：").append(DateUtils.parseDateTimeToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, activityParam.getActStartDate())).append(" 至 ")
                .append(DateUtils.parseDateTimeToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, activityParam.getActEndDate()))
                .append(" 业务类型：");
        if (CollUtil.isNotEmpty(recommendUpgradeList)) {
            sb.append(EApprovalBusiness.DIRECT_PUSH_UPGRADE.getLabel()).append(" ");
        }
        sb.append(" 操作类型：").append(transactionMap.get(EnumsPrefixConstants.CONTROL_TYPE + controlType));
        BusinessLogDTO businessLog = BusinessLogDTO.builder()
                .businessType(approvalBusiness.getValue())
                .businessCode(businessCode)
                .content(sb.toString()).pkCreator(param.getPkMember())
                .pkCountry(param.getPkCountry())
                .freeSignFlag(false).build();
        rabbitTemplate.convertAndSend(RabbitMqConstants.BUSINESS_LOG_EXCHANGE, RabbitMqConstants.BUSINESS_LOG_KEY, Collections.singleton(businessLog));
    }

    private void deleteDirectPushUpgradeData(AcDirectPushUpgradeParam acDirectPushUpgradeParam) {
        Long pkBaseId = acDirectPushUpgradeParam.getActivityParam().getPkId();
        Long pkModifyId = acDirectPushUpgradeParam.getPkMember();
        //删除活动基本信息
        iAcBaseConfigService.deleteBaseConfigById(pkBaseId, pkModifyId);
        //删除直推升级配置信息
        List<AcRecommendUpgradeVO> recommendUpgradeList = acDirectPushUpgradeParam.getRecommendUpgradeList();
        if (CollUtil.isNotEmpty(recommendUpgradeList)) {
            iAcRecomUpgradeRuleConfigService.deleteUpgradeRuleConfigByBaseId(pkBaseId, pkModifyId);
        }
    }


    private String saveDirectPushUpgradeData(AcDirectPushUpgradeParam acDirectPushUpgradeParam) {
        //保存活动基本信息
        String message = saveDirectPushUpgradeBaseConfig(acDirectPushUpgradeParam);
        //保存直推升级配置信息
        saveDirectPushUpgradeRuleInfo(acDirectPushUpgradeParam);
        return message;
    }

    private void saveDirectPushUpgradeRuleInfo(AcDirectPushUpgradeParam acDirectPushUpgradeParam) {
        AcRecomUpgradeRuleConfig recomUpgradeRuleConfig;
        //直推升级规则配置
        List<AcRecommendUpgradeVO> recommendUpgradeList = acDirectPushUpgradeParam.getRecommendUpgradeList();
        if (CollUtil.isNotEmpty(recommendUpgradeList)) {
            for (AcRecommendUpgradeVO recommendUpgradeVO : recommendUpgradeList) {
                recommendUpgradeVO.setPkCreator(acDirectPushUpgradeParam.getPkMember());
                recommendUpgradeVO.setPkCountry(acDirectPushUpgradeParam.getPkCountry());
                recomUpgradeRuleConfig = BeanUtil.copyProperties(recommendUpgradeVO, AcRecomUpgradeRuleConfig.class);
                recomUpgradeRuleConfig.setPkBaseId(acDirectPushUpgradeParam.getPkBaseId());
                iAcRecomUpgradeRuleConfigService.save(recomUpgradeRuleConfig);
                List<AcGiftConfig> giftConfigs = saveRecommendUpgradeGift(recommendUpgradeVO, recomUpgradeRuleConfig.getPkId());
                giftConfigService.saveBatch(giftConfigs);
            }
        }
    }


    private String saveDirectPushUpgradeBaseConfig(AcDirectPushUpgradeParam acDirectPushUpgradeParam) {
        ActivityParam activityParam = acDirectPushUpgradeParam.getActivityParam();
        if (null == activityParam) {
            return "没有活动的基本信息！";
        }
        List<AcRecommendUpgradeVO> recommendUpgradeList = acDirectPushUpgradeParam.getRecommendUpgradeList();
        if (CollUtil.isNotEmpty(recommendUpgradeList)) {
            activityParam.setAtcModule(EActModule.DIRECT_PUSH_UPGRADE.getValue());
        }
        activityParam.setActType(EActType.RECOMMEND_UPGRADE.getValue());
        //保存基础配置
        activityParam.setPkCreator(acDirectPushUpgradeParam.getPkMember());
        activityParam.setPkCountry(acDirectPushUpgradeParam.getPkCountry());
        AcBaseConfig acBaseConfig = iActivityService.saveBaseConfig(activityParam);
        acDirectPushUpgradeParam.setPkBaseId(acBaseConfig.getPkId());
        iActivityService.saveGoalMemberConfig(activityParam, acBaseConfig);

        return null;
    }


}
