EmergencyAnalysisController.java 10.5 KB
package com.skua.modules.emergency.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.skua.core.api.vo.Result;
import com.skua.core.aspect.annotation.AutoLog;
import com.skua.modules.emergency.entity.*;
import com.skua.modules.emergency.service.*;
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.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 应急统计
 */
@Slf4j
@Api(tags = "应急模块统计分析")
@RestController
@RequestMapping("/emergency/analysis")
public class EmergencyAnalysisController {
    @Autowired
    private IEmergencyCartsService emergencyCartsService;
    @Autowired
    private IEmergencyGoodsService goodsService;
    @Autowired
    private IEmergencyMobileDangerReportService mobileDangerReportService;

    @Autowired
    private IEmergencyEventsCostService eventsCostService;
    @Autowired
    private IEmergencyEventsService emergencyEventsService;

    @Autowired
    private IEmergencyEventsGoodsService emergencyEventsGoodsService;
    @Autowired
    private IEmergencyEventsCartService emergencyEventsCartService;
    @Autowired
    private FlowBusinessService flowBusinessService;

    /**
     * @param req
     * @return
     */
    @AutoLog(value = "应急-移动险情数量统计")
    @ApiOperation(value = "应急-移动险情数量统计", notes = "应急-移动险情数量统计")
    @GetMapping(value = "/mobileDangerCount")
    public Result mobileDangerCount(
            HttpServletRequest req) {
        Result result = new Result<>();
        try {
            List<Map<String, Object>> resultMap = mobileDangerReportService.mobileDangerCount();
            result.setSuccess(true);
            result.setResult(resultMap);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage(), e);
            result.error500("查询失败");
        }
        return result;
    }


    /**
     * @param req
     * @return
     */
    @AutoLog(value = "应急-移动险情等级统计")
    @ApiOperation(value = "应急-移动险情等级统计", notes = "应急-移动险情等级统计")
    @GetMapping(value = "/mobileDangerLevelCount")
    public Result mobileDangerLevelCount(
            HttpServletRequest req) {
        Result result = new Result<>();
        try {
            List<Map<String, Object>> resultMap = mobileDangerReportService.mobileDangerLevelCount();
            result.setSuccess(true);
            result.setResult(resultMap);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage(), e);
            result.error500("查询失败");
        }
        return result;
    }


    /**
     * 应急资源统计
     * @param departId
     * @return
     */
    @AutoLog(value = "应急-应急资源统计")
    @ApiOperation(value = "应急-应急资源统计", notes = "应急-应急资源统计")
    @GetMapping(value = "/resource")
    public Result resource(String moduleType,String departId) {
        Result result = new Result<>();
        try {
            ArrayList<Object> resultList = new ArrayList<>();
            QueryWrapper<EmergencyGoods> emergencyGoodsQueryWrapper = new QueryWrapper<EmergencyGoods>()
                    .select("id,goods_type as goodsType,goods_name as goodsName,goods_unit as goodsUnit,sum(goods_num)as totalGoods")
                    .eq(StringUtils.isNotEmpty(moduleType),"module_type", moduleType)
                    .eq(StringUtils.isNotEmpty(departId),"depart_id", departId)
                    .eq("del_flag", 1)
                    .groupBy("goods_name");
            List<Map<String, Object>> totalGoodsList = goodsService.listMaps(emergencyGoodsQueryWrapper);
            QueryWrapper<EmergencyCarts> emergencyCartsQueryWrapper = new QueryWrapper<EmergencyCarts>().eq("del_flag", 1);
            int totalCarts = emergencyCartsService.count(emergencyCartsQueryWrapper);
            for (Map<String, Object> goodsMap : totalGoodsList) {
                Double inUseNum = 0.0;
                goodsMap.put("inUseNum", inUseNum);
                goodsMap.put("freeNum", Double.parseDouble(String.valueOf(goodsMap.get("totalGoods"))) - inUseNum>0?Double.parseDouble(String.valueOf(goodsMap.get("totalGoods"))) - inUseNum:0);
            }
            int useCartNum = 0;
            HashMap<String, Object> map = new HashMap<>();
            map.put("goodsName", "应急车辆");
            map.put("goodsUnit", "辆");
            map.put("goodsType", "应急车辆");
            map.put("totalGoods", totalCarts);
            map.put("inUseNum", useCartNum);
            map.put("freeNum", totalCarts - useCartNum>0?totalCarts - useCartNum:0);
            resultList.addAll(totalGoodsList);
            resultList.add(map);
            result.setSuccess(true);
            result.setResult(resultList);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage(), e);
            result.error500("查询失败");
        }
        return result;
    }


    /**
     * @param req
     * @return
     */
    @AutoLog(value = "应急-年度应急事件统计")
    @ApiOperation(value = "应急-年度应急事件统计", notes = "应急-年度应急事件统计")
    @GetMapping(value = "/incident")
    public Result incident(
            HttpServletRequest req, String moduleType) {
        Result result = new Result<>();
        try {
            //ArrayList<Object> resultList = new ArrayList<>();
            //String moduleType = req.getParameter("moduleType");
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy");
            String thisYear = LocalDateTime.now().format(dateTimeFormatter);
            HashMap<String, Object> map = new HashMap<>();
            QueryWrapper<EmergencyEvents> emergencyEventsQueryWrapper = new QueryWrapper<>();
            emergencyEventsQueryWrapper.eq("module_type", moduleType).eq("del_flag", 1).isNotNull("process_instance_id").like("create_time", thisYear);
            List<EmergencyEvents> eventsList = emergencyEventsService.list(emergencyEventsQueryWrapper);
            //待处理
            List<EmergencyEvents> processEventsList = eventsList.stream().filter(events -> StringUtils.isNotBlank(events.getProcessInstanceId())).collect(Collectors.toList());
            Set<String> processInstanceIdS = processEventsList.stream().map(events -> events.getProcessInstanceId()).collect(Collectors.toSet());
            Map<String,Map<String, String>> processStateMap = flowBusinessService.getProcessState(processInstanceIdS);
            for (EmergencyEvents events : eventsList) {
                events.setProcessState(processStateMap.get(events.getProcessInstanceId()).getOrDefault("state", "pending"));
            }
            int processed = 0;
            int inHand = 0;
            int pending = 0;
            for (EmergencyEvents emergencyEvents : eventsList) {
                if ("finished".equals(emergencyEvents.getProcessState())) {
                    processed++;
                } else if ("unfinished".equals(emergencyEvents.getProcessState())) {
                    inHand++;
                } else {
                    pending++;
                }
            }
            map.put("total", !CollectionUtils.isEmpty(eventsList) ? eventsList.size() : 0);
            //已处理
            map.put("processed", processed);
            //处理中
            map.put("inHand", inHand);
            //待处理
            map.put("pending", pending);
            // resultList.add(map);
            result.setSuccess(true);
            result.setResult(map);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage(), e);
            result.error500("查询失败");
        }
        return result;
    }

    /**
     * @param req
     * @return
     */
    @AutoLog(value = "应急-应急费用统计")
    @ApiOperation(value = "应急-应急费用统计", notes = "应急-应急费用统计")
    @GetMapping(value = "/cost")
    public Result cost(
            HttpServletRequest req, String time) {
        Result result = new Result<>();
        try {
            //ArrayList<Object> resultList = new ArrayList<>();
            DateTimeFormatter yearFormatter = DateTimeFormatter.ofPattern("yyyy");
            String thisYear = LocalDateTime.now().format(yearFormatter);
            String lastYear = LocalDateTime.now().minusYears(1).format(yearFormatter);
            QueryWrapper<EmergencyEventsCost> eventsCostQueryWrapper = new QueryWrapper<>();
            DateTimeFormatter monthFormatter = DateTimeFormatter.ofPattern("yyyy-MM");
            String lastMonth = LocalDateTime.now().minusMonths(1).format(monthFormatter);
            String lastYearMonth = LocalDateTime.now().minusYears(1).format(monthFormatter);

            Map<String, Object> thisYeardata = eventsCostService.costByYear(thisYear);
            Map<String, Object> lastYeardata = eventsCostService.costByYear(lastYear);
            Map<String, Object> lastMonthdata = eventsCostService.costByMonth(lastMonth);
            Map<String, Object> lastYearMonthdata = eventsCostService.costByMonth(lastYearMonth);
            HashMap<String, Object> map = new HashMap<>();
            map.put("lastMonth", ObjectUtils.isEmpty(lastMonthdata)?0:lastMonthdata.getOrDefault("cost", ""));
            //去年同期
            map.put("samePeriod", ObjectUtils.isEmpty(lastYearMonthdata)?0:lastYearMonthdata.getOrDefault("cost", ""));
            //今年
            map.put("thisYear", ObjectUtils.isEmpty(thisYeardata)?0:thisYeardata.getOrDefault("cost", ""));
            //去年
            map.put("lastYear", ObjectUtils.isEmpty(lastYeardata)?0:lastYeardata.getOrDefault("cost", ""));
            // resultList.add(map);
            result.setSuccess(true);
            result.setResult(map);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage(), e);
            result.error500("查询失败");
        }
        return result;
    }

}