package cn.cecep.talroad.data.analyse.task.factory.label.change;

import cn.cecep.talroad.domain.PcLabel;
import cn.cecep.talroad.domain.PcLabelFactory;
import cn.cecep.talroad.domain.dto.FactoryLabelChangeDTO;
import cn.cecep.talroad.mapper.PcLabelFactoryMapper;
import cn.cecep.talroad.mapper.PcLabelMapper;
import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.ListUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @Author: Frank
 * @createTime: 2023年06月28日 16:20:10
 * @version:
 * @Description:
 */
public abstract class AbstractFactoryLabelChangeService implements FactoryLabelChangeService {

    private static final Logger logger = LoggerFactory.getLogger(AbstractFactoryLabelChangeService.class);

    @Autowired
    private PcLabelMapper pcLabelMapper;

    @Autowired
    private PcLabelFactoryMapper pcLabelFactoryMapper;

    @Autowired
    private PcLabelFactoryMapper labelFactoryMapper;

    @Override
    public boolean support(String enumId) {
        return false;
    }

    @Override
    public void execute(FactoryLabelChangeDTO factoryLabelChangeDTO) {
        logger.error("没有对应实现!");
    }

    public PcLabel obtainLabel(String labelCode) {
        LambdaQueryWrapper<PcLabel> queryWrapper = new LambdaQueryWrapper<PcLabel>()
                .eq(PcLabel::getLabelCode, labelCode)
                .last("limit 1");
        List<PcLabel> labelList = pcLabelMapper.selectList(queryWrapper);
        logger.info("AbstractFactoryLabelChangeService->obtainLabel->[labelList]:{}", JSON.toJSONString(labelList));
        if (CollUtil.isNotEmpty(labelList)) {
            return labelList.get(0);
        }
        return new PcLabel();
    }

    @Transactional(rollbackFor = Exception.class)
    public void insertOrUpdateFactoryLabel(Set<String> factoryIds, String argumentsCode, String labelCode) {
        PcLabel pcLabel = obtainLabel(labelCode);
        //这里查询一次数据库是为了区分哪些标签数据需要修改，哪些标签数据需要新增
        LambdaQueryWrapper<PcLabelFactory> queryWrapper = new LambdaQueryWrapper<PcLabelFactory>()
                .in(PcLabelFactory::getFactoryId, factoryIds)
                .eq(PcLabelFactory::getLabelId, pcLabel.getId());
        List<PcLabelFactory> labelFactories = pcLabelFactoryMapper.selectList(queryWrapper);
        logger.info("查询出的公司标签关系:{}", JSON.toJSONString(labelFactories));
        Map<String, PcLabelFactory> labelFactoryMap = labelFactories.stream()
                .collect(Collectors.toMap(PcLabelFactory::getFactoryId, entity -> entity));
        List<PcLabelFactory> insertList = Lists.newArrayList();
        List<PcLabelFactory> updateList = Lists.newArrayList();
        for (String factoryId : factoryIds) {
            PcLabelFactory pcLabelFactory = new PcLabelFactory();
            if (!labelFactoryMap.containsKey(factoryId)) {
                pcLabelFactory.setFactoryId(factoryId);
                pcLabelFactory.setLabelId(pcLabel.getId());
                pcLabelFactory.setLableUseing("1");
                pcLabelFactory.setLabelValue(argumentsCode);
                pcLabelFactory.setId(UUID.randomUUID().toString());
                insertList.add(pcLabelFactory);
            } else {
                String labelValue = labelFactoryMap.get(factoryId).getLabelValue();
                PcLabelFactory factory = labelFactoryMap.get(factoryId);
                if (!Objects.equals(argumentsCode, labelValue)) {
                    pcLabelFactory.setLabelValue(argumentsCode);
                    pcLabelFactory.setId(factory.getId());
                    updateList.add(pcLabelFactory);
                }
            }
        }
        if (CollUtil.isNotEmpty(insertList)) {
            logger.info("需要新增的企业标签关系集合:{}", insertList);
            List<List<PcLabelFactory>> partition = ListUtils.partition(insertList, 2000);
            for (List<PcLabelFactory> pcLabelFactories : partition) {
                labelFactoryMapper.batchSave(pcLabelFactories);
            }
        }
        if (CollUtil.isNotEmpty(updateList)) {
            logger.info("需要修改的企业标签关系集合:{}", updateList);
            List<List<PcLabelFactory>> partition = ListUtils.partition(updateList, 2000);
            for (List<PcLabelFactory> pcLabelFactories : partition) {
                labelFactoryMapper.batchUpdateValue(pcLabelFactories);
            }
        }
    }
}
