package com.fdkankan.sale.controller; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.io.FileUtil; import com.alibaba.fastjson.JSONObject; import com.baomidou.dynamic.datasource.annotation.DS; import com.dtflys.forest.annotation.Post; import com.dtflys.forest.annotation.Var; import com.fdkankan.fyun.face.FYunFileServiceInterface; import com.fdkankan.sale.common.ResultCode; import com.fdkankan.sale.common.ResultData; import com.fdkankan.sale.entity.Camera; import com.fdkankan.sale.entity.CameraDetail; import com.fdkankan.sale.exception.BusinessException; import com.fdkankan.sale.httpClient.client.DingClient; import com.fdkankan.sale.httpClient.request.DingMsgReq; import com.fdkankan.sale.httpClient.request.MsgData; import com.fdkankan.sale.service.ICameraDetailService; import com.fdkankan.sale.service.ICameraService; import com.fdkankan.sale.service.IPriceListService; import com.fdkankan.sale.service.ISysUserService; import com.fdkankan.sale.service.impl.RepairInfoService; import com.fdkankan.sale.util.DateUtil; import com.fdkankan.sale.util.ExcelUtil; import com.fdkankan.sale.util.OrderListVo; import com.fdkankan.sale.util.pdf.PdfUtil; import com.fdkankan.sale.util.pdf.PdfUtils; import com.fdkankan.sale.vo.response.PriceListExcelVo; import com.itextpdf.text.DocumentException; import lombok.Data; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.OutputStream; import java.net.URLEncoder; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; @RestController @RequestMapping("/sale/test") @Slf4j public class TestController { @Autowired ISysUserService sysUserService; @Autowired PdfUtil pdfUtil; @Autowired IPriceListService priceListService; @Autowired RepairInfoService repairInfoService; @Autowired ICameraService cameraService; @Autowired ICameraDetailService cameraDetailService; @Autowired DingClient dingClient; @Autowired FYunFileServiceInterface fYunFileServiceInterface; @GetMapping("/testD") public ResultData testD(HttpServletResponse response) throws IOException, DocumentException { //模板中需要批量添加的,list{.**},obj{**}中的数据, JSONObject token = dingClient.getToken("dingefudkq6mrpijygvk", "CcFoQeo3g3ZOPzyWdpFe89b_bFq4TJfmbqG2ycmbESzMu3nlCeXjvKyj7vTaSyyC"); String access_token = token.getString("access_token"); //JSONObject userByMobile = dingClient.getUserByMobile(access_token, "13825625448"); //01102853516526346000,020000431330571403 //String userid = userByMobile.getString("userid"); DingMsgReq dingMsgReq = new DingMsgReq(); dingMsgReq.setAgent_id("2581138442"); dingMsgReq.setUserid_list("01102853516526346000"); String fileContent = fYunFileServiceInterface.getFileContent("sale/file/common/sale/OA.json"); fileContent = fileContent.replaceAll("\\{redirectUrl}", URLEncoder.encode("https://v4-uat.4dkankan.com/aftermarketmp/#/work/detail/20230330163516368")); dingMsgReq.setMsg(JSONObject.parseObject(fileContent)); JSONObject jsonObject = dingClient.sendMsg(access_token, dingMsgReq); return ResultData.ok(jsonObject); } @GetMapping("/test1") public ResultData test1(HttpServletResponse response) throws IOException, DocumentException { //模板中需要批量添加的,list{.**},obj{**}中的数据, pdfUtil.createPdfFile(response); return ResultData.ok(); } /** * 导出数据 生成pdf * * http://localhost:8188/service/test/exportPdf?repairId=20230317163855405 */ @GetMapping("/exportPdf") public void exportPdf(@RequestParam(required = false) String repairId,HttpServletResponse response) { // 模板路径 String tempPath = "G:\\templates\\pdf\\四维时代售后服务清单-模版.pdf"; //文字类 // dataMap中的key要和模板中的域名对应 OrderListVo orderListVo = repairInfoService.getExportVo(repairId,true); Map dataMap = BeanUtil.beanToMap(orderListVo); //图片 Map imgMap = BeanUtil.beanToMap(orderListVo.getExportImg()); // imgMap中的key要和模板中的域名对应 //表格 一行数据是一个list List list = new ArrayList<>(); list.add("序号"); list.add("项目"); list.add("单价(元)"); list.add("数量"); list.add("总价(元)"); list.add("备注"); List> partTableList = new ArrayList<>(); partTableList.add(list); List priceListExcelVos = orderListVo.getPriceListExcelVos(); for (PriceListExcelVo priceListExcelVo : priceListExcelVos) { List listVo = new ArrayList<>(); Map map = BeanUtil.beanToMap(priceListExcelVo); for (Map.Entry entry : map.entrySet()) { listVo.add(entry.getValue().toString()); } if(listVo.size() <6){ listVo.add(""); } partTableList.add(listVo); } Map>> listMap = new HashMap<>(); // 这里的listMap中key要和模板中的域名对应 listMap.put("partTable", partTableList); Map o = new HashMap<>(); o.put("tempPath", tempPath); o.put("dataMap", dataMap); o.put("imgMap", imgMap); o.put("tableList", listMap); try { OutputStream out = response.getOutputStream(); response.setCharacterEncoding("utf-8"); response.setHeader("content-type", "application/octet-stream"); String fileName = URLEncoder.encode("pdf模板导出" + ".pdf", "UTF-8"); response.setHeader("Content-Disposition", "attachment;filename=" + fileName); try { PdfUtils.creatPdf(o, out); } catch (Exception e) { e.printStackTrace(); } out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } } @PostMapping("/reCameraBuyDate") public ResultData reCameraBuyDate(@RequestParam(required = false) MultipartFile file) throws IOException { String originalFilename = file.getOriginalFilename(); assert originalFilename != null; String fileType=originalFilename.substring(originalFilename.lastIndexOf(".")+1); if (!fileType.equalsIgnoreCase("xlsx")) { throw new BusinessException(-1,"文件错误"); } for(int i = 0; i<=5; i++){ List> excelRowList = ExcelUtil.getExcelRowList(file,i); this.updateBuyDate(excelRowList,i); } return ResultData.ok(); } @DS("db2") private void updateBuyDate(List> excelRowList,Integer Index){ Date parse = null; String date = null; try { int i = 0; for (HashMap map : excelRowList) { i ++; if(map.isEmpty()){ continue; } date = map.get(0); String snCode = map.get(1); Camera camera = cameraService.getBySnCode(snCode); if(camera == null){ log.info("db-not-in:date:{},snCode:{},index:{},colum:{}",date,snCode,Index+1,i); continue; } if(StringUtils.isBlank(date)){ log.info("excel-not-in:date:{}",date); continue; } String date2 = checkChinaDate(date); if(date2 == null){ log.info("excel-not-format:date:{}",date); continue; } try { parse = new SimpleDateFormat("yyyy/MM/dd").parse(date2); }catch ( Exception e){ parse = new SimpleDateFormat("yyyy.MM.dd").parse(date2); } if(parse == null){ log.info("data:{},snCode:{}处理失败",date,snCode); continue; } CameraDetail cameraDetail = cameraService.getCameraTypeByCameraId(camera.getId()); String datePre = DateUtil.getDate("yyyy-MM-dd",parse) + " 23:59:59"; cameraDetail.setBuyDate(datePre); cameraService.updateDetail(cameraDetail); } }catch (Exception e){ log.error("excel-error-in:date:{}",date); e.printStackTrace(); } } private static String checkChinaDate(String date) { if(date.contains("月")){ String[] split = date.split("-"); String chinaMother = getChinaMother(split[1]); if(StringUtils.isBlank(chinaMother)){ return null; } date = split[2] + "/" + chinaMother + "/"+ split[0]; } return date; } private static String getChinaMother(String Mother) { switch (Mother){ case "一月" :return "01"; case "二月" :return "02"; case "三月" :return "03"; case "四月" :return "04"; case "五月" :return "05"; case "六月" :return "06"; case "七月" :return "07"; case "八月" :return "08"; case "九月" :return "09"; case "十月" :return "10"; case "十一月" :return "11"; case "十二月" :return "12"; } return null; } public static void main(String[] args) throws ParseException { System.out.println(1337 + 286 + 49 +202 + 50 +200); } }