package com.hotent.uc.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.hotent.base.feign.BpmModelFeignService;
import com.hotent.base.manager.impl.BaseManagerImpl;
import com.hotent.base.query.PageList;
import com.hotent.base.query.QueryFilter;
import com.hotent.base.util.BeanUtils;
import com.hotent.uc.dao.MatrixProcRelationDao;
import com.hotent.uc.manager.MatrixProcRelationManager;
import com.hotent.uc.model.MatrixProcRelation;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

import static java.util.stream.Collectors.toList;

/**
 * <pre>
 * 描述：矩阵流程关系表 处理实现类
 * 构建组：x7
 * 作者:zjq
 * 邮箱:zjq@jee-soft.cn
 * 日期:2021-01-16 17:10:08
 * 版权：广州宏天软件股份有限公司
 * </pre>
 */
@Service("matrixProcRelationManager")
public class MatrixProcRelationManagerImpl extends BaseManagerImpl<MatrixProcRelationDao, MatrixProcRelation> implements MatrixProcRelationManager {
    @Resource
    BpmModelFeignService bpmModelFeignService;

    @Transactional
    @Override
    public void batchHandle(List<MatrixProcRelation> oldMatrixProcRelations, List<MatrixProcRelation> newMatrixProcRelations) {
        if (oldMatrixProcRelations == null) {
            oldMatrixProcRelations = new ArrayList<>();
        }
        if (newMatrixProcRelations == null) {
            newMatrixProcRelations = new ArrayList<>();
        }

        //重建关系表条目, 避免手动删除数据表条目后数据不一致
        //Set<MatrixProcRelation> allRelations = CollUtil.intersectionDistinct(oldMatrixProcRelations, newMatrixProcRelations);
        Set<MatrixProcRelation> newMatrixProcRelationsSet = new HashSet<>(newMatrixProcRelations);
        List<MatrixProcRelation> allRelations = oldMatrixProcRelations.stream().filter(newMatrixProcRelationsSet::contains).collect(toList());
        for (MatrixProcRelation relation : allRelations) {
            String matrixCode = relation.getMatrixCode();
            String procDefKey = relation.getProcDefKey();
            QueryWrapper<MatrixProcRelation> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("MATRIX_CODE_", matrixCode);
            queryWrapper.eq("PROC_DEF_KEY_", procDefKey);
            List<MatrixProcRelation> relations = this.list(queryWrapper);
            if (BeanUtils.isEmpty(relations)) {
                create(relation);
            }
        }
        //删除的关系表条目
        //List<MatrixProcRelation> delRelations = CollUtil.subtractToList(oldMatrixProcRelations, newMatrixProcRelations);
        List<MatrixProcRelation> delRelations = oldMatrixProcRelations.stream().filter(item -> !newMatrixProcRelationsSet.contains(item)).collect(toList());
        for (MatrixProcRelation delRelation : delRelations) {
            String matrixCode = delRelation.getMatrixCode();
            String procDefKey = delRelation.getProcDefKey();
            QueryWrapper<MatrixProcRelation> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("MATRIX_CODE_", matrixCode);
            queryWrapper.eq("PROC_DEF_KEY_", procDefKey);
            this.remove(queryWrapper);
        }
        //新增的关系表条目
        //List<MatrixProcRelation> addRelations = CollUtil.subtractToList(newMatrixProcRelations, oldMatrixProcRelations);
        Set<MatrixProcRelation> oldMatrixProcRelationsSet = new HashSet<>(oldMatrixProcRelations);
        List<MatrixProcRelation> addRelations = newMatrixProcRelations.stream().filter(item -> !oldMatrixProcRelationsSet.contains(item)).collect(toList());
        for (MatrixProcRelation addRelation : addRelations) {
            String matrixCode = addRelation.getMatrixCode();
            String procDefKey = addRelation.getProcDefKey();
            QueryWrapper<MatrixProcRelation> queryWrapper = new QueryWrapper<>();
            queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("MATRIX_CODE_", matrixCode);
            queryWrapper.eq("PROC_DEF_KEY_", procDefKey);
            List<MatrixProcRelation> relations = this.list(queryWrapper);
            if (BeanUtils.isEmpty(relations)) {
                create(addRelation);
            }
        }
    }

    @Override
    public PageList<MatrixProcRelation> getList(QueryFilter queryFilter) throws Exception {
        PageList<MatrixProcRelation> relationPageList = this.query(queryFilter);
        List<MatrixProcRelation> relations = relationPageList.getRows();
        Iterator<MatrixProcRelation> iterator = relations.iterator();
        while (iterator.hasNext()) {
            MatrixProcRelation relation = iterator.next();
            ObjectNode procDefObjNode = bpmModelFeignService.getProcDefByDefKey(relation.getProcDefKey());
            if (BeanUtils.isNotEmpty(procDefObjNode)) {
                String procDefId = procDefObjNode.get("defId").asText();
                String procDefName = procDefObjNode.get("name").asText();
                if (BeanUtils.isNotEmpty(procDefId) && BeanUtils.isNotEmpty(procDefName)) {
                    relation.setProcDefId(procDefId);
                    if (!procDefName.equals(relation.getProcDefName())) {
                        relation.setProcDefName(procDefName);
                        save(relation);
                    }
                }
            }
        }
        relationPageList.setRows(relations);
        return relationPageList;
    }


    @Override
    public PageList<MatrixProcRelation> list(QueryFilter queryFilter) {
        PageList<MatrixProcRelation> relationPageList = query(queryFilter);
        List<MatrixProcRelation> relations = relationPageList.getRows();
        Iterator<MatrixProcRelation> iterator = relations.iterator();
        while (iterator.hasNext()) {
            MatrixProcRelation relation = iterator.next();
            ObjectNode procDefObjNode = bpmModelFeignService.getProcDefByDefKey(relation.getProcDefKey());
            if (BeanUtils.isNotEmpty(procDefObjNode)) {
                String procDefId = procDefObjNode.get("defId").asText();
                String procDefName = procDefObjNode.get("name").asText();
                if (BeanUtils.isNotEmpty(procDefId) && BeanUtils.isNotEmpty(procDefName)) {
                    relation.setProcDefId(procDefId);
                    if (!procDefName.equals(relation.getProcDefName())) {
                        relation.setProcDefName(procDefName);
                        save(relation);
                    }
                }
            }
        }
        relationPageList.setRows(relations);
        return relationPageList;
    }
}
