OutsourcingPlanServiceImpl.java 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. package easydo.technology.service.impl;
  2. import easydo.technology.components.JdbcClient;
  3. import easydo.technology.exception.BizException;
  4. import easydo.technology.model.*;
  5. import easydo.technology.service.FlowNoService;
  6. import easydo.technology.service.OutsourcingPlanService;
  7. import easydo.technology.system.model.SysUser;
  8. import easydo.technology.utils.StringUtil;
  9. import org.springframework.stereotype.Service;
  10. import javax.annotation.Resource;
  11. import javax.sql.DataSource;
  12. import java.sql.Connection;
  13. import java.util.ArrayList;
  14. import java.util.List;
  15. import java.util.Map;
  16. @Service
  17. public class OutsourcingPlanServiceImpl implements OutsourcingPlanService {
  18. @Resource
  19. private JdbcClient jdbcClient;
  20. @Resource
  21. private DataSource dataSource;
  22. @Resource
  23. private FlowNoService flowNoService;
  24. @Override
  25. @SuppressWarnings("unchecked")
  26. public Map<String, Object> getPage(Map<String, Object> map) throws Exception {
  27. Connection connection = dataSource.getConnection();
  28. try {
  29. // 1. 分页查询委外计划主表
  30. Map<String, Object> result = jdbcClient.getJdbcPage(map, OutsourcingPlan.class, connection);
  31. List<OutsourcingPlan> list = (List<OutsourcingPlan>) result.get("records");
  32. if (list == null || list.isEmpty()) {
  33. if (result != null) {
  34. result.put("records", new ArrayList<>());
  35. }
  36. return result;
  37. }
  38. for (OutsourcingPlan model : list) {
  39. // 2. 补全文件列表 (JdbcClient 会根据 @Minio 注解自动处理)
  40. try {
  41. jdbcClient.getMinioFile(model, connection);
  42. } catch (Exception e) {
  43. // 打印错误但继续执行
  44. }
  45. // 3. 补全销售订单 SaleOrder 及其明细
  46. if (model.getSaleOrderId() != null) {
  47. SaleOrder saleOrderParam = new SaleOrder();
  48. saleOrderParam.setId(model.getSaleOrderId());
  49. SaleOrder saleOrder = jdbcClient.getJdbcModelById(saleOrderParam, connection);
  50. if (saleOrder != null) {
  51. // 3.1 获取销售订单明细列表
  52. SaleOrderDetail saleOrderDetailParam = new SaleOrderDetail();
  53. saleOrderDetailParam.setOrderId(saleOrder.getId());
  54. List<SaleOrderDetail> saleOrderDetailList = jdbcClient.getJdbcList(saleOrderDetailParam, connection);
  55. if (saleOrderDetailList != null) {
  56. for (SaleOrderDetail detail : saleOrderDetailList) {
  57. // 3.2 补全销售订单明细中的物料信息
  58. if (detail.getMaterialCode() != null) {
  59. ProductMaterial materialParam = new ProductMaterial();
  60. materialParam.setCode(detail.getMaterialCode());
  61. detail.setMaterial(jdbcClient.getJdbcModel(materialParam, connection));
  62. }
  63. }
  64. saleOrder.setChildrenList(saleOrderDetailList);
  65. }
  66. // 3.3 补全销售订单的负责人姓名 ManagerName
  67. if (StringUtil.isNotEmpty(saleOrder.getManagerId())) {
  68. SysUser managerParam = new SysUser();
  69. managerParam.setId(saleOrder.getManagerId());
  70. SysUser manager = jdbcClient.getJdbcModelById(managerParam, connection);
  71. if (manager != null) {
  72. saleOrder.setManagerName(manager.getNickName());
  73. }
  74. }
  75. // 3.4 补全销售订单的客户名称 CustomerName
  76. if (StringUtil.isNotEmpty(saleOrder.getCustomerId())) {
  77. Customer customerParam = new Customer();
  78. customerParam.setId(saleOrder.getCustomerId());
  79. Customer customer = jdbcClient.getJdbcModelById(customerParam, connection);
  80. if (customer != null) {
  81. saleOrder.setCustomerName(customer.getName());
  82. }
  83. }
  84. model.setSaleOrder(saleOrder);
  85. }
  86. }
  87. // 4. 补全明细列表及物料信息
  88. OutsourcingPlanDetail detailParam = new OutsourcingPlanDetail();
  89. detailParam.setPlanId(model.getId());
  90. List<OutsourcingPlanDetail> detailList = jdbcClient.getJdbcList(detailParam, connection);
  91. if (detailList != null) {
  92. for (OutsourcingPlanDetail detail : detailList) {
  93. if (detail.getMaterialCode() != null) {
  94. ProductMaterial materialParam = new ProductMaterial();
  95. materialParam.setCode(detail.getMaterialCode());
  96. detail.setMaterial(jdbcClient.getJdbcModel(materialParam, connection));
  97. }
  98. }
  99. model.setChildrenList(detailList);
  100. } else {
  101. model.setChildrenList(new ArrayList<>());
  102. }
  103. }
  104. result.put("records", list);
  105. return result;
  106. } catch (Exception e) {
  107. throw new BizException(e.getMessage());
  108. } finally {
  109. if (connection != null) {
  110. connection.close();
  111. }
  112. }
  113. }
  114. @Override
  115. public OutsourcingPlan save(OutsourcingPlan model) throws Exception {
  116. Connection connection = dataSource.getConnection();
  117. connection.setAutoCommit(false);
  118. try {
  119. // 1. 生成委外计划流水号 (支持手动编号校验与自动生成)
  120. String code = flowNoService.generateOutsourcingPlanCode(model, connection);
  121. model.setCode(code);
  122. // 2. 设置初始状态为待处理 (pending)
  123. model.setStatus("pending");
  124. // 3. 插入主表 (JdbcClient 会自动处理 model 继承自 CommonModel 的 fileList 附件)
  125. jdbcClient.jdbcInsert(model, connection);
  126. // 4. 插入委外计划明细
  127. List<OutsourcingPlanDetail> detailList = model.getChildrenList();
  128. if (detailList != null) {
  129. for (OutsourcingPlanDetail detail : detailList) {
  130. detail.setPlanId(model.getId());
  131. jdbcClient.jdbcInsert(detail, connection);
  132. }
  133. }
  134. connection.commit();
  135. return model;
  136. } catch (Exception e) {
  137. connection.rollback();
  138. throw new BizException(e.getMessage());
  139. } finally {
  140. connection.close();
  141. }
  142. }
  143. @Override
  144. public OutsourcingPlan update(OutsourcingPlan model) throws Exception {
  145. Connection connection = dataSource.getConnection();
  146. connection.setAutoCommit(false);
  147. try {
  148. // 1. 更新主表 (JdbcClient 自动同步 fileList 附件)
  149. jdbcClient.jdbcUpdateById(model, connection);
  150. // 2. 级联更新明细:先按 planId 物理删除旧明细
  151. OutsourcingPlanDetail removeParam = new OutsourcingPlanDetail();
  152. removeParam.setPlanId(model.getId());
  153. jdbcClient.jdbcRemove(removeParam, connection);
  154. // 3. 重新插入新明细列表
  155. List<OutsourcingPlanDetail> detailList = model.getChildrenList();
  156. if (detailList != null) {
  157. for (OutsourcingPlanDetail detail : detailList) {
  158. detail.setPlanId(model.getId());
  159. jdbcClient.jdbcInsert(detail, connection);
  160. }
  161. }
  162. connection.commit();
  163. return model;
  164. } catch (Exception e) {
  165. connection.rollback();
  166. throw new BizException(e.getMessage());
  167. } finally {
  168. connection.close();
  169. }
  170. }
  171. @Override
  172. public OutsourcingPlan remove(OutsourcingPlan model) throws Exception {
  173. Connection connection = dataSource.getConnection();
  174. connection.setAutoCommit(false);
  175. try {
  176. // 1. 删除主表 (JdbcClient 自动删除关联的 MinioFile)
  177. jdbcClient.jdbcRemoveById(model, connection);
  178. // 2. 删除关联的明细记录
  179. OutsourcingPlanDetail removeParam = new OutsourcingPlanDetail();
  180. removeParam.setPlanId(model.getId());
  181. jdbcClient.jdbcRemove(removeParam, connection);
  182. connection.commit();
  183. return model;
  184. } catch (Exception e) {
  185. connection.rollback();
  186. throw new BizException(e.getMessage());
  187. } finally {
  188. connection.close();
  189. }
  190. }
  191. }