DistributeBatchController.java 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package com.ruoyi.web.controller.app;
  2. import java.util.HashSet;
  3. import java.util.List;
  4. import java.util.Set;
  5. import java.util.stream.Collectors;
  6. import javax.servlet.http.HttpServletResponse;
  7. import com.ruoyi.app.DTO.ImportDistributeBatchDTO;
  8. import com.ruoyi.app.DTO.ValidDistributeListDTO;
  9. import com.ruoyi.app.DTO.ValidSlaughterCodeDTO;
  10. import com.ruoyi.app.domain.Purchaser;
  11. import com.ruoyi.app.domain.request.AddDistributeBatch;
  12. import com.ruoyi.common.core.domain.entity.SysUser;
  13. import com.ruoyi.common.utils.DateUtils;
  14. import com.ruoyi.common.utils.NumberUtil;
  15. import com.ruoyi.common.utils.StringUtils;
  16. import io.swagger.annotations.Api;
  17. import io.swagger.annotations.ApiOperation;
  18. import org.springframework.security.access.prepost.PreAuthorize;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.validation.annotation.Validated;
  21. import org.springframework.web.bind.annotation.GetMapping;
  22. import org.springframework.web.bind.annotation.PostMapping;
  23. import org.springframework.web.bind.annotation.PutMapping;
  24. import org.springframework.web.bind.annotation.DeleteMapping;
  25. import org.springframework.web.bind.annotation.PathVariable;
  26. import org.springframework.web.bind.annotation.RequestBody;
  27. import org.springframework.web.bind.annotation.RequestMapping;
  28. import org.springframework.web.bind.annotation.RestController;
  29. import com.ruoyi.common.annotation.Log;
  30. import com.ruoyi.common.core.controller.BaseController;
  31. import com.ruoyi.common.core.domain.AjaxResult;
  32. import com.ruoyi.common.enums.BusinessType;
  33. import com.ruoyi.app.domain.DistributeBatch;
  34. import com.ruoyi.app.service.IDistributeBatchService;
  35. import com.ruoyi.common.utils.poi.ExcelUtil;
  36. import com.ruoyi.common.core.page.TableDataInfo;
  37. import org.springframework.web.multipart.MultipartFile;
  38. /**
  39. * 分销批次Controller
  40. *
  41. * @author coede
  42. * @date 2025-03-19
  43. */
  44. @Api("分销批次信息管理")
  45. @RestController
  46. @RequestMapping("/app/distributeBatch")
  47. public class DistributeBatchController extends BaseController
  48. {
  49. @Autowired
  50. private IDistributeBatchService distributeBatchService;
  51. /**
  52. * 查询分销批次列表
  53. */
  54. @ApiOperation("查询分销批次列表")
  55. @PreAuthorize("@ss.hasPermi('app:distributeBatch:list')")
  56. @GetMapping("/list")
  57. public TableDataInfo list(DistributeBatch distributeBatch)
  58. {
  59. startPage();
  60. List<DistributeBatch> list = distributeBatchService.selectDistributeBatchList(distributeBatch);
  61. return getDataTable(list);
  62. }
  63. /**
  64. * 查询指定血码还未关闭的分销批次,也就是可以继续绑吊钩,
  65. * 通过判断24小时内的分销批次
  66. */
  67. @ApiOperation("血码可用批次")
  68. @GetMapping("/getValidList")
  69. public AjaxResult getValidList(DistributeBatch distributeBatch)
  70. {
  71. List<ValidDistributeListDTO> list = distributeBatchService.selectValidDistribute(distributeBatch);
  72. return success(list);
  73. }
  74. /**
  75. * 获取存在未完成(24小时内)的分销批次的血码
  76. */
  77. @ApiOperation("可用血码")
  78. @GetMapping("/getValidCodeList")
  79. public AjaxResult getValidCodeList()
  80. {
  81. List<ValidSlaughterCodeDTO> list = distributeBatchService.selectValidCode();
  82. return success(list);
  83. }
  84. /**
  85. * 导出分销批次列表
  86. */
  87. @ApiOperation("导出分销批次列表")
  88. @PreAuthorize("@ss.hasPermi('app:distributeBatch:export')")
  89. @Log(title = "分销批次", businessType = BusinessType.EXPORT)
  90. @PostMapping("/export")
  91. public void export(HttpServletResponse response, DistributeBatch distributeBatch)
  92. {
  93. List<DistributeBatch> list = distributeBatchService.selectDistributeBatchList(distributeBatch);
  94. ExcelUtil<DistributeBatch> util = new ExcelUtil<DistributeBatch>(DistributeBatch.class);
  95. util.exportExcel(response, list, "分销批次数据");
  96. }
  97. /**
  98. * 导入分销批次列表
  99. */
  100. @ApiOperation("导入分销批次列表")
  101. @PreAuthorize("@ss.hasPermi('app:distributeBatch:import')")
  102. @PostMapping("/importData")
  103. public AjaxResult importData(MultipartFile file) throws Exception
  104. {
  105. ExcelUtil<ImportDistributeBatchDTO> util = new ExcelUtil<ImportDistributeBatchDTO>(ImportDistributeBatchDTO.class);
  106. List<ImportDistributeBatchDTO> importList = util.importExcel(file.getInputStream());
  107. String operName = getUsername();
  108. String message = distributeBatchService.importDistributeBatch(importList, operName);
  109. return success(message);
  110. }
  111. /**
  112. * 获取分销批次详细信息
  113. */
  114. @ApiOperation("获取分销批次详细信息")
  115. @PreAuthorize("@ss.hasPermi('app:distributeBatch:query')")
  116. @GetMapping(value = "/{id}")
  117. public AjaxResult getInfo(@PathVariable("id") Long id)
  118. {
  119. return success(distributeBatchService.selectDistributeBatchById(id));
  120. }
  121. /**
  122. * 新增分销批次
  123. */
  124. @ApiOperation("新增分销批次")
  125. @PreAuthorize("@ss.hasPermi('app:distributeBatch:add')")
  126. @Log(title = "分销批次", businessType = BusinessType.INSERT)
  127. @PostMapping
  128. public AjaxResult add(@Validated @RequestBody DistributeBatch distributeBatch)
  129. {
  130. if(!distributeBatchService.checkTotalLessThanAmount(distributeBatch)){
  131. return error("新增分销批次失败,分销数量输入错误");
  132. }
  133. distributeBatch.setCreateBy(getUsername());
  134. return toAjax(distributeBatchService.insertDistributeBatch(distributeBatch));
  135. }
  136. /**
  137. * 批量新增分销批次
  138. */
  139. @ApiOperation("批量新增分销批次")
  140. @PreAuthorize("@ss.hasPermi('app:distributeBatch:add')")
  141. @Log(title = "分销批次", businessType = BusinessType.INSERT)
  142. @PostMapping(value = "/addBatch")
  143. public AjaxResult addBatch(@Validated @RequestBody AddDistributeBatch req)
  144. {
  145. //生成新对象
  146. List<DistributeBatch> distributeArr = req.getDistributeArr().stream()
  147. .peek(item -> {
  148. item.setEntranceBatchId(req.getEntranceBatchId());
  149. item.setCreateTime(DateUtils.getNowDate());
  150. item.setCreateBy(getUsername());
  151. })
  152. .collect(Collectors.toList());
  153. //先判断本身数组内是否有重复的
  154. Set<String> uniqueIds = new HashSet<>();
  155. for (DistributeBatch distribute : distributeArr) {
  156. String key = String.valueOf(distribute.getPurchaserId());
  157. //noinspection ConstantConditions 忽略错误的IDEA提示
  158. if (!uniqueIds.add(key)) {
  159. return error("新增分销批次失败,本次添加存在相同的肉商");
  160. }
  161. }
  162. //校验数据库的
  163. int add = 0;
  164. for (DistributeBatch distribute : distributeArr) {
  165. add += NumberUtil.getIntValue(distribute.getAmount());
  166. DistributeBatch check = distributeBatchService.checkPurchaserUnique(distribute);
  167. if (StringUtils.isNotNull(check))
  168. {
  169. Purchaser purchaser = check.getPurchaser();
  170. return error("新增分销批次失败,该批次下肉商'" + purchaser.getPurchaserName() + "'已存在");
  171. }
  172. }
  173. //判断分销数量是否超过批次实际数量
  174. DistributeBatch distributeBuild = new DistributeBatch();
  175. distributeBuild.setEntranceBatchId(req.getEntranceBatchId());
  176. distributeBuild.setAmount(add);
  177. if(!distributeBatchService.checkTotalLessThanAmount(distributeBuild)){
  178. return error("新增分销批次失败,分销数量输入错误");
  179. }
  180. return toAjax(distributeBatchService.insertDistributeBatchByBatch(distributeArr));
  181. }
  182. /**
  183. * 修改分销批次
  184. */
  185. @ApiOperation("修改分销批次")
  186. @PreAuthorize("@ss.hasPermi('app:distributeBatch:edit')")
  187. @Log(title = "分销批次", businessType = BusinessType.UPDATE)
  188. @PutMapping
  189. public AjaxResult edit(@Validated @RequestBody DistributeBatch distributeBatch)
  190. {
  191. DistributeBatch check = distributeBatchService.checkPurchaserUnique(distributeBatch);
  192. if (StringUtils.isNotNull(check)&& check.getId().longValue() != distributeBatch.getId().longValue())
  193. {
  194. Purchaser purchaser = check.getPurchaser();
  195. return error("修改分销批次失败,该批次下肉商'" + purchaser.getPurchaserName() + "'已存在");
  196. }
  197. //判断分销数量是否超过批次实际数量
  198. if(!distributeBatchService.checkTotalLessThanAmount(distributeBatch)){
  199. return error("修改分销批次失败,分销数量输入错误");
  200. }
  201. distributeBatch.setUpdateBy(getUsername());
  202. return toAjax(distributeBatchService.updateDistributeBatch(distributeBatch));
  203. }
  204. /**
  205. * 删除分销批次
  206. */
  207. @ApiOperation("删除分销批次")
  208. @PreAuthorize("@ss.hasPermi('app:distributeBatch:remove')")
  209. @Log(title = "分销批次", businessType = BusinessType.DELETE)
  210. @DeleteMapping("/{ids}")
  211. public AjaxResult remove(@PathVariable Long[] ids)
  212. {
  213. return toAjax(distributeBatchService.deleteDistributeBatchByIds(ids));
  214. }
  215. }