package com.kiisoo.ic.activity.listener;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kiisoo.ic.activity.bean.ActivityInstanceVO;
import com.kiisoo.ic.activity.entity.PmnActivityInstance;
import com.kiisoo.ic.activity.entity.PmnActivityStoreRelation;
import com.kiisoo.ic.activity.entity.PmnPromotionSrc;
import com.kiisoo.ic.activity.event.ActivityInstanceCreatedEvent;
import com.kiisoo.ic.activity.service.IPmnActivityStoreRelationService;
import com.kiisoo.ic.activity.service.IPmnPromotionSrcService;
import com.kiisoo.ic.generalize.entity.PrivilageOrganizational;
import com.kiisoo.ic.store.entity.PoiStore;
import com.kiisoo.ic.store.entity.PoiStoreStaff;
import com.kiisoo.ic.store.mapper.PoiStoreDOMapper;
import com.kiisoo.ic.store.mapper.PoiStoreStaffDOMapper;
import com.kiisoo.ic.store.service.PoiStoreService;
import com.kiisoo.ic.store.service.PoiStoreStaffService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

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

@Component
@Slf4j
public class GenerateQRCodeListener {
    @Autowired
    private IPmnPromotionSrcService pmnPromotionSrcService;
    @Autowired
    private PoiStoreService poiStoreService;
    @Autowired
    private IPmnActivityStoreRelationService pmnActivityStoreRelationService;
    @Autowired
    private PoiStoreStaffDOMapper poiStoreStaffDOMapper;
    @Autowired
    private PoiStoreDOMapper poiStoreDOMapper;
    @Async
    @EventListener
    //后台创建活动实例并绑定店铺之后，给店铺批量生成二维码
    public void GenerateQRCode(ActivityInstanceCreatedEvent event) {
        log.info("生成店铺码: {}", event.instance());
        try {
            PmnActivityInstance instance = event.instance();
            ActivityInstanceVO activityInstanceVO = event.instanceVO();
            Set<Long> storeIds = new HashSet<>();
            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(instance.getId());
                pmnActivityStoreRelation.setCreateTime(new Date());
                int status = 1;
                if(instance.getStatus() > 2) {
                    status = 2;
                }
                pmnActivityStoreRelation.setStatus(status);
                pmnActivityStoreRelation.setCaseId(instance.getId());
                pmnActivityStoreRelation.setOrgId(org.getId());
                pmnActivityStoreRelationService.saveOrUpdate(pmnActivityStoreRelation, Wrappers.<PmnActivityStoreRelation>lambdaQuery().eq(PmnActivityStoreRelation::getInstanceId, instance.getId()).eq(PmnActivityStoreRelation::getStoreId, store.getId()));
                storeIds.add(store.getId());
            }
            if (storeIds.size() > 0) {
                pmnActivityStoreRelationService.remove(Wrappers.<PmnActivityStoreRelation>lambdaQuery().eq(PmnActivityStoreRelation::getInstanceId, instance.getId()).notIn(PmnActivityStoreRelation::getStoreId, storeIds));
            }
            log.info("storeIds: {}", storeIds);
            List<PoiStoreStaff> staffs = poiStoreStaffDOMapper.selectList(Wrappers.<PoiStoreStaff>lambdaQuery().in(PoiStoreStaff::getStoreId, storeIds).eq(PoiStoreStaff::getType, 1).eq(PoiStoreStaff::getStatus, 1));
            log.info("staffs: {}", staffs);
            List<PoiStoreStaff> fails = new LinkedList<>();
            staffs.stream().collect(
                    Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(staff -> staff.getStoreId()))), ArrayList::new)).forEach(item -> {
                PmnPromotionSrc save = pmnPromotionSrcService.createCode(item, instance.getId());
                if (save == null) {
                    fails.add(item);
                }
            });
            log.info("生成失败数据: {}", fails);
        }catch (Exception e) {
            e.printStackTrace();
            log.error("生成失败: {}", e);
        }
    }
}
