package com.moral.api.service.impl; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.system.ApplicationHome; import org.springframework.stereotype.Service; import org.springframework.util.ObjectUtils; import org.springframework.web.multipart.MultipartFile; import java.text.DecimalFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.UUID; import java.util.stream.Collectors; 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.moral.api.entity.Dustld; import com.moral.api.mapper.DustldMapper; import com.moral.api.mapper.HistorySecondCruiserMapper; import com.moral.api.pojo.dust.DustForm; import com.moral.api.pojo.dust.TimeForm; import com.moral.api.service.DustldService; import com.moral.util.DateUtils; import com.moral.util.FileUtils; @Service @Slf4j public class DustldServiceImpl implements DustldService { @Autowired private HistorySecondCruiserMapper historySecondCruiserMapper; @Autowired private DustldMapper dustldMapper; /** * 保存日报信息 * @param params * @param file * @return * @throws ParseException */ @Override public Integer getDailyDustld(Map params,MultipartFile file ) { //获取jar包所在目录 ApplicationHome applicationHome = new ApplicationHome(getClass()); //在jar包所在目录下生成一个upload文件夹用来存储上传的图片 String path = applicationHome.getSource().getParentFile().toString() + "/static/img"; String mac = params.get("mac").toString(); String time3 = params.get("time3").toString(); String time4 = params.get("time4").toString(); String table = params.get("table").toString(); String substring = time3.substring(0, 10); //路段名 String road = params.get("road").toString(); ArrayList list = new ArrayList<>(); List tables = JSONArray.parseArray(table, TimeForm.class); if (!ObjectUtils.isEmpty(tables)){ for (TimeForm timeForm : tables) { String start = timeForm.getStart(); String end = timeForm.getEnd(); List dust = historySecondCruiserMapper.getDust(start, end, mac); ArrayList rsDouble = new ArrayList<>(); for (Double aDouble : dust) { double v =0.0; if (aDouble>=0 && aDouble<40 ){ v = aDouble + 170; } if (aDouble>=40 && aDouble<60 ){ v = aDouble + 130; } if (aDouble>=60 && aDouble<100 ){ v = aDouble + 110; } if (aDouble>=100 && aDouble<150 ){ v = aDouble + 70; } if (aDouble>=150 && aDouble<180 ){ v = aDouble + 30; } if (aDouble>=180){ v= aDouble; } rsDouble.add(v); } list.addAll(rsDouble); } } String format = ""; if (!ObjectUtils.isEmpty(list)){ Double collect = (list.stream().collect(Collectors.averagingDouble(Double::doubleValue)))/1000; format = new DecimalFormat("0.000").format(collect); } QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("mac",mac).likeRight("time",substring); Dustld dustld = dustldMapper.selectOne(queryWrapper); if (ObjectUtils.isEmpty(dustld)){ Dustld dustld1 = new Dustld(); if (!ObjectUtils.isEmpty(file)){ HashMap hashMap = new HashMap<>(); String fileType = file.getContentType(); if ("image/jpg".equals(fileType) || "image/png".equals(fileType) || "image/jpeg".equals(fileType)){ //获取文件名 String fileName = file.getOriginalFilename(); //获取文件后缀名 String suffixName = fileName.substring(fileName.lastIndexOf(".")); //每一次都需要 fileName.substring(fileName.lastIndexOf(".")); //重新生成文件名 fileName = UUID.randomUUID() + suffixName; //图片上传 if (FileUtils.upload(file, path, fileName)) { hashMap.put("file1",fileName); String s = JSON.toJSONString(hashMap); dustld1.setImages(s); } } } HashMap map = new HashMap<>(); map.put(road,format); String value = JSON.toJSONString(map); dustld1.setMac(mac); dustld1.setTime(DateUtils.getDate(time3,DateUtils.yyyy_MM_dd_HH_mm_ss_EN)); dustld1.setValue(value); dustld1.setEndTime(DateUtils.getDate(time4,DateUtils.yyyy_MM_dd_HH_mm_ss_EN)); dustldMapper.insert(dustld1); return 200; }else { if (!ObjectUtils.isEmpty(file)){ HashMap hashMap = new HashMap<>(); String fileType = file.getContentType(); if ("image/jpg".equals(fileType) || "image/png".equals(fileType) || "image/jpeg".equals(fileType)){ //获取文件名 String fileName = file.getOriginalFilename(); //获取文件后缀名 String suffixName = fileName.substring(fileName.lastIndexOf(".")); //每一次都需要 fileName.substring(fileName.lastIndexOf(".")); //重新生成文件名 fileName = UUID.randomUUID() + suffixName; //图片上传 if (FileUtils.upload(file, path, fileName)) { hashMap.put("file1",fileName); String s = JSON.toJSONString(hashMap); dustld.setImages(s); } } } String value = dustld.getValue(); JSONObject jsonObject = JSONObject.parseObject(value); jsonObject.put(road,format); String rsValue = JSONObject.toJSONString(jsonObject); dustld.setValue(rsValue); dustldMapper.updateById(dustld); return 200; } } /** * 下载日报 * @param id * @return */ @Override public Map dailyDustld(Integer id,List files) { //获取jar包所在目录 ApplicationHome applicationHome = new ApplicationHome(getClass()); //在jar包所在目录下生成一个upload文件夹用来存储上传的图片 String path = applicationHome.getSource().getParentFile().toString() + "/static/img"; HashMap map = new HashMap<>(); ArrayList list1 = new ArrayList<>(); ArrayList list2= new ArrayList<>(); Dustld dustld = dustldMapper.selectById(id); if (ObjectUtils.isEmpty(dustld)){ return null; } String pathList = getList(path, files); String images = dustld.getImages(); if (ObjectUtils.isEmpty(images)){ HashMap map1 = new HashMap<>(); map1.put("file1",""); if (pathList==null){ map1.put("file2",""); }else { map1.put("file2",pathList); } String s = JSON.toJSONString(map1); dustld.setImages(s); dustldMapper.updateById(dustld); }else { JSONObject jsonObject = JSONObject.parseObject(images); if (ObjectUtils.isEmpty(pathList)){ jsonObject.put("file2",""); }else { jsonObject.put("file2",pathList); } String s = JSON.toJSONString(jsonObject); dustld.setImages(s); dustldMapper.updateById(dustld); } SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒"); String stsrtTime = sdf.format(dustld.getTime()); String substring = stsrtTime.substring(5, 14); String endTime = sdf.format(dustld.getEndTime()); String substring1 = endTime.substring(5, 14); String value = dustld.getValue(); JSONObject jsonObject = JSONObject.parseObject(value); Iterator> iterator = jsonObject.entrySet().iterator(); while (iterator.hasNext()) { DustForm dustForm = new DustForm(); Map.Entry entry = (Map.Entry) iterator.next(); dustForm.setRoad(entry.getKey().toString()); dustForm.setValue(Double.parseDouble(entry.getValue().toString())); double aDouble = Double.parseDouble(entry.getValue().toString()); list2.add(dustForm); if (aDouble>=0.3){ list1.add(dustForm); } } //排序 list1.sort(Comparator.comparing(DustForm::getValue).reversed()); list2.sort(Comparator.comparing(DustForm::getValue).reversed()); map.put("list1",list1); map.put("list2",list2); map.put("time",substring+"-"+substring1); map.put("images",JSON.parseObject(dustld.getImages())); return map; } /** * 查询尘负荷高值 * @param id * @return */ @Override public Map selectDust(Integer id) { HashMap rsMap = new HashMap<>(); ArrayList list1 = new ArrayList<>(); Dustld dustld = dustldMapper.selectById(id); if (ObjectUtils.isEmpty(dustld)){ return null; } String value = dustld.getValue(); JSONObject jsonObject = JSONObject.parseObject(value); Iterator> iterator = jsonObject.entrySet().iterator(); while (iterator.hasNext()) { DustForm dustForm = new DustForm(); Map.Entry entry = (Map.Entry) iterator.next(); dustForm.setRoad(entry.getKey().toString()); dustForm.setValue(Double.parseDouble(entry.getValue().toString())); double aDouble = Double.parseDouble(entry.getValue().toString()); if (aDouble>=0.3){ list1.add(dustForm); } } if (!ObjectUtils.isEmpty(list1)){ list1.sort(Comparator.comparing(DustForm::getValue).reversed()); } rsMap.put("list",list1); rsMap.put("count",list1.size()); return rsMap; } /** * 查询日报记录 * @param params * @return */ @Override public List selectAll(Map params) { String mac = params.get("mac").toString(); String time1 = params.get("startTime").toString(); String time2 = params.get("endTime").toString(); QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("mac",mac).between("time",time1,time2); List dustlds = dustldMapper.selectList(queryWrapper); if (ObjectUtils.isEmpty(dustlds)){ return null; } return dustlds; } //获取图片地址 private String getList(String path, List files1) { ArrayList images = new ArrayList<>(); if (!ObjectUtils.isEmpty(files1)){ for (MultipartFile file : files1) { String fileType = file.getContentType(); if ("image/jpg".equals(fileType) || "image/png".equals(fileType) || "image/jpeg".equals(fileType)){ //获取文件名 String fileName = file.getOriginalFilename(); //获取文件后缀名 String suffixName = fileName.substring(fileName.lastIndexOf(".")); //每一次都需要 fileName.substring(fileName.lastIndexOf(".")); //重新生成文件名 fileName = UUID.randomUUID() + suffixName; //图片上传 if (FileUtils.upload(file, path, fileName)) { images.add(fileName); } } } } if (!ObjectUtils.isEmpty(images)) { String image = images.stream() .map(String::valueOf) .collect(Collectors.joining(",")); return image; } return null; } }