package com.kiisoo.ic.activity.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kiisoo.ic.activity.entity.*;
import com.kiisoo.ic.activity.event.ActivityInstanceCreatedEvent;
import com.kiisoo.ic.activity.service.*;
import com.kiisoo.ic.activity.bean.ActivityInstanceVO;
import com.kiisoo.ic.activity.mapper.PmnActivityInstanceDOMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kiisoo.ic.domain.service.PrivilageDomainService;
import com.kiisoo.ic.generalize.entity.PrivilageOrganizational;
import com.kiisoo.ic.generalize.entity.PrivilageOrganizationalMember;
import com.kiisoo.ic.generalize.mapper.OrganizationalMemberMapper;
import com.kiisoo.ic.generalize.mapper.RetailCompanyMapper;
import com.kiisoo.ic.generalize.service.RetailCompanyService;
import com.kiisoo.ic.store.entity.PoiStore;
import com.kiisoo.ic.store.mapper.PoiStoreDOMapper;
import com.kiisoo.ic.system.entity.PrivilageAccountDO;
import com.kiisoo.ic.system.entity.PrivilageRoleDO;
import com.kiisoo.ic.system.enums.RoleEnum;
import com.kiisoo.ic.system.mapper.PrivilageAccountDOMapper;
import com.kiisoo.ic.system.mapper.PrivilageRoleDOMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * <p>
 * 活动实例表 服务实现类
 * </p>
 *
 * @author zheng
 * @since 2020-05-19
 */
@Service
@Slf4j
public class PmnActivityInstanceServiceImpl extends ServiceImpl<PmnActivityInstanceDOMapper, PmnActivityInstance> implements IPmnActivityInstanceService {
    @Autowired
    private IPmnActivityInstanceService pmnActivityInstanceService;

    @Autowired
    private IPmnActivityScheduleService pmnActivityScheduleService;

    @Autowired
    private IPmnActivityPromotionCaseService pmnActivityPromotionCaseService;

    @Autowired
    private RetailCompanyService retailCompanyService;
    @Autowired
    private IPmnActivityInstanceConfigService pmnActivityInstanceConfigService;

    @Autowired
    private IPmnActivityStoreRelationService pmnActivityStoreRelationService;

    @Autowired
    private PmnActivityInstanceDOMapper pmnActivityInstanceDOMapper;

    @Autowired
    private RetailCompanyMapper retailCompanyMapper;

    @Autowired
    private ApplicationContext applicationContext;

    /**
     * 用户权限mapper
     */
    @Autowired
    private PrivilageDomainService privilageDomainService;

    @Autowired
    private PoiStoreDOMapper poiStoreDOMapper;
    @Autowired
    private PrivilageRoleDOMapper privilageRoleDOMapper;
    @Autowired
    private PrivilageAccountDOMapper privilageAccountDOMapper;
    @Autowired
    private OrganizationalMemberMapper organizationalMemberMapper;
    @Autowired
    private IPmnActivityPromotionService activityPromotionService;

    @Autowired
    private IPmnActivityPromotionParamService pmnActivityPromotionParamService;
    @Autowired
    private IPmnPromotionSrcService pmnPromotionSrcService;

    @Transactional
    public boolean saveVO(ActivityInstanceVO activityInstanceVO, boolean isTerminate) {
        // TODO 创建活动实例的时候，自动关联一个 activity_promotion_case 一对一关系
        // TODO 活动类型就是从活动池的参数中进行多选的结果，从 activity_promotion_params 存储到 activity_instance_config
        // TODO 店铺列表存储到 activity_store_relation
        // TODO 一对多的存储，都是有ID时修改，无ID时新增
        PmnActivityInstance pmnActivityInstance = new PmnActivityInstance();
        if (activityInstanceVO.getId() != null) {
            pmnActivityInstance = getById(activityInstanceVO.getId());
        }
        pmnActivityInstance.setId(activityInstanceVO.getId());
        pmnActivityInstance.setBeginTime(activityInstanceVO.getBeginTime());
        if (activityInstanceVO.getId() == null) {
            pmnActivityInstance.setCreateTime(new Date());
        }
        pmnActivityInstance.setEndTime(activityInstanceVO.getEndTime());
        pmnActivityInstance.setContent("");
        pmnActivityInstance.setCategoryCode(activityInstanceVO.getCategoryCode());
        pmnActivityInstance.setName(activityInstanceVO.getName());
        pmnActivityInstance.setIsAddition(2L);
        if (pmnActivityInstance.getStatus() == null) {
            if (pmnActivityInstance.getBeginTime().compareTo(new Date()) == -1) {
                pmnActivityInstance.setStatus(2L);
            } else {
                pmnActivityInstance.setStatus(1L);
            }
        }
        if (isTerminate) {
            pmnActivityInstance.setStatus(3L);
        }
        pmnActivityInstance.setScheduleId(activityInstanceVO.getScheduleId());
        pmnActivityInstance.setUpdateTime(new Date());
        pmnActivityInstance.setCode("");
        boolean saveInstance = pmnActivityInstanceService.saveOrUpdate(pmnActivityInstance);
//        String code = pmn_[instance_id]_[type]_[promoter_id];
        PmnActivityPromotionCase pmnActivityPromotionCase = new PmnActivityPromotionCase();
        pmnActivityPromotionCase.setCreateTime(new Date());
        pmnActivityPromotionCase.setInstanceId(pmnActivityInstance.getId());
        pmnActivityPromotionCase.setStatus(pmnActivityInstance.getStatus());
        pmnActivityPromotionCase.setUpdateTime(new Date());
        pmnActivityPromotionCase.setTag("");
        PmnActivityPromotionCase oldPmnActivityPromotionCase = pmnActivityPromotionCaseService.getOne(Wrappers.<PmnActivityPromotionCase>lambdaQuery().eq(PmnActivityPromotionCase::getInstanceId, pmnActivityInstance.getId()));
        if (oldPmnActivityPromotionCase != null) {
            pmnActivityPromotionCase.setCreateTime(null);
            pmnActivityPromotionCase.setId(oldPmnActivityPromotionCase.getId());
        }
        boolean saveCase = pmnActivityPromotionCaseService.saveOrUpdate(pmnActivityPromotionCase);
        for (PmnActivityPromotionParam param : activityInstanceVO.getParams()) {
            PmnActivityInstanceConfig config = new PmnActivityInstanceConfig();
            config.setCaseId(pmnActivityPromotionCase.getId());
            config.setCreateTime(new Date());
            config.setInstanceId(pmnActivityInstance.getId());
            config.setKey(param.getName());
            config.setVal(param.getKey());
            config.setStatus(pmnActivityInstance.getStatus());
            config.setUpdateTime(new Date());
            config.setParamId(param.getId());
            config.setName(param.getName());
            config.setType(Long.valueOf(param.getType()));
            pmnActivityInstanceConfigService.saveOrUpdate(config, Wrappers.<PmnActivityInstanceConfig>lambdaQuery().eq(PmnActivityInstanceConfig::getInstanceId, pmnActivityInstance.getId()).eq(PmnActivityInstanceConfig::getParamId, param.getId()));
        }
        applicationContext.publishEvent(new ActivityInstanceCreatedEvent(this, pmnActivityInstance, activityInstanceVO));
        return saveInstance && saveCase;
    }

    @Override
    public ActivityInstanceVO getVO(Long id) {
        ActivityInstanceVO vo = pmnActivityInstanceDOMapper.getVO(id);
        vo.setScheduleVO(pmnActivityScheduleService.getVO(vo.getScheduleId()));
        return vo;
    }

    @Override
    public List<ActivityInstanceVO> listByStoreId(Long storeId, Integer status, Date beginTime, Date endTime) {
        return null;
    }

    @Override
    public Map<String, Object> pageVO(Long userId, Date startDate, Date endDate, Long organizationId, Long storeId, Integer status, String categoryCode, Integer pageNum, Integer pageSize, Date beginTime, Date endTime) {
        Map<String, Object> results = new HashMap<>();
        List<Long> shopIds = new ArrayList<>();
        if (userId != null) {
            PrivilageAccountDO privilageAccountDO = privilageAccountDOMapper.selectOne(Wrappers.<PrivilageAccountDO>lambdaQuery().eq(PrivilageAccountDO::getUserId, userId));
            PrivilageRoleDO roleDO = privilageRoleDOMapper.selectRoleName(privilageAccountDO.getUserId());
            System.out.println(roleDO.getCode());
            System.out.println(RoleEnum.getCodeByName("运营人员").getRoleCode());
            if (roleDO.getCode().equals(RoleEnum.getCodeByName("运营人员").getRoleCode())) {
                PrivilageOrganizationalMember organizationalMember = organizationalMemberMapper.selectOne(Wrappers.<PrivilageOrganizationalMember>lambdaQuery().eq(PrivilageOrganizationalMember::getUserId, userId));
                organizationId = organizationalMember.getOrgId();
            }
        }
//        if(userId != null) {
//            if (storeId != null) {
//                shopIds.add(storeId);
//            } else {
//                shopIds = privilageDomainService.listUserDatePermission(userId);
//            }
//        }
//        List<CompanyStoreDO> list = retailCompanyMapper.selectShopIdByRetail(orgId, shopIds);
//
//        //查询所有的店铺信息
//        QueryWrapper<PoiStore> poiStoreQueryWrapper = new QueryWrapper<>();
//        poiStoreQueryWrapper.in("id", shopIds);
//        List<PoiStore> list2 = shopIds.size() == 0 ? new ArrayList<>() : poiStoreDOMapper.selectList(poiStoreQueryWrapper);
        PageHelper.startPage(pageNum, pageSize);
        List<ActivityInstanceVO> activityInstanceVOS = pmnActivityInstanceDOMapper.pageVO(startDate, endDate, organizationId, storeId, status, categoryCode, null, beginTime, endTime);
        if (CollectionUtils.isEmpty(activityInstanceVOS)) {
            results.put("this", null);
            return results;
        }
        AtomicInteger number = new AtomicInteger(1 + ((pageNum - 1) * pageSize));
        //求分页总数
        Page<ActivityInstanceVO> pageList = (Page<ActivityInstanceVO>) activityInstanceVOS;
        Long totalSize = pageList.getTotal();
        for (ActivityInstanceVO m : activityInstanceVOS) {
            m.setScheduleVO(pmnActivityScheduleService.getVO(m.getScheduleId()));
            m.setTotalSize(totalSize);
            m.setNumber(number.get());
            number.incrementAndGet();
        }
        results.put("this", new PageInfo<>(activityInstanceVOS));
        return results;
    }

    @Override
    @Transactional
    public boolean removeInstance(Long id) {
        ActivityInstanceVO instanceVO = pmnActivityInstanceService.getVO(id);
        if (instanceVO.getStatus() != 3 || instanceVO.getStatus() != 4 || instanceVO.getStores().size() > 0) {
            return false;
        }
        boolean removeInstance = removeById(id);
        boolean removeConfig = pmnActivityInstanceConfigService.remove(Wrappers.<PmnActivityInstanceConfig>lambdaQuery().eq(PmnActivityInstanceConfig::getInstanceId, id));
        boolean removeCase = pmnActivityPromotionCaseService.remove(Wrappers.<PmnActivityPromotionCase>lambdaQuery().eq(PmnActivityPromotionCase::getInstanceId, id));
        boolean removeStore = pmnActivityStoreRelationService.remove(Wrappers.<PmnActivityStoreRelation>lambdaQuery().eq(PmnActivityStoreRelation::getInstanceId, id));
        return removeInstance && removeConfig && removeCase && removeStore;
    }

    @Override
    @Transactional
    public boolean removeWelcome(Long scheduleId) {
        List<PmnActivityInstance> instances = pmnActivityInstanceService.list(Wrappers.<PmnActivityInstance>lambdaQuery().eq(PmnActivityInstance::getScheduleId, scheduleId).select(PmnActivityInstance::getId));
        Set<Long> instanceIds = instances.stream().map(PmnActivityInstance::getId).collect(Collectors.toSet());
        List<PmnActivityPromotion> pmnActivityPromotions = activityPromotionService.list(Wrappers.<PmnActivityPromotion>lambdaQuery().eq(PmnActivityPromotion::getScheduleId, scheduleId));
        Set<Long> pmnActivityPromotionIds = pmnActivityPromotions.stream().map(PmnActivityPromotion::getId).collect(Collectors.toSet());

        boolean removeSchedule = pmnActivityScheduleService.removeById(scheduleId);
        boolean removeInstance = removeByIds(instanceIds);
        boolean removePromotions = activityPromotionService.removeByIds(pmnActivityPromotionIds);
        boolean removeParams = pmnActivityPromotionParamService.remove(Wrappers.<PmnActivityPromotionParam>lambdaQuery().in(PmnActivityPromotionParam::getPromotionId, pmnActivityPromotionIds));
        boolean removeConfig = pmnActivityInstanceConfigService.remove(Wrappers.<PmnActivityInstanceConfig>lambdaQuery().in(PmnActivityInstanceConfig::getInstanceId, instanceIds));
        boolean removeCase = pmnActivityPromotionCaseService.remove(Wrappers.<PmnActivityPromotionCase>lambdaQuery().in(PmnActivityPromotionCase::getInstanceId, instanceIds));
        boolean removeStore = pmnActivityStoreRelationService.remove(Wrappers.<PmnActivityStoreRelation>lambdaQuery().in(PmnActivityStoreRelation::getInstanceId, instanceIds));
        return removePromotions && removeParams && removeSchedule && removeInstance && removeConfig && removeCase && removeStore;
    }

    @Transactional
    @Override
    public boolean saveWelcome(ActivityInstanceVO activityInstanceVO) {
        //保存活动池
        PmnActivitySchedule schedule = new PmnActivitySchedule();
        schedule.setCategoryCode("welcome");
        schedule.setCategoryId(2);
        schedule.setUpdateTime(new Date());
        schedule.setLogo("");
        schedule.setName("欢迎语");
        schedule.setId(activityInstanceVO.getScheduleId());
        schedule.setStatus(1);
        schedule.setCreateTime(new Date());
        schedule.setDescription(activityInstanceVO.getName());
        boolean saveSchedule = pmnActivityScheduleService.saveOrUpdate(schedule);
        PmnActivityPromotion pmnActivityPromotion = PmnActivityPromotion
                .builder()
                .content(schedule.getDescription())
                .createTime(new Date())
                .name(schedule.getName())
                .status(1)
                .scheduleId(schedule.getId())
                .uri("")
                .updateTime(new Date())
                .type(1)
                .build();
        PmnActivityPromotion oldPmnActivityPromotion = activityPromotionService.getOne(Wrappers.<PmnActivityPromotion>lambdaQuery().eq(PmnActivityPromotion::getScheduleId, schedule.getId()));
        if (oldPmnActivityPromotion != null) {
            pmnActivityPromotion.setId(oldPmnActivityPromotion.getId());
        }
        boolean saveActivityPromotion = activityPromotionService.saveOrUpdate(pmnActivityPromotion, Wrappers.<PmnActivityPromotion>lambdaQuery().eq(PmnActivityPromotion::getScheduleId, schedule.getId()));
        //保存实例
        PmnActivityInstance pmnActivityInstance = new PmnActivityInstance();
        if (activityInstanceVO.getId() != null) {
            pmnActivityInstance = getById(activityInstanceVO.getId());
        }
        pmnActivityInstance.setId(activityInstanceVO.getId());
        pmnActivityInstance.setBeginTime(new Date());
        if (activityInstanceVO.getId() == null) {
            pmnActivityInstance.setCreateTime(new Date());
        }
        pmnActivityInstance.setEndTime(new Date());
        pmnActivityInstance.setContent(activityInstanceVO.getContent());
        pmnActivityInstance.setCategoryCode("welcome");
        pmnActivityInstance.setName("欢迎语");
        pmnActivityInstance.setIsAddition(2L);
        pmnActivityInstance.setStatus(1L);
        pmnActivityInstance.setScheduleId(schedule.getId());
        pmnActivityInstance.setUpdateTime(new Date());
        pmnActivityInstance.setCode("");
        boolean saveInstance = pmnActivityInstanceService.saveOrUpdate(pmnActivityInstance);
        //保存case
        PmnActivityPromotionCase pmnActivityPromotionCase = new PmnActivityPromotionCase();
        pmnActivityPromotionCase.setCreateTime(new Date());
        pmnActivityPromotionCase.setInstanceId(schedule.getId());
        pmnActivityPromotionCase.setStatus(schedule.getStatus().longValue());
        pmnActivityPromotionCase.setUpdateTime(new Date());
        pmnActivityPromotionCase.setTag("");
        PmnActivityPromotionCase oldPmnActivityPromotionCase = pmnActivityPromotionCaseService.getOne(Wrappers.<PmnActivityPromotionCase>lambdaQuery().eq(PmnActivityPromotionCase::getInstanceId, pmnActivityInstance.getId()));
        if (oldPmnActivityPromotionCase != null) {
            pmnActivityPromotionCase.setCreateTime(null);
            pmnActivityPromotionCase.setId(oldPmnActivityPromotionCase.getId());
        }
        boolean saveCase = pmnActivityPromotionCaseService.saveOrUpdate(pmnActivityPromotionCase);
        List<Long> paramIds = new ArrayList<>();
        for (PmnActivityPromotionParam param : activityInstanceVO.getParams()) {
            param.setPromotionId(pmnActivityPromotion.getId());
            param.setAttr(2);
            boolean saveParam = pmnActivityPromotionParamService.saveOrUpdate(param);
            if (param.getId() != null && param.getId() > 0) {
                paramIds.add(param.getId());
            }
            if (!saveParam) throw new RuntimeException("活动类型保存失败");
            PmnActivityInstanceConfig config = new PmnActivityInstanceConfig();
            config.setCaseId(pmnActivityPromotionCase.getId());
            config.setCreateTime(new Date());
            config.setInstanceId(pmnActivityInstance.getId());
            config.setKey(param.getName());
            config.setVal(param.getKey());
            config.setStatus(pmnActivityInstance.getStatus());
            config.setUpdateTime(new Date());
            config.setParamId(param.getId());
            config.setName(param.getName());
            config.setType(Long.valueOf(param.getType()));
            pmnActivityInstanceConfigService.saveOrUpdate(config, Wrappers.<PmnActivityInstanceConfig>lambdaQuery().eq(PmnActivityInstanceConfig::getInstanceId, pmnActivityInstance.getId()).eq(PmnActivityInstanceConfig::getParamId, param.getId()));
        }
        if (paramIds.size() > 0) {
            pmnActivityPromotionParamService.remove(Wrappers.<PmnActivityPromotionParam>lambdaQuery().eq(PmnActivityPromotionParam::getPromotionId, pmnActivityPromotion.getId()).notIn(PmnActivityPromotionParam::getId, paramIds).eq(PmnActivityPromotionParam::getAttr, 2));
        }
        Set<Long> storeIds = new HashSet<>();
        List<PmnActivityStoreRelation> saveStores = new ArrayList<>();
        for (PoiStore store : activityInstanceVO.getStores()) {
            PrivilageOrganizational org = poiStoreDOMapper.findStoreCompany(store.getId());
            PmnActivityStoreRelation pmnActivityStoreRelation = new PmnActivityStoreRelation();
            pmnActivityStoreRelation.setUpdateTime(new Date());
            pmnActivityStoreRelation.setStoreId(store.getId());
            pmnActivityStoreRelation.setInstanceId(pmnActivityInstance.getId());
            pmnActivityStoreRelation.setCreateTime(new Date());
            int status = 1;
            if (pmnActivityInstance.getStatus() > 2) {
                status = 2;
            }
            pmnActivityStoreRelation.setStatus(status);
            pmnActivityStoreRelation.setCaseId(pmnActivityInstance.getId());
            pmnActivityStoreRelation.setOrgId(org.getId());
            saveStores.add(pmnActivityStoreRelation);
//            pmnActivityStoreRelationService.saveOrUpdate(pmnActivityStoreRelation, Wrappers.<PmnActivityStoreRelation>lambdaQuery().eq(PmnActivityStoreRelation::getInstanceId, pmnActivityInstance.getId()).eq(PmnActivityStoreRelation::getStoreId, store.getId()));
            storeIds.add(store.getId());
        }
        List<PmnActivityStoreRelation> relations = pmnActivityStoreRelationService.list(Wrappers.<PmnActivityStoreRelation>lambdaQuery().eq(PmnActivityStoreRelation::getInstanceId, pmnActivityInstance.getId()).in(PmnActivityStoreRelation::getStoreId, storeIds).select(PmnActivityStoreRelation::getStoreId));
        Set<Long> hasStores = relations.stream().map(PmnActivityStoreRelation::getStoreId).collect(Collectors.toSet());
        if (hasStores.size() > 0) {
            saveStores = saveStores.stream().filter(store -> hasStores.contains(store.getStoreId())).collect(Collectors.toList());
        }
        boolean saveStore = pmnActivityStoreRelationService.saveBatch(saveStores);
        return saveStore && saveSchedule && saveCase && saveInstance && saveActivityPromotion;
    }

    @Transactional
    @Override
    public void scanValidAndSet() {
        Date now = new Date();
        List<PmnActivityInstance> starts = pmnActivityInstanceService.list(Wrappers.<PmnActivityInstance>lambdaQuery().eq(PmnActivityInstance::getStatus, 1).le(PmnActivityInstance::getBeginTime, now).ge(PmnActivityInstance::getEndTime, now).select(PmnActivityInstance::getId));
        if(starts.size() > 0) {
            starts.stream().forEach(item -> item.setStatus(2L));
            Set<Long> startInstances = starts.stream().map(PmnActivityInstance::getId).collect(Collectors.toSet());
            pmnActivityInstanceService.updateBatchById(starts);
            PmnPromotionSrc startPromotionSrc = new PmnPromotionSrc();
            startPromotionSrc.setStatus(1L);
            PmnActivityInstanceConfig startConfig = new PmnActivityInstanceConfig();
            startConfig.setStatus(1L);
            PmnActivityStoreRelation startStoreRelation = new PmnActivityStoreRelation();
            startStoreRelation.setStatus(1);
            PmnActivityPromotionCase startCase = new PmnActivityPromotionCase();
            startCase.setStatus(1L);
            pmnPromotionSrcService.update(startPromotionSrc, Wrappers.<PmnPromotionSrc>lambdaQuery().in(PmnPromotionSrc::getInstanceId, startInstances).ne(PmnPromotionSrc::getStatus, 1));
            pmnActivityInstanceConfigService.update(startConfig, Wrappers.<PmnActivityInstanceConfig>lambdaQuery().in(PmnActivityInstanceConfig::getInstanceId, startInstances).ne(PmnActivityInstanceConfig::getStatus, 1));
            pmnActivityStoreRelationService.update(startStoreRelation, Wrappers.<PmnActivityStoreRelation>lambdaQuery().in(PmnActivityStoreRelation::getInstanceId, startInstances).ne(PmnActivityStoreRelation::getStatus, 1));
            pmnActivityPromotionCaseService.update(startCase, Wrappers.<PmnActivityPromotionCase>lambdaQuery().in(PmnActivityPromotionCase::getInstanceId, startInstances).ne(PmnActivityPromotionCase::getStatus, 1));
            log.info("starts: {}", starts);
        }
    }

    @Transactional
    @Override
    public void scanInvalidAndSer() {
        Date now = new Date();
        List<PmnActivityInstance> ends = pmnActivityInstanceService.list(Wrappers.<PmnActivityInstance>lambdaQuery().ne(PmnActivityInstance::getStatus, 3).ne(PmnActivityInstance::getStatus, 4).le(PmnActivityInstance::getEndTime, now).select(PmnActivityInstance::getId));
        if(ends.size() > 0) {
            ends.stream().forEach(item -> item.setStatus(3L));
            Set<Long> endInstances = ends.stream().map(PmnActivityInstance::getId).collect(Collectors.toSet());
            pmnActivityInstanceService.updateBatchById(ends);
            PmnPromotionSrc endPromotionSrc = new PmnPromotionSrc();
            endPromotionSrc.setStatus(2L);
            PmnActivityInstanceConfig endConfig = new PmnActivityInstanceConfig();
            endConfig.setStatus(2L);
            PmnActivityStoreRelation endStoreRelation = new PmnActivityStoreRelation();
            endStoreRelation.setStatus(2);
            PmnActivityPromotionCase endCase = new PmnActivityPromotionCase();
            endCase.setStatus(2L);
            pmnPromotionSrcService.update(endPromotionSrc, Wrappers.<PmnPromotionSrc>lambdaQuery().in(PmnPromotionSrc::getInstanceId, endInstances).ne(PmnPromotionSrc::getStatus, 2));
            pmnActivityInstanceConfigService.update(endConfig, Wrappers.<PmnActivityInstanceConfig>lambdaQuery().in(PmnActivityInstanceConfig::getInstanceId, endInstances).ne(PmnActivityInstanceConfig::getStatus, 2));
            pmnActivityStoreRelationService.update(endStoreRelation, Wrappers.<PmnActivityStoreRelation>lambdaQuery().in(PmnActivityStoreRelation::getInstanceId, endInstances).ne(PmnActivityStoreRelation::getStatus, 2));
            pmnActivityPromotionCaseService.update(endCase, Wrappers.<PmnActivityPromotionCase>lambdaQuery().in(PmnActivityPromotionCase::getInstanceId, endInstances).ne(PmnActivityPromotionCase::getStatus, 2));
        }
    }
}
