DigitalEquipmentController.java 6.5 KB
package com.skua.modules.digitaltwins;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.skua.core.api.vo.Result;
import com.skua.core.api.vo.TransTreeModel;
import com.skua.core.api.vo.TreeData;
import com.skua.core.context.SpringContextUtils;
import com.skua.core.util.ConvertUtils;
import com.skua.core.util.TreeUtils;
import com.skua.modules.equipment.entity.EquipmentInfo;
import com.skua.modules.equipment.service.IEquipmentInfoService;
import com.skua.modules.system.datestandard.entity.SysMonitorMetricInfo;
import com.skua.modules.system.datestandard.service.ISysMonitorMetricInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <pre>
 * 	设备中心
 * </pre>
 * @author Li Yuanyuan
 * @version V0.1, 2024年8月5日 下午5:02:16
 */
@Slf4j
@Api(tags="/数字孪生/设备中心")
@RestController
@RequestMapping("/v1/digital/equipmentAnalysis")
public class DigitalEquipmentController {
	@Autowired
    private IEquipmentInfoService equipmentInfoService;
	@Autowired
    private ISysMonitorMetricInfoService sysMonitorMetricInfoService;
   /**
    * 智能报警按构筑物统计
    * @return
    * @throws ParseException
    * @author Li Yuanyuan, 2024年8月6日 下午2:54:53
    * @Description: TODO(这里描述这个方法的需求变更情况)
    */
   @ApiOperation(value="构筑物设备树", notes="构造构筑物和设备分组树")
   @GetMapping(value = "/getEquipmentTree")
   public Result<List<TreeData>> getEquipmentTree() throws ParseException{
	   Result<List<TreeData>> result = new Result<List<TreeData>>();
	   JdbcTemplate masterDB = (JdbcTemplate) SpringContextUtils.getBean("master");
	   //使用sql统计
	   String sql = "SELECT id,struct_name AS title,'root' AS pid,'struct' as tree_type FROM sys_struct_dict \r\n" +
	   		"UNION ALL\r\n" +
	   		"SELECT id,equipment_name,structures AS pid,'equipment' as tree_type FROM equipment_info where digital_twins_struct_code!='' and digital_twins_struct_code is not null";
	   List<Map<String,Object>> dataList = masterDB.queryForList(sql);
	   List<TransTreeModel> transTreeModels = dataList.stream().map(bo -> convertToTreeModel(bo)).collect(Collectors.toList());
	   List<TreeData> treeList=TreeUtils.buildTreeForParentId(transTreeModels, "root");
	   result.setResult(treeList);
	   result.setSuccess(true);
	   return result;
   }
   /**
	 * 将SysDcodeConfig对象数据填充到TransTreeModel中
	 * @param dataMap
	 * @return
	 */
	private TransTreeModel convertToTreeModel(Map<String,Object> dataMap) {
		TransTreeModel transTreeModel = new TransTreeModel();
		transTreeModel.setId(ConvertUtils.getString(dataMap.get("id")));
		transTreeModel.setPid(ConvertUtils.getString(dataMap.get("pid")));
		transTreeModel.setTitle(ConvertUtils.getString(dataMap.get("title")));
		Map<String,String> map=new HashMap<String,String>();
		map.put("treeType", ConvertUtils.getString(dataMap.get("tree_type")));
		transTreeModel.setMap(map);
		return transTreeModel;
	}

	/**
	 * 根据传入的构筑物id或设备id获取相关设备运行状态
	 * @param treeType
	 * @param ids
	 * @return
	 */
	@ApiOperation(value = "设备-设备运行状态", notes = "设备-设备运行状态,根据构筑物id/设备id获取设备状态【0停止/1运行/2故障】")
    @GetMapping(value = "/queryEquipmentStatusById")
    public Result<Map<String,Object>> queryEquipmentStatusById(@RequestParam(value = "treeType") String treeType,@RequestParam(value = "ids") String ids) {
    	Result<Map<String,Object>> result = new Result<>();
    	//结果接收对象
    	Map<String,Object> resultMap = new HashMap<String,Object>();
    	List<EquipmentInfo> equipmentInfoList = new ArrayList<EquipmentInfo>();
    	//判断参数类型,如果是构筑物类型,则先获取设备id集合,如果是设备类型,则直接使用
    	if("struct".equals(treeType)) {
    		QueryWrapper<EquipmentInfo> equipmentQueryWrapper = new QueryWrapper<>();
    		equipmentQueryWrapper.isNotNull("digital_twins_struct_code");
    		equipmentQueryWrapper.ne("digital_twins_struct_code", "");
        	equipmentInfoList = equipmentInfoService.list(equipmentQueryWrapper);
    	}else {
    		List<String> idList = Arrays.asList(ids.split(","));
        	QueryWrapper<EquipmentInfo> equipmentQueryWrapper = new QueryWrapper<>();
        	equipmentQueryWrapper.in("id", idList);
        	equipmentInfoList = equipmentInfoService.list(equipmentQueryWrapper);
    	}
    	//获取已关联设备的运行状态点
    	List<String> equipmentIdList = new ArrayList<>();
    	for(EquipmentInfo equipmentInfo : equipmentInfoList) {
    		equipmentIdList.add(equipmentInfo.getId());
    	}
    	//根据设备id集合获取设备运行点位
    	QueryWrapper<SysMonitorMetricInfo> queryWrapper = new QueryWrapper<>();
    	queryWrapper.eq("metric_type", "1");
    	queryWrapper.ne("metric_express1", "");
    	queryWrapper.isNotNull("metric_express1");
    	queryWrapper.in("equipment_code", equipmentIdList);
    	List<SysMonitorMetricInfo> sysMonitorMetricInfoList = sysMonitorMetricInfoService.list(queryWrapper);

    	List<String> equipmentRunPointList = new ArrayList<>();
    	Map<String,String> equipmentToPointMap = new HashMap<String,String>();
    	for(SysMonitorMetricInfo sysMonitorMetricInfo : sysMonitorMetricInfoList) {
    		equipmentRunPointList.add(sysMonitorMetricInfo.getId());
    		equipmentToPointMap.put(sysMonitorMetricInfo.getEquipmentCode(), sysMonitorMetricInfo.getId());
    	}
    	Map<String, Map<String, Object>> tempEquipmentStateMap = sysMonitorMetricInfoService.getEquipRunState(equipmentRunPointList);

    	for(EquipmentInfo equipmentInfo : equipmentInfoList) {
    		String equipmentId = equipmentInfo.getId();
    		String point = equipmentToPointMap.get(equipmentId);
    		Map<String, Object> tempMap = tempEquipmentStateMap.get(point);
    		if(tempMap!=null&&tempMap.size()>0) {
    			resultMap.put(equipmentInfo.getId(), ConvertUtils.getString(tempMap.get("value")));
    		}else{
    			resultMap.put(equipmentInfo.getId(), "-1");
    		}
    	}
    	result.setResult(resultMap);
        return result;
    }

}