houzhongjian
2024-07-23 a6de490948278991e47952e90671ddba4555e9a2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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<DeviceAccountDao, DeviceAccountEntity, DeviceAccountDTO> implements DeviceAccountService {
    @Resource
    DeviceAccountHistoryService deviceAccountHistoryService;
    @Resource
    HttpsRequest httpsRequest;
 
    @Override
    public QueryWrapper<DeviceAccountEntity> getWrapper(Map<String, Object> params) {
        String deviceCode = (String) params.get("deviceCode");
        String deviceName = (String) params.get("deviceName");
 
        QueryWrapper<DeviceAccountEntity> 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<String, String> tMap) {
        Map<String, String> 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<String> getDeviceIdList() {
        return baseDao.getDeviceIdList();
    }
}