package com.zkqy.execution.produce.utils;

import com.zkqy.execution.produce.dispersed.entity.BpmExecuteNode;
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 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;

    /**
     * 返回弹窗数据的通用工具类
     * <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();
        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("taskProcessKey", bpmRunNodeFromVo.getTaskProcessKey());
                item.getCondition().put("taskNodeKey", bpmRunNodeFromVo.getTaskNodeKey());
            }
            //是否是节点回填数据（默认是true）
            String listKey = item.getListKey();
            if (listKey != null) {
                //下拉框数据、或者表格多条数据
                List<Map<String, Object>> maps = commonMapper.selectListMap(item.getTableName(),item.getCondition());
                commonEntity.get().getResultMap().put(item.getListKey(), maps);
            } else {
                //单条数据的回显逻辑
                CommonEntity common = commonMapper.selectOne(item.getTableName(), item.getCondition());
                commonEntity.get().getResultMap().putAll(common.getResultMap());
            }
        });
        return commonEntity.get();
    }
}
