|  |  |  | 
|---|
|  |  |  | package com.moral.api.service.impl; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.alibaba.fastjson.JSONObject; | 
|---|
|  |  |  | import com.moral.api.entity.HistoryHourly; | 
|---|
|  |  |  | import com.moral.api.mapper.HistoryHourlyMapper; | 
|---|
|  |  |  | import com.moral.api.service.DeviceService; | 
|---|
|  |  |  | import com.moral.api.service.HistoryHourlyService; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | 
|---|
|  |  |  | import com.moral.constant.Constants; | 
|---|
|  |  |  | import com.moral.constant.RedisConstants; | 
|---|
|  |  |  | import com.moral.util.DateUtils; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.data.redis.core.RedisTemplate; | 
|---|
|  |  |  | import org.springframework.stereotype.Service; | 
|---|
|  |  |  | import org.springframework.transaction.annotation.Transactional; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.text.SimpleDateFormat; | 
|---|
|  |  |  | import java.util.Date; | 
|---|
|  |  |  | import java.util.HashMap; | 
|---|
|  |  |  | import java.util.Map; | 
|---|
|  |  |  | 
|---|
|  |  |  | * @since 2021-06-04 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Service | 
|---|
|  |  |  | public class HistoryHourlyServiceImpl extends ServiceImpl<HistoryHourlyMapper, HistoryHourly> implements HistoryHourlyService { | 
|---|
|  |  |  | public class HistoryHourlyServiceImpl implements HistoryHourlyService { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private HistoryHourlyMapper historyHourlyMapper; | 
|---|
|  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private DeviceService deviceService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private RedisTemplate redisTemplate; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | @Transactional | 
|---|
|  |  |  | public void insertHistoryHourly(Map<String, Object> data) { | 
|---|
|  |  |  | //用来校准数据 | 
|---|
|  |  |  | Map<String, Object> dataAdjust = new HashMap<>(data); | 
|---|
|  |  |  | String mac = data.remove("mac").toString(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | Date time = DateUtils.getDate((String) data.remove("DataTime"), DateUtils.yyyyMMddHHmmss_EN); | 
|---|
|  |  |  | Integer version = (Integer) data.remove("ver"); | 
|---|
|  |  |  | //存入数据库对象 | 
|---|
|  |  |  | Map<String, Object> result = new HashMap<>(); | 
|---|
|  |  |  | String mac = data.remove("mac").toString(); | 
|---|
|  |  |  | //根据mac获取型号 | 
|---|
|  |  |  | Map<String, Object> deviceInfo = (Map<String, Object>) redisTemplate.opsForHash().get(RedisConstants.DEVICE, mac); | 
|---|
|  |  |  | Map<String, Object> versionInfo = (Map<String, Object>) deviceInfo.get("version"); | 
|---|
|  |  |  | Integer version = (Integer) versionInfo.get("id"); | 
|---|
|  |  |  | //获取时间戳 | 
|---|
|  |  |  | String time1 = data.get("time").toString(); | 
|---|
|  |  |  | long l = Long.parseLong(time1); | 
|---|
|  |  |  | SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:00:00"); | 
|---|
|  |  |  | String format1 = dateFormat.format(l); | 
|---|
|  |  |  | Date time = DateUtils.getDate(format1, DateUtils.yyyy_MM_dd_HH_mm_ss_EN); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //        Date time = DateUtils.getDate((String) data.remove("DataTime"), DateUtils.yyyyMMddHHmmss_EN); | 
|---|
|  |  |  | String yearAndMonth = DateUtils.dateToDateString(DateUtils.addHours(time, -1), DateUtils.yyyyMM_EN); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | result.put("mac", mac); | 
|---|
|  |  |  | result.put("time", time); | 
|---|
|  |  |  | result.put("time", DateUtils.addHours(time, -1)); | 
|---|
|  |  |  | result.put("version", version); | 
|---|
|  |  |  | result.put("timeUnits", Constants.UN_ADJUST); | 
|---|
|  |  |  | result.put("timeUnits", yearAndMonth + "_" + Constants.UN_ADJUST); | 
|---|
|  |  |  | result.put("value", JSONObject.toJSONString(data)); | 
|---|
|  |  |  | //未校准小时表insert | 
|---|
|  |  |  | historyHourlyMapper.insertHistoryHourly(result); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //数据校准 | 
|---|
|  |  |  | dataAdjust = deviceService.adjustDeviceData(dataAdjust); | 
|---|
|  |  |  | dataAdjust.remove("mac"); | 
|---|
|  |  |  | dataAdjust.remove("DataTime"); | 
|---|
|  |  |  | dataAdjust.remove("ver"); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | HistoryHourly historyHourly = new HistoryHourly(); | 
|---|
|  |  |  | historyHourly.setMac(mac); | 
|---|
|  |  |  | historyHourly.setTime(time); | 
|---|
|  |  |  | historyHourly.setVersion(version); | 
|---|
|  |  |  | historyHourly.setValue(JSONObject.toJSONString(dataAdjust)); | 
|---|
|  |  |  | dataAdjust = deviceService.adjustDeviceData(dataAdjust,"2"); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //校准后小时表insert | 
|---|
|  |  |  | historyHourlyMapper.insert(historyHourly); | 
|---|
|  |  |  | result.put("timeUnits", yearAndMonth); | 
|---|
|  |  |  | result.put("value", JSONObject.toJSONString(dataAdjust)); | 
|---|
|  |  |  | historyHourlyMapper.insertHistoryHourly(result); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|