EmergencyRiskEventController.java 10.5 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.dto.EmergencyRiskEventAddDTO;
import com.skua.modules.emergency.dto.EmergencyRiskEventHandleDto;
import com.skua.modules.emergency.entity.EmergencyRiskEvent;
import com.skua.modules.emergency.service.IEmergencyRiskEventService;
import com.skua.modules.flow.business.service.FlowBusinessService;
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;

/**
 * <pre>
 * 应急风险事件
 * </pre>
 * @author 开发者姓名
 * @version V0.1, 开发时间
 */
@Slf4j
@Api(tags="应急风险事件")
@RestController("webEmergencyRiskEventController")
@RequestMapping("/web/emergency/emergencyRiskEvent")
public class EmergencyRiskEventController {
	@Autowired
	private IEmergencyRiskEventService emergencyRiskEventService;

    @Autowired
    private FlowBusinessService flowBusinessService;
	/**
	 * <pre>
	 * 	查询处理数量
	 * </pre>
	 * @param equipmentDefectManagement
	 * @param req
	 * @return
	 * @author xiechao, 开发时间
	 * @Description: TODO(这里描述这个方法的需求变更情况)
	 */

	@AutoLog(value = "处理数量统计")
	@ApiOperation(value="处理数量统计", notes="处理数量统计")
	@GetMapping(value = "/getDefectHandleCount")
	public Result<List<Map<String,Object>>> getDefectHandleCount(EmergencyRiskEvent equipmentDefectManagement, HttpServletRequest req){
		Result<List<Map<String,Object>>> result = new Result<>();
		QueryWrapper<EmergencyRiskEvent> queryWrapper = new QueryWrapper<EmergencyRiskEvent>();//QueryGenerator.initQueryWrapper(equipmentDefectManagement, req.getParameterMap());
		queryWrapper.select("count(id) count,handle_result handleResult");
		queryWrapper.ge(StringUtils.isNotEmpty(equipmentDefectManagement.getStartTime()),"report_date",equipmentDefectManagement.getStartTime());
		queryWrapper.le(StringUtils.isNotEmpty(equipmentDefectManagement.getEndTime()),"report_date",equipmentDefectManagement.getEndTime());
		queryWrapper.groupBy("handle_result");
		List<Map<String,Object>> list = emergencyRiskEventService.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(EmergencyRiskEvent equipmentDefectManagement, HttpServletRequest req){
		Result<List<Map<String,Object>>> result = new Result<>();
		//未处理应急事件
		QueryWrapper<EmergencyRiskEvent> 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("handle_result","1");
		emergencyRiskEventQueryWrapper.ne("event_type","");
		emergencyRiskEventQueryWrapper.isNotNull("event_type");
		emergencyRiskEventQueryWrapper.ge(StringUtils.isNotEmpty(equipmentDefectManagement.getStartTime()),"report_date",equipmentDefectManagement.getStartTime());
		emergencyRiskEventQueryWrapper.le(StringUtils.isNotEmpty(equipmentDefectManagement.getEndTime()),"report_date",equipmentDefectManagement.getEndTime());
		emergencyRiskEventQueryWrapper.groupBy("handle_result","event_type");
		List<Map<String,Object>> list = emergencyRiskEventService.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("handle_result","3");
		emergencyRiskEventQueryWrapper.ne("event_type","");
		emergencyRiskEventQueryWrapper.isNotNull("event_type");
		emergencyRiskEventQueryWrapper.ge(StringUtils.isNotEmpty(equipmentDefectManagement.getStartTime()),"report_date",equipmentDefectManagement.getStartTime());
		emergencyRiskEventQueryWrapper.le(StringUtils.isNotEmpty(equipmentDefectManagement.getEndTime()),"report_date",equipmentDefectManagement.getEndTime());
		emergencyRiskEventQueryWrapper.groupBy("handle_result","event_type");
		List<Map<String,Object>> yclList = emergencyRiskEventService.listMaps(emergencyRiskEventQueryWrapper);
		//获取未处理、已处理中的应急事件类型

		Map<String, Map<String,Object>> mapping = new HashMap<>();
		list.forEach(map ->{
			String eventType = ConvertUtils.getString(map.get("event_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("event_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<EmergencyRiskEvent>> queryPageList(EmergencyRiskEvent emergencyRiskEvent,
														   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
														   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
														   HttpServletRequest req) {
		Result<IPage<EmergencyRiskEvent>> result = new Result<IPage<EmergencyRiskEvent>>();
		QueryWrapper<EmergencyRiskEvent> queryWrapper = QueryGenerator.initQueryWrapper(emergencyRiskEvent, req.getParameterMap());
		Page<EmergencyRiskEvent> page = new Page<EmergencyRiskEvent>(pageNo, pageSize);
		IPage<EmergencyRiskEvent> pageList = emergencyRiskEventService.page(page, queryWrapper);

		//获取流程状态
        List<EmergencyRiskEvent> records = pageList.getRecords();
        Set<String> processInstanceIdSet = records.stream().filter(data-> StringUtils.isNotBlank(data.getProcessInstanceId()))
                .map(EmergencyRiskEvent::getProcessInstanceId).collect(Collectors.toSet());
        Map<String, Map<String, String>> processInstanceMap;
        try {
            processInstanceMap = flowBusinessService.getProcessState(processInstanceIdSet);
        } catch (Exception e) {
            e.printStackTrace();
            processInstanceMap = new HashMap<>();
        }
        for (EmergencyRiskEvent 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 emergencyRiskEvent
	 * @return
	 * @author 开发者姓名, 开发时间
	 * @Description: TODO(这里描述这个方法的需求变更情况)
	 */
	@AutoLog(value = "应急风险事件-开始流程")
	@ApiOperation(value="应急风险事件-开始流程", notes="应急风险事件-开始流程")
	@PostMapping(value = "/startProcess")
	public Result<EmergencyRiskEvent> startProcess(@Valid @RequestBody EmergencyRiskEventAddDTO emergencyRiskEvent) {
		Result<EmergencyRiskEvent> result = new Result<EmergencyRiskEvent>();
		try {
			emergencyRiskEventService.startProcess(emergencyRiskEvent);
			result.success("添加成功!");
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			result.error500("操作失败");
		}
		return result;
	}
	/**
	 * <pre>
	 * 	 编辑
	 * </pre>
	 * @param emergencyRiskEvent
	 * @return
	 * @author 开发者姓名, 开发时间
	 * @Description: TODO(这里描述这个方法的需求变更情况)
	 */
	@AutoLog(value = "应急风险事件-处理")
	@ApiOperation(value="应急风险事件-处理", notes="应急风险事件-处理")
	@PostMapping(value = "/handle")
	public Result<EmergencyRiskEvent> handle(@Valid @RequestBody EmergencyRiskEventHandleDto dto) {
		Result<EmergencyRiskEvent> result = new Result<EmergencyRiskEvent>();
		emergencyRiskEventService.handle(dto);
		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 {
			emergencyRiskEventService.removeById(id);
		} catch (Exception e) {
			log.error("删除失败",e.getMessage());
			return Result.error("删除失败!");
		}
		return Result.ok("删除成功!");
	}


}