EquipmentCenterController.java 18.3 KB
package com.skua.modules.equipment.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.skua.common.constant.BusinessConstant;
import com.skua.core.api.vo.Result;
import com.skua.core.aspect.annotation.AutoLog;
import com.skua.core.context.BaseContextHandler;
import com.skua.core.util.ConvertUtils;
import com.skua.modules.equipment.dto.ReportStatisticsDTO;
import com.skua.modules.equipment.service.IEquipmentAnalysisService;
import com.skua.modules.equipment.service.IProductionEquipmentService;
import com.skua.modules.equipment.vo.*;
import com.skua.modules.system.service.ISysDepartService;
import com.skua.tool.util.DateUtils;
import com.skua.tool.util.DigitalUtils;
import com.skua.tool.util.JSUtils;
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.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Api(tags = "设备中心")
@RestController
@RequestMapping("/web/equipment/equipmentCenter")
public class EquipmentCenterController {
    @Autowired
    private IProductionEquipmentService iProductionEquipmentService;
    @Autowired
    private ISysDepartService departService;
    @Autowired
    private IEquipmentAnalysisService equipmentAnalysisService;

    @AutoLog(value = "设备中心-顶部总览")
    @ApiOperation(value = "设备中心-顶部总览", notes = "设备中心-顶部总览")
    @GetMapping(value = "/topOverview")
    public Result<ProductionEquipmentVO> topOverview(ReportStatisticsDTO reportStatisticsDTO) {
        Result<ProductionEquipmentVO> result = new Result<ProductionEquipmentVO>();
        setDepartIds(reportStatisticsDTO);
        ProductionEquipmentVO productionEquipmentVO = iProductionEquipmentService.getProductionEquipmentVO(reportStatisticsDTO);
        result.setResult(productionEquipmentVO);
        return result;
    }

    @AutoLog(value = "设备中心-设备统计")
    @ApiOperation(value = "设备中心-设备统计", notes = "设备中心-设备统计")
    @GetMapping(value = "/equipmentStatistics")
    public Result<FaultStatisticsVO> equipmentStatistics(ReportStatisticsDTO reportStatisticsDTO) {
        Result<FaultStatisticsVO> result = new Result<>();
        setDepartIds(reportStatisticsDTO);
        //取消时间参数
        reportStatisticsDTO.setStartTime(null);
        reportStatisticsDTO.setEndTime(null);
        FaultStatisticsVO equipmentStatistics = iProductionEquipmentService.getEquipmentStatistics(reportStatisticsDTO);
        result.setResult(equipmentStatistics);
        result.setSuccess(true);
        return result;
    }

    @AutoLog(value = "设备中心-设备统计详情")
    @ApiOperation(value = "设备中心-设备统计详情", notes = "设备中心-设备统计详情")
    @GetMapping(value = "/equipmentStatisticsDetails")
    public Result<IPage<EquipmentNewMaintenancePlanVO>> equipmentStatisticsDetails(ReportStatisticsDTO reportStatisticsDTO,
                                                                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Result<IPage<EquipmentNewMaintenancePlanVO>> result = new Result<IPage<EquipmentNewMaintenancePlanVO>>();
        Page<EquipmentNewMaintenancePlanVO> pageList = new Page<EquipmentNewMaintenancePlanVO>(pageNo, pageSize);
        setDepartIds(reportStatisticsDTO);
        //取消时间参数
        reportStatisticsDTO.setStartTime(null);
        reportStatisticsDTO.setEndTime(null);
        pageList = iProductionEquipmentService.getEquipmentStatisticsDetails(pageList, reportStatisticsDTO);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    @AutoLog(value = "设备中心-设备统计详情(新)")
    @ApiOperation(value = "设备中心-设备统计详情(新)", notes = "设备中心-设备统计详情(新)")
    @GetMapping(value = "/equipmentStatisticsDetailsNew")
    public Result<IPage<EquipmentStatisticsCountVO>> equipmentStatisticsDetailsNew(ReportStatisticsDTO reportStatisticsDTO,
                                                                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Result<IPage<EquipmentStatisticsCountVO>> result = new Result<IPage<EquipmentStatisticsCountVO>>();
        Page<EquipmentStatisticsCountVO> page = new Page<EquipmentStatisticsCountVO>(pageNo, pageSize);
        setDepartIds(reportStatisticsDTO);//设置departIds
        // bug11036:暂时先隐藏,不然和设备台账统计不一致
        reportStatisticsDTO.setStartTime(null);
        reportStatisticsDTO.setEndTime(null);
        IPage<EquipmentStatisticsCountVO> pageList =iProductionEquipmentService.getEquipmentStatisticsDetailsNew(page, reportStatisticsDTO);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    @AutoLog(value = "设备中心-设备状态")
    @ApiOperation(value = "设备中心-设备状态", notes = "设备中心-设备状态")
    @GetMapping(value = "/equipmentState")
    public Result<List<TOPTenVO>> equipmentState(ReportStatisticsDTO reportStatisticsDTO) {
        Result<List<TOPTenVO>> result = new Result<>();
        setDepartIds(reportStatisticsDTO);
        //取消时间参数
       // reportStatisticsDTO.setStartTime(null);
     //   reportStatisticsDTO.setEndTime(null);
        List<TOPTenVO> equipmentState = iProductionEquipmentService.getEquipmentState(reportStatisticsDTO);
        result.setSuccess(true);
        result.setResult(equipmentState);
        return result;
    }

    @AutoLog(value = "设备中心-设备状态详情")
    @ApiOperation(value = "设备中心-设备状态详情", notes = "设备中心-设备状态详情")
    @GetMapping(value = "/equipmentStateDetails")
    public Result<IPage<EquipmentNewMaintenancePlanVO>> equipmentStateDetails(ReportStatisticsDTO reportStatisticsDTO,
                                                                              @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                                              @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Result<IPage<EquipmentNewMaintenancePlanVO>> result = new Result<IPage<EquipmentNewMaintenancePlanVO>>();
        Page<EquipmentNewMaintenancePlanVO> pageList = new Page<EquipmentNewMaintenancePlanVO>(pageNo, pageSize);
        setDepartIds(reportStatisticsDTO);
        pageList = iProductionEquipmentService.getEquipmentStateDetailsNew(pageList, reportStatisticsDTO);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    @AutoLog(value = "设备中心-设备完好率")
    @ApiOperation(value = "设备中心-设备完好率", notes = "设备中心-设备完好率")
    @GetMapping(value = "/equipmentIntactRate")
    public Result<List<ContrastVO>> equipmentIntactRate(ReportStatisticsDTO reportStatisticsDTO) {
        Result<List<ContrastVO>> result = new Result<List<ContrastVO>>();
        setDepartIds(reportStatisticsDTO);
        // 当前时间
        Date nowDate = new Date();
        String nowDateStr = DateUtils.date2Str(nowDate, BusinessConstant.dateFormat);
        Date startDate;
        if (reportStatisticsDTO.getTimeUnit() == 6) {
            startDate = DateUtils.someDate(nowDate, Calendar.MONTH, -6);
        } else {
            startDate = DateUtils.prevYear(nowDate);
        }
        String startTime = DateUtils.date2Str(startDate, BusinessConstant.dateFormat).substring(0, 7) + "-01" + BusinessConstant.startTimeSuffix;
        int days = DateUtils.lengthOfSomeMonth(Integer.parseInt(nowDateStr.split("-")[0]), Integer.parseInt(nowDateStr.split("-")[1]));
        String endTime = nowDateStr.substring(0, 7) + "-" + days + BusinessConstant.endTimeSuffix;
        // 同比时间
        String[] tbTimeArray = DateUtils.tbTime(startTime, endTime);
        // departId删除''
        String departId = Arrays.stream(reportStatisticsDTO.getDepartIds().split(",")).map(item -> item.replaceAll("'", "")).collect(Collectors.joining(","));
        List<Map<String, Object>> entityList = equipmentAnalysisService.equipmentInTactRateByTime(new HashMap<String, Object>(){{
            put("startTime", startTime);
            put("endTime", endTime);
            put("timeType", "month");
            put("departId", departId);
        }});
        List<Map<String, Object>> entityListTb = equipmentAnalysisService.equipmentInTactRateByTime(new HashMap<String, Object>(){{
            put("startTime", tbTimeArray[0]);
            put("endTime", tbTimeArray[1]);
            put("timeType", "month");
            put("departId", departId);
        }});
        Map<String,  String> tbTime2ValueMap = entityListTb.stream().collect(Collectors.toMap(item -> ConvertUtils.getString(item.get("time")).substring(0, 7), item -> DigitalUtils.nPoint(ConvertUtils.getDouble(item.get("intactRate"), 0D) * 100, 2) + "%"));
        // 封装结果集
        List<ContrastVO> resMapList = new ArrayList<>();
        for (Map<String, Object> item: entityList) {
            ContrastVO map = new ContrastVO();
            String yearMonth = ConvertUtils.getString(item.get("time")).substring(0, 7);
            map.setMonth(yearMonth);
            // 完好率
            map.setRepairNum(DigitalUtils.nPoint(ConvertUtils.getDouble(item.get("intactRate"), 0D) * 100, 2) + "%");
            // 同比
            String tbYearMonth = (Integer.parseInt(yearMonth.split("-")[0]) - 1) + "-" + yearMonth.split("-")[1];
            map.setMaintainNum(tbTime2ValueMap.get(tbYearMonth));
            resMapList.add(map);
        }
        result.setResult(resMapList);
//         List<ContrastVO> equipmentIntactRate = iProductionEquipmentService.getEquipmentIntactRate(reportStatisticsDTO);
        result.setSuccess(true);
        // result.setResult(equipmentIntactRate);
        return result;
    }

    @AutoLog(value = "设备中心-设备完好率详情")
    @ApiOperation(value = "设备中心-设备完好率详情", notes = "设备中心-设备完好率详情")
    @GetMapping(value = "/equipmentIntactRateDetails")
    public Result<List<Map>> equipmentIntactRateDetails(ReportStatisticsDTO reportStatisticsDTO) {
        Result<List<Map>> result = new Result<List<Map>>();
        setDepartIds(reportStatisticsDTO);
        List<Map> equipmentIntactRateDetails = iProductionEquipmentService.getEquipmentIntactRateDetails(reportStatisticsDTO);
        result.setResult(equipmentIntactRateDetails);
        result.setSuccess(true);
        return result;
    }

    @AutoLog(value = "设备中心-设备完好率详情(新)")
    @ApiOperation(value = "设备中心-设备完好率详情(新)", notes = "设备中心-设备完好率详情(新)")
    @GetMapping(value = "/equipmentIntactRateDetailsNew")
    public Result<List<Map>> equipmentIntactRateDetailsNew(ReportStatisticsDTO reportStatisticsDTO) {
        Result<List<Map>> result = new Result<List<Map>>();
        setDepartIds(reportStatisticsDTO);
        // List<Map> equipmentIntactRateDetails = iProductionEquipmentService.getEquipmentIntactRateDetailsNew(reportStatisticsDTO);
        // result.setResult(equipmentIntactRateDetails);
        // 请求参数
        String startTime = reportStatisticsDTO.getStartTime();
        String endTime = reportStatisticsDTO.getEndTime();
        // endTime修改到本月月底
        int  days = DateUtils.lengthOfSomeMonth(Integer.parseInt(endTime.split("-")[0]), Integer.parseInt(endTime.split("-")[1]));
        endTime = endTime.substring(0, 7) + "-" + days + BusinessConstant.endTimeSuffix;
        String finalEndTime = endTime;
        // 同比时间
        String[] tbTimeArray = DateUtils.tbTime(startTime, endTime);
        // departId删除''
        String departId = Arrays.stream(reportStatisticsDTO.getDepartIds().split(",")).map(item -> item.replaceAll("'", "")).collect(Collectors.joining(","));
        List<Map<String, Object>> entityList = equipmentAnalysisService.equipmentInTactRateByDepart(new HashMap<String,  Object>(){{
            put("startTime", startTime);
            put("endTime", finalEndTime);
            put("departId", departId);
        }});
        List<Map<String, Object>> entityListTb = equipmentAnalysisService.equipmentInTactRateByDepart(new HashMap<String,  Object>(){{
            put("startTime", tbTimeArray[0]);
            put("endTime", tbTimeArray[1]);
            put("departId", departId);
        }});
        Map<String, String> tbDepartId2intactRateMap = entityListTb.stream().collect(Collectors.toMap(item -> ConvertUtils.getString(item.get("departId")), item -> DigitalUtils.nPoint(ConvertUtils.getDouble(item.get("intactRate"), 0D) * 100, 2)));
        // 封装结果集
        List<Map> resMapList = new ArrayList<>();
        for (Map<String, Object> item : entityList) {
            Map<String, Object> map = new HashMap<>();
            map.put("depart_id", item.get("departId"));
            map.put("depart_name", item.get("departName"));
            map.put("intact", DigitalUtils.nPoint(ConvertUtils.getDouble(item.get("intactRate"), 0) * 100, 2));
            map.put("percentage", tbDepartId2intactRateMap.get(ConvertUtils.getString(item.get("departId"))));
            resMapList.add(map);
        }
        result.setSuccess(true);
        result.setResult(resMapList);
        return result;
    }
    @AutoLog(value = "设备中心-MTTR&MTBF")
    @ApiOperation(value = "设备中心-MTTR&MTBF", notes = "设备中心-MTTR&MTBF")
    @GetMapping(value = "/equipmentMF")
    public Result<List<ContrastVO>> equipmentMF(ReportStatisticsDTO reportStatisticsDTO) {
        Result<List<ContrastVO>> result = new Result<List<ContrastVO>>();
        setDepartIds(reportStatisticsDTO);
        List<ContrastVO> equipmentMF = iProductionEquipmentService.getEquipmentMF(reportStatisticsDTO);
        result.setResult(equipmentMF);
        result.setSuccess(true);
        return result;
    }

    @AutoLog(value = "设备中心-MTTR&MTBF详情")
    @ApiOperation(value = "设备中心-MTTR&MTBF详情", notes = "设备中心-MTTR&MTBF详情")
    @GetMapping(value = "/equipmentMFDetails")
    public Result<List<ContrastDetailVO>> equipmentMFDetails(ReportStatisticsDTO reportStatisticsDTO) {
        Result<List<ContrastDetailVO>> result = new Result<List<ContrastDetailVO>>();
        setDepartIds(reportStatisticsDTO);
        List<ContrastDetailVO> equipmentMFDetails = iProductionEquipmentService.getEquipmentMFDetailsNew(reportStatisticsDTO.getDepartIds(),reportStatisticsDTO.getStartTime(),reportStatisticsDTO.getEndTime());
        result.setResult(equipmentMFDetails);
        result.setSuccess(true);
        return result;
    }

    @AutoLog(value = "设备中心-维护完成率趋势")
    @ApiOperation(value = "设备中心-维护完成率趋势", notes = "设备中心-维护完成率趋势")
    @GetMapping(value = "/maintainCompleteRate")
    public Result<List<ContrastVO>> maintainCompleteRate(ReportStatisticsDTO reportStatisticsDTO) {
        Result<List<ContrastVO>> result = new Result<List<ContrastVO>>();
        setDepartIds(reportStatisticsDTO);
        List<ContrastVO> maintainCompleteRate = iProductionEquipmentService.getMaintainCompleteRate(reportStatisticsDTO);
        result.setResult(maintainCompleteRate);
        result.setSuccess(true);
        return result;
    }

    @AutoLog(value = "设备中心-维护完成率趋势详情")
    @ApiOperation(value = "设备中心-维护完成率趋势详情", notes = "设备中心-维护完成率趋势详情")
    @GetMapping(value = "/maintainCompleteRateDetails")
    public Result<List<Map>> maintainCompleteRateDetails(ReportStatisticsDTO reportStatisticsDTO) {
        Result<List<Map>> result = new Result<>();
        setDepartIds(reportStatisticsDTO);
        List<Map> maintainCompleteRateDetails = iProductionEquipmentService.getMaintainCompleteRateDetailsNew(reportStatisticsDTO);
        result.setResult(maintainCompleteRateDetails);
        result.setSuccess(true);
        return result;
    }

    @AutoLog(value = "设备中心-维护维修费用")
    @ApiOperation(value = "设备中心-维护维修费用", notes = "设备中心-维护维修费用")
    @GetMapping(value = "/maintainRepairCost")
    public Result<List<ContrastVO>> maintainRepairCost(ReportStatisticsDTO reportStatisticsDTO) {
        Result<List<ContrastVO>> result = new Result<List<ContrastVO>>();
        setDepartIds(reportStatisticsDTO);
        List<ContrastVO> maintainCompleteRate = iProductionEquipmentService.getContrastCost(reportStatisticsDTO);
        result.setResult(maintainCompleteRate);
        result.setSuccess(true);
        return result;
    }

    @AutoLog(value = "设备中心-维护维修费用详情")
    @ApiOperation(value = "设备中心-维护维修费用详情", notes = "设备中心-维护维修费用详情")
    @GetMapping(value = "/maintainRepairCostDetails")
    public Result<List<Map>> maintainRepairCostDetails(ReportStatisticsDTO reportStatisticsDTO) {
        Result<List<Map>> result = new Result<>();
        setDepartIds(reportStatisticsDTO);
        List<Map> contrastCostDetails = iProductionEquipmentService.getContrastCostDetails(reportStatisticsDTO);
        result.setResult(contrastCostDetails);
        result.setSuccess(true);
        return result;
    }

    /***
     * 设置查询条件的departIds属性
     * @param reportStatisticsDTO
     */
    private void setDepartIds(ReportStatisticsDTO reportStatisticsDTO){
        String departIds = null;
        if (StringUtils.isNotEmpty(reportStatisticsDTO.getFactoryId())) {
            departIds = departService.getChildDepartId(reportStatisticsDTO.getFactoryId());
        }else{
            departIds = BaseContextHandler.getDeparts();
        }
        reportStatisticsDTO.setDepartIds(JSUtils.quoteEach(departIds,","));
    }
}