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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzs.activity.base.param.*;
import com.hzs.activity.base.service.IActivityService;
import com.hzs.activity.base.service.IAcBaseConfigService;
import com.hzs.activity.base.vo.BaseConfigVo;
import com.hzs.activity.base.vo.ConsumeRuleConfigVo;
import com.hzs.activity.consume.service.IAcConsumeShieldConfigService;
import com.hzs.activity.draw.service.IAcDrawGiftRuleConfigService;
import com.hzs.activity.draw.service.IAcDrawRewardNumService;
import com.hzs.activity.draw.service.IAcDrawRuleConfigService;
import com.hzs.activity.draw.vo.AcDrawGiftRuleConfigVo;
import com.hzs.activity.people.service.IAcPeopleServiceService;
import com.hzs.activity.pick.service.IAcPickUpConfigService;
import com.hzs.activity.policy.service.IAcPolicyConfigService;
import com.hzs.activity.recommend.service.IAcRepurCouponsRuleConfigService;
import com.hzs.activity.wares.service.IAcGiftConfigService;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.*;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.domain.activity.base.AcBaseConfig;
import com.hzs.common.domain.activity.base.AcGoalMemberConfig;
import com.hzs.common.domain.activity.base.AcPickUpConfig;
import com.hzs.common.domain.activity.base.ext.AcGiftConfigExt;
import com.hzs.common.domain.activity.consume.AcConsumeRuleConfig;
import com.hzs.common.domain.activity.consume.AcConsumeShieldConfig;
import com.hzs.common.domain.activity.consume.ext.AcConsumeShieldConfigExt;
import com.hzs.common.domain.activity.draw.AcDrawGiftRuleConfig;
import com.hzs.common.domain.activity.draw.AcDrawRuleConfig;
import com.hzs.common.domain.activity.base.AcGiftConfig;
import com.hzs.common.domain.activity.tree.AcTreeRuleConfig;
import com.hzs.common.domain.activity.wares.AcWaresRuleConfig;
import com.hzs.common.domain.sale.ext.SaOrderExt;
import com.hzs.common.domain.system.config.BdAwards;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.activity.consume.service.IAcConsumeRuleConfigService;
import com.hzs.activity.base.service.IAcGoalMemberConfigService;
import com.hzs.activity.wares.service.IAcWaresRuleConfigService;
import com.hzs.member.base.IMemberServiceApi;
import com.hzs.sale.product.IProductServiceApi;
import com.hzs.sale.wares.IWaresServiceApi;
import com.hzs.sale.wares.dto.WaresDTO;
import com.hzs.system.config.IAwardsServiceApi;
import com.hzs.system.config.IGradeServiceApi;
import com.hzs.system.config.dto.GradeDTO;
import com.hzs.system.sys.dto.BusinessLogDTO;
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.*;
import java.util.stream.Collectors;

/**
 * @BelongsProject: hzs_cloud
 * @BelongsPackage: com.hzs.activity.service.impl
 * @Author: yh
 * @CreateTime: 2023-04-20  14:06
 * @Description: 活动配置实现类
 * @Version: 1.0
 */
@Service
public class ActivityServiceImpl implements IActivityService {
//    @Autowired
//    private RabbitTemplate rabbitTemplate;
    @DubboReference
    private IWaresServiceApi waresServiceApi;
    @DubboReference
    private IGradeServiceApi gradeServiceApi;
    @DubboReference
    private IAwardsServiceApi awardsServiceApi;
    @Autowired
    private IAcDrawRuleConfigService drawRuleConfigService;
    @Autowired
    private IAcBaseConfigService baseConfigService;
    @Autowired
    private IAcWaresRuleConfigService waresRuleConfigService;
    @Autowired
    private IAcGiftConfigService waresGiftConfigService;
    @Autowired
    private IAcGoalMemberConfigService goalMemberConfigService;
    @Autowired
    private IAcConsumeRuleConfigService consumeRuleConfigService;
    @Autowired
    private IAcConsumeShieldConfigService consumeShieldConfigService;
    @Autowired
    private IAcDrawGiftRuleConfigService drawGiftConfigService;
    @Autowired
    private IAcDrawRewardNumService acDrawRewardNumService;
    @Autowired
    private IAcPeopleServiceService acPeopleServiceService;
    @Autowired
    private IAcRepurCouponsRuleConfigService acRepurCouponsRuleConfigService;
    @Autowired
    private IAcPickUpConfigService pickUpConfigService;
    @Autowired
    private IAcPolicyConfigService iAcPolicyConfigService;

    @DubboReference
    private IMemberServiceApi memberServiceApi;
    @DubboReference
    private IProductServiceApi productServiceApi;

    /**
     * 新增活动配置
     *
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long saveActivity(ActivityParam activityParam) {
        // 增加基础配置
        AcBaseConfig baseConfig = saveBaseConfig(activityParam);
        // 商品买赠配置
        saveWaresRuleConfig(activityParam, baseConfig);
        //目标用户配置
        saveGoalMemberConfig(activityParam, baseConfig);
        return baseConfig.getPkId();
    }

    /**
     * 新增消费赠送规格
     *
     * @param activityParam
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long saveConsumptionGift(ActivityParam activityParam) {
        // 增加基础配置
        AcBaseConfig baseConfig = saveBaseConfig(activityParam);
        // 目标群体配置
        saveGoalMemberConfig(activityParam, baseConfig);
        // 消费赠送规格配置
        saveConsumeRuleConfigService(activityParam, baseConfig);
        // 消费赠送屏蔽配置表
        saveConsumeShieldConfig(activityParam, baseConfig);
        return baseConfig.getPkId();
    }

    /**
     * 新增抽奖活动配置
     *
     * @param activityParam
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveRaffleActivity(ActivityParam activityParam) {
        // 增加基础配置
        AcBaseConfig baseConfig = saveBaseConfig(activityParam);
        // 目标群体配置
        saveGoalMemberConfig(activityParam, baseConfig);

        // 抽奖活动赠品配置
        saveDrawGiftConfig(activityParam, baseConfig);

        // 抽奖赠送次数规则配置表
        saveDrawRuleConfig(activityParam, baseConfig);

        // 抽奖 提货配置
        saveAcPickUpConfig(activityParam, baseConfig);
    }

    /**
     * 抽奖提货配置
     *
     * @param activityParam
     * @param baseConfig
     */
    private void saveAcPickUpConfig(ActivityParam activityParam, AcBaseConfig baseConfig) {
        AcPickUpConfig pickUpConfig = activityParam.getAcPickUpConfig();
        pickUpConfig.setPkBaseId(baseConfig.getPkId());
        pickUpConfig.setPickUpType(EPickUpType.QUANTITY.getValue());
        pickUpConfig.setPkCountry(baseConfig.getPkCountry());
        pickUpConfig.setPkCreator(baseConfig.getPkCreator());

        pickUpConfigService.save(pickUpConfig);
    }

    /***
     * 抽奖赠送次数规则配置表
     * @param activityParam
     * @param baseConfig
     */
    public void saveDrawRuleConfig(ActivityParam activityParam, AcBaseConfig baseConfig) {
        for (AcDrawRuleConfigParam acDrawRuleConfigParam : activityParam.getDrawRuleConfigParam()) {
            List<AcDrawRuleConfig> drawRuleConfigList = acDrawRuleConfigParam.getDrawRuleConfigList().stream().peek(acDrawRuleConfig -> {
                acDrawRuleConfig.setOrderType(acDrawRuleConfigParam.getOrderType());
                acDrawRuleConfig.setPkBaseId(baseConfig.getPkId());
                acDrawRuleConfig.setPkCreator(baseConfig.getPkCreator());
                acDrawRuleConfig.setCreationTime(DateUtils.currentDateTime());
                acDrawRuleConfig.setPkCountry(baseConfig.getPkCountry());
            }).collect(Collectors.toList());
            drawRuleConfigService.saveBatch(drawRuleConfigList);
        }


    }

    /**
     * 抽奖活动赠品配置
     *
     * @param activityParam
     */
    public void saveDrawGiftConfig(ActivityParam activityParam, AcBaseConfig baseConfig) {

        for (AcDrawGiftRuleConfigVo drawGiftConfig : activityParam.getDrawGiftConfigList()) {
            drawGiftConfig.setPkBaseId(baseConfig.getPkId());
            drawGiftConfig.setPkCreator(baseConfig.getPkCreator());
            drawGiftConfig.setPkCountry(baseConfig.getPkCountry());
            AcDrawGiftRuleConfig dg = BeanUtil.copyProperties(drawGiftConfig, AcDrawGiftRuleConfig.class);
            drawGiftConfigService.save(dg);
            if (drawGiftConfig.getGiftConfigList() != null) {
                for (AcGiftConfig giftConfig : drawGiftConfig.getGiftConfigList()) {
                    giftConfig.setPkRuleId(dg.getPkId());
                    giftConfig.setActType(baseConfig.getActType());
                    giftConfig.setCreationTime(DateUtils.currentDateTime());
                    giftConfig.setPkCreator(baseConfig.getPkCreator());
                    giftConfig.setPkCountry(baseConfig.getPkCountry());
                    waresGiftConfigService.save(giftConfig);
                }

            }
        }
    }

    /**
     * 消费赠送屏蔽配置表
     *
     * @param activityParam
     * @param baseConfig
     */
    public void saveConsumeShieldConfig(ActivityParam activityParam, AcBaseConfig baseConfig) {
        List<AcConsumeShieldConfig> consumeShieldConfigList = activityParam.getConsumeShieldConfigList().stream().peek(consumeShieldConfig -> {
            consumeShieldConfig.setPkBaseId(baseConfig.getPkId());
            consumeShieldConfig.setPkCreator(activityParam.getPkCreator());
            consumeShieldConfig.setPkCountry(activityParam.getPkCountry());
        }).collect(Collectors.toList());
        consumeShieldConfigService.saveBatch(consumeShieldConfigList);
    }

    /**
     * 消费赠送规格配置
     *
     * @param activityParam
     * @param baseConfig
     */
    public void saveConsumeRuleConfigService(ActivityParam activityParam, AcBaseConfig baseConfig) {
        List<ConsumeRuleConfigParam> consumeRuleConfigParam = activityParam.getConsumeRuleConfigParamList();

        for (ConsumeRuleConfigParam ruleConfigParam : consumeRuleConfigParam) {
            List<ConsumeRuleConfigVo> consumeRuleConfigList = ruleConfigParam.getConsumeRuleConfigList();
            for (ConsumeRuleConfigVo consumeRuleConfig : consumeRuleConfigList) {
                consumeRuleConfig.setPkBaseId(baseConfig.getPkId());
                consumeRuleConfig.setPkCreator(activityParam.getPkCreator());
                consumeRuleConfig.setPkCountry(activityParam.getPkCountry());
                consumeRuleConfig.setOrderType(ruleConfigParam.getOrderType());
                consumeRuleConfigService.save(consumeRuleConfig);
                /**
                 * 活动赠品配置
                 */
                List<AcGiftConfigExt> waresGiftConfigList = consumeRuleConfig.getWaresGiftConfigList();
                List<AcGiftConfig> giftConfigList = new ArrayList<>();
                for (AcGiftConfigExt acWaresGiftConfig : waresGiftConfigList) {
                    acWaresGiftConfig.setPkRuleId(consumeRuleConfig.getPkId());
                    acWaresGiftConfig.setActType(baseConfig.getActType());
                    acWaresGiftConfig.setPkCreator(activityParam.getPkCreator());
                    acWaresGiftConfig.setPkCountry(activityParam.getPkCountry());
                    AcGiftConfig acGiftConfig = BeanUtil.copyProperties(acWaresGiftConfig, AcGiftConfig.class);
                    giftConfigList.add(acGiftConfig);
                }
                waresGiftConfigService.saveBatch(giftConfigList);
            }
        }
    }

    /**
     * 目标群体配置
     *
     * @param activityParam
     * @param baseConfig
     */
    @Override
    public Boolean saveGoalMemberConfig(ActivityParam activityParam, AcBaseConfig baseConfig) {
        List<AcGoalMemberConfig> goalMemberConfig = activityParam.getGoalMemberConfigList();
        for (AcGoalMemberConfig acGoalMemberConfig : goalMemberConfig) {
            acGoalMemberConfig.setPkBaseId(baseConfig.getPkId());
            acGoalMemberConfig.setPkCountry(activityParam.getPkCountry());
            acGoalMemberConfig.setPkCreator(activityParam.getPkCreator());
        }
        return goalMemberConfigService.saveBatch(goalMemberConfig);
    }

    /**
     * 新增商品产品买赠规则配置
     *
     * @param activityParam
     */
    @Override
    public void saveWaresRuleConfig(ActivityParam activityParam, AcBaseConfig baseConfig) {
        for (WaresConfigParam waresConfigParam : activityParam.getWaresConfigParam()) {
            /**
             * 活动商品配置
             */
            List<WaresRuleConfigParam> waresRuleConfigParam = waresConfigParam.getWaresRuleConfigParam();
            for (WaresRuleConfigParam ruleConfigParam : waresRuleConfigParam) {
                // AcWaresRuleConfig waresRuleConfig = new AcWaresRuleConfig();
                AcWaresRuleConfig waresRuleConfig = BeanUtil.copyProperties(ruleConfigParam, AcWaresRuleConfig.class);
                waresRuleConfig.setPkBaseId(baseConfig.getPkId());
                waresRuleConfig.setOrderType(waresConfigParam.getOrderType());
                waresRuleConfig.setPkCreator(activityParam.getPkCreator());
                waresRuleConfig.setPkCountry(activityParam.getPkCountry());
                waresRuleConfigService.save(waresRuleConfig);
                /**
                 * 活动赠品配置
                 */
                List<AcGiftConfigExt> waresGiftConfigList = ruleConfigParam.getWaresGiftConfigList();
                List<AcGiftConfig> giftConfigList = new ArrayList<>();
                for (AcGiftConfigExt acWaresGiftConfig : waresGiftConfigList) {
                    acWaresGiftConfig.setPkRuleId(waresRuleConfig.getPkId());
                    acWaresGiftConfig.setActType(baseConfig.getActType());
                    acWaresGiftConfig.setPkCreator(activityParam.getPkCreator());
                    acWaresGiftConfig.setPkCountry(activityParam.getPkCountry());
                    AcGiftConfig giftConfig = BeanUtil.copyProperties(acWaresGiftConfig, AcGiftConfig.class);
                    giftConfigList.add(giftConfig);
                }
                waresGiftConfigService.saveBatch(giftConfigList);
            }

        }

    }

    /**
     * 新增活动基础配置
     *
     * @param activityParam
     */
    @Override
    public AcBaseConfig saveBaseConfig(ActivityParam activityParam) {
        AcBaseConfig baseConfig = BeanUtil.copyProperties(activityParam, AcBaseConfig.class);
        baseConfigService.save(baseConfig);
        return baseConfig;
    }

    /**
     * 修改基础配置
     *
     * @param activityParam
     */
    @Override
    public AcBaseConfig updateBaseConfig(ActivityParam activityParam) {
        AcBaseConfig baseConfig = BeanUtil.copyProperties(activityParam, AcBaseConfig.class);
        baseConfigService.updateById(baseConfig);
        return baseConfig;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean handleActivity(SaOrderExt saOrderExt) {
        if (EOrderType.REGISTER_ORDER.getValue() == saOrderExt.getOrderType()) {
            // 直推赠送政策币
            iAcPolicyConfigService.handleOrderPolicyActivity(saOrderExt);

            // 抽奖
            acDrawRewardNumService.giveMemberDrawNum(saOrderExt);
//            // 新人礼包
//            acPeopleServiceService.mateRule(saOrderExt);
//            // 直推升级
//            acRepurCouponsRuleConfigService.invokeRecommendUpgrade(AcRecommendUpgrade.builder().saOrderExt(saOrderExt).orderOperateStatus(EOrderOperateStatus.NORMAL).build());
        } else if (EOrderType.UPGRADE_ORDER.getValue() == saOrderExt.getOrderType()) {
            // 抽奖
            acDrawRewardNumService.giveMemberDrawNum(saOrderExt);
//            // 直推升级
//            acRepurCouponsRuleConfigService.invokeRecommendUpgrade(AcRecommendUpgrade.builder().saOrderExt(saOrderExt).orderOperateStatus(EOrderOperateStatus.NORMAL).build());
        }
        return true;
    }

    @Override
    public AcBaseConfig updateRaffleActivity(ActivityParam activityParam) {
        // 增加基础配置
        AcBaseConfig baseConfig = BeanUtil.copyProperties(activityParam, AcBaseConfig.class);
        baseConfigService.updateById(baseConfig);
        // 目标群体配置
        saveGoalMemberConfig(activityParam, baseConfig);
        //   updateGoalMemberConfig(activityParam, baseConfig);

        // 抽奖活动赠品配置
        saveDrawGiftConfig(activityParam, baseConfig);

        // 抽奖赠送次数规则配置表
        saveDrawRuleConfig(activityParam, baseConfig);

        // 抽奖 提货配置
        //     saveAcPickUpConfig(activityParam, baseConfig);
        updateAcPickUpConfig(activityParam, baseConfig);
        return baseConfig;
    }

    /**
     * 查询活动数据列表
     *
     * @param queryActivityParam
     * @return
     */
    @Override
    public List<BaseConfigVo> activityList(QueryActivityParam queryActivityParam) {
        QueryWrapper<AcBaseConfig> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(queryActivityParam.getActName())) {
            queryWrapper.eq("ACT_NAME", queryActivityParam.getActName());
        }
        if (queryActivityParam.getActivityStatus() != null) {
            // 状态筛选
            if (queryActivityParam.getActivityStatus() == EActivityStatus.NOT_STARTED.getValue()) {
                // 未开始  当前时间小于开始时间
                queryWrapper.lt("ACT_START_DATE", DateUtils.currentDate());
                queryWrapper.gt("ACT_START_DATE", DateUtils.currentDate());
            } else if (queryActivityParam.getActivityStatus() == EActivityStatus.IN_PROGRESS.getValue()) {
                // 进行中 大于开始 小于结束
                queryWrapper.le("ACT_START_DATE", DateUtils.currentDate());
                queryWrapper.ge("ACT_END_DATE", DateUtils.currentDate());
            } else if (queryActivityParam.getActivityStatus() == EActivityStatus.EXPIRED.getValue()) {
                // 已失效 当前时间大于结束时间
                queryWrapper.lt("ACT_START_DATE", DateUtils.currentDate());
                queryWrapper.lt("ACT_END_DATE", DateUtils.currentDate());
            }
        }
        // 活动开始时间
        if (queryActivityParam.getStartActStartDate() != null && queryActivityParam.getEndActStartDate() != null) {
            queryWrapper.between("ACT_START_DATE", queryActivityParam.getStartActStartDate(), queryActivityParam.getEndActStartDate());
        }
        // 活动结束时间
        if (queryActivityParam.getStartActEndDate() != null && queryActivityParam.getEndActEndDate() != null) {
            queryWrapper.between("ACT_END_DATE", queryActivityParam.getStartActEndDate(), queryActivityParam.getEndActEndDate());
        }
        // 创建时间
        if (queryActivityParam.getStartCreationTime() != null && queryActivityParam.getEndCreationTime() != null) {
            queryWrapper.between("CREATION_TIME", queryActivityParam.getStartCreationTime(), queryActivityParam.getEndCreationTime());
        }
        if (queryActivityParam.getActType() != null) {
            queryWrapper.eq("ACT_TYPE", queryActivityParam.getActType());
        }
        queryWrapper.eq("PK_COUNTRY", SecurityUtils.getPkCountry());
        // 创建时间倒叙
        queryWrapper.orderByDesc("CREATION_TIME");
        List<BaseConfigVo> resultList = new ArrayList<>();
        List<AcBaseConfig> list = baseConfigService.list(queryWrapper);
        for (AcBaseConfig acBaseConfig : list) {
            BaseConfigVo baseConfigVo = BeanUtil.copyProperties(acBaseConfig, BaseConfigVo.class);
            // 判断活动状态
            // 开始时间
            long startTime = acBaseConfig.getActStartDate().getTime();
            // 结束时间
            long endTime = acBaseConfig.getActEndDate().getTime();
            // 当前时间
            long currentTime = DateUtils.currentDate().getTime();
            if ((currentTime > startTime && currentTime < endTime) || currentTime == startTime || currentTime == endTime) {
                // 当前时间大于开始 小于结束
                baseConfigVo.setStatus(EActivityStatus.IN_PROGRESS.getLabel());
            } else if (currentTime < startTime) {
                // 当前时间小于开始时间
                baseConfigVo.setStatus(EActivityStatus.NOT_STARTED.getLabel());
            } else if (currentTime > endTime) {
                // 当前时间大于结束时间
                baseConfigVo.setStatus(EActivityStatus.EXPIRED.getLabel());
            }
            resultList.add(baseConfigVo);
        }
        return resultList;
    }

    private void updateAcPickUpConfig(ActivityParam activityParam, AcBaseConfig baseConfig) {
        AcPickUpConfig pickUpConfig = activityParam.getAcPickUpConfig();
        if (pickUpConfig != null) {
            AcPickUpConfig pc = pickUpConfigService.getById(pickUpConfig.getPkId());
            if (pc != null) {
                BeanUtil.copyProperties(pickUpConfig, pc);
                pickUpConfigService.updateById(pc);
            } else {
                pickUpConfig.setPkBaseId(baseConfig.getPkId());
                pickUpConfig.setPickUpType(EPickUpType.QUANTITY.getValue());
                pickUpConfig.setPkCountry(baseConfig.getPkCountry());
                pickUpConfig.setPkCreator(baseConfig.getPkCreator());

                pickUpConfigService.save(pickUpConfig);
            }
        }

    }
    /**
     * 活动日志基本信息拼装
     *
     * @param activityParam 活动基本信息
     * @param str           拼接字符
     * @param pkBaseId      活动主键
     * @param businessType  类型
     * @param freeSignFlag  是否需要签呈
     * @return
     */
    @Override
    public StringBuffer createBaseConfigLog(ActivityParam activityParam, StringBuffer str, Long pkBaseId, Integer businessType, Boolean freeSignFlag) {

        Integer pkCountry = activityParam.getPkCountry();
        Long userId = activityParam.getPkCreator();
        if (StringUtils.isNotBlank(activityParam.getActName())) {
            // 活动名称
            str.append("活动名称:").append(activityParam.getActName()).append("；\t\t");
        }
        if (StringUtils.isNotBlank(activityParam.getActExplain())) {
            // 活动说明
            str.append("活动说明:").append(activityParam.getActExplain()).append("；\t\t");
        }
        // 活动时间
        if (activityParam.getActStartDate() != null && activityParam.getActEndDate() != null) {
            str.append("活动开始时间:").append(DateUtil.format(activityParam.getActStartDate(), DateUtils.YYYY_MM_DD_HH_MM_SS));
            str.append("活动结束时间:").append(DateUtil.format(activityParam.getActEndDate(), DateUtils.YYYY_MM_DD_HH_MM_SS)).append("；\t\t");
        }
        // 展示时间
        if (activityParam.getDisStartDate() != null && activityParam.getDisEndDate() != null) {
            str.append("展示开始时间:").append(DateUtil.format(activityParam.getDisStartDate(), DateUtils.YYYY_MM_DD_HH_MM_SS));
            str.append("展示结束时间:").append(DateUtil.format(activityParam.getDisEndDate(), DateUtils.YYYY_MM_DD_HH_MM_SS)).append("；\t\t");
        }
        // 活动日志 目标用户群体拼接
        if (activityParam.getGoalMemberConfigList() != null && activityParam.getGoalMemberConfigList().size() > 0) {
            Map<Integer, List<AcGoalMemberConfig>> memberConfigList = activityParam.getGoalMemberConfigList().stream().collect(Collectors.groupingBy(AcGoalMemberConfig::getMemberType));

            for (Map.Entry<Integer, List<AcGoalMemberConfig>> item : memberConfigList.entrySet()) {
                if (item.getKey().equals(1)) {
                    // 等级
                    R<List<GradeDTO>> gradeDTO = gradeServiceApi.selectGradeAll(activityParam.getPkCountry());
                    List<GradeDTO> gradeList = gradeDTO.getData();
                    str.append("注册等级:");
                    for (AcGoalMemberConfig acGoalMemberConfig : item.getValue()) {
                        for (GradeDTO dto : gradeList) {
                            if (dto.getPkId().equals(acGoalMemberConfig.getPkLevel())) {
                                str.append(dto.getGradeName()).append(", ");
                            }
                        }

                    }
                    str.append("；\t\t");
                } else if (item.getKey().equals(2)) {
                    // 奖衔
                    R<List<BdAwards>> awardsDTO = awardsServiceApi.queryAwards(activityParam.getPkCountry());
                    List<BdAwards> awardsList = awardsDTO.getData();
                    str.append("真实奖衔:");
                    for (AcGoalMemberConfig acGoalMemberConfig : item.getValue()) {
                        for (BdAwards awards : awardsList) {
                            if (awards.getPkId().equals(acGoalMemberConfig.getPkLevel())) {
                                str.append(awards.getAwardsName()).append(", ");
                            }
                        }

                    }
                    str.append("；\t\t");
                } else if (item.getKey().equals(3)) {
                    // 权限中心
                    str.append("权限中心:");
                    for (AcGoalMemberConfig acGoalMemberConfig : item.getValue()) {
                        ERegistrationAuthority registrationAuthority = ERegistrationAuthority.getEnumByValue(acGoalMemberConfig.getPkLevel());
                        if (registrationAuthority != null) {
                            str.append(registrationAuthority.getLabel()).append(" ,");
                        }
                    }
                    str.append("；\t\t");
                }
            }
        }
        // 规则设置
        if (activityParam.getWaresConfigParam() != null && activityParam.getWaresConfigParam().size() > 0) {
            R<List<WaresDTO>> waresAllDTO = waresServiceApi.findAll(pkCountry);
            List<WaresDTO> waresList = waresAllDTO.getData();
            for (WaresConfigParam waresConfigParam : activityParam.getWaresConfigParam()) {
                if (waresConfigParam.getWaresRuleConfigParam() != null && waresConfigParam.getWaresRuleConfigParam().size() > 0) {
                    EOrderType enumByValue = EOrderType.getEnumByValue(waresConfigParam.getOrderType());
                    if (enumByValue != null) {
                        str.append(enumByValue.getLabel()).append(" :");
                        for (WaresRuleConfigParam waresRuleConfigParam : waresConfigParam.getWaresRuleConfigParam()) {
                            for (WaresDTO wares : waresList) {
                                if (waresRuleConfigParam.getPkWares().equals(wares.getPkId())) {
                                    str.append(" 商品名称:").append(wares.getWaresName());
                                    str.append("数量:").append(waresRuleConfigParam.getQuantity());
                                    if (waresRuleConfigParam.getWaresGiftConfigList() != null && waresRuleConfigParam.getWaresGiftConfigList().size() > 0) {
                                        for (AcGiftConfigExt acGiftConfigExt : waresRuleConfigParam.getWaresGiftConfigList()) {
                                            str.append("赠送产品:").append(acGiftConfigExt.getProductName());
                                            str.append("产品规格:").append(acGiftConfigExt.getSpecsName());
                                            str.append("赠送数量:").append(acGiftConfigExt.getQuantity());
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        // 屏蔽设置
        if (activityParam.getConsumeShieldConfigList() != null && activityParam.getConsumeShieldConfigList().size() > 0) {
            str.append("屏蔽配置： ");
            for (AcConsumeShieldConfigExt consumeShieldConfigExt : activityParam.getConsumeShieldConfigList()) {
                str.append("产品名称：").append(consumeShieldConfigExt.getProductName());
                str.append("产品规格：").append(consumeShieldConfigExt.getSpecsName());
                if (consumeShieldConfigExt.getCondition() != null){
                    str.append("条件：").append(ECondition.getCondition(consumeShieldConfigExt.getCondition()).getLabel());
                }
                str.append("产品数量：").append(consumeShieldConfigExt.getQuantity());
            }
        }
        // 是否强弹 (种植公益配置) 是否强制弹框 0 是  1 否
        if (activityParam.getIsStrongAlert() != null) {
            if (activityParam.getIsStrongAlert().equals(EYesNo.YES.getIntValue())) {
                str.append("是否强制弹框：是");
            } else if (activityParam.getIsStrongAlert().equals(EYesNo.NO.getIntValue())) {
                str.append("是否强制弹框：否");
            }
        }
        //种植公益活动规则配置
        if (activityParam.getTreeRuleConfigList() != null && activityParam.getTreeRuleConfigList().size() > 0) {
            // 奖衔
            R<List<BdAwards>> awardsDTO = awardsServiceApi.queryAwards(activityParam.getPkCountry());
            List<BdAwards> awardsList = awardsDTO.getData();
            Map<Integer, String> awardsMap = awardsList.stream().collect(Collectors.toMap(BdAwards::getPkId, BdAwards::getAwardsName));
            for (AcTreeRuleConfig acTreeRuleConfig : activityParam.getTreeRuleConfigList()) {
                str.append("活动名称：").append(acTreeRuleConfig.getName());
                str.append("真实奖衔：").append(awardsMap.get(acTreeRuleConfig.getPkAwards()));
                str.append("价格：").append(acTreeRuleConfig.getPrice());
                str.append("数量：").append(acTreeRuleConfig.getQuantity());
            }
        }
        // 抽奖赠送次数规则配置表
        if (activityParam.getDrawRuleConfigParam() != null && activityParam.getDrawRuleConfigParam().size() > 0) {
            for (AcDrawRuleConfigParam drawRuleConfigParam : activityParam.getDrawRuleConfigParam()) {
                if (drawRuleConfigParam.getDrawRuleConfigList() != null && drawRuleConfigParam.getDrawRuleConfigList().size() > 0) {
                    EOrderType enumByValue = EOrderType.getEnumByValue(drawRuleConfigParam.getOrderType());
                    str.append("订单类型：").append(enumByValue.getLabel());
                    for (AcDrawRuleConfig acDrawRuleConfig : drawRuleConfigParam.getDrawRuleConfigList()) {
                        str.append("订单金额：").append(acDrawRuleConfig.getOrderAmount());
                        str.append("订单业绩：").append(acDrawRuleConfig.getOrderAchieve());
                        str.append("赠送次数：").append(acDrawRuleConfig.getGiveNumber());
                    }
                }
            }
        }
        // 支付金额
        if (activityParam.getPayMoney() != null) {
            str.append("支付金额：").append(activityParam.getPayMoney());
        }
        // 奖品设置
        if (activityParam.getDrawGiftConfigList() != null && activityParam.getDrawGiftConfigList().size() > 0) {
            str.append("奖品设置：");
            for (AcDrawGiftRuleConfigVo drawGiftRuleConfigVo : activityParam.getDrawGiftConfigList()) {
                str.append("奖品名称：").append(drawGiftRuleConfigVo.getGiftName());
                str.append("奖品总数：").append(drawGiftRuleConfigVo.getGiftNumber());
                str.append("最小中奖数量：").append(drawGiftRuleConfigVo.getMinNumber());
                str.append("发放方式：").append(EActDrawGiftType.getActDrawGiftType(drawGiftRuleConfigVo.getGrantWay()));
                // 产品
                if (drawGiftRuleConfigVo.getGiftConfigList() != null && drawGiftRuleConfigVo.getGiftConfigList().size() > 0) {
                    for (AcGiftConfigExt giftConfigExt : drawGiftRuleConfigVo.getGiftConfigList()) {
                        str.append("产品名称：").append(giftConfigExt.getProductName());
                        str.append("产品规格：").append(giftConfigExt.getSpecsName());
                        str.append("数量：").append(giftConfigExt.getQuantity());
                    }
                }
            }
        }

        if (activityParam.getAcPickUpConfig() != null) {
            AcPickUpConfig acPickUpConfig = activityParam.getAcPickUpConfig();
            // 提货天数
            if (acPickUpConfig.getDeliveryDays() != null) {
                str.append("提货天数：").append(acPickUpConfig.getDeliveryDays());
            }
            // 包邮类型
            if (acPickUpConfig.getIsFreeMail() != null) {
                if (acPickUpConfig.getIsFreeMail().equals(EYesNo.YES.getIntValue())) {
                    str.append("包邮类型：包邮");
                } else if (acPickUpConfig.getIsFreeMail().equals(EYesNo.NO.getIntValue())) {
                    str.append("包邮类型：不包邮");
                }
            }
        }
        if (freeSignFlag == null) {
            freeSignFlag = false;
        }

        // 拼装发送mq
        BusinessLogDTO log = BusinessLogDTO.builder()
                .businessType(businessType)
                .businessId(pkBaseId)
                .businessCode(activityParam.getBusinessCode())
                .pkMember(null)
                .content(str.toString())
                .pkCreator(userId)
                .pkCountry(pkCountry)
                .freeSignFlag(freeSignFlag)
                .build();
//        rabbitTemplate.convertAndSend(RabbitMqConstants.BUSINESS_LOG_EXCHANGE, RabbitMqConstants.BUSINESS_LOG_KEY, log);
        return str;
    }

    /**
     * 删除活动日志
     *
     * @param baseConfig
     * @param str
     * @param pkBaseId
     * @param businessType
     * @param freeSignFlag
     * @return
     */
    @Override
    public StringBuffer removeBaseConfigLog(AcBaseConfig baseConfig, StringBuffer str, Long pkBaseId, Integer businessType, Boolean freeSignFlag) {
        if (baseConfig != null){
            Integer pkCountry = baseConfig.getPkCountry();
            Long userId = baseConfig.getPkCreator();
            str.append("删除活动：").append(baseConfig.getActName());
            // 拼装发送mq
            BusinessLogDTO log = BusinessLogDTO.builder()
                    .businessType(businessType)
                    .businessId(pkBaseId)
                    .businessCode(baseConfig.getBusinessCode())
                    .pkMember(null)
                    .content(str.toString())
                    .pkCreator(userId)
                    .pkCountry(pkCountry)
                    .freeSignFlag(freeSignFlag)
                    .build();
//            rabbitTemplate.convertAndSend(RabbitMqConstants.BUSINESS_LOG_EXCHANGE, RabbitMqConstants.BUSINESS_LOG_KEY, log);
        }

        return str;
    }

    /**
     * 修改消费赠送规格
     *
     * @param activityParam
     */
    @Override
    public void updateConsumptionGift(ActivityParam activityParam) {
        Long pkId = activityParam.getPkId();
        // 查询基础信息
        AcBaseConfig baseConfig = BeanUtil.copyProperties(activityParam, AcBaseConfig.class);
        baseConfig.setPkId(activityParam.getPkId());
        baseConfigService.updateById(baseConfig);

        QueryWrapper<AcGoalMemberConfig> qt = new QueryWrapper<>();
        qt.eq("PK_BASE_ID", pkId);
        goalMemberConfigService.remove(qt);
        // 目标群体配置
        saveGoalMemberConfig(activityParam, baseConfig);

        // 消费赠送规格配置
        QueryWrapper<AcConsumeRuleConfig> qx = new QueryWrapper<>();
        qx.eq("PK_BASE_ID", pkId);
        consumeRuleConfigService.remove(qx);
        saveConsumeRuleConfigService(activityParam, baseConfig);

        // 消费赠送屏蔽配置表
        QueryWrapper<AcConsumeShieldConfig> cr = new QueryWrapper<>();
        cr.eq("PK_BASE_ID", pkId);
        consumeShieldConfigService.remove(cr);
        saveConsumeShieldConfig(activityParam, baseConfig);
    }
}
