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;
|
}
|
}
|