package com.moral.strategy; 
 | 
  
 | 
import com.alibaba.fastjson.JSONArray; 
 | 
import com.alibaba.fastjson.JSONObject; 
 | 
import com.moral.service.DeviceService; 
 | 
import com.moral.service.HistoryHourlyService; 
 | 
import com.moral.service.HistoryMinutelyService; 
 | 
import com.moral.service.SensorService; 
 | 
import com.moral.util.DateUtil; 
 | 
import com.xxl.job.core.biz.model.ReturnT; 
 | 
import com.xxl.job.core.log.XxlJobLogger; 
 | 
import org.springframework.data.redis.core.RedisTemplate; 
 | 
import org.springframework.stereotype.Service; 
 | 
import org.springframework.util.CollectionUtils; 
 | 
import org.springframework.util.LinkedMultiValueMap; 
 | 
import org.springframework.util.MultiValueMap; 
 | 
import org.springframework.util.ObjectUtils; 
 | 
  
 | 
import javax.annotation.Resource; 
 | 
import java.math.BigDecimal; 
 | 
import java.text.SimpleDateFormat; 
 | 
import java.util.*; 
 | 
  
 | 
@Service 
 | 
public class RepairHourlyData implements RepairDataStrategy { 
 | 
    @Resource 
 | 
    SensorService sensorService; 
 | 
    @Resource 
 | 
    DeviceService deviceService; 
 | 
    @Resource 
 | 
    HistoryMinutelyService historyMinutelyService; 
 | 
    @Resource 
 | 
    RedisTemplate redisTemplate; 
 | 
    @Resource 
 | 
    HistoryHourlyService historyHourlyService; 
 | 
  
 | 
  
 | 
    @Override 
 | 
    public void repairData(String time,MultiValueMap<String,String> result) { 
 | 
  
 | 
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
 | 
        Date endTimeDate = new Date(Long.parseLong(time) * 1000); 
 | 
        Date startTimeDate = DateUtil.rollHour(endTimeDate, -1); 
 | 
        StringBuilder endTime = new StringBuilder(sdf.format(endTimeDate)); 
 | 
        StringBuilder startTime = new StringBuilder(sdf.format(startTimeDate)); 
 | 
        endTime.replace(14, 19, "00:00"); 
 | 
        startTime.replace(14, 19, "00:00"); 
 | 
        String yearAndMonth = DateUtil.getYear(startTimeDate) + DateUtil.getMonth(startTimeDate); 
 | 
  
 | 
        List<String> sensorKeys = sensorService.getSensorKeys(); 
 | 
        List<String> macs = deviceService.getMacs(); 
 | 
        Map<String, Object> devices = new HashMap<>(); 
 | 
        devices.put("sensorKeys", sensorKeys); 
 | 
        devices.put("start", startTime.toString()); 
 | 
        devices.put("end", endTime.toString()); 
 | 
        devices.put("macs", macs); 
 | 
        devices.put("yearAndMonth", yearAndMonth); 
 | 
  
 | 
        try { 
 | 
            List<Map<String, Object>> hourlyData = historyMinutelyService.getMinutelySensorData(devices); 
 | 
            XxlJobLogger.log("historyHourlyData:" + hourlyData.size()); 
 | 
            List<Map<String, Object>> hourlyDataList = new ArrayList<>(); 
 | 
            for (Map<String, Object> deviceData : hourlyData) { 
 | 
                if (!ObjectUtils.isEmpty(deviceData)) { 
 | 
                    Map<String, Object> hourlyDataMap = new LinkedHashMap<>(); 
 | 
                    JSONObject jo = new JSONObject(true); 
 | 
                    hourlyDataMap.put("mac", deviceData.get("mac")); 
 | 
                    hourlyDataMap.put("time", endTime.toString()); 
 | 
                    JSONArray jsonArray = new JSONArray(); 
 | 
                    for (String key : deviceData.keySet()) { 
 | 
                        if (!key.equals("mac") && !key.startsWith("M")) { 
 | 
                            List<Object> date = new ArrayList<>(); 
 | 
                            date.add(deviceData.get(key)); 
 | 
                            if (deviceData.get("MIN" + key) instanceof String) { 
 | 
                                date.add(new BigDecimal(deviceData.get("MIN" + key).toString())); 
 | 
                                date.add(new BigDecimal(deviceData.get("MAX" + key).toString())); 
 | 
                            } else if (deviceData.get("MIN" + key) instanceof byte[]) { 
 | 
                                date.add(new BigDecimal(new String((byte[]) (deviceData.get("MIN" + key))))); 
 | 
                                date.add(new BigDecimal(new String((byte[]) (deviceData.get("MAX" + key))))); 
 | 
                            } 
 | 
                            jo.put(key, date); 
 | 
                        } 
 | 
                    } 
 | 
                    jsonArray.add(jo); 
 | 
                    hourlyDataMap.put("json", jsonArray.get(0).toString()); 
 | 
                    hourlyDataList.add(hourlyDataMap); 
 | 
                } 
 | 
            } 
 | 
            if (!CollectionUtils.isEmpty(hourlyDataList)) { 
 | 
                historyHourlyService.insertHistoryHourly(hourlyDataList); 
 | 
                result.add("200", "修补小时数据成功-"+endTime); 
 | 
                return; 
 | 
            } 
 | 
        } catch (Exception e) { 
 | 
            XxlJobLogger.log("historyHourlyException:" + e.getMessage()); 
 | 
            result.add("500", "修补小时数据失败-"+endTime); 
 | 
            redisTemplate.opsForList().leftPush("unSuccessRepair_data","hourly_"+endTime); 
 | 
            return; 
 | 
        } 
 | 
        result.add("500", "修补小时数据失败-"+endTime); 
 | 
        redisTemplate.opsForList().leftPush("unSuccessRepair_data","hourly_"+endTime); 
 | 
    } 
 | 
} 
 |