ProcessController.java 18.0 KB
package com.skua.modules.app.process;

import com.alibaba.fastjson.JSONObject;
import com.skua.core.api.ISysBaseAPI;
import com.skua.common.system.listener.ProcessHandleService;
import com.skua.common.system.listener.ProcessService;
import com.skua.core.api.vo.Result;
import com.skua.core.aspect.annotation.AutoLog;
import com.skua.core.constant.CommonConstant;
import com.skua.core.context.SpringContextUtils;
import com.skua.core.util.ConvertUtils;
import com.skua.modules.audit.entity.FAuditDoing;
import com.skua.modules.audit.entity.FAuditHistory;
import com.skua.modules.audit.service.IFAuditDoingService;
import com.skua.modules.audit.service.IFAuditHistoryService;
import com.skua.modules.process.entity.FProcessCfg;
import com.skua.modules.process.entity.FProcessNode;
import com.skua.modules.process.entity.FProcessNodeApprover;
import com.skua.modules.process.service.IFProcessCfgService;
import com.skua.modules.process.service.IFProcessNodeApproverService;
import com.skua.modules.process.service.IFProcessNodePercfgService;
import com.skua.modules.process.service.IFProcessNodeService;
import com.skua.modules.system.entity.CustomDictVO;
import com.skua.modules.system.entity.SysCustomField;
import com.skua.modules.system.entity.SysCustomTable;
import com.skua.modules.system.service.ICustomDictService;
import com.skua.modules.system.service.ISysCustomFieldService;
import com.skua.modules.system.service.ISysCustomTableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Field;
import java.util.*;

/**
 * 待办APP接口
 * @author
 */
@Slf4j
@Api(tags="待办APP接口")
@RestController
@RequestMapping("/v1/app/process")
public class ProcessController {

    @Autowired
    private ISysBaseAPI iSysBaseAPI;
    @Autowired
    private IFAuditDoingService fAuditDoingService;
    @Autowired
    private IFProcessNodePercfgService fProcessNodePercfgService;
    @Autowired
    private IFProcessNodeService fProcessNodeService;
    @Autowired
    private IFProcessCfgService fProcessCfgService;
    @Autowired
    private IFProcessNodeApproverService fProcessNodeApproverService;
    @Autowired
    private ISysCustomTableService sysCustomTableService;
    @Autowired
    private IFAuditHistoryService fAuditHistoryService;
    @Autowired
    private ISysCustomFieldService sysCustomFieldService;
    @Autowired
    private ICustomDictService customDictService;

    @AutoLog(value = "获取人员待办事项")
    @ApiOperation(value="获取人员待办事项", notes="获取人员待办事项")
    @GetMapping(value = "/getToDoByUser")
    public Result<Map<String,Object>> getToDoByUser(@RequestParam(name="userId") String userId) {
        Result<Map<String,Object>> result = new Result<Map<String,Object>>();
        Map<String,Object> resMap = new HashMap<>();
        String rId = null;
        List<Map<String,Object>> proList = new ArrayList<>();
        List<Map<String,Object>> resList = new ArrayList<>();
        StringBuffer sql = new StringBuffer("select * from view_audit_doing where approver in (''");
        //获取人员ID
        sql.append(",'"+userId+"'");
        //获取角色ID
        List<String> roleCodes = iSysBaseAPI.getRolesByUserId(userId);
        for (int i = 0; i < roleCodes.size(); i++) {
            String roleCode = roleCodes.get(i);
            String roleId = iSysBaseAPI.getRoleIdByCode(roleCode);
            sql.append(",'"+roleId+"'");
        }
        //获取部门ID
        List<String> deptIds = iSysBaseAPI.getDeptIdsByUserId(userId);
        for (int j = 0; j < deptIds.size(); j++) {
            String deptId = deptIds.get(j);
            sql.append(",'"+deptId+"'");
        }
        sql.append(") and node_value != '-1' ");
        sql.append(" and (report_id like 'equip%' or report_id like 'medica%' or report_id like 'document%') ");
        sql.append(" order by audit_time desc");
        List<Map<String,Object>> list = fAuditDoingService.getToBySql(sql.toString());
        if(list!=null){
            rId = list.get(0).get("report_id").toString();
            proList = getAllByReportId(rId);
            for (int i = 0; i < list.size(); i++) {
                String reportId = list.get(i).get("report_id").toString();
                String nodeId = list.get(i).get("node_id").toString();
                String dataId = list.get(i).get("report_content_id").toString();
                String nodeValue = list.get(i).get("node_value").toString();
                String toDoTime = list.get(i).get("audit_time").toString();
                String processId = list.get(i).get("process_id").toString();
                Map<String,Object> map = new HashMap<>();
                SysCustomTable sysCustomTable = sysCustomTableService.getOneByTable(reportId);
                if(sysCustomTable!=null){
                    map.put("toDoName","待办事项-"+sysCustomTable.getTableDescribe());
                }else{
                    map.put("toDoName","待办事项-"+toDoTime.substring(0,10));
                }
                map.put("reportId",reportId);
                map.put("nodeId",nodeId);
                map.put("dataId",dataId);
                map.put("processId",processId);

                map.put("toDoTime", toDoTime);
                map.put("nodeValue",nodeValue);
                resList.add(i,map);
            }
        }
        resMap.put("data",resList);
        resMap.put("process",proList);
        result.setResult(resMap);
        return result;
    }

    private List<Map<String,Object>> getAllByReportId(String tableName) {
        List<Map<String,Object>> allList = new ArrayList<>();
        try {
            String reportId = getCustomTableIdByTableName(tableName);
            if(reportId!=null){
                FProcessCfg fProcessCfg = fProcessCfgService.getByReportId(reportId);
                String processCfgId = fProcessCfg.getId();
                List<FProcessNode> nodeList = fProcessNodeService.getListByProcessId(processCfgId);
                if(nodeList.size() > 0){
                    for (int i = 1; i < nodeList.size(); i++) {
                        Map<String,Object> map = new HashMap();
                        String nodeId = nodeList.get(i).getId();
                        map.put("id",nodeId);
                        map.put("nodeName",nodeList.get(i).getNodeName());
                        map.put("nodeValue",nodeList.get(i).getNodeValue());
                        map.put("monitorClass",nodeList.get(i).getMonitorClass());
                        map.put("proId",nodeList.get(i).getProId());
                        map.put("proVer",nodeList.get(i).getProVer());
                        List<FProcessNodeApprover> approverList = fProcessNodeApproverService.getByNodeId(nodeId);
                        if(approverList.size() > 0){
                            map.put("approver",approverList.get(0));
                        }else{
                            FProcessNodeApprover approver = new FProcessNodeApprover();
                            approver.setNodeId(nodeId);
                            map.put("approver",approver);
                        }
                        allList.add(map);
                    }
                    Map<String,Object> bjMap = new HashMap();
                    String nodeId_bj = nodeList.get(0).getId();
                    bjMap.put("id",nodeId_bj);
                    bjMap.put("nodeName",nodeList.get(0).getNodeName());
                    bjMap.put("nodeValue",nodeList.get(0).getNodeValue());
                    bjMap.put("monitorClass",nodeList.get(0).getMonitorClass());
                    bjMap.put("proId",nodeList.get(0).getProId());
                    bjMap.put("proVer",nodeList.get(0).getProVer());
                    List<FProcessNodeApprover> lastApproverList = fProcessNodeApproverService.getByNodeId(nodeId_bj);
                    if(lastApproverList.size() > 0){
                        bjMap.put("approver",lastApproverList.get(0));
                    }else{
                        FProcessNodeApprover approver = new FProcessNodeApprover();
                        approver.setNodeId(nodeId_bj);
                        bjMap.put("approver",approver);
                    }
                    allList.add(bjMap);
                }else{
                    //发起-节点
                    FProcessNode nodeStart = new FProcessNode();
                    nodeStart.setNodeName("发起");
                    nodeStart.setNodeValue(0);
                    nodeStart.setProId(processCfgId);
                    fProcessNodeService.save(nodeStart);
                    //办结-节点
                    FProcessNode nodeEnd = new FProcessNode();
                    nodeEnd.setNodeName("办结");
                    nodeEnd.setNodeValue(-1);
                    nodeEnd.setProId(processCfgId);
                    fProcessNodeService.save(nodeEnd);
                    List<FProcessNode> list = fProcessNodeService.getListByProcessId(processCfgId);
                    for (int i = 0; i < list.size(); i++) {
                        Map<String,Object> map = new HashMap();
                        FProcessNodeApprover approver = new FProcessNodeApprover();
                        map.put("id",list.get(i).getId());
                        map.put("nodeName",list.get(i).getNodeName());
                        map.put("nodeValue",list.get(i).getNodeValue());
                        map.put("monitorClass",list.get(i).getMonitorClass());
                        map.put("proId",list.get(i).getProId());
                        map.put("proVer",list.get(i).getProVer());
                        approver.setNodeId(list.get(i).getId());
                        map.put("approver",approver);
                        allList.add(map);
                    }
                }
            }
            return allList;
        } catch (Exception e) {
            return new ArrayList<>();
        }

    }

    private String getCustomTableIdByTableName(String tableName) {
        SysCustomTable sysCustomTable = sysCustomTableService.getOneByTable(tableName);
        if(sysCustomTable==null){
            return null;
        }else{
            return sysCustomTable.getId();
        }
    }

    @AutoLog(value = "APP审批流程")
    @ApiOperation(value="APP审批流程", notes="APP审批流程")
    @PutMapping(value = "/spAudit")
    public Result<FAuditDoing> saveAudit(@RequestBody JSONObject jsonObject) {
        String reportId = jsonObject.getString("reportId");//库表管理中的ID
        String processId = jsonObject.getString("processId");//流程ID
        String dataId = jsonObject.getString("dataId");//数据ID
        String userId = jsonObject.getString("userId");//用户ID
        String nodeValue = jsonObject.getString("nodeValue");//节点值
        String approveRecord = jsonObject.getString("approveRecord");
        Result<FAuditDoing> result = new Result<FAuditDoing>();
        FAuditDoing fAuditDoing = fAuditDoingService.getBeanByAllId(reportId,processId,dataId);
        if(fAuditDoing!=null){//修改节点值
            fAuditDoing.setNodeValue(nodeValue);
            fAuditDoing.setOpeId(userId);
            fAuditDoing.setReprotAttr(approveRecord);
            boolean ok = fAuditDoingService.updateById(fAuditDoing);
            if(ok){
                FAuditHistory fAuditHistory = new FAuditHistory();
                fAuditHistory.setReportId(reportId);
                fAuditHistory.setProcessId(processId);
                fAuditHistory.setReportContentId(dataId);
                fAuditHistory.setOpeId(userId);
                fAuditHistory.setNodeValue(nodeValue);
                fAuditHistory.setReprotAttr(approveRecord);
                fAuditHistoryService.save(fAuditHistory);
            }
        }else{
            FAuditDoing audit = new FAuditDoing();
            audit.setReportId(reportId);
            audit.setProcessId(processId);
            audit.setReportContentId(dataId);
            audit.setOpeId(userId);
            audit.setNodeValue(nodeValue);
            audit.setReprotAttr(approveRecord);
            boolean ok = fAuditDoingService.save(audit);
            if(ok){
                FAuditHistory fAuditHistory = new FAuditHistory();
                fAuditHistory.setReportId(reportId);
                fAuditHistory.setProcessId(processId);
                fAuditHistory.setReportContentId(dataId);
                fAuditHistory.setOpeId(userId);
                fAuditHistory.setNodeValue(nodeValue);
                fAuditHistory.setReprotAttr(approveRecord);
                fAuditHistoryService.save(fAuditHistory);
            }
        }
        FProcessNode fProcessNode = fProcessNodeService.getByProIdAndNodeValue(processId,nodeValue);
        if(fProcessNode.getMonitorClass()!=null){
            ProcessHandleService createPlanService = (ProcessHandleService)SpringContextUtils.getBean(fProcessNode.getMonitorClass());
            boolean flag = createPlanService.doService(dataId);
            if(!flag){ System.out.print("监听类执行:"+flag);}
        }
        result.setSuccess(true);
        return result;
    }


    @AutoLog(value = "待办事项详情")
    @ApiOperation(value="待办事项详情", notes="待办事项详情")
    @PutMapping(value = "/getInfo")
    public Result<List<Map<String,Object>>> getInfo(@RequestBody JSONObject jsonObject) throws IllegalAccessException {
        Result<List<Map<String,Object>>> result = new Result<List<Map<String,Object>>>();
        String reportId = jsonObject.getString("report_id");
        String nodeId = jsonObject.getString("node_id");
        String dataId = jsonObject.getString("report_content_id");
        Map<String,Object> resMap = new HashMap<>();
        List<Map<String,Object>> resList = new ArrayList<>();

        SysCustomTable sysCustomTable=new SysCustomTable();
        sysCustomTable.setTableName(reportId);
        List<SysCustomField> headList = sysCustomFieldService.queryDictField(sysCustomTable);
        //定义字典map集合
        Map<String,Map<String,String>> dictMap=new HashMap<String,Map<String,String>>();
        for(SysCustomField field:headList) {
            String fieldType=field.getFieldType();//获取字段类型
            String dictType=field.getDictType();
            if(fieldType.equals(CommonConstant.COMMON_DICT_TYPE)) {
                //普通字典
                List<CustomDictVO> dictList=customDictService.queryDictItemsByCode(dictType);
                Map<String,String> dictItemMap=new HashMap<String,String>();
                for(int j=0;j<dictList.size();j++) {
                    dictItemMap.put(dictList.get(j).getDictCode(), dictList.get(j).getDictName());
                }
                dictMap.put(field.getEntityFieldCode(), dictItemMap);
            }else if(fieldType.equals(CommonConstant.BUSINESS_DICT_TYPE)) {
                if(dictType.equals("chicePerson")) {
                    List<Map<String,Object>> userList=iSysBaseAPI.queryUserList();
                    Map<String,String> dictItemMap=new HashMap<String,String>();
                    for(int j=0;j<userList.size();j++) {
                        dictItemMap.put(userList.get(j).get("userId").toString(), userList.get(j).get("userName").toString());
                    }
                    dictMap.put(field.getEntityFieldCode(), dictItemMap);
                }

            }
        }

        List<Map<String,Object>> list = fProcessNodePercfgService.getByNodeId(nodeId,reportId);
        ProcessService processService = (ProcessService) SpringContextUtils.getBean(ConvertUtils.camelName(reportId+"_service_impl"));
        Object obj = processService.getInfoById(dataId);
        Map<String,Object> map = getObjectToMap(obj);

        for (int i = 0; i < list.size(); i++) {
            Map<String,Object> entity = list.get(i);
            String entityFieldCode = list.get(i).get("entity_field_code").toString();
            String fieldType = list.get(i).get("field_type").toString();
            String checkRule = list.get(i).get("check_rule").toString();
            entity.put("dictType",mapToList(dictMap.get(entityFieldCode)));
            entity.put("key",entityFieldCode);
            entity.put("value",map.get(entityFieldCode));
            entity.put("fieldType",fieldType);
            entity.put("checkRule",checkRule);
            resList.add(entity);
        }
        result.setResult(resList);
        return result;
    }

    private List<Map<String,String>> mapToList(Map<String, String> entity) {
        List<Map<String,String>> list = new ArrayList<>();
        if(entity!=null){
            for (String key : entity.keySet()) {
                Map<String, String> map = new HashMap<>();
                map.put("key",key);
                map.put("value",entity.get(key));
                list.add(map);
            }
        }
        return list;
    }

    //Object转Map
    public static Map<String, Object> getObjectToMap(Object obj) throws IllegalAccessException {
        Map<String, Object> map = new LinkedHashMap<String, Object>();
        Class<?> clazz = obj.getClass();
        for (Field field : clazz.getDeclaredFields()) {
            field.setAccessible(true);
            String fieldName = field.getName();
            Object value = field.get(obj);
            if (value == null) {
                value = "";
            }
            map.put(fieldName, value);
        }
        return map;
    }

    @AutoLog(value = "上传待办填报详情")
    @ApiOperation(value="上传待办填报详情", notes="上传待办填报详情")
    @PutMapping(value = "/saveInfo")
    public Result<List<Map<String,Object>>> saveInfo(@RequestBody JSONObject jsonObject){
        Result<List<Map<String,Object>>> result = new Result<List<Map<String,Object>>>();

        String reportId = jsonObject.getString("report_id");
        ProcessService processService = (ProcessService) SpringContextUtils.getBean(ConvertUtils.camelName(reportId+"_service_impl"));
        boolean ok = processService.saveInfo(jsonObject);
        if(ok){
            result.setSuccess(true);
        }else{
            result.error500("上传失败");
        }
        return result;
    }

}