package com.iailab.module.device.service.impl; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.iailab.framework.common.service.impl.CrudServiceImpl; import com.iailab.common.utils.HttpsRequest; import com.iailab.module.device.dao.DeviceAccountDao; import com.iailab.module.device.dto.DeviceAccountDTO; import com.iailab.module.device.entity.DeviceAccountEntity; import com.iailab.module.device.entity.DeviceAccountHistoryEntity; import com.iailab.module.device.service.DeviceAccountHistoryService; import com.iailab.module.device.service.DeviceAccountService; import org.apache.commons.lang3.StringUtils; import javax.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import java.util.*; /** * 设备台账; * * @author lirm ${email} * @since 1.0.0 2024-05-20 */ @Service public class DeviceAccountServiceImpl extends CrudServiceImpl implements DeviceAccountService { @Resource DeviceAccountHistoryService deviceAccountHistoryService; @Resource HttpsRequest httpsRequest; @Override public QueryWrapper getWrapper(Map params) { String deviceCode = (String) params.get("deviceCode"); String deviceName = (String) params.get("deviceName"); QueryWrapper wrapper = new QueryWrapper<>(); wrapper.like(StringUtils.isNotBlank(deviceCode), "device_code", deviceCode); wrapper.like(StringUtils.isNotBlank(deviceName), "device_name", deviceName); wrapper.orderByAsc("device_id"); return wrapper; } @Override public void insertDeviceList(Map tMap) { Map queryParams = new HashMap<>(); queryParams.put("per_page", "100"); String responseStr = httpsRequest.doGetDeviceList(tMap.get("url"), queryParams, "utf-8", tMap.get("token")); JSONObject responseObj = JSON.parseObject(responseStr); if ("200".equals(responseObj.get("code").toString())) { JSONObject dataObject = (JSONObject) responseObj.get("data"); JSONArray stateArray = dataObject.getJSONArray("items"); if (!CollectionUtils.isEmpty(stateArray)) { if (!CollectionUtils.isEmpty(stateArray)) { baseDao.updateMonitorState();//先更新监测状态为0 for (int i = 0; i < stateArray.size(); i++) { JSONObject item = stateArray.getJSONObject(i); if (!item.get("de_serial_no").toString().contains("ams")) { DeviceAccountEntity entity = baseDao.getByDeviceId(Integer.parseInt(item.get("id").toString())); if (entity != null) { entity.setRunState(Integer.parseInt(item.get("status").toString())); if(Integer.parseInt(item.get("status").toString()) == 0){ entity.setRemark("设备离线"); }else if(Integer.parseInt(item.get("status").toString()) == 1){ entity.setRemark("设备空闲停机"); }if(Integer.parseInt(item.get("status").toString()) == 2){ entity.setRemark("设备运行正常"); } entity.setMonitorState(1); entity.setUpdateDate(new Date()); baseDao.updateById(entity); } else { //解析json DeviceAccountEntity deviceAccountEntity = new DeviceAccountEntity(); deviceAccountEntity.setId(UUID.randomUUID().toString()); deviceAccountEntity.setDeviceId(Integer.parseInt(item.get("id").toString())); deviceAccountEntity.setDeviceCode(item.get("de_serial_no").toString()); deviceAccountEntity.setDeviceName(item.get("name").toString()); deviceAccountEntity.setWorkShop(item.get("category").toString()); deviceAccountEntity.setDeviceClass(item.get("class").toString()); deviceAccountEntity.setRunState(Integer.parseInt(item.get("status").toString())); deviceAccountEntity.setMonitorState(1); deviceAccountEntity.setIsPush(0); deviceAccountEntity.setCreateDate(new Date()); if(Integer.parseInt(item.get("status").toString()) == 0){ entity.setRemark("设备离线"); }else if(Integer.parseInt(item.get("status").toString()) == 1){ entity.setRemark("设备空闲停机"); }if(Integer.parseInt(item.get("status").toString()) == 2){ entity.setRemark("设备运行正常"); } baseDao.insert(deviceAccountEntity); } //解析json DeviceAccountHistoryEntity deviceAccountHistoryEntity = new DeviceAccountHistoryEntity(); deviceAccountHistoryEntity.setId(UUID.randomUUID().toString()); deviceAccountHistoryEntity.setDeviceId(Integer.parseInt(item.get("id").toString())); deviceAccountHistoryEntity.setDeviceCode(item.get("de_serial_no").toString()); deviceAccountHistoryEntity.setDeviceName(item.get("name").toString()); deviceAccountHistoryEntity.setWorkShop(item.get("category").toString()); deviceAccountHistoryEntity.setDeviceClass(item.get("class").toString()); deviceAccountHistoryEntity.setRunState(Integer.parseInt(item.get("status").toString())); deviceAccountHistoryEntity.setMonitorState(1); deviceAccountHistoryEntity.setIsPush(0); deviceAccountHistoryEntity.setCreateDate(new Date()); deviceAccountHistoryService.insert(deviceAccountHistoryEntity); } } } } } } @Override public List getDeviceIdList() { return baseDao.getDeviceIdList(); } }