TestMetricInfoController.java 6.8 KB
package com.skua.modules.erp.controller;

import cn.hutool.core.util.RandomUtil;
import com.skua.common.util.ChineseInital;
import com.skua.core.api.vo.Result;
import com.skua.core.aspect.annotation.AutoLog;
import com.skua.core.context.SpringContextUtils;
import com.skua.core.service.IFactoryInfoService;
import com.skua.core.util.ConvertUtils;
import com.skua.core.util.DateUtils;
import com.skua.modules.equipment.vo.EquipmentRealTimeVO;
import com.skua.modules.erp.vo.MetricInfoVO;
import com.skua.modules.erp.vo.RealTimeHistoryDataVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
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.*;

@Api(tags="设备-设备运行台时报表")
@RestController
@RequestMapping("/test/metricInfo")
public class TestMetricInfoController {

    @Autowired
    private IFactoryInfoService iFactoryInfoService;


    @AutoLog(value = "设备-设备运行台时报表")
    @ApiOperation(value = "设备-设备运行台时报表", notes = "设备-设备运行台时报表")
    @GetMapping(value = "/realTimeHistoryData")
    public Result<RealTimeHistoryDataVO> equipRealTimeHistoryDataCtrl(@RequestParam(value = "departId") String departId,
                                                                      @RequestParam(value = "startTime") String startTime,
                                                                      @RequestParam(value = "endTime") String endTime) {
        Result<RealTimeHistoryDataVO> result = new Result<>();
        RealTimeHistoryDataVO realTimeHistoryDataVO  = new  RealTimeHistoryDataVO();
        //String fields = "TCSWSCLC_PLC300_Tag182,TCSWSCLC_PLC300_Tag185";
        JdbcTemplate masterDB = (JdbcTemplate)SpringContextUtils.getBean("master");
        String metricInfoSql = "select   ei.equipment_name ,  t.metric_express1 'gzExpress' , t.metric_express2 'yxExpress' ,t.metric_express3   'tzExpress' from sys_monitor_metric_info t ";
        metricInfoSql += " left join equipment_info ei on ei.id =  t.equipment_code ";
        metricInfoSql   += "  where t.metric_type  = 1 and  t.depart_id = '"+departId+"'  and t.equipment_code > 0  " ;
        List<MetricInfoVO> metricInfoVOList = masterDB.query(metricInfoSql,new BeanPropertyRowMapper<MetricInfoVO>(MetricInfoVO.class));

        //StringBuffer fields= new StringBuffer();
        String fields = "";
        if(metricInfoVOList != null ){
            for(MetricInfoVO metricInfoVO : metricInfoVOList){
                String equipmentName = metricInfoVO.getEquipmentName().replaceAll("[^\\u4e00-\\u9fa5]", "");
                String  equipmentCode  = ChineseInital.getAllFirstLetter(equipmentName)+"_"+ RandomUtil.randomNumbers(2);
                metricInfoVO.setEquipmentName(equipmentName );
                metricInfoVO.setEquipmentCode(equipmentCode );
                if(StringUtils.isNotEmpty(metricInfoVO.getGzExpress())){
                    fields += metricInfoVO.getGzExpress()+",";
                }
                if(StringUtils.isNotEmpty(metricInfoVO.getYxExpress())){
                    fields += metricInfoVO.getYxExpress()+",";
                }
                if(StringUtils.isNotEmpty(metricInfoVO.getTzExpress())){
                    fields += metricInfoVO.getTzExpress()+",";
                }
            }
        }

        if (fields.endsWith(",")) {
            fields = fields.substring(0, fields.length() - 1);
        }
        List<Map<String, Object>> pgDataList = iFactoryInfoService.queryFactoryInfos(departId, fields, startTime, endTime, "1");

        List<Map<String, Object>>  dataList = null;
        if(pgDataList != null ) {
            dataList = new ArrayList<>(pgDataList.size());
            Map<String, Object>  dataMap = null;
            //elect ts as time,nm as monitorId,v as monitorValue
            String equipmentCode = null;
            for (Map<String, Object> paramMap : pgDataList) {
                dataMap = new HashMap<>();
                dataMap.put("time",DateUtils.formatTime(Long.parseLong(paramMap.get("time") + "000")) );
                for(MetricInfoVO metricInfoVO : metricInfoVOList){
                    dataMap.put(  metricInfoVO.getEquipmentCode()+"_YX" , ConvertUtils.getString(paramMap.get(metricInfoVO.getYxExpress())));//运行
                    dataMap.put(  metricInfoVO.getEquipmentCode()+"_GZ" , ConvertUtils.getString(paramMap.get(metricInfoVO.getGzExpress())));//故障
                    dataMap.put(  metricInfoVO.getEquipmentCode()+"_ZT" ,60 - ConvertUtils.getDouble(paramMap.get(metricInfoVO.getYxExpress()),0) - ConvertUtils.getDouble(paramMap.get(metricInfoVO.getGzExpress()) ,0) );//暂停
                }

                dataList.add( dataMap );
            }
        }
       /* //拼装数据
        Map<String, Map<String,String>> linkedHashMap = new LinkedHashMap<String, Map<String,String>>();//返回数据结果
        String timeTemp  = null;
        Map<String,String> dataMap = null;
        if(pgDataList != null ){
            //elect ts as time,nm as monitorId,v as monitorValue
            for( Map<String, Object>   paramMap  : pgDataList){
                timeTemp = DateUtils.formatTime(Long.parseLong(paramMap.get("time") + "000"));
                dataMap = linkedHashMap.get(timeTemp);
                if(dataMap == null ){
                    dataMap = new HashMap<>();
                    dataMap.put("time",timeTemp);
                    for (Map.Entry<String, String> entry : nameMap.entrySet()) {
                        dataMap.put(entry.getKey(),"0");
                    }
                }
                // 使用For-Each迭代entries,通过Map.entrySet遍历key和value
                for (Map.Entry<String, String> entry : nameMap.entrySet()) {
                    System.out.println("entry.getKey()" +entry.getKey()+ "****"+"paramMap.get() = "+paramMap.get(entry.getKey()));
                    if(entry.getKey().equals( ConvertUtils.getString(paramMap.get(entry.getKey())) )){
                        dataMap.put(  entry.getValue() , ConvertUtils.getString(paramMap.get(entry.getKey())));
                    }
                }
                linkedHashMap.put(timeTemp,dataMap);
            }
        }*/

        realTimeHistoryDataVO.setHeadList( metricInfoVOList);
        realTimeHistoryDataVO.setDataList( dataList );
        result.setResult( realTimeHistoryDataVO);
        return result;
    }

}