| | |
| | | import com.emergencyrepair.business.entity.Fault; |
| | | import com.emergencyrepair.business.entity.WorkOrder; |
| | | import com.emergencyrepair.business.service.BusinessService; |
| | | import com.emergencyrepair.business.service.EngineeringOrderPrintService; |
| | | import com.emergencyrepair.common.Result; |
| | | import lombok.RequiredArgsConstructor; |
| | | import org.springframework.http.HttpHeaders; |
| | | import org.springframework.http.HttpStatus; |
| | | import org.springframework.http.MediaType; |
| | | import org.springframework.http.ResponseEntity; |
| | | import org.springframework.web.bind.annotation.DeleteMapping; |
| | | import org.springframework.web.bind.annotation.GetMapping; |
| | | import org.springframework.web.bind.annotation.PathVariable; |
| | |
| | | import org.springframework.format.annotation.DateTimeFormat; |
| | | |
| | | import java.time.LocalDate; |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.net.URLEncoder; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.util.List; |
| | | |
| | | @RestController |
| | |
| | | @RequiredArgsConstructor |
| | | public class FaultController { |
| | | private final BusinessService businessService; |
| | | private final EngineeringOrderPrintService engineeringOrderPrintService; |
| | | |
| | | @GetMapping("/page") |
| | | public Result<Page<Fault>> page(@RequestParam(defaultValue = "1") long current, |
| | |
| | | public Result<List<WorkOrder>> dispatch(@RequestBody Requests.DispatchRequest request) { |
| | | return Result.ok(businessService.dispatch(request)); |
| | | } |
| | | |
| | | /** |
| | | * 工程单打印:把选中的故障记录套用 Word 模板生成四联工程单。 |
| | | */ |
| | | @PostMapping("/engineering-print") |
| | | public ResponseEntity<byte[]> engineeringPrint(@RequestBody Requests.IdList request) { |
| | | return documentResponse(engineeringOrderPrintService.render(request.getIds()), |
| | | "工程单.docx", "engineering-order.docx"); |
| | | } |
| | | |
| | | /** |
| | | * 打印工程单2:第1联全部明细后换页,再输出第2联,以此类推。 |
| | | */ |
| | | @PostMapping("/engineering-print-v2") |
| | | public ResponseEntity<byte[]> engineeringPrintV2(@RequestBody Requests.IdList request) { |
| | | return documentResponse(engineeringOrderPrintService.renderVersion2(request.getIds()), |
| | | "工程单2.docx", "engineering-order-2.docx"); |
| | | } |
| | | |
| | | private ResponseEntity<byte[]> documentResponse(byte[] document, String displayName, String fallbackName) { |
| | | String filename = encodeFilename(displayName, fallbackName); |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | headers.setContentType(MediaType.parseMediaType( |
| | | "application/vnd.openxmlformats-officedocument.wordprocessingml.document")); |
| | | headers.set(HttpHeaders.CONTENT_DISPOSITION, |
| | | "attachment; filename=\"" + fallbackName + "\"; filename*=UTF-8''" + filename); |
| | | headers.setContentLength(document.length); |
| | | return new ResponseEntity<>(document, headers, HttpStatus.OK); |
| | | } |
| | | |
| | | private String encodeFilename(String name, String fallbackName) { |
| | | try { |
| | | return URLEncoder.encode(name, StandardCharsets.UTF_8.name()).replace("+", "%20"); |
| | | } catch (UnsupportedEncodingException exception) { |
| | | return fallbackName; |
| | | } |
| | | } |
| | | } |