package test;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kiisoo.ic.activity.entity.*;
import com.kiisoo.ic.activity.service.*;
import com.kiisoo.ic.app.IcApplication;
import com.kiisoo.ic.generalize.entity.PrivilageOrganizational;
import com.kiisoo.ic.store.mapper.PoiStoreDOMapper;
import com.kiisoo.ic.wx.service.AddWXService;
import lombok.extern.slf4j.Slf4j;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = IcApplication.class)
@Slf4j
public class Test {

    @Autowired
    AddWXService addWXService;
    @Autowired
    PoiStoreDOMapper poiStoreDOMapper;
    @Autowired
    IPmnActivityStoreRelationService pmnActivityStoreRelationService;
    @Autowired
    IPmnPromotionSrcService pmnPromotionSrcService;
    @Autowired
    IPmnActivityInstanceService pmnActivityInstanceService;
    @Autowired
    IPmnActivityInstanceConfigService pmnActivityInstanceConfigService;
    @Autowired
    IPmnActivityPromotionCaseService pmnActivityPromotionCaseService;

    @org.junit.Test
    public void fillStoreCompany() {
        List<PmnActivityStoreRelation> relations = pmnActivityStoreRelationService.list();
        relations.stream().forEach(item -> {
            PrivilageOrganizational org = poiStoreDOMapper.findStoreCompany(item.getStoreId());
            item.setOrgId(org.getId());
            pmnActivityStoreRelationService.updateById(item);
        });
    }

    @org.junit.Test
    @Transactional
    public void activityStatusTest() {
        /**
         * 有效
         */
        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);
        }
        /**
         * 失效
         */
        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));
        }
        log.info("ends: {}", ends);
    }
}
