package com.zkqy.execution.produce.utils;

import com.zkqy.execution.produce.dispersed.entity.BpmExecuteNode;
import com.zkqy.execution.produce.dispersed.entity.runbpm.BpmRunNodeFormDateVo;
import com.zkqy.execution.produce.dispersed.entity.runbpm.BpmRunNodeFormFilterConditionsVo;
import com.zkqy.execution.produce.dispersed.entity.runbpm.BpmRunNodeFromVo;
import com.zkqy.execution.produce.dispersed.mapper.BpmExecuteNodeFormMapper;
import com.zkqy.execution.produce.dispersed.mapper.BpmExecuteNodeMapper;
import com.zkqy.system.entity.CommonEntity;
import com.zkqy.system.mapper.CommonMapper;
import com.zkqy.system.service.ICommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 离散型流程表单相关处理
 *
 * @author hmc
 * @date 2023-12-12 12:29
 * @Description:
 */
@Service
public class EchoNodeFormData {

    @Autowired
    private BpmExecuteNodeFormMapper bpmExecuteNodeFormMapper;

    @Autowired
    private BpmExecuteNodeMapper executeNodeMapper;

    @Autowired
    private CommonMapper commonMapper;

    @Autowired  // 共同处理表接口
    private ICommonService iCommonService;

    /**
     * 返回弹窗数据的通用工具类
     * <p>
     * 回显节点表单数据
     *
     * @return
     */
    public CommonEntity getEchoNodeFormData(BpmRunNodeFromVo bpmRunNodeFromVo) {
        //返回的东西定义
        AtomicReference<CommonEntity> commonEntity = new AtomicReference<>(new CommonEntity());
        //默认查询的节点重量数据
        BpmExecuteNode bpmExecuteNode = executeNodeMapper
                .queryBpmExecuteNodeBytaskNodeKey(bpmRunNodeFromVo.getTaskProcessKey(), bpmRunNodeFromVo.getTaskNodeKey());
        //到时候默认回显的数据
        HashMap<String, String> bpmExecuteNodeMap = new HashMap();
        if (bpmExecuteNode != null) {
            bpmExecuteNodeMap.put("taskNodeNumber", bpmExecuteNode.getTaskNodeNumber());
            bpmExecuteNodeMap.put("taskNodeWeight", bpmExecuteNode.getTaskNodeWeight());
        }
        commonEntity.get().getResultMap().put("bpmExecuteNode", bpmExecuteNodeMap);
        //查询表名、条件封装的集合
        List<BpmRunNodeFormFilterConditionsVo> formDataVoList = bpmRunNodeFromVo.getFormDataVoList();
        formDataVoList.forEach(item -> {
            //表单回填数据查询
            if (item.isNodeOnlyData()) {//默认是带这个流程条件信息的
                item.getCondition().put("task_key", bpmRunNodeFromVo.getTaskProcessKey());
                item.getCondition().put("task_node_key", bpmRunNodeFromVo.getTaskNodeKey());
            }
            //是否是节点回填数据（默认是true）
            String listKey = item.getListKey();
            if (listKey != null && !listKey.isEmpty()) {
                //下拉框数据、或者表格多条数据
                List<Map<String, Object>> maps = commonMapper.selectListMap(item.getTableName(), item.getCondition());
                if(maps!=null){
                    commonEntity.get().getResultMap().put(item.getListKey(), maps);
                }
            } else {
                //单条数据的回显逻辑
                CommonEntity common = commonMapper.selectOne(item.getTableName(), item.getCondition());
                if(common!=null){
                    commonEntity.get().getResultMap().putAll(common.getResultMap());
                }
            }
        });
        return commonEntity.get();
    }

    /**
     * 节点表单中的新增方法
     *
     * @param bpmRunNodeFormDateVo
     * @return
     */
    public int handleSubmitForm(BpmRunNodeFormDateVo bpmRunNodeFormDateVo) {
        // 处理新增
        if (bpmRunNodeFormDateVo.getInsertCommonEntityList().size() != 0) {
            bpmRunNodeFormDateVo.getInsertCommonEntityList().forEach(item -> {
                iCommonService.batchInsert(item);
            });
        }
        // 处理修改
        if (bpmRunNodeFormDateVo.getUpdateCommonEntityList().size() != 0) {
            bpmRunNodeFormDateVo.getUpdateCommonEntityList().forEach(item -> {
                iCommonService.edit(item);
            });
        }
        // 删除
        if (bpmRunNodeFormDateVo.getDeleteCommonEntityList().size() != 0) {
            bpmRunNodeFormDateVo.getDeleteCommonEntityList().forEach(item -> {
                iCommonService.batchDelete(item);
            });
        }
        return 1;
    }
}
