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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package com.iailab.module.device.service.impl;
 
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
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.common.enums.CommonConstant;
import com.iailab.framework.common.service.impl.BaseServiceImpl;
import com.iailab.common.utils.DateUtils;
import com.iailab.common.utils.HttpsRequest;
import com.iailab.module.data.dto.FeignHttpApiDTO;
import com.iailab.module.data.api.IFeignDataApi;
import com.iailab.module.device.commons.HealthLevelEnum;
import com.iailab.module.device.dao.DeviceStatUsageDao;
import com.iailab.module.device.dto.DeviceCurrentRateDTO;
import com.iailab.module.device.dto.DeviceInfoDTO;
import com.iailab.module.device.entity.DeviceInfoEntity;
import com.iailab.module.device.entity.DeviceStatUsageEntity;
import com.iailab.module.device.service.DeviceInfoService;
import com.iailab.module.device.service.DeviceHealthEvaluateService;
import com.iailab.module.device.service.DeviceStatUsageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;
 
@Slf4j
@Service
public class DeviceStatUsageServiceImpl extends BaseServiceImpl<DeviceStatUsageDao, DeviceStatUsageEntity> implements DeviceStatUsageService {
 
    private String HTTP_API_CODE = "Dev.RunReportSumMonth";
 
    @Resource
    private IFeignDataApi feignDataApi;
 
    @Resource
    private HttpsRequest httpsRequest;
 
    @Resource
    private DeviceInfoService deviceInfoService;
 
    @Resource
    private DeviceHealthEvaluateService deviceHealthEvaluateService;
 
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void syncData() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR,-1);
        String ym = DateUtils.format(calendar.getTime(), DateUtils.DATE_PATTERN_MON);
        FeignHttpApiDTO apiDTO = feignDataApi.getHttpApi(HTTP_API_CODE);
        Map<String, String> params = new HashMap<>();
        params.put("ym", ym);
        String responseStr = httpsRequest.doGet(apiDTO.getUrl(), params, "utf-8", "");
        JSONObject responseObj = JSON.parseObject(responseStr);
        if (!CommonConstant.STA_TRUE.equals(responseObj.get("sta").toString())) {
            log.info("接口异常");
        }
        JSONArray datas = responseObj.getJSONArray(CommonConstant.ZX_RES);
        if (CollectionUtils.isEmpty(datas)) {
            return;
        }
 
        // 计划运行时间
        Map<String,Object> ListParams = new HashMap<>();
        ListParams.put("pid","0");
        List<DeviceInfoDTO> deviceInfoList = deviceInfoService.list(ListParams);
        Map<String, Integer> noPlanTimeMap = deviceInfoList.stream().filter(e -> ObjectUtil.isNotNull(e.getPlanTime())).collect(Collectors.toMap(DeviceInfoDTO::getNo, e -> e.getPlanTime() * 60));
        int planTime = 24;
        String key = "z" + calendar.get(Calendar.DAY_OF_MONTH);
 
        List<DeviceStatUsageEntity> list = new ArrayList<>(datas.size());
        List<DeviceInfoEntity> deviceInfoEntities = new ArrayList<>(datas.size());
        for (Object data : datas) {
            JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(data));
 
            DeviceStatUsageEntity deviceStatUsageEntity = new DeviceStatUsageEntity();
            DeviceInfoEntity deviceInfoEntity = new DeviceInfoEntity();
 
            deviceStatUsageEntity.setId(UUID.randomUUID().toString());
            String deviceNo = jsonObject.get("no").toString();
            deviceStatUsageEntity.setDevNo(deviceNo);
            deviceStatUsageEntity.setDevName(jsonObject.get("name").toString());
            deviceStatUsageEntity.setDate(DateUtils.format(calendar.getTime()));
            int runTime = Integer.parseInt(jsonObject.get(key).toString());
            deviceStatUsageEntity.setRunTime(runTime);
 
            if (noPlanTimeMap.containsKey(deviceNo)){
                // 计划运行时间
                planTime = MapUtil.get(noPlanTimeMap,deviceNo,Integer.class);
                // 计算使用率
                deviceStatUsageEntity.setPlanTime(planTime);
                BigDecimal d1 = new BigDecimal(runTime);
                BigDecimal d2 = new BigDecimal(planTime);
                BigDecimal usage = d1.divide(d2, 4, RoundingMode.HALF_UP);
                deviceStatUsageEntity.setUsageRate(usage);
                deviceInfoEntity.setUsageRate(usage);
            }else {
                deviceStatUsageEntity.setPlanTime(null);
            }
 
            deviceStatUsageEntity.setCreateDate(new Date());
            list.add(deviceStatUsageEntity);
 
            deviceInfoEntity.setNo(deviceNo);
            deviceInfoEntity.setUpdateDate(calendar.getTime());
            deviceInfoEntities.add(deviceInfoEntity);
        }
        insertBatch(list);
        // 修改deviceInfo
        for (DeviceInfoEntity entity : deviceInfoEntities) {
            QueryWrapper<DeviceInfoEntity> wrapper = new QueryWrapper<>();
            wrapper.eq("no",entity.getNo());
            wrapper.eq("pid","0");
            deviceInfoService.update(entity,wrapper);
        }
 
    }
 
    @Override
    public DeviceStatUsageEntity getLastByNo(String no) {
        QueryWrapper<DeviceStatUsageEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("dev_no",no);
        wrapper.orderByDesc("create_date");
        wrapper.last("limit 1");
        return baseDao.selectOne(wrapper);
    }
 
    @Override
    public DeviceCurrentRateDTO getDeviceCurrentRate() {
        DeviceCurrentRateDTO result = new DeviceCurrentRateDTO();
        //健康设备数量
        Long healthyNum = deviceHealthEvaluateService.selectHealthLevalCount(HealthLevelEnum.Level1.getCode());
        //待检修设备数量
        Long repairNum = deviceHealthEvaluateService.selectHealthLevalCount(HealthLevelEnum.Level3.getCode());
        //总设备数量
        Long totalNum = deviceHealthEvaluateService.selectHealthLevalCount(null);
 
        //完好率
        BigDecimal healthRate = BigDecimal.valueOf(healthyNum).divide(BigDecimal.valueOf(totalNum), 4, RoundingMode.HALF_UP);
        result.setHealthyRate(healthRate);
        //待修率
        BigDecimal repairRate = BigDecimal.valueOf(repairNum).divide(BigDecimal.valueOf(totalNum), 4, RoundingMode.HALF_UP);
        result.setRepairRate(repairRate);
        //todo 事故率
        result.setAccidentRate(BigDecimal.valueOf(0));
 
        return result;
    }
 
    @Override
    public List<DeviceStatUsageEntity> list(Map<String, Object> params) {
        return baseDao.selectList(getWrapper(params));
    }
 
    public QueryWrapper<DeviceStatUsageEntity> getWrapper(Map<String, Object> params) {
        String no = (String) params.get("no");
        String startTime = (String) params.get("startTime");
        String endTime = (String) params.get("endTime");
 
        QueryWrapper<DeviceStatUsageEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(no), "dev_no", no);
        wrapper.ge(StringUtils.isNotBlank(startTime), "date", startTime);
        wrapper.le(StringUtils.isNotBlank(endTime), "date", endTime);
        wrapper.orderByDesc("create_date");
        return wrapper;
    }
}