MesDevicePreventTaskService.java 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. package com.jeesite.modules.mes.service;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import com.jeesite.common.lang.StringUtils;
  5. import com.jeesite.common.mybatis.mapper.query.QueryType;
  6. import com.jeesite.modules.mes.dao.MesDeviceConsumableRecordDao;
  7. import com.jeesite.modules.mes.entity.MesDeviceConsumableRecord;
  8. import com.jeesite.modules.mes.entity.MesDevicePreventTaskUser;
  9. import com.jeesite.modules.sys.entity.User;
  10. import com.jeesite.modules.sys.utils.UserUtils;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.stereotype.Service;
  13. import org.springframework.transaction.annotation.Transactional;
  14. import com.jeesite.common.entity.Page;
  15. import com.jeesite.common.service.CrudService;
  16. import com.jeesite.modules.mes.entity.MesDevicePreventTask;
  17. import com.jeesite.modules.mes.dao.MesDevicePreventTaskDao;
  18. import com.jeesite.modules.file.utils.FileUploadUtils;
  19. /**
  20. * 预防性维修计划Service
  21. * @author hzd
  22. * @version 2023-08-17
  23. */
  24. @Service
  25. public class MesDevicePreventTaskService extends CrudService<MesDevicePreventTaskDao, MesDevicePreventTask> {
  26. @Autowired
  27. private MesDeviceConsumableRecordDao mesDeviceConsumableRecordDao;
  28. @Autowired
  29. private MesDevicePreventTaskUserService mesDevicePreventTaskUserService;
  30. /**
  31. * 获取单条数据
  32. * @param mesDevicePreventTask
  33. * @return
  34. */
  35. @Override
  36. public MesDevicePreventTask get(MesDevicePreventTask mesDevicePreventTask) {
  37. MesDevicePreventTask entity = super.get(mesDevicePreventTask);
  38. if (entity != null){
  39. MesDeviceConsumableRecord mesDeviceConsumableRecord = new MesDeviceConsumableRecord();
  40. mesDeviceConsumableRecord.setStatus(MesDeviceConsumableRecord.STATUS_NORMAL);
  41. mesDeviceConsumableRecord.setBusType("2");
  42. mesDeviceConsumableRecord.setBusId(entity.getId());
  43. entity.setMesDeviceConsumableRecordList(mesDeviceConsumableRecordDao.findList(mesDeviceConsumableRecord));
  44. fillAssigneeDisplay(entity);
  45. }
  46. return entity;
  47. }
  48. /**
  49. * 查询分页数据
  50. * @param mesDevicePreventTask 查询条件
  51. * @param mesDevicePreventTask.page 分页对象
  52. * @return
  53. */
  54. @Override
  55. public Page<MesDevicePreventTask> findPage(MesDevicePreventTask mesDevicePreventTask) {
  56. Page<MesDevicePreventTask> page = super.findPage(mesDevicePreventTask);
  57. if (page != null && page.getList() != null) {
  58. for (MesDevicePreventTask item : page.getList()) {
  59. fillAssigneeDisplay(item);
  60. }
  61. }
  62. return page;
  63. }
  64. /**
  65. * 查询列表数据
  66. * @param mesDevicePreventTask
  67. * @return
  68. */
  69. @Override
  70. public List<MesDevicePreventTask> findList(MesDevicePreventTask mesDevicePreventTask) {
  71. List<MesDevicePreventTask> list = super.findList(mesDevicePreventTask);
  72. if (list != null) {
  73. for (MesDevicePreventTask item : list) {
  74. fillAssigneeDisplay(item);
  75. }
  76. }
  77. return list;
  78. }
  79. /**
  80. * 保存数据(插入或更新)
  81. * @param mesDevicePreventTask
  82. */
  83. @Override
  84. @Transactional
  85. public void save(MesDevicePreventTask mesDevicePreventTask) {
  86. super.save(mesDevicePreventTask);
  87. // 保存上传图片
  88. FileUploadUtils.saveFileUpload(mesDevicePreventTask, mesDevicePreventTask.getId(), "mesDevicePreventTask_image");
  89. for (MesDeviceConsumableRecord mesDeviceConsumableRecord : mesDevicePreventTask.getMesDeviceConsumableRecordList()){
  90. if (!MesDeviceConsumableRecord.STATUS_DELETE.equals(mesDeviceConsumableRecord.getStatus())){
  91. mesDeviceConsumableRecord.setBusType("2");
  92. mesDeviceConsumableRecord.setBusId(mesDevicePreventTask.getId());
  93. mesDeviceConsumableRecord.setDeviceId(mesDevicePreventTask.getDeviceId());
  94. if (mesDeviceConsumableRecord.getIsNewRecord()){
  95. mesDeviceConsumableRecordDao.insert(mesDeviceConsumableRecord);
  96. }else{
  97. mesDeviceConsumableRecordDao.update(mesDeviceConsumableRecord);
  98. }
  99. }else{
  100. mesDeviceConsumableRecordDao.delete(mesDeviceConsumableRecord);
  101. }
  102. }
  103. }
  104. /**
  105. * 保存计划并同步执行人关系(一人完成即整单完成)
  106. * @param mesDevicePreventTask 计划
  107. * @param userCodes 执行人编码列表
  108. */
  109. @Transactional
  110. public void saveWithAssignees(MesDevicePreventTask mesDevicePreventTask, List<String> userCodes) {
  111. if (mesDevicePreventTask.getIsNewRecord() && StringUtils.isBlank(mesDevicePreventTask.getState())) {
  112. mesDevicePreventTask.setState("0");
  113. }
  114. // 主表冗余存逗号分隔,便于表单回显与PDA字段兼容
  115. mesDevicePreventTask.setUserCode(StringUtils.join(userCodes, ","));
  116. StringBuilder nameBuilder = new StringBuilder();
  117. for (int i = 0; i < userCodes.size(); i++) {
  118. User user = UserUtils.get(userCodes.get(i));
  119. if (user != null && StringUtils.isNotBlank(user.getUserName())) {
  120. if (nameBuilder.length() > 0) {
  121. nameBuilder.append(",");
  122. }
  123. nameBuilder.append(user.getUserName());
  124. }
  125. }
  126. mesDevicePreventTask.setUserName(nameBuilder.toString());
  127. this.save(mesDevicePreventTask);
  128. mesDevicePreventTaskUserService.deleteByTaskId(mesDevicePreventTask.getId());
  129. for (String userCode : userCodes) {
  130. MesDevicePreventTaskUser link = new MesDevicePreventTaskUser();
  131. link.setIsNewRecord(true);
  132. link.setPreventTaskId(mesDevicePreventTask.getId());
  133. link.setUserCode(userCode);
  134. mesDevicePreventTaskUserService.save(link);
  135. }
  136. }
  137. /**
  138. * 按执行人过滤计划(我的计划 / PDA ulist)
  139. * 不改接口入参:外部仍可传 userCode,此处改为走关系表
  140. */
  141. public void applyAssigneeFilter(MesDevicePreventTask mesDevicePreventTask, String userCode) {
  142. if (mesDevicePreventTask == null || StringUtils.isBlank(userCode)) {
  143. return;
  144. }
  145. // 避免主表 user_code 等值/模糊条件干扰
  146. mesDevicePreventTask.setUserCode(null);
  147. List<MesDevicePreventTaskUser> links = mesDevicePreventTaskUserService.findByUserCode(userCode);
  148. List<String> taskIds = new ArrayList<>();
  149. if (links != null) {
  150. for (MesDevicePreventTaskUser link : links) {
  151. if (link != null && StringUtils.isNotBlank(link.getPreventTaskId())
  152. && !taskIds.contains(link.getPreventTaskId())) {
  153. taskIds.add(link.getPreventTaskId());
  154. }
  155. }
  156. }
  157. if (taskIds.isEmpty()) {
  158. mesDevicePreventTask.getSqlMap().getWhere().and("id", QueryType.EQ, "__NONE__");
  159. } else {
  160. mesDevicePreventTask.getSqlMap().getWhere().and("id", QueryType.IN, taskIds.toArray(new String[0]));
  161. }
  162. }
  163. /**
  164. * 判断当前用户是否可处理该计划
  165. */
  166. public boolean canDeal(String taskId, String userCode) {
  167. return mesDevicePreventTaskUserService.isAssignee(taskId, userCode);
  168. }
  169. /**
  170. * 更新状态
  171. * @param mesDevicePreventTask
  172. */
  173. @Override
  174. @Transactional
  175. public void updateStatus(MesDevicePreventTask mesDevicePreventTask) {
  176. super.updateStatus(mesDevicePreventTask);
  177. }
  178. /**
  179. * 删除数据(物理删除,避免 status 字段配置导致逻辑删除后仍出现在列表)
  180. * @param mesDevicePreventTask
  181. */
  182. @Override
  183. @Transactional
  184. public void delete(MesDevicePreventTask mesDevicePreventTask) {
  185. if (mesDevicePreventTask != null && StringUtils.isNotBlank(mesDevicePreventTask.getId())) {
  186. // 先删执行人关系
  187. mesDevicePreventTaskUserService.deleteByTaskId(mesDevicePreventTask.getId());
  188. // 同步清理关联耗材记录
  189. MesDeviceConsumableRecord consumable = new MesDeviceConsumableRecord();
  190. consumable.setBusType("2");
  191. consumable.setBusId(mesDevicePreventTask.getId());
  192. mesDeviceConsumableRecordDao.deleteByEntity(consumable);
  193. }
  194. dao.phyDelete(mesDevicePreventTask);
  195. }
  196. /**
  197. * 填充执行人名称(关系表优先,兼容主表冗余)
  198. */
  199. private void fillAssigneeDisplay(MesDevicePreventTask entity) {
  200. if (entity == null || StringUtils.isBlank(entity.getId())) {
  201. return;
  202. }
  203. List<MesDevicePreventTaskUser> links = mesDevicePreventTaskUserService.findByTaskId(entity.getId());
  204. List<String> codes = new ArrayList<>();
  205. List<String> names = new ArrayList<>();
  206. if (links != null && !links.isEmpty()) {
  207. for (MesDevicePreventTaskUser link : links) {
  208. if (link == null || StringUtils.isBlank(link.getUserCode())) {
  209. continue;
  210. }
  211. codes.add(link.getUserCode());
  212. User user = UserUtils.get(link.getUserCode());
  213. names.add(user != null && StringUtils.isNotBlank(user.getUserName())
  214. ? user.getUserName() : link.getUserCode());
  215. }
  216. entity.setUserCode(StringUtils.join(codes, ","));
  217. entity.setUserName(StringUtils.join(names, ","));
  218. return;
  219. }
  220. // 无关系数据时,按主表冗余解析名称
  221. if (StringUtils.isNotBlank(entity.getUserCode())) {
  222. String[] arr = StringUtils.split(entity.getUserCode(), ",");
  223. if (arr != null) {
  224. for (String code : arr) {
  225. String userCode = StringUtils.trim(code);
  226. if (StringUtils.isBlank(userCode)) {
  227. continue;
  228. }
  229. codes.add(userCode);
  230. User user = UserUtils.get(userCode);
  231. names.add(user != null && StringUtils.isNotBlank(user.getUserName())
  232. ? user.getUserName() : userCode);
  233. }
  234. entity.setUserName(StringUtils.join(names, ","));
  235. }
  236. }
  237. }
  238. }