Showing
1 changed file
with
100 additions
and
0 deletions
| 1 | package com.lframework.xingyun.sc.controller.sample; | 1 | package com.lframework.xingyun.sc.controller.sample; |
| 2 | 2 | ||
| 3 | +import cn.hutool.core.io.resource.ClassPathResource; | ||
| 3 | import com.lframework.xingyun.sc.bo.sample.feedback.GetSampleResultFeedbackBo; | 4 | import com.lframework.xingyun.sc.bo.sample.feedback.GetSampleResultFeedbackBo; |
| 4 | import com.lframework.xingyun.sc.bo.sample.feedback.QuerySampleResultFeedbackBo; | 5 | import com.lframework.xingyun.sc.bo.sample.feedback.QuerySampleResultFeedbackBo; |
| 6 | +import com.lframework.xingyun.sc.entity.SampleFeedbackTrackingDetail; | ||
| 7 | +import com.lframework.xingyun.sc.utils.ExcelUtil; | ||
| 8 | +import com.lframework.xingyun.sc.utils.LatexFormulaExcelExporterUtil; | ||
| 9 | +import com.lframework.xingyun.sc.utils.ResponseUtil; | ||
| 5 | import com.lframework.xingyun.sc.vo.sample.feedback.QuerySampleResultFeedbackVo; | 10 | import com.lframework.xingyun.sc.vo.sample.feedback.QuerySampleResultFeedbackVo; |
| 6 | import com.lframework.xingyun.sc.service.sample.SampleResultFeedbackService; | 11 | import com.lframework.xingyun.sc.service.sample.SampleResultFeedbackService; |
| 7 | import com.lframework.xingyun.sc.entity.SampleResultFeedback; | 12 | import com.lframework.xingyun.sc.entity.SampleResultFeedback; |
| @@ -10,6 +15,7 @@ import com.lframework.starter.web.core.components.resp.PageResult; | @@ -10,6 +15,7 @@ import com.lframework.starter.web.core.components.resp.PageResult; | ||
| 10 | import com.lframework.starter.web.core.components.resp.InvokeResult; | 15 | import com.lframework.starter.web.core.components.resp.InvokeResult; |
| 11 | 16 | ||
| 12 | import javax.annotation.Resource; | 17 | import javax.annotation.Resource; |
| 18 | +import javax.servlet.http.HttpServletResponse; | ||
| 13 | import javax.validation.constraints.NotBlank; | 19 | import javax.validation.constraints.NotBlank; |
| 14 | import io.swagger.annotations.ApiImplicitParam; | 20 | import io.swagger.annotations.ApiImplicitParam; |
| 15 | import com.lframework.starter.web.core.components.resp.InvokeResultBuilder; | 21 | import com.lframework.starter.web.core.components.resp.InvokeResultBuilder; |
| @@ -17,6 +23,11 @@ import com.lframework.starter.common.exceptions.impl.DefaultClientException; | @@ -17,6 +23,11 @@ import com.lframework.starter.common.exceptions.impl.DefaultClientException; | ||
| 17 | import io.swagger.annotations.ApiOperation; | 23 | import io.swagger.annotations.ApiOperation; |
| 18 | import com.lframework.starter.common.utils.CollectionUtil; | 24 | import com.lframework.starter.common.utils.CollectionUtil; |
| 19 | import io.swagger.annotations.Api; | 25 | import io.swagger.annotations.Api; |
| 26 | +import lombok.extern.slf4j.Slf4j; | ||
| 27 | +import org.apache.commons.lang3.StringUtils; | ||
| 28 | +import org.apache.poi.ss.usermodel.Sheet; | ||
| 29 | +import org.apache.poi.ss.usermodel.Workbook; | ||
| 30 | +import org.apache.poi.xssf.usermodel.XSSFWorkbook; | ||
| 20 | import org.springframework.web.bind.annotation.DeleteMapping; | 31 | import org.springframework.web.bind.annotation.DeleteMapping; |
| 21 | import com.lframework.starter.web.core.controller.DefaultBaseController; | 32 | import com.lframework.starter.web.core.controller.DefaultBaseController; |
| 22 | import com.lframework.starter.web.core.annotations.security.HasPermission; | 33 | import com.lframework.starter.web.core.annotations.security.HasPermission; |
| @@ -24,7 +35,12 @@ import org.springframework.validation.annotation.Validated; | @@ -24,7 +35,12 @@ import org.springframework.validation.annotation.Validated; | ||
| 24 | import org.springframework.web.bind.annotation.*; | 35 | import org.springframework.web.bind.annotation.*; |
| 25 | 36 | ||
| 26 | import javax.validation.Valid; | 37 | import javax.validation.Valid; |
| 38 | +import java.io.InputStream; | ||
| 39 | +import java.time.format.DateTimeFormatter; | ||
| 40 | +import java.util.ArrayList; | ||
| 41 | +import java.util.HashMap; | ||
| 27 | import java.util.List; | 42 | import java.util.List; |
| 43 | +import java.util.Map; | ||
| 28 | import java.util.stream.Collectors; | 44 | import java.util.stream.Collectors; |
| 29 | 45 | ||
| 30 | /** | 46 | /** |
| @@ -32,6 +48,7 @@ import java.util.stream.Collectors; | @@ -32,6 +48,7 @@ import java.util.stream.Collectors; | ||
| 32 | * | 48 | * |
| 33 | */ | 49 | */ |
| 34 | @Api(tags = "产品试样结果反馈单") | 50 | @Api(tags = "产品试样结果反馈单") |
| 51 | +@Slf4j | ||
| 35 | @Validated | 52 | @Validated |
| 36 | @RestController | 53 | @RestController |
| 37 | @RequestMapping("/sample/feedback") | 54 | @RequestMapping("/sample/feedback") |
| @@ -86,4 +103,87 @@ public class SampleResultFeedbackController extends DefaultBaseController { | @@ -86,4 +103,87 @@ public class SampleResultFeedbackController extends DefaultBaseController { | ||
| 86 | sampleResultFeedbackService.deleteById(id); | 103 | sampleResultFeedbackService.deleteById(id); |
| 87 | return InvokeResultBuilder.success(); | 104 | return InvokeResultBuilder.success(); |
| 88 | } | 105 | } |
| 106 | + | ||
| 107 | + | ||
| 108 | + /** | ||
| 109 | + * 导出反馈单数据 | ||
| 110 | + * 支持PDF、EXCEL俩种格式 | ||
| 111 | + * @param id 主键ID | ||
| 112 | + * @param response 响应体 | ||
| 113 | + */ | ||
| 114 | + @ApiOperation("规格变更打印") | ||
| 115 | + @GetMapping("/export") | ||
| 116 | + public void export(@NotBlank(message = "id不能为空") String id, @NotBlank(message = "导出类型不能为空") String exportType | ||
| 117 | + , HttpServletResponse response) { | ||
| 118 | + SampleResultFeedback data = sampleResultFeedbackService.findById(id); | ||
| 119 | + if (data == null) { | ||
| 120 | + throw new DefaultClientException("样品反馈单数据不存在!"); | ||
| 121 | + } | ||
| 122 | + ClassPathResource templateResource = new ClassPathResource("templates/sampleFeedbackTemplate.xlsx"); | ||
| 123 | + try (InputStream inputStream = templateResource.getStream(); | ||
| 124 | + Workbook workbook = new XSSFWorkbook(inputStream)) { | ||
| 125 | + Sheet sheet = workbook.getSheetAt(0); | ||
| 126 | + ResponseUtil.setExcelResponseHead(response, data.getOrderNo() + "-试样结果反馈单.xlsx"); | ||
| 127 | + DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); | ||
| 128 | + List<SampleFeedbackTrackingDetail> detailList = data.getDetailList(); | ||
| 129 | + // ========== 起始行:5========== | ||
| 130 | + int startRow = 4; | ||
| 131 | + for (int i = startRow + 1; i < startRow + detailList.size(); i++) { | ||
| 132 | + ExcelUtil.copyRow(workbook, sheet, startRow, i); | ||
| 133 | + } | ||
| 134 | + for (int i = 0; i < detailList.size(); i++) { | ||
| 135 | + SampleFeedbackTrackingDetail detail = detailList.get(i); | ||
| 136 | + int rowIdx = startRow + i; | ||
| 137 | + ExcelUtil.setCellValue(sheet, rowIdx, 1, detail.getBrand()); | ||
| 138 | + List<LatexFormulaExcelExporterUtil.FormulaComponent> formulaComponentList = new ArrayList<>(3); | ||
| 139 | + if (detail.getThickness() != null) { | ||
| 140 | + LatexFormulaExcelExporterUtil.FormulaComponent formulaComponent = new LatexFormulaExcelExporterUtil.FormulaComponent(); | ||
| 141 | + formulaComponent.setBase(detail.getThickness()); | ||
| 142 | + formulaComponent.setSup(detail.getThicknessTolPos()); | ||
| 143 | + formulaComponent.setSub(detail.getThicknessTolNeg()); | ||
| 144 | + formulaComponentList.add(formulaComponent); | ||
| 145 | + } | ||
| 146 | + if (detail.getWidth() != null) { | ||
| 147 | + LatexFormulaExcelExporterUtil.FormulaComponent formulaComponent = new LatexFormulaExcelExporterUtil.FormulaComponent(); | ||
| 148 | + formulaComponent.setBase(detail.getWidth()); | ||
| 149 | + formulaComponent.setSup(detail.getWidthTolPos()); | ||
| 150 | + formulaComponent.setSub(detail.getWidthTolNeg()); | ||
| 151 | + formulaComponentList.add(formulaComponent); | ||
| 152 | + } | ||
| 153 | + if (detail.getLength() != null) { | ||
| 154 | + LatexFormulaExcelExporterUtil.FormulaComponent formulaComponent = new LatexFormulaExcelExporterUtil.FormulaComponent(); | ||
| 155 | + formulaComponent.setBase(detail.getLength()); | ||
| 156 | + formulaComponent.setSup(detail.getLengthTolPos()); | ||
| 157 | + formulaComponent.setSub(detail.getLengthTolNeg()); | ||
| 158 | + formulaComponentList.add(formulaComponent); | ||
| 159 | + } | ||
| 160 | + String latex = LatexFormulaExcelExporterUtil.convertToLatex(formulaComponentList); | ||
| 161 | + if (StringUtils.isNotBlank(latex)) { | ||
| 162 | + LatexFormulaExcelExporterUtil.insertLatexImageToCell(workbook, sheet, latex, rowIdx, 2, 1, 2); | ||
| 163 | + } | ||
| 164 | + ExcelUtil.setCellValue(sheet, rowIdx, 4, detail.getStatus()); | ||
| 165 | + ExcelUtil.setCellValue(sheet, rowIdx, 5, detail.getShipmentDate()); | ||
| 166 | + ExcelUtil.setCellValue(sheet, rowIdx, 6, detail.getQuantity()); | ||
| 167 | + ExcelUtil.setCellValue(sheet, rowIdx, 7, detail.getYieldBatchNo()); | ||
| 168 | + } | ||
| 169 | + // ========== 填充顶部固定字段 ========== | ||
| 170 | + Map<String, Object> dataMap = new HashMap<>(); | ||
| 171 | + dataMap.put("customerName", data.getCustomerName()); | ||
| 172 | + dataMap.put("workshopName", data.getWorkshopName()); | ||
| 173 | + dataMap.put("orderNo", data.getOrderNo()); | ||
| 174 | + | ||
| 175 | + ExcelUtil.processTemplate(workbook, dataMap); | ||
| 176 | + if ("PDF".equals(exportType)) { | ||
| 177 | + // 输出为PDF文档 | ||
| 178 | + } else { | ||
| 179 | + // 输出 | ||
| 180 | + workbook.write(response.getOutputStream()); | ||
| 181 | + response.getOutputStream().flush(); | ||
| 182 | + } | ||
| 183 | + } catch (Exception e) { | ||
| 184 | + log.error("导出反馈单导出失败:", e); | ||
| 185 | + throw new DefaultClientException("导出反馈单导出失败:" + e.getMessage()); | ||
| 186 | + } | ||
| 187 | + } | ||
| 188 | + | ||
| 89 | } | 189 | } |