SaleBatchController.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package com.izouma.awesomeAdmin.web;
  2. import com.izouma.awesomeAdmin.domain.SaleBatch;
  3. import com.izouma.awesomeAdmin.domain.SalesBatchExtension;
  4. import com.izouma.awesomeAdmin.dto.SaleBatchDto;
  5. import com.izouma.awesomeAdmin.repo.SalesBatchExtensionRepo;
  6. import com.izouma.awesomeAdmin.service.SaleBatchExtensionService;
  7. import com.izouma.awesomeAdmin.service.SaleBatchService;
  8. import com.izouma.awesomeAdmin.exception.BusinessException;
  9. import com.izouma.awesomeAdmin.repo.SaleBatchRepo;
  10. import com.izouma.awesomeAdmin.utils.Translator;
  11. import com.izouma.awesomeAdmin.utils.excel.ExcelUtils;
  12. import lombok.AllArgsConstructor;
  13. import org.springframework.data.domain.Page;
  14. import org.springframework.data.domain.PageImpl;
  15. import org.springframework.security.access.prepost.PreAuthorize;
  16. import org.springframework.web.bind.annotation.*;
  17. import javax.servlet.http.HttpServletResponse;
  18. import java.io.IOException;
  19. import java.math.BigDecimal;
  20. import java.util.ArrayList;
  21. import java.util.HashMap;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.stream.Collectors;
  25. @RestController
  26. @RequestMapping("/saleBatch")
  27. @AllArgsConstructor
  28. public class SaleBatchController extends BaseController {
  29. private SaleBatchService saleBatchService;
  30. private SaleBatchRepo saleBatchRepo;
  31. private SalesBatchExtensionRepo extensionRepo;
  32. private SaleBatchExtensionService extensionService;
  33. /*@PreAuthorize("hasRole('ADMIN')")
  34. @PostMapping("/save")
  35. public SaleBatch save(@RequestBody SaleBatch record) {
  36. if (record.getId() == null) {
  37. return saleBatchService.create(record);
  38. } else {
  39. return saleBatchService.update(record);
  40. }
  41. }*/
  42. @PreAuthorize("hasRole('ADMIN')")
  43. @PostMapping("/save")
  44. public SaleBatch saveOne(@RequestBody SaleBatch record) {
  45. if (record.getId() == null) {
  46. return saleBatchService.create1(record);
  47. } else {
  48. return saleBatchService.update1(record);
  49. }
  50. }
  51. @PreAuthorize("hasRole('ADMIN')")
  52. @PostMapping("/saveExtensions")
  53. public SaleBatchDto saveExtensions(@RequestBody List<SalesBatchExtension> extensions) {
  54. return saleBatchService.addExtensions(extensions);
  55. }
  56. @PostMapping("/all")
  57. public Page<SaleBatch> all() {
  58. PageImpl<SaleBatch> saleBatches = new PageImpl<>(saleBatchService.all());
  59. List<SaleBatch> content = saleBatches.getContent();
  60. List<SalesBatchExtension> list = extensionRepo.findAll();
  61. Map<Long, List<SalesBatchExtension>> map = new HashMap<>();
  62. list.forEach(c -> {
  63. if (map.containsKey(c.getBatchId())) {
  64. map.get(c.getBatchId()).add(c);
  65. } else {
  66. List<SalesBatchExtension> salesBatchExtensions = new ArrayList<>();
  67. salesBatchExtensions.add(c);
  68. map.put(c.getBatchId(), salesBatchExtensions);
  69. }
  70. });
  71. content.forEach(c -> c.setExtensions(map.get(c.getId())));
  72. return new PageImpl<>(content);
  73. }
  74. /*@PostMapping("/all")
  75. public Page<SaleBatch> all() {
  76. return new PageImpl<>(saleBatchService.all());
  77. }*/
  78. /*@GetMapping("/get/{id}")
  79. public SaleBatch get(@PathVariable Long id) {
  80. return saleBatchRepo.findById(id).orElseThrow(new BusinessException(Translator.toLocale("record.not_found")));
  81. }*/
  82. @GetMapping("/get/{id}")
  83. public SaleBatch getOne(@PathVariable Long id) {
  84. SaleBatch saleBatch = saleBatchRepo.findById(id).orElseThrow(new BusinessException(Translator.toLocale("record.not_found")));
  85. List<SalesBatchExtension> extensions = extensionRepo.findAllByBatchId(saleBatch.getId());
  86. saleBatch.setExtensions(extensions);
  87. return saleBatch;
  88. }
  89. @PostMapping("/del/{id}")
  90. public void deOne(@PathVariable Long id) {
  91. saleBatchService.del(id);
  92. }
  93. /*@PostMapping("/del/{id}")
  94. public void del(@PathVariable Long id) {
  95. saleBatchRepo.deleteById(id);
  96. }*/
  97. @GetMapping("/excel")
  98. @ResponseBody
  99. public void excel(HttpServletResponse response) throws IOException {
  100. List<SaleBatch> data = all().getContent();
  101. ExcelUtils.export(response, data);
  102. }
  103. }