package com.jiayue.biz.service.impl; import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.jiayue.biz.domain.StatisticsSituation; import com.jiayue.common.core.redis.RedisCache; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.jiayue.biz.mapper.WindTowerInfoMapper; import com.jiayue.biz.domain.WindTowerInfo; import com.jiayue.biz.service.WindTowerInfoService; import java.sql.Struct; import java.text.SimpleDateFormat; import java.util.*; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; /** * 测风塔信息Service业务层处理 * * @author L.ym * @date 2022-05-11 */ @Service public class WindTowerInfoServiceImpl extends ServiceImpl implements WindTowerInfoService { @Autowired StatisticsSituationServiceImpl statisticsSituationService; @Autowired RedisCache redisCache; @Override public List getDashAllWindInfo() { List list = new ArrayList(); List windTowerInfos = this.list(); if (windTowerInfos.size() > 0) { for (WindTowerInfo windTowerInfo : windTowerInfos) { Map map = new HashMap(); map.put("name", windTowerInfo.getName()); map.put("lon", windTowerInfo.getLongitude()); map.put("lat", windTowerInfo.getLatitude()); list.add(map); } } return list; } //获取所有测风塔信息 public List getAllWindTower(){ return this.lambdaQuery().list(); } /** * 新建测风塔时生成对应的测风塔表 * * @param windTowerInfo * @return */ public boolean saveWindTowerInfo(WindTowerInfo windTowerInfo) { baseMapper.createCalculationTable("wind_tower_calculation_data_" + windTowerInfo.getEquipmentNo()); boolean save = this.save(windTowerInfo); return save; } public boolean update(WindTowerInfo windTowerInfo){ boolean b = this.updateById(windTowerInfo); return b; } /** * 删除塔时删除表 * * @param ids * @return */ public boolean deleteWindTowerInfo(List ids) { for (String id : ids) { WindTowerInfo w = this.getById(id); baseMapper.deleteCalculationTable("wind_tower_calculation_data_" + w.getEquipmentNo()); } boolean b = this.removeByIds(ids); return b; } /* * 根据设备编号查询测风塔信息 */ public List getByEquipmentNo(String equipmentNo) { List allWindTower = getAllWindTower(); return allWindTower.stream().filter(w -> w.getEquipmentNo().equals(equipmentNo)).collect(Collectors.toList()); } /* * 根据设备编号查询测风塔信息 */ public List getByProjectId(String projectId) { QueryWrapper wrapper = new QueryWrapper(); if (projectId != null && !projectId.equals("")) { wrapper.eq("project_id", projectId); } return baseMapper.selectList(wrapper); } /* * 分页查询设备编号查询测风塔信息 */ @Override public Page pageByEquipmentNo(Integer current, Integer size, String equipmentNo) { QueryWrapper wrapper = new QueryWrapper(); if (equipmentNo != null && !equipmentNo.equals("")) { wrapper.eq("equipment_no", equipmentNo); } wrapper.orderByAsc("equipment_no"); Page page = new Page(current, size); return page(page, wrapper); } public List> listEquipmentIdAndDataTime() { //TODO韩雪峰 新建的塔没有统计概述的时候无法显示 需要考虑到这种情况 List windTowerInfoList = this.list(); List statisticsSituationList = statisticsSituationService.list(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); List> mapList = new ArrayList<>(); List> statusMapList = new ArrayList<>(); //List windTowerInfoListCollect = windTowerInfoList.stream().sorted(Comparator.comparing(WindTowerInfo::getEquipmentNo)).collect(Collectors.toList()); Map maps = new HashMap<>(); for (StatisticsSituation s : statisticsSituationList) { maps.put(s.getEquipmentId(), Long.parseLong(s.getStartTimeAndEndTime().split(",")[1])); } List> lstEntry = new ArrayList<>(maps.entrySet()); //根据value 排序 降序 lstEntry.sort(((o1, o2) -> o2.getValue().compareTo(o1.getValue()))); List windTowerInfos = new ArrayList<>(); lstEntry.forEach(o -> { //lstEntry 已经是排完序的了 for循环对比设备编号 添加到windTowerInfoList里 结果就是根据统计概述的数据截止时间从大到小的结果 for (WindTowerInfo w : windTowerInfoList) { if (w.getEquipmentNo().equals(o.getKey())) { windTowerInfos.add(w); } } }); if (windTowerInfoList.size() != windTowerInfos.size()) { for (WindTowerInfo w : windTowerInfoList) { if (windTowerInfos.stream().filter(wt -> wt.getId().equals(w.getId())).collect(Collectors.toList()).isEmpty()) { windTowerInfos.add(w); } } } Map map; for (WindTowerInfo w : windTowerInfos) { map = new HashMap<>(); map.put("value", w.getEquipmentNo()); map.put("label", w.getName()); map.put("wdHeights", w.getWdHeights()); map.put("heights", w.getHeights()); map.put("status", w.getStatus()); List collect = statisticsSituationList.stream().filter(s -> s.getEquipmentId().equals(w.getEquipmentNo())).collect(Collectors.toList()); if (!collect.isEmpty()) { //collect.get(0).getStartTimeAndEndTime().split(",") = "1638288000000,1638373800000" String[] strings = collect.get(0).getStartTimeAndEndTime().split(","); String str = sdf.format(new Date(Long.parseLong(strings[0]))) + "-" + sdf.format(new Date(Long.parseLong(strings[1]))); map.put("date", str); } if (w.getStatus() == null || w.getStatus().equals("")) { statusMapList.add(map); } else { mapList.add(map); } } mapList.addAll(statusMapList); //redisCache.setCacheObject("listEquipmentIdAndDataTime", mapList, 1, TimeUnit.HOURS); return mapList; } /** * TODO 需要改的地方过多 重写list * * @return List */ // public List list() { // return this.lambdaQuery().eq(WindTowerInfo::getType, "1").list(); // } //获取测风塔层高 public String getWsHeights(String equipmentId) { String wsHeights = ""; try { if (StrUtil.isNotBlank(equipmentId)) { List equipmentNo = this.getByEquipmentNo(equipmentId); if (!equipmentNo.isEmpty()) { wsHeights = equipmentNo.get(0).getHeights(); } } } catch (Exception e) { log.error("测风塔编号错误"); throw new RuntimeException(e); } return wsHeights; } }