TestController.java 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. package com.fdkankan.sale.controller;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import cn.hutool.core.io.FileUtil;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.baomidou.dynamic.datasource.annotation.DS;
  6. import com.dtflys.forest.annotation.Post;
  7. import com.dtflys.forest.annotation.Var;
  8. import com.fdkankan.fyun.face.FYunFileServiceInterface;
  9. import com.fdkankan.sale.common.ResultCode;
  10. import com.fdkankan.sale.common.ResultData;
  11. import com.fdkankan.sale.entity.Camera;
  12. import com.fdkankan.sale.entity.CameraDetail;
  13. import com.fdkankan.sale.exception.BusinessException;
  14. import com.fdkankan.sale.httpClient.client.DingClient;
  15. import com.fdkankan.sale.httpClient.request.DingMsgReq;
  16. import com.fdkankan.sale.httpClient.request.MsgData;
  17. import com.fdkankan.sale.service.ICameraDetailService;
  18. import com.fdkankan.sale.service.ICameraService;
  19. import com.fdkankan.sale.service.IPriceListService;
  20. import com.fdkankan.sale.service.ISysUserService;
  21. import com.fdkankan.sale.service.impl.RepairInfoService;
  22. import com.fdkankan.sale.util.DateUtil;
  23. import com.fdkankan.sale.util.ExcelUtil;
  24. import com.fdkankan.sale.util.OrderListVo;
  25. import com.fdkankan.sale.util.pdf.PdfUtil;
  26. import com.fdkankan.sale.util.pdf.PdfUtils;
  27. import com.fdkankan.sale.vo.response.PriceListExcelVo;
  28. import com.itextpdf.text.DocumentException;
  29. import lombok.Data;
  30. import lombok.extern.slf4j.Slf4j;
  31. import org.apache.commons.lang3.StringUtils;
  32. import org.springframework.beans.factory.annotation.Autowired;
  33. import org.springframework.web.bind.annotation.*;
  34. import org.springframework.web.multipart.MultipartFile;
  35. import javax.servlet.http.HttpServletResponse;
  36. import java.io.IOException;
  37. import java.io.OutputStream;
  38. import java.net.URLEncoder;
  39. import java.text.ParseException;
  40. import java.text.SimpleDateFormat;
  41. import java.util.*;
  42. @RestController
  43. @RequestMapping("/sale/test")
  44. @Slf4j
  45. public class TestController {
  46. @Autowired
  47. ISysUserService sysUserService;
  48. @Autowired
  49. PdfUtil pdfUtil;
  50. @Autowired
  51. IPriceListService priceListService;
  52. @Autowired
  53. RepairInfoService repairInfoService;
  54. @Autowired
  55. ICameraService cameraService;
  56. @Autowired
  57. ICameraDetailService cameraDetailService;
  58. @Autowired
  59. DingClient dingClient;
  60. @Autowired
  61. FYunFileServiceInterface fYunFileServiceInterface;
  62. @GetMapping("/testD")
  63. public ResultData testD(HttpServletResponse response) throws IOException, DocumentException {
  64. //模板中需要批量添加的,list{.**},obj{**}中的数据,
  65. JSONObject token = dingClient.getToken("dingefudkq6mrpijygvk", "CcFoQeo3g3ZOPzyWdpFe89b_bFq4TJfmbqG2ycmbESzMu3nlCeXjvKyj7vTaSyyC");
  66. String access_token = token.getString("access_token");
  67. //JSONObject userByMobile = dingClient.getUserByMobile(access_token, "13825625448");
  68. //01102853516526346000,020000431330571403
  69. //String userid = userByMobile.getString("userid");
  70. DingMsgReq dingMsgReq = new DingMsgReq();
  71. dingMsgReq.setAgent_id("2581138442");
  72. dingMsgReq.setUserid_list("01102853516526346000");
  73. String fileContent = fYunFileServiceInterface.getFileContent("sale/file/common/sale/OA.json");
  74. fileContent = fileContent.replaceAll("\\{redirectUrl}", URLEncoder.encode("https://v4-uat.4dkankan.com/aftermarketmp/#/work/detail/20230330163516368"));
  75. dingMsgReq.setMsg(JSONObject.parseObject(fileContent));
  76. JSONObject jsonObject = dingClient.sendMsg(access_token, dingMsgReq);
  77. return ResultData.ok(jsonObject);
  78. }
  79. @GetMapping("/test1")
  80. public ResultData test1(HttpServletResponse response) throws IOException, DocumentException {
  81. //模板中需要批量添加的,list{.**},obj{**}中的数据,
  82. pdfUtil.createPdfFile(response);
  83. return ResultData.ok();
  84. }
  85. /**
  86. * 导出数据 生成pdf
  87. *
  88. * http://localhost:8188/service/test/exportPdf?repairId=20230317163855405
  89. */
  90. @GetMapping("/exportPdf")
  91. public void exportPdf(@RequestParam(required = false) String repairId,HttpServletResponse response) {
  92. // 模板路径
  93. String tempPath = "G:\\templates\\pdf\\四维时代售后服务清单-模版.pdf";
  94. //文字类
  95. // dataMap中的key要和模板中的域名对应
  96. OrderListVo orderListVo = repairInfoService.getExportVo(repairId,true);
  97. Map<String, Object> dataMap = BeanUtil.beanToMap(orderListVo);
  98. //图片
  99. Map<String, Object> imgMap = BeanUtil.beanToMap(orderListVo.getExportImg());
  100. // imgMap中的key要和模板中的域名对应
  101. //表格 一行数据是一个list
  102. List<String> list = new ArrayList<>();
  103. list.add("序号");
  104. list.add("项目");
  105. list.add("单价(元)");
  106. list.add("数量");
  107. list.add("总价(元)");
  108. list.add("备注");
  109. List<List<String>> partTableList = new ArrayList<>();
  110. partTableList.add(list);
  111. List<PriceListExcelVo> priceListExcelVos = orderListVo.getPriceListExcelVos();
  112. for (PriceListExcelVo priceListExcelVo : priceListExcelVos) {
  113. List<String> listVo = new ArrayList<>();
  114. Map<String, Object> map = BeanUtil.beanToMap(priceListExcelVo);
  115. for (Map.Entry<String, Object> entry : map.entrySet()) {
  116. listVo.add(entry.getValue().toString());
  117. }
  118. if(listVo.size() <6){
  119. listVo.add("");
  120. }
  121. partTableList.add(listVo);
  122. }
  123. Map<String, List<List<String>>> listMap = new HashMap<>();
  124. // 这里的listMap中key要和模板中的域名对应
  125. listMap.put("partTable", partTableList);
  126. Map<String, Object> o = new HashMap<>();
  127. o.put("tempPath", tempPath);
  128. o.put("dataMap", dataMap);
  129. o.put("imgMap", imgMap);
  130. o.put("tableList", listMap);
  131. try {
  132. OutputStream out = response.getOutputStream();
  133. response.setCharacterEncoding("utf-8");
  134. response.setHeader("content-type", "application/octet-stream");
  135. String fileName = URLEncoder.encode("pdf模板导出" + ".pdf", "UTF-8");
  136. response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
  137. try {
  138. PdfUtils.creatPdf(o, out);
  139. } catch (Exception e) {
  140. e.printStackTrace();
  141. }
  142. out.flush();
  143. out.close();
  144. } catch (IOException e) {
  145. e.printStackTrace();
  146. }
  147. }
  148. @PostMapping("/reCameraBuyDate")
  149. public ResultData reCameraBuyDate(@RequestParam(required = false) MultipartFile file) throws IOException {
  150. String originalFilename = file.getOriginalFilename();
  151. assert originalFilename != null;
  152. String fileType=originalFilename.substring(originalFilename.lastIndexOf(".")+1);
  153. if (!fileType.equalsIgnoreCase("xlsx")) {
  154. throw new BusinessException(-1,"文件错误");
  155. }
  156. for(int i = 0; i<=5; i++){
  157. List<HashMap<Integer, String>> excelRowList = ExcelUtil.getExcelRowList(file,i);
  158. this.updateBuyDate(excelRowList,i);
  159. }
  160. return ResultData.ok();
  161. }
  162. @DS("db2")
  163. private void updateBuyDate(List<HashMap<Integer, String>> excelRowList,Integer Index){
  164. Date parse = null;
  165. String date = null;
  166. try {
  167. int i = 0;
  168. for (HashMap<Integer, String> map : excelRowList) {
  169. i ++;
  170. if(map.isEmpty()){
  171. continue;
  172. }
  173. date = map.get(0);
  174. String snCode = map.get(1);
  175. Camera camera = cameraService.getBySnCode(snCode);
  176. if(camera == null){
  177. log.info("db-not-in:date:{},snCode:{},index:{},colum:{}",date,snCode,Index+1,i);
  178. continue;
  179. }
  180. if(StringUtils.isBlank(date)){
  181. log.info("excel-not-in:date:{}",date);
  182. continue;
  183. }
  184. String date2 = checkChinaDate(date);
  185. if(date2 == null){
  186. log.info("excel-not-format:date:{}",date);
  187. continue;
  188. }
  189. try {
  190. parse = new SimpleDateFormat("yyyy/MM/dd").parse(date2);
  191. }catch ( Exception e){
  192. parse = new SimpleDateFormat("yyyy.MM.dd").parse(date2);
  193. }
  194. if(parse == null){
  195. log.info("data:{},snCode:{}处理失败",date,snCode);
  196. continue;
  197. }
  198. CameraDetail cameraDetail = cameraService.getCameraTypeByCameraId(camera.getId());
  199. String datePre = DateUtil.getDate("yyyy-MM-dd",parse) + " 23:59:59";
  200. cameraDetail.setBuyDate(datePre);
  201. cameraService.updateDetail(cameraDetail);
  202. }
  203. }catch (Exception e){
  204. log.error("excel-error-in:date:{}",date);
  205. e.printStackTrace();
  206. }
  207. }
  208. private static String checkChinaDate(String date) {
  209. if(date.contains("月")){
  210. String[] split = date.split("-");
  211. String chinaMother = getChinaMother(split[1]);
  212. if(StringUtils.isBlank(chinaMother)){
  213. return null;
  214. }
  215. date = split[2] + "/" + chinaMother + "/"+ split[0];
  216. }
  217. return date;
  218. }
  219. private static String getChinaMother(String Mother) {
  220. switch (Mother){
  221. case "一月" :return "01";
  222. case "二月" :return "02";
  223. case "三月" :return "03";
  224. case "四月" :return "04";
  225. case "五月" :return "05";
  226. case "六月" :return "06";
  227. case "七月" :return "07";
  228. case "八月" :return "08";
  229. case "九月" :return "09";
  230. case "十月" :return "10";
  231. case "十一月" :return "11";
  232. case "十二月" :return "12";
  233. }
  234. return null;
  235. }
  236. public static void main(String[] args) throws ParseException {
  237. System.out.println(1337 + 286 + 49 +202 + 50 +200);
  238. }
  239. }