package com.moral.api.service.impl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.ObjectUtils; import java.util.ArrayList; import java.util.Date; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import com.alibaba.fastjson.JSON; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.moral.api.config.Interceptor.UserHelper; import com.moral.api.entity.Device; import com.moral.api.entity.HandDevice; import com.moral.api.entity.HistoryFiveMinutely; import com.moral.api.entity.HistoryHourly; import com.moral.api.mapper.DeviceMapper; import com.moral.api.mapper.HandDeviceMapper; import com.moral.api.pojo.query.handdevice.HandDevicePageCond; import com.moral.api.pojo.vo.user.QxUser; import com.moral.api.service.DeviceService; import com.moral.api.service.HandDeviceService; import com.moral.api.service.HistoryFiveMinutelyService; import com.moral.api.service.HistoryHourlyService; import com.moral.constant.Constants; import com.moral.util.DateUtils; /** * Description //todo * * @author swb * @ClassName HandDeviceServiceImpl * @date 2024.02.27 10:25 */ @Service public class HandDeviceServiceImpl extends ServiceImpl implements HandDeviceService { @Autowired private HandDeviceMapper handDeviceMapper; @Autowired private DeviceService deviceService; @Autowired private DeviceMapper deviceMapper; @Autowired private HistoryHourlyService historyHourlyService; @Autowired private HistoryFiveMinutelyService historyFiveMinutelyService; /** * 分页 * * @param handDevicePageCond * @return */ @Override public Page page(HandDevicePageCond handDevicePageCond) { Page page = handDeviceMapper.Page(handDevicePageCond.getPage().convertPage(), handDevicePageCond.getMac(), handDevicePageCond.getName(), handDevicePageCond.getStartTime(), handDevicePageCond.getEndTime()); return page; } /** * 查询手持设备 * @return */ @Override public List check() { LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.select(Device::getMac,Device::getName,Device::getId); wrapper.eq(Device::getMonitorPointId,123); wrapper.eq(Device::getIsDelete,Constants.NOT_DELETE); wrapper.orderByAsc(Device::getCreateTime); List devices = deviceMapper.selectList(wrapper); return devices; } /** * 修改手持设备 * * @param handDevice * @return */ @Override public HandDevice update(HandDevice handDevice) { String state = handDevice.getState(); if (state.equals("0")){ HandDevice handDevice1 = getHand(handDevice); handDeviceMapper.insert(handDevice1); return handDevice1; }else { handDevice.setEndTime(new Date()); handDevice.setState("0"); handDeviceMapper.updateById(handDevice); return handDevice; } } @Override public HandDevice query(String mac) { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("mac",mac); queryWrapper.orderByDesc("create_time"); List handDevices = handDeviceMapper.selectList(queryWrapper); HandDevice handDevice; if (ObjectUtils.isEmpty(handDevices)){ handDevice = new HandDevice(); Device deviceByMac = deviceService.getDeviceByMac(mac); handDevice.setName(deviceByMac.getName()); handDevice.setMac(deviceByMac.getMac()); handDevice.setState("0"); }else { handDevice = handDevices.get(0); } return handDevice; } /** * 详情 * * @param mac * @param startTime * @param endTime * @return */ @Override public List> details(String mac, String startTime, String endTime,String type) { Date startDate = DateUtils.getDate(startTime,DateUtils.yyyy_MM_dd_HH_mm_EN); Date endDate = DateUtils.getDate(endTime,DateUtils.yyyy_MM_dd_HH_mm_EN); List> rsMap = new ArrayList<>(); if (type.equals("hour")){ List valueByMacAndTime = historyHourlyService.getValueByMacAndTime(mac, startDate, endDate); for (HistoryHourly historyHourly : valueByMacAndTime) { String value = historyHourly.getValue(); Map map = JSON.parseObject(value, Map.class); map.put("time",DateUtils.dateToDateString(historyHourly.getTime())); map.put("a21001",ObjectUtils.isEmpty(map.get("a21001"))?0:map.get("a21001")); map.put("a21028",ObjectUtils.isEmpty(map.get("a21028"))?0:map.get("a21028")); rsMap.add(map); } }else { List historyFiveMinutelies = historyFiveMinutelyService.queryFiveMinutely(mac, startDate, endDate); for (HistoryFiveMinutely historyFiveMinutely : historyFiveMinutelies) { String value = historyFiveMinutely.getValue(); Map map = JSON.parseObject(value, Map.class); map.put("time",DateUtils.dateToDateString(historyFiveMinutely.getTime())); map.put("a21001",ObjectUtils.isEmpty(map.get("a21001"))?0:map.get("a21001")); map.put("a21028",ObjectUtils.isEmpty(map.get("a21028"))?0:map.get("a21028")); map.put("a31001",ObjectUtils.isEmpty(map.get("a31001"))?0:map.get("a31001")); rsMap.add(map); } } return rsMap; } /** * 导出 * @param params * @return */ @Override public List> detailsExecl(Map params) { String type = params.get("type").toString(); String mac = params.get("mac").toString(); Date startDate = DateUtils.getDate(params.get("startTime").toString(),DateUtils.yyyy_MM_dd_HH_mm_EN); Date endDate = DateUtils.getDate(params.get("endTime").toString(),DateUtils.yyyy_MM_dd_HH_mm_EN); List> rsMap = new ArrayList<>(); if (type.equals("hour")){ List valueByMacAndTime = historyHourlyService.getValueByMacAndTime(mac, startDate, endDate); for (HistoryHourly historyHourly : valueByMacAndTime) { LinkedHashMap map1 = new LinkedHashMap<>(); String value = historyHourly.getValue(); Map map = JSON.parseObject(value, Map.class); map1.put("时间",DateUtils.dateToDateString(historyHourly.getTime())); map1.put("pm25",ObjectUtils.isEmpty(map.get("a34004"))?0:map.get("a34004")); map1.put("pm10",ObjectUtils.isEmpty(map.get("a34002"))?0:map.get("a34002")); map1.put("二氧化氮",ObjectUtils.isEmpty(map.get("a21004"))?0:map.get("a21004")); map1.put("二氧化硫",ObjectUtils.isEmpty(map.get("a21026"))?0:map.get("a21026")); map1.put("一氧化碳",ObjectUtils.isEmpty(map.get("a21005"))?0:map.get("a21005")); map1.put("气压",ObjectUtils.isEmpty(map.get("a01006"))?0:map.get("a01006")); map1.put("温度",ObjectUtils.isEmpty(map.get("a01001"))?0:map.get("a01001")); map1.put("湿度",ObjectUtils.isEmpty(map.get("a01002"))?0:map.get("a01002")); map1.put("tvoc",ObjectUtils.isEmpty(map.get("a99054"))?0:map.get("a99054")); map1.put("甲醛",ObjectUtils.isEmpty(map.get("a31001"))?0:map.get("a31001")); map1.put("硫化氢",ObjectUtils.isEmpty(map.get("a21028"))?0:map.get("a21028")); map1.put("氨气",ObjectUtils.isEmpty(map.get("a21001"))?0:map.get("a21001")); rsMap.add(map1); } }else { List historyFiveMinutelies = historyFiveMinutelyService.queryFiveMinutely(mac, startDate, endDate); for (HistoryFiveMinutely historyFiveMinutely : historyFiveMinutelies) { LinkedHashMap map1 = new LinkedHashMap<>(); String value = historyFiveMinutely.getValue(); Map map = JSON.parseObject(value, Map.class); map1.put("时间",DateUtils.dateToDateString(historyFiveMinutely.getTime())); map1.put("pm25",ObjectUtils.isEmpty(map.get("a34004"))?0:map.get("a34004")); map1.put("pm10",ObjectUtils.isEmpty(map.get("a34002"))?0:map.get("a34002")); map1.put("二氧化氮",ObjectUtils.isEmpty(map.get("a21004"))?0:map.get("a21004")); map1.put("二氧化硫",ObjectUtils.isEmpty(map.get("a21026"))?0:map.get("a21026")); map1.put("一氧化碳",ObjectUtils.isEmpty(map.get("a21005"))?0:map.get("a21005")); map1.put("气压",ObjectUtils.isEmpty(map.get("a01006"))?0:map.get("a01006")); map1.put("温度",ObjectUtils.isEmpty(map.get("a01001"))?0:map.get("a01001")); map1.put("湿度",ObjectUtils.isEmpty(map.get("a01002"))?0:map.get("a01002")); map1.put("tvoc",ObjectUtils.isEmpty(map.get("a99054"))?0:map.get("a99054")); map1.put("甲醛",ObjectUtils.isEmpty(map.get("a31001"))?0:map.get("a31001")); map1.put("硫化氢",ObjectUtils.isEmpty(map.get("a21028"))?0:map.get("a21028")); map1.put("氨气",ObjectUtils.isEmpty(map.get("a21001"))?0:map.get("a21001")); rsMap.add(map1); } } return rsMap; } private HandDevice getHand(HandDevice handDevice){ QxUser currentUser = UserHelper.getCurrentUser(); HandDevice rsHandDevice = new HandDevice(); rsHandDevice.setName(handDevice.getName()); rsHandDevice.setMac(handDevice.getMac()); rsHandDevice.setAddress(handDevice.getAddress()); rsHandDevice.setLatitude(handDevice.getLatitude()); rsHandDevice.setLongitude(handDevice.getLongitude()); rsHandDevice.setIsDelete(Constants.NOT_DELETE); rsHandDevice.setUpdateName(currentUser.getUserName()); rsHandDevice.setStartTime(new Date()); rsHandDevice.setUpdateTime(new Date()); rsHandDevice.setCreateTime(new Date()); rsHandDevice.setState("1"); return rsHandDevice; } }