| | |
| | | package com.moral.api.controller; |
| | | |
| | | import com.deepoove.poi.XWPFTemplate; |
| | | import com.deepoove.poi.data.MiniTableRenderData; |
| | | import com.deepoove.poi.data.RowRenderData; |
| | | import com.deepoove.poi.data.TextRenderData; |
| | | import com.moral.api.pojo.dto.historySecondCruiser.HistorySecondCruiserResultDTO; |
| | | |
| | | import cn.afterturn.easypoi.exception.word.WordExportException; |
| | | import cn.afterturn.easypoi.word.WordExportUtil; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiImplicitParam; |
| | | import io.swagger.annotations.ApiImplicitParams; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.poi.xwpf.usermodel.XWPFDocument; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.util.ObjectUtils; |
| | | import org.springframework.util.ResourceUtils; |
| | | import org.springframework.web.bind.annotation.CrossOrigin; |
| | | import org.springframework.web.bind.annotation.GetMapping; |
| | | import org.springframework.web.bind.annotation.PostMapping; |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | import org.springframework.web.multipart.MultipartHttpServletRequest; |
| | | |
| | | import java.io.File; |
| | | import java.io.FileOutputStream; |
| | | import java.io.IOException; |
| | | import java.io.OutputStream; |
| | | import java.net.URLEncoder; |
| | | import java.text.ParseException; |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | import javax.servlet.ServletOutputStream; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | |
| | | import com.moral.api.entity.Dustld; |
| | | import com.moral.api.pojo.bo.ExcelBO; |
| | | import com.moral.api.pojo.dust.DustForm; |
| | | import com.moral.api.pojo.vo.excel.DailyVo; |
| | | import com.moral.api.service.DustldService; |
| | | import com.moral.api.service.SpecialDeviceService; |
| | | import com.moral.constant.ResponseCodeEnum; |
| | | import com.moral.constant.ResultMessage; |
| | | import com.moral.util.WebUtils; |
| | | |
| | | import static com.moral.api.controller.SysTestController.getPath; |
| | | |
| | | @Slf4j |
| | | @Api(tags = {"走航车"}) |
| | |
| | | @ApiImplicitParams(value = { |
| | | @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header", dataType = "String"), |
| | | @ApiImplicitParam(name = "mac", value = "设备mac", required = true, paramType = "query", dataType = "String"), |
| | | @ApiImplicitParam(name = "time", value = "时间,2021-08-18", required = true, paramType = "query", dataType = "String") |
| | | @ApiImplicitParam(name = "time1", value = "时间,2021-08-18", required = true, paramType = "query", dataType = "String"), |
| | | @ApiImplicitParam(name = "time2", value = "时间,2021-08-18", required = true, paramType = "query", dataType = "String") |
| | | }) |
| | | public ResultMessage carTrajectory(HttpServletRequest request) { |
| | | Map<String, Object> params = WebUtils.getParametersStartingWith(request, null); |
| | | if (!params.containsKey("mac") || !params.containsKey("time")) { |
| | | if (!params.containsKey("mac") || !params.containsKey("time1")|| !params.containsKey("time2")) { |
| | | return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(), ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg()); |
| | | } |
| | | List<Map<String, Object>> response = specialDeviceService.carTrajectory(params); |
| | | List<Map<String, Object>> response = specialDeviceService.carTrajectory(params,true); |
| | | return ObjectUtils.isEmpty(response) ? ResultMessage.ok() : ResultMessage.ok(response); |
| | | } |
| | | @GetMapping("carTrajectoryNewAvg") |
| | | @ApiOperation(value = "走航车轨迹", notes = "走航车轨迹") |
| | | @ApiImplicitParams(value = { |
| | | @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header", dataType = "String"), |
| | | @ApiImplicitParam(name = "mac", value = "设备mac", required = true, paramType = "query", dataType = "String"), |
| | | @ApiImplicitParam(name = "time1", value = "时间,2021-08-18", required = true, paramType = "query", dataType = "String"), |
| | | @ApiImplicitParam(name = "time2", value = "时间,2021-08-18", required = true, paramType = "query", dataType = "String"), |
| | | @ApiImplicitParam(name = "type", value = "采集因子", required = true, paramType = "query", dataType = "String"), |
| | | @ApiImplicitParam(name = "range", value = "范围", required = true, paramType = "query", dataType = "int") |
| | | }) |
| | | public ResultMessage carTrajectoryNewAvg(HttpServletRequest request) { |
| | | Map<String, Object> params = WebUtils.getParametersStartingWith(request, null); |
| | | if (!params.containsKey("mac") || !params.containsKey("time1")||!params.containsKey("range")|| !params.containsKey("type")|| !params.containsKey("time2")) { |
| | | return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(), ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg()); |
| | | } |
| | | List<HistorySecondCruiserResultDTO> response = specialDeviceService.carTrajectoryNewAvg(params); |
| | | |
| | | //计算执行时间 |
| | | |
| | | return ObjectUtils.isEmpty(response) ? ResultMessage.ok() : ResultMessage.ok(response); |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 走航车日报 |
| | | * @param request |
| | | * @return |
| | | */ |
| | | @PostMapping("getDaily") |
| | | @ApiOperation(value = "走航车日报", notes = "走航车日报") |
| | | @ApiImplicitParams(value = { |
| | | @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header", dataType = "String"), |
| | | @ApiImplicitParam(name = "mac", value = "设备mac", required = true, paramType = "query", dataType = "String"), |
| | | @ApiImplicitParam(name = "time1", value = "开始时间,2021-08-18", required = true, paramType = "query", dataType = "String"), |
| | | @ApiImplicitParam(name = "time2", value = "结束时间,2021-08-18", required = true, paramType = "query", dataType = "String") |
| | | }) |
| | | public ResultMessage getDaily(HttpServletRequest request) throws ParseException { |
| | | Map<String, Object> params = WebUtils.getParametersStartingWith(request, null); |
| | | if (!params.containsKey("mac") || !params.containsKey("time1")|| !params.containsKey("time2")) { |
| | | return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(), ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg()); |
| | | } |
| | | List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("files"); |
| | | ExcelBO daily = specialDeviceService.getDaily(params, files); |
| | | if (daily==null){ |
| | | return ResultMessage.fail(ResponseCodeEnum.TARGET_IS_NULL.getCode(), ResponseCodeEnum.TARGET_IS_NULL.getMsg()); |
| | | } |
| | | return ObjectUtils.isEmpty(daily) ? ResultMessage.ok() : ResultMessage.ok(daily); |
| | | } |
| | | |
| | | /** |
| | | * 下载走航车日报 |
| | | * @param id |
| | | * @return |
| | | */ |
| | | @GetMapping("loadDaily") |
| | | public ResultMessage loadDaily(Integer id){ |
| | | |
| | | DailyVo dailyVo = specialDeviceService.loadDaily(id); |
| | | if (dailyVo==null){ |
| | | return ResultMessage.fail(ResponseCodeEnum.TARGET_IS_NULL.getCode(), ResponseCodeEnum.TARGET_IS_NULL.getMsg()); |
| | | } |
| | | return new ResultMessage(ResponseCodeEnum.SUCCESS.getCode(), ResponseCodeEnum.SUCCESS.getMsg(),dailyVo); |
| | | } |
| | | |
| | | /** |
| | | * 查询日报 |
| | | * @param request |
| | | * @return |
| | | */ |
| | | @GetMapping("selectDaily") |
| | | public ResultMessage selectDaily(HttpServletRequest request){ |
| | | Map<String, Object> params = WebUtils.getParametersStartingWith(request, null); |
| | | if ( !params.containsKey("startTime")|| !params.containsKey("endTime")) { |
| | | return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(), ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg()); |
| | | } |
| | | List<ExcelBO> excelBOS = specialDeviceService.selectDaily(params); |
| | | if (excelBOS==null){ |
| | | return ResultMessage.fail(ResponseCodeEnum.TARGET_IS_NULL.getCode(), ResponseCodeEnum.TARGET_IS_NULL.getMsg()); |
| | | } |
| | | return new ResultMessage(ResponseCodeEnum.SUCCESS.getCode(), ResponseCodeEnum.SUCCESS.getMsg(),excelBOS); |
| | | } |
| | | |
| | | @Autowired |
| | | private DustldService dustldService; |
| | | |
| | | /** |
| | | * 尘负荷走航 |
| | | * @param request |
| | | * @return |
| | | */ |
| | | @PostMapping("getDailyDustld") |
| | | public ResultMessage getDailyDustld(HttpServletRequest request) { |
| | | Map<String, Object> params = WebUtils.getParametersStartingWith(request, null); |
| | | if (!params.containsKey("mac") || !params.containsKey("road") || !params.containsKey("table")){ |
| | | return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(), ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg()); |
| | | } |
| | | MultipartFile file = ((MultipartHttpServletRequest) request).getFile("files"); |
| | | |
| | | Integer dailyDustld = null; |
| | | try { |
| | | dailyDustld = dustldService.getDailyDustld(params,file); |
| | | } catch (ParseException e) { |
| | | log.error("{错误}"+e.getMessage()); |
| | | } |
| | | if (ObjectUtils.isEmpty(dailyDustld)){ |
| | | return ResultMessage.fail(ResponseCodeEnum.TARGET_IS_NULL.getCode(), ResponseCodeEnum.TARGET_IS_NULL.getMsg()); |
| | | } |
| | | return new ResultMessage(ResponseCodeEnum.SUCCESS.getCode(),ResponseCodeEnum.SUCCESS.getMsg(),dailyDustld); |
| | | } |
| | | |
| | | /** |
| | | * 下载尘负荷报告 |
| | | * @return |
| | | */ |
| | | @PostMapping("dailyDustld") |
| | | public ResultMessage dailyDustld(HttpServletRequest request,Integer id){ |
| | | List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("files"); |
| | | |
| | | Map<String, Object> map = dustldService.dailyDustld(id,files); |
| | | if (ObjectUtils.isEmpty(map)){ |
| | | return ResultMessage.fail(ResponseCodeEnum.TARGET_IS_NULL.getCode(), ResponseCodeEnum.TARGET_IS_NULL.getMsg()); |
| | | } |
| | | return new ResultMessage(ResponseCodeEnum.SUCCESS.getCode(),ResponseCodeEnum.SUCCESS.getMsg(),map); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 下载尘负荷报告 |
| | | * @return |
| | | */ |
| | | @PostMapping("dailyDustlds") |
| | | public ResultMessage dailyDustlds(HttpServletRequest request) throws IOException { |
| | | // List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("files"); |
| | | Map<String, Object> params = WebUtils.getParametersStartingWith(request, null); |
| | | |
| | | |
| | | Map<String, Object> map = dustldService.dailyDustlds(null,params); |
| | | test(map); |
| | | if (ObjectUtils.isEmpty(map)){ |
| | | return ResultMessage.fail(ResponseCodeEnum.TARGET_IS_NULL.getCode(), ResponseCodeEnum.TARGET_IS_NULL.getMsg()); |
| | | } |
| | | return new ResultMessage(ResponseCodeEnum.SUCCESS.getCode(),ResponseCodeEnum.SUCCESS.getMsg(),map); |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | /** |
| | | * 查看路段高值 |
| | | * @param id |
| | | * @return |
| | | */ |
| | | @GetMapping("selectDust") |
| | | public ResultMessage selectDust(Integer id){ |
| | | Map<String, Object> map = null; |
| | | try { |
| | | map = dustldService.selectDust(id); |
| | | } catch (Exception e) { |
| | | log.error("{错误}"+e.getMessage()); |
| | | } |
| | | return new ResultMessage(ResponseCodeEnum.SUCCESS.getCode(),ResponseCodeEnum.SUCCESS.getMsg(),map); |
| | | } |
| | | |
| | | /** |
| | | * 查询一段时间的路段数据 |
| | | * @param request |
| | | * @return |
| | | */ |
| | | @GetMapping("selectAll") |
| | | public ResultMessage selectAll(HttpServletRequest request){ |
| | | Map<String, Object> params = WebUtils.getParametersStartingWith(request, null); |
| | | List<Dustld> dustlds = null; |
| | | try { |
| | | dustlds = dustldService.selectAll(params); |
| | | } catch (Exception e) { |
| | | log.error("{错误}"+e.getMessage()); |
| | | } |
| | | return new ResultMessage(ResponseCodeEnum.SUCCESS.getCode(),ResponseCodeEnum.SUCCESS.getMsg(),dustlds); |
| | | |
| | | } |
| | | |
| | | |
| | | |
| | | public static void downloadWord(String fileName, Map<String, Object> params, HttpServletResponse response, String newFileName) { |
| | | String path = getPath(fileName);//我这放 resources包下 |
| | | try { |
| | | //获取模板文档 |
| | | OutputStream out = new FileOutputStream("G:\\home\\mon1\\" + newFileName); |
| | | File rootFile = new File(ResourceUtils.getURL("classpath:").getPath()); |
| | | File file= new File(rootFile, path); |
| | | XWPFDocument word = WordExportUtil.exportWord07(file.getPath(), params); |
| | | response.setCharacterEncoding("utf-8"); |
| | | response.setContentType("multipart/form-data"); |
| | | response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(newFileName, "utf-8")); |
| | | //OutputStream out = response.getOutputStream(); |
| | | word.write(out); |
| | | out.flush(); |
| | | out.close(); |
| | | } catch (Exception e) { |
| | | log.error ("导出失败,请联系网站管理员!", e); |
| | | } |
| | | } |
| | | |
| | | |
| | | public void test( Map<String, Object> params) throws IOException { |
| | | // String path="D:\\尘负荷监测报告.docx"; |
| | | String path = getPath("尘负荷监测报告.docx"); |
| | | //获取当前模板的路径 |
| | | File rootFile = new File(ResourceUtils.getURL("classpath:").getPath()); |
| | | File file= new File(rootFile, path); |
| | | //第一步:读取模板 |
| | | XWPFTemplate compile = XWPFTemplate.compile(file.getPath()); |
| | | //第二步:创建数据,用于把我们模板中的{{}}包裹的变量替换掉,map中保存的key一定要和模板中的变量保持一致 |
| | | HashMap<String, Object> map = new HashMap<>(); |
| | | //设置单元格,表头 |
| | | RowRenderData header1 = RowRenderData.build(new TextRenderData("6495ED", "序号"), new TextRenderData("6495ED", "道路"),new TextRenderData("6495ED", "平均尘负荷(克/平方米)")); |
| | | RowRenderData header2 = RowRenderData.build(new TextRenderData("6495ED", "序号"), new TextRenderData("6495ED", "道路"),new TextRenderData("6495ED", "平均尘负荷(克/平方米)")); |
| | | |
| | | //设置表内容,如果在实际应用中,传入一个list集合或者数组,方可用foreach进行循环填充 |
| | | List<DustForm> list1 = (List<DustForm>) params.get("list1"); |
| | | List<DustForm> list2 = (List<DustForm>) params.get("list2"); |
| | | ArrayList<RowRenderData> rsList1 = new ArrayList<>(); |
| | | ArrayList<RowRenderData> rsList2 = new ArrayList<>(); |
| | | |
| | | for (int i = 0; i < list1.size(); i++) { |
| | | DustForm dustForm = list1.get(i); |
| | | RowRenderData row0 = RowRenderData.build(i+1+"",dustForm.getRoad(), dustForm.getValue()+""); |
| | | rsList1.add(row0); |
| | | } |
| | | for (int i = 0; i < list2.size(); i++) { |
| | | DustForm dustForm = list2.get(i); |
| | | RowRenderData row0 = RowRenderData.build(i+1+"",dustForm.getRoad(), dustForm.getValue()+""); |
| | | rsList2.add(row0); |
| | | } |
| | | |
| | | map.put("table1", new MiniTableRenderData(header1, rsList1)); |
| | | map.put("table2", new MiniTableRenderData(header2, rsList2)); |
| | | map.put("data",params.get("data").toString()); |
| | | map.put("time",params.get("time").toString()); |
| | | |
| | | compile.render(map); |
| | | // compile.write(new FileOutptStream("尘负荷监测报告"+paramsu.get("time").toString()+".docx")); |
| | | compile.writeToFile("G:\\home\\mon1\\" + "尘负荷监测报告"+params.get("time").toString()+".docx"); |
| | | compile.close(); |
| | | } |
| | | |
| | | |
| | | } |