123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- 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<String, Object> dataMap = BeanUtil.beanToMap(orderListVo);
- //图片
- Map<String, Object> imgMap = BeanUtil.beanToMap(orderListVo.getExportImg());
- // imgMap中的key要和模板中的域名对应
- //表格 一行数据是一个list
- List<String> list = new ArrayList<>();
- list.add("序号");
- list.add("项目");
- list.add("单价(元)");
- list.add("数量");
- list.add("总价(元)");
- list.add("备注");
- List<List<String>> partTableList = new ArrayList<>();
- partTableList.add(list);
- List<PriceListExcelVo> priceListExcelVos = orderListVo.getPriceListExcelVos();
- for (PriceListExcelVo priceListExcelVo : priceListExcelVos) {
- List<String> listVo = new ArrayList<>();
- Map<String, Object> map = BeanUtil.beanToMap(priceListExcelVo);
- for (Map.Entry<String, Object> entry : map.entrySet()) {
- listVo.add(entry.getValue().toString());
- }
- if(listVo.size() <6){
- listVo.add("");
- }
- partTableList.add(listVo);
- }
- Map<String, List<List<String>>> listMap = new HashMap<>();
- // 这里的listMap中key要和模板中的域名对应
- listMap.put("partTable", partTableList);
- Map<String, Object> 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<HashMap<Integer, String>> excelRowList = ExcelUtil.getExcelRowList(file,i);
- this.updateBuyDate(excelRowList,i);
- }
- return ResultData.ok();
- }
- @DS("db2")
- private void updateBuyDate(List<HashMap<Integer, String>> excelRowList,Integer Index){
- Date parse = null;
- String date = null;
- try {
- int i = 0;
- for (HashMap<Integer, String> 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);
- }
- }
|