DangerousOperationController.java 12.6 KB
package com.skua.modules.emergency.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.skua.core.api.vo.Result;
import com.skua.core.aspect.annotation.AutoLog;
import com.skua.core.query.QueryGenerator;
import com.skua.core.util.ConvertUtils;
import com.skua.modules.emergency.entity.DangerousOperation;
import com.skua.modules.emergency.service.IDangerousOperationService;
import com.skua.modules.flow.business.service.FlowBusinessService;
import com.skua.modules.flow.business.service.IFlowService;
import com.skua.modules.flow.core.constant.ProcessConstant;
import com.skua.modules.flow.core.entity.FlowAssemblyEntity;
import com.skua.modules.system.vo.UserCertificate;
import com.skua.tool.util.BeanExtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 危险作业事件
 */
@Slf4j
@Api(tags="危险作业事件")
@RestController
@RequestMapping("/danger/dangerOperation")
public class DangerousOperationController {
	@Autowired
	private IDangerousOperationService dangerousOperationService;
	@Autowired
	private IFlowService flowService;
    @Autowired
    private FlowBusinessService flowBusinessService;
	/**
	 * <pre>
	 * 	查询处理数量
	 * </pre>
	 * @param dangerousOperation
	 * @return
	 * @author xiechao, 开发时间
	 * @Description: TODO(这里描述这个方法的需求变更情况)
	 */

	@AutoLog(value = "处理数量统计")
	@ApiOperation(value="处理数量统计", notes="处理数量统计")
	@GetMapping(value = "/getDefectHandleCount")
	public Result<List<Map<String,Object>>> getDefectHandleCount(DangerousOperation dangerousOperation){
		Result<List<Map<String,Object>>> result = new Result<>();
		QueryWrapper<DangerousOperation> queryWrapper = new QueryWrapper<DangerousOperation>();
		queryWrapper.select("count(id) count,operation_status operationStatus");
		queryWrapper.ge(StringUtils.isNotEmpty(dangerousOperation.getStartTime()),"report_date",dangerousOperation.getStartTime());
		queryWrapper.le(StringUtils.isNotEmpty(dangerousOperation.getEndTime()),"report_date",dangerousOperation.getEndTime());
		queryWrapper.groupBy("operation_status");
		List<Map<String,Object>> list = dangerousOperationService.listMaps(queryWrapper);
		result.setSuccess(true);
		result.setResult(list);
		return result;
	}

	@AutoLog(value = "按时间类型统计处理数量")
	@ApiOperation(value="按时间类型统计处理数量", notes="按时间类型统计处理数量")
	@GetMapping(value = "/getDefectHandleCountByEventType")
	public Result<List<Map<String,Object>>> getDefectHandleCountByEventType(DangerousOperation dangerousOperation){
		Result<List<Map<String,Object>>> result = new Result<>();
		//未处理应急事件
		QueryWrapper<DangerousOperation> emergencyRiskEventQueryWrapper = new QueryWrapper<>();//QueryGenerator.initQueryWrapper(equipmentDefectManagement, req.getParameterMap());
		emergencyRiskEventQueryWrapper.select("count(id) wclCount,event_type,(select item_text as event_type_dictText from sys_dict_item where dict_id =(select id from sys_dict where dict_code='emergency_risk_event_eventtype') and item_value=event_type) as event_type_dictText");
		emergencyRiskEventQueryWrapper.eq("operation_status","1");
		emergencyRiskEventQueryWrapper.ne("operation_type","");
		emergencyRiskEventQueryWrapper.isNotNull("operation_type");
		emergencyRiskEventQueryWrapper.ge(StringUtils.isNotEmpty(dangerousOperation.getStartTime()),"report_date",dangerousOperation.getStartTime());
		emergencyRiskEventQueryWrapper.le(StringUtils.isNotEmpty(dangerousOperation.getEndTime()),"report_date",dangerousOperation.getEndTime());
		emergencyRiskEventQueryWrapper.groupBy("operation_status","operation_type");
		List<Map<String,Object>> list = dangerousOperationService.listMaps(emergencyRiskEventQueryWrapper);
		//已处理应急事件
		emergencyRiskEventQueryWrapper = new QueryWrapper<>();//QueryGenerator.initQueryWrapper(equipmentDefectManagement, req.getParameterMap());
		emergencyRiskEventQueryWrapper.select("count(id) yclCount,event_type,(select item_text as event_type_dictText from sys_dict_item where dict_id =(select id from sys_dict where dict_code='emergency_risk_event_eventtype') and item_value=event_type) as event_type_dictText");
		emergencyRiskEventQueryWrapper.eq("operation_status","2");
		emergencyRiskEventQueryWrapper.ne("operation_type","");
		emergencyRiskEventQueryWrapper.isNotNull("operation_type");
		emergencyRiskEventQueryWrapper.ge(StringUtils.isNotEmpty(dangerousOperation.getStartTime()),"report_date",dangerousOperation.getStartTime());
		emergencyRiskEventQueryWrapper.le(StringUtils.isNotEmpty(dangerousOperation.getEndTime()),"report_date",dangerousOperation.getEndTime());
		emergencyRiskEventQueryWrapper.groupBy("operation_status","operation_type");
		List<Map<String,Object>> yclList = dangerousOperationService.listMaps(emergencyRiskEventQueryWrapper);
		//获取未处理、已处理中的应急事件类型

		Map<String, Map<String,Object>> mapping = new HashMap<>();
		list.forEach(map ->{
			String eventType = ConvertUtils.getString(map.get("operation_type"));
			Map<String,Object> eventMap = mapping.get(eventType);
			if(eventMap==null){
				eventMap = new HashMap<>();
				mapping.put(eventType,eventMap);
			}
			eventMap.putAll(map);
		});
		yclList.forEach(map ->{
			String eventType = ConvertUtils.getString(map.get("operation_type"));
			Map<String,Object> eventMap = mapping.get(eventType);
			if(eventMap==null){
				eventMap = new HashMap<>();
				mapping.put(eventType,eventMap);
			}
			eventMap.putAll(map);
		});
		//构造结果集合
		List<Map<String,Object>> resultList = new ArrayList<>();
		Set<String> keySet = mapping.keySet();
		for(String key : keySet){
			resultList.add(mapping.get(key));
		}
		result.setSuccess(true);
		result.setResult(resultList);
		return result;
	}

	@AutoLog(value = "危险作业事件-分页列表查询")
	@ApiOperation(value="危险作业事件-分页列表查询", notes="危险作业事件-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<DangerousOperation>> queryPageList(DangerousOperation dangerousOperation,
														   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
														   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
														   HttpServletRequest req) {
		Result<IPage<DangerousOperation>> result = new Result<IPage<DangerousOperation>>();
		QueryWrapper<DangerousOperation> queryWrapper = QueryGenerator.initQueryWrapper(dangerousOperation, req.getParameterMap());
		Page<DangerousOperation> page = new Page<DangerousOperation>(pageNo, pageSize);
		IPage<DangerousOperation> pageList = dangerousOperationService.page(page, queryWrapper);

		//获取流程状态
        List<DangerousOperation> records = pageList.getRecords();
        Set<String> processInstanceIdSet = records.stream().filter(data-> StringUtils.isNotBlank(data.getProcessInstanceId()))
                .map(DangerousOperation::getProcessInstanceId).collect(Collectors.toSet());
        Map<String, Map<String, String>> processInstanceMap;
        try {
            processInstanceMap = flowBusinessService.getProcessState(processInstanceIdSet);
        } catch (Exception e) {
            e.printStackTrace();
            processInstanceMap = new HashMap<>();
        }
        for (DangerousOperation record : records) {
            record.getFlow().setProcessDefinitionKey(processInstanceMap.getOrDefault(record.getProcessInstanceId(), new HashMap<>()).get("processDefinitionKey"));
            record.getFlow().setTaskDefinitionKey(processInstanceMap.getOrDefault(record.getProcessInstanceId(), new HashMap<>()).get("taskDefinitionKey"));
            record.getFlow().setStatus(processInstanceMap.getOrDefault(record.getProcessInstanceId(), new HashMap<>()).get("state"));
            record.getFlow().setTaskName(processInstanceMap.getOrDefault(record.getProcessInstanceId(), new HashMap<>()).get("processDefinitionName"));
        }

		result.setSuccess(true);
		result.setResult(pageList);
		return result;
	}

	/**

	/**
	 * <pre>
	 * 	添加
	 * </pre>
	 * @param dangerousOperation
	 * @return
	 * @author 开发者姓名, 开发时间
	 * @Description: TODO(这里描述这个方法的需求变更情况)
	 */
	@AutoLog(value = "危险作业事件-开始流程")
	@ApiOperation(value="危险作业事件-开始流程", notes="危险作业事件-开始流程")
	@PostMapping(value = "/start-process")
	public Result<DangerousOperation> startProcess(@RequestBody DangerousOperation dangerousOperation) {
		Result<DangerousOperation> result = new Result<>();
		try {
			dangerousOperation.setDelFlag(0);
			DangerousOperation data = dangerousOperationService.startProcess(dangerousOperation);
			result.setResult(data);
			result.success("添加成功!");
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			result.error500("操作失败");
		}
		return result;
	}
	/**
	 * <pre>
	 * 	 编辑
	 * </pre>
	 * @param dangerousOperation
	 * @return
	 * @author 开发者姓名, 开发时间
	 * @Description: TODO(这里描述这个方法的需求变更情况)
	 */
	@AutoLog(value = "危险作业事件-处理")
	@ApiOperation(value="危险作业事件-处理", notes="危险作业事件-处理")
	@PostMapping(value = "/handle")
	public Result<DangerousOperation> handle(@Valid @RequestBody DangerousOperation dangerousOperation) {
		Result<DangerousOperation> result = new Result<DangerousOperation>();
		dangerousOperationService.handle(dangerousOperation);
		result.success("处理成功!");
		return result;
	}
	/**
	 * <pre>
	 * 	 通过id删除
	 * </pre>
	 * @param id
	 * @return
	 * @author 开发者姓名, 开发时间
	 * @Description: TODO(这里描述这个方法的需求变更情况)
	 */
	@AutoLog(value = "危险作业事件-通过id删除")
	@ApiOperation(value="危险作业事件-通过id删除", notes="危险作业事件-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		try {
			dangerousOperationService.removeById(id);
		} catch (Exception e) {
			log.error("删除失败",e.getMessage());
			return Result.error("删除失败!");
		}
		return Result.ok("删除成功!");
	}

	//指派作业任务给某人
	@PostMapping(value = "/complete-task-choose-user-pass")
	public Result<?> completeTaskChooseUserPass(@RequestBody DangerousOperation dangerousOperation) throws Exception {
		Result<?> result = new Result();
		//流程执行id
		String executionId = dangerousOperation.getFlow().getExecutionId();
		DangerousOperation dangerOperation = BeanExtUtils.bean2Bean(dangerousOperation, DangerousOperation.class);
		dangerousOperationService.updateById(dangerOperation);
		//流程进入下一节点,并行网关设置判定条件
		Map<String, Object> mapVariable = new HashMap<String,Object>();
		mapVariable.put("dangerDeal", "taskUser_"+dangerousOperation.getAppointUser());
		flowService.configureVariables(executionId, mapVariable);
		//动态设置流程接收人
		flowBusinessService.completeTask(ProcessConstant.PASS_KEY, dangerousOperation.getFlow());
		result.success("成功");
		return result;
	}

	@AutoLog(value = "危险作业事件-办结流程")
	@ApiOperation(value = "危险作业事件-办结流程", notes = "危险作业事件-办结流程")
	@PostMapping(value = "/complete-process")
	public Result<DangerousOperation> completeProcess(@RequestBody DangerousOperation dangerousOperation) {
		Result<DangerousOperation> result = new Result<>();
		try {
			dangerousOperation.setDelFlag(0);
			boolean flag = dangerousOperationService.completeProcess(dangerousOperation);
			if (flag) {
				result.success("办结成功!");
			} else {
				result.error500("办结失败!");
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			result.error500("办结失败");
		}
		return result;
	}

	@AutoLog(value = "危险作业事件-人员证书列表")
	@ApiOperation(value="危险作业事件-人员证书列表", notes="危险作业事件-人员证书列表")
	@GetMapping(value = "/getFileList")
	public Result<List<UserCertificate>> getFileList(String userType, String userId) {
		List<UserCertificate> list = new ArrayList<>();
		Result<List<UserCertificate>> result = new Result<List<UserCertificate>>();
		list = dangerousOperationService.getFileList(userType, userId);
		result.setResult(list);
		result.setSuccess(true);
		return result;
	}

}