ProcessRouteServiceImpl.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. package easydo.technology.service.impl;
  2. import easydo.technology.components.JdbcClient;
  3. import easydo.technology.enums.MESEnum;
  4. import easydo.technology.model.*;
  5. import easydo.technology.system.model.SysUser;
  6. import easydo.technology.service.FlowNoService;
  7. import easydo.technology.service.ProcessRouteService;
  8. import easydo.technology.utils.SecurityUtils;
  9. import easydo.technology.utils.StringUtil;
  10. import com.fasterxml.jackson.databind.ObjectMapper;
  11. import com.fasterxml.jackson.core.type.TypeReference;
  12. import org.springframework.stereotype.Service;
  13. import javax.annotation.Resource;
  14. import javax.sql.DataSource;
  15. import java.sql.Connection;
  16. import java.util.*;
  17. import java.util.stream.Collectors;
  18. @Service
  19. public class ProcessRouteServiceImpl implements ProcessRouteService {
  20. @Resource
  21. JdbcClient jdbcClient;
  22. @Resource
  23. DataSource dataSource;
  24. @Resource
  25. FlowNoService flowNoService;
  26. private static final ObjectMapper objectMapper = new ObjectMapper();
  27. @Override
  28. public Object getList(Map<String, Object> map) throws Exception {
  29. List<ProcessRoute> list = jdbcClient.getJdbcList(map, ProcessRoute.class);
  30. Connection connection = dataSource.getConnection();
  31. try {
  32. for (ProcessRoute model : list) {
  33. fillRouteDetails(model, connection);
  34. }
  35. } finally {
  36. connection.close();
  37. }
  38. return list;
  39. }
  40. @Override
  41. public Object getPage(Map<String, Object> map) throws Exception {
  42. Connection connection = dataSource.getConnection();
  43. try {
  44. Map<String, Object> recordsPage = jdbcClient.getJdbcPage(map, ProcessRoute.class, connection);
  45. List<ProcessRoute> records = (List<ProcessRoute>) recordsPage.get("records");
  46. for (ProcessRoute model : records) {
  47. fillRouteDetails(model, connection);
  48. }
  49. return recordsPage;
  50. } finally {
  51. connection.close();
  52. }
  53. }
  54. private void fillRouteDetails(ProcessRoute model, Connection connection) throws Exception {
  55. // 1. 回填明细与工序
  56. ProcessRouteDetail routeDetail = new ProcessRouteDetail();
  57. routeDetail.setRouteId(model.getId());
  58. List<ProcessRouteDetail> routeDetailList = jdbcClient.getJdbcList(routeDetail, connection);
  59. for (ProcessRouteDetail detail : routeDetailList) {
  60. ProcessStage stage = new ProcessStage();
  61. stage.setId(detail.getStageId());
  62. stage = jdbcClient.getJdbcModelById(stage, connection);
  63. detail.setStage(stage);
  64. // 补齐逻辑:解析 device_list 并回显名称数组
  65. if (StringUtil.isNotEmpty(detail.getDeviceList())) {
  66. try {
  67. List<String> deviceIds = objectMapper.readValue(detail.getDeviceList(), new TypeReference<List<String>>() {});
  68. List<String> deviceNames = new ArrayList<>();
  69. for (String deviceId : deviceIds) {
  70. Device device = new Device();
  71. device.setId(deviceId);
  72. device = jdbcClient.getJdbcModelById(device, connection);
  73. deviceNames.add(device != null ? device.getName() : "未知设备");
  74. }
  75. detail.setDeviceNameList(deviceNames);
  76. } catch (Exception e) {
  77. // 忽略解析异常
  78. }
  79. }
  80. }
  81. model.setDetailList(routeDetailList);
  82. // 2. 回填附件
  83. jdbcClient.getMinioFile(model, connection);
  84. // 3. 回填历史标记
  85. ProcessRoute child = new ProcessRoute();
  86. child.setParentId(model.getId());
  87. int count = jdbcClient.getJdbcCount(child, connection);
  88. model.setIsHaveHistory(count > 0);
  89. // 4. 回填检验方案对象
  90. if (StringUtil.isNotEmpty(model.getInspectProgramId())) {
  91. QualityInspectProgram inspectProgram = new QualityInspectProgram();
  92. inspectProgram.setId(model.getInspectProgramId());
  93. inspectProgram = jdbcClient.getJdbcModelById(inspectProgram, connection);
  94. if (inspectProgram != null) {
  95. if (inspectProgram.getInspectUserId() != null) {
  96. SysUser inspectUser = new SysUser();
  97. inspectUser.setId(inspectProgram.getInspectUserId());
  98. inspectUser = jdbcClient.getJdbcModelById(inspectUser, connection);
  99. if (inspectUser != null) {
  100. inspectProgram.setInspectUserName(inspectUser.getNickName());
  101. }
  102. }
  103. if (inspectProgram.getReviewUserId() != null) {
  104. SysUser reviewUser = new SysUser();
  105. reviewUser.setId(inspectProgram.getReviewUserId());
  106. reviewUser = jdbcClient.getJdbcModelById(reviewUser, connection);
  107. if (reviewUser != null) {
  108. inspectProgram.setReviewUserName(reviewUser.getNickName());
  109. }
  110. }
  111. // 补齐质检方案自身的附件回填(质检方案可能已被删除或 id 为空,避免 NPE)
  112. try {
  113. if (StringUtil.isNotEmpty(inspectProgram.getId())) {
  114. jdbcClient.getMinioFile(inspectProgram, connection);
  115. }
  116. } catch (Exception ignore) {
  117. // 忽略附件回填异常
  118. }
  119. }
  120. model.setInspectProgram(inspectProgram);
  121. }
  122. }
  123. @Override
  124. public Object save(ProcessRoute model) throws Exception {
  125. Connection connection = dataSource.getConnection();
  126. try {
  127. connection.setAutoCommit(false);
  128. model.setCreateId(SecurityUtils.getCurrentUserId());
  129. model.setCode(flowNoService.generateProcessRouteCode(model, connection));
  130. model.setParentId("0");
  131. model.setStatus(MESEnum.PROCESS_OF_STATUS_ENABLE.getValue());
  132. jdbcClient.jdbcInsert(model, connection);
  133. List<ProcessRouteDetail> detailList = model.getDetailList();
  134. if (detailList != null) {
  135. for (ProcessRouteDetail detail : detailList) {
  136. detail.setRouteId(model.getId());
  137. jdbcClient.jdbcInsert(detail, connection);
  138. }
  139. }
  140. connection.commit();
  141. return model;
  142. } catch (Exception e) {
  143. connection.rollback();
  144. throw e;
  145. } finally {
  146. connection.close();
  147. }
  148. }
  149. @Override
  150. public Object update(ProcessRoute model) throws Exception {
  151. Connection connection = dataSource.getConnection();
  152. try {
  153. connection.setAutoCommit(false);
  154. model.setUpdateId(SecurityUtils.getCurrentUserId());
  155. jdbcClient.jdbcUpdateById(model, connection);
  156. ProcessRouteDetail routeDetail = new ProcessRouteDetail();
  157. routeDetail.setRouteId(model.getId());
  158. jdbcClient.jdbcRemove(routeDetail, connection);
  159. List<ProcessRouteDetail> detailList = model.getDetailList();
  160. if (detailList != null) {
  161. for (ProcessRouteDetail detail : detailList) {
  162. detail.setRouteId(model.getId());
  163. jdbcClient.jdbcInsert(detail, connection);
  164. }
  165. }
  166. connection.commit();
  167. return model;
  168. } catch (Exception e) {
  169. connection.rollback();
  170. throw e;
  171. } finally {
  172. connection.close();
  173. }
  174. }
  175. @Override
  176. public Object remove(ProcessRoute model) throws Exception {
  177. Connection connection = dataSource.getConnection();
  178. try {
  179. connection.setAutoCommit(false);
  180. jdbcClient.jdbcRemoveById(model, connection);
  181. ProcessRouteDetail routeDetail = new ProcessRouteDetail();
  182. routeDetail.setRouteId(model.getId());
  183. jdbcClient.jdbcRemove(routeDetail, connection);
  184. ProcessRoute history = new ProcessRoute();
  185. history.setParentId(model.getId());
  186. List<ProcessRoute> historyList = jdbcClient.getJdbcList(history, connection);
  187. for (ProcessRoute hismodel : historyList) {
  188. jdbcClient.jdbcRemoveById(hismodel, connection);
  189. ProcessRouteDetail routeHisDetail = new ProcessRouteDetail();
  190. routeHisDetail.setRouteId(hismodel.getId());
  191. jdbcClient.jdbcRemove(routeHisDetail, connection);
  192. }
  193. connection.commit();
  194. return model;
  195. } catch (Exception e) {
  196. connection.rollback();
  197. throw e;
  198. } finally {
  199. connection.close();
  200. }
  201. }
  202. @Override
  203. public Object upgrade(ProcessRoute model) throws Exception {
  204. Connection connection = dataSource.getConnection();
  205. try {
  206. connection.setAutoCommit(false);
  207. Long userId = SecurityUtils.getCurrentUserId();
  208. // 对齐 Go 执行顺序:先插入新版本,再归档旧版本,再迁移历史链
  209. String oldId = model.getId();
  210. model.setCreateId(userId);
  211. model.setCode(flowNoService.generateProcessRouteCode(model, connection));
  212. model.setParentId("0");
  213. model.setId(UUID.randomUUID().toString());
  214. model.setStatus(MESEnum.PROCESS_OF_STATUS_ENABLE.getValue());
  215. jdbcClient.jdbcInsert(model, connection);
  216. ProcessRoute existRoute = new ProcessRoute();
  217. existRoute.setId(oldId);
  218. existRoute.setParentId(model.getId());
  219. existRoute.setStatus(MESEnum.PROCESS_OF_STATUS_DISABLE.getValue());
  220. existRoute.setUpdateId(userId);
  221. jdbcClient.jdbcUpdateById(existRoute, connection);
  222. ProcessRoute childrenRoute = new ProcessRoute();
  223. childrenRoute.setParentId(model.getId());
  224. childrenRoute.setUpdateId(userId);
  225. Map<String, Object> paramMap = new HashMap<>();
  226. paramMap.put("parentId", oldId);
  227. jdbcClient.jdbcUpdate(childrenRoute, paramMap, connection);
  228. List<ProcessRouteDetail> detailList = model.getDetailList();
  229. if (detailList != null) {
  230. for (ProcessRouteDetail detail : detailList) {
  231. detail.setRouteId(model.getId());
  232. jdbcClient.jdbcInsert(detail, connection);
  233. }
  234. }
  235. connection.commit();
  236. return model;
  237. } catch (Exception e) {
  238. connection.rollback();
  239. throw e;
  240. } finally {
  241. connection.close();
  242. }
  243. }
  244. @Override
  245. public Object regrade(ProcessRoute model) throws Exception {
  246. Connection connection = dataSource.getConnection();
  247. try {
  248. connection.setAutoCommit(false);
  249. Long userId = SecurityUtils.getCurrentUserId();
  250. ProcessRoute children = new ProcessRoute();
  251. children.setParentId(model.getId());
  252. List<ProcessRoute> childrenList = jdbcClient.getJdbcList(children, connection);
  253. if (!childrenList.isEmpty()) {
  254. childrenList = childrenList.stream()
  255. .sorted(Comparator.comparing(ProcessRoute::getCreateTime).reversed())
  256. .collect(Collectors.toList());
  257. ProcessRoute child = childrenList.get(0);
  258. child.setParentId("0");
  259. child.setStatus(MESEnum.PROCESS_OF_STATUS_ENABLE.getValue());
  260. child.setUpdateId(userId);
  261. jdbcClient.jdbcUpdateById(child, connection);
  262. ProcessRoute childrenRoute = new ProcessRoute();
  263. childrenRoute.setParentId(child.getId());
  264. childrenRoute.setUpdateId(userId);
  265. Map<String, Object> paramMap = new HashMap<>();
  266. paramMap.put("parentId", model.getId());
  267. jdbcClient.jdbcUpdate(childrenRoute, paramMap, connection);
  268. jdbcClient.jdbcRemoveById(model, connection);
  269. ProcessRouteDetail routeDetail = new ProcessRouteDetail();
  270. routeDetail.setRouteId(model.getId());
  271. jdbcClient.jdbcRemove(routeDetail, connection);
  272. }
  273. connection.commit();
  274. return model;
  275. } catch (Exception e) {
  276. connection.rollback();
  277. throw e;
  278. } finally {
  279. connection.close();
  280. }
  281. }
  282. }