TaskService.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. package com.ruoyi.sim.service.impl;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import com.ruoyi.common.core.domain.AjaxResult;
  5. import com.ruoyi.common.utils.DateUtils;
  6. import com.ruoyi.common.utils.SecurityUtils;
  7. import com.ruoyi.sim.domain.Fault;
  8. import com.ruoyi.sim.domain.TaskFault;
  9. import com.ruoyi.sim.domain.vo.FaultTreeVo;
  10. import com.ruoyi.sim.domain.vo.TaskVo;
  11. import org.apache.commons.lang3.StringUtils;
  12. import org.slf4j.Logger;
  13. import org.slf4j.LoggerFactory;
  14. import org.springframework.beans.BeanUtils;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.stereotype.Service;
  17. import com.ruoyi.sim.mapper.TaskMapper;
  18. import com.ruoyi.sim.domain.Task;
  19. import org.springframework.transaction.annotation.Transactional;
  20. /**
  21. * 任务Service业务层处理
  22. *
  23. * @author tom
  24. * @date 2024-12-13
  25. */
  26. @Service
  27. public class TaskService {
  28. private static final Logger l = LoggerFactory.getLogger(TaskService.class);
  29. @Autowired
  30. private TaskMapper taskMapper;
  31. @Autowired
  32. private FaultService faultService;
  33. @Autowired
  34. private TaskFaultService taskFaultService;
  35. @Autowired
  36. private SimService simService;
  37. /**
  38. * 查询任务
  39. *
  40. * @param taskId 任务主键
  41. * @return 任务
  42. */
  43. public AjaxResult selectTaskByTaskId(Long taskId) {
  44. // check
  45. if (taskId == null) {
  46. return AjaxResult.error("taskId is null");
  47. }
  48. Task t = taskMapper.selectTaskByTaskId(taskId);
  49. if (t == null) {
  50. return AjaxResult.error("task is null");
  51. }
  52. //
  53. TaskVo vo = new TaskVo();
  54. String simType = t.getSimType();
  55. BeanUtils.copyProperties(t, vo);
  56. // 查询获得数据结构。
  57. List<FaultTreeVo> listToQ = (List<FaultTreeVo>) faultService.listAllARTreeStyleBySimType(simType).get(AjaxResult.DATA_TAG);
  58. // 变成扁平list
  59. List<FaultTreeVo> listToF = FaultService.flatten(listToQ);
  60. for (FaultTreeVo o : listToF) {
  61. if (o == null) {
  62. continue;
  63. }
  64. if (Fault.Type.TYPE_GZBW.equals(o.getFaultType())) {
  65. TaskFault tf = o.getTaskFault();
  66. TaskFault tfQ = taskFaultService.selectUniqueTaskFault(taskId, tf.getFaultId());
  67. // 存在就设置上数据库中TaskFault值。
  68. if (tfQ != null) {
  69. o.setTaskFault(tfQ);
  70. }
  71. }
  72. }
  73. // 变成树list
  74. vo.setSelectedData(FaultService.toTree(listToF, Fault.ROOT_FAULT_ID));
  75. //
  76. return AjaxResult.success(vo);
  77. }
  78. /**
  79. * 查询任务列表
  80. *
  81. * @param q 任务
  82. * @return 任务
  83. */
  84. public List<TaskVo> list(TaskVo q) {
  85. Task qT = new Task();
  86. BeanUtils.copyProperties(q, qT);
  87. List<Task> listT = taskMapper.selectTaskList(qT);
  88. List<TaskVo> listTVo = new ArrayList<>(listT.size());
  89. Long taskId = q.getTaskId();
  90. listT.stream().forEach(t -> {
  91. TaskVo vo = new TaskVo();
  92. BeanUtils.copyProperties(t, vo);
  93. {
  94. int countGZXX = 0;
  95. int countGZBW = 0;
  96. //
  97. countGZBW = taskFaultService.countTypeGZBW(taskId);
  98. // todo:故障现象
  99. // countGZXX = faultService.listType1ByType3
  100. vo.setCountGZXX(countGZXX);
  101. vo.setCountGZBW(countGZBW);
  102. }
  103. listTVo.add(vo);
  104. });
  105. return listTVo;
  106. }
  107. /**
  108. * 修改任务
  109. *
  110. * @param tv 任务
  111. * @return 结果
  112. */
  113. public AjaxResult updateTaskByTeacher(TaskVo tv) {
  114. l.info("updateTaskByTeacher " + tv);
  115. // check
  116. if (tv == null) {
  117. return AjaxResult.error("TaskVo empty!");
  118. }
  119. if (!simService.checkSimTypeOk(tv.getSimType())) {
  120. return AjaxResult.error("getSimType error!");
  121. }
  122. if (!Task.Type.TEACHER_ADD.equals(tv.getTaskType())) {
  123. return AjaxResult.error("taskType value error!");
  124. }
  125. if (StringUtils.isEmpty(tv.getName())) {
  126. return AjaxResult.error("name isEmpty!");
  127. }
  128. //
  129. tv.setCreateByUserId(SecurityUtils.getUserId());
  130. tv.setCreateBy(SecurityUtils.getUsername());
  131. tv.setCreateTime(DateUtils.getNowDate());
  132. tv.setUpdateBy(SecurityUtils.getUsername());
  133. tv.setUpdateTime(DateUtils.getNowDate());
  134. tv.setRemark("");
  135. tv.setUpdateTime(DateUtils.getNowDate());
  136. taskMapper.updateTask(tv);
  137. List<FaultTreeVo> selectedData = tv.getSelectedData();
  138. if (selectedData != null) {
  139. List<FaultTreeVo> list = FaultService.flatten(selectedData);
  140. for (FaultTreeVo ftv : list) {
  141. if (ftv == null) {
  142. continue;
  143. }
  144. if (!Fault.Type.TYPE_GZBW.equals(ftv.getFaultType())) {
  145. continue;
  146. }
  147. TaskFault tf = ftv.getTaskFault();
  148. // check
  149. if (tf == null) {
  150. return AjaxResult.error("TaskFault empty!");
  151. }
  152. String flag = tf.getFlag();
  153. if (StringUtils.isEmpty(flag)) {
  154. return AjaxResult.error("flag empty!");
  155. }
  156. if (TaskFault.UNKNOWN.equals(flag)) {
  157. return AjaxResult.error("flag UNKNOWN!");
  158. }
  159. if (!TaskFault.YES.equals(flag) && !TaskFault.NO.equals(flag)) {
  160. return AjaxResult.error("flag must YES or NO!");
  161. }
  162. // todo:选中数量限制
  163. // todo:故障部位冲突
  164. tf.setTaskId(tv.getTaskId());
  165. taskFaultService.insertOrUpdateTaskFault(tf);
  166. }
  167. }
  168. return AjaxResult.success();
  169. }
  170. /**
  171. * 批量删除任务
  172. *
  173. * @param ids 需要删除的任务主键
  174. * @return 结果
  175. */
  176. @Transactional
  177. public AjaxResult deleteTaskByTaskIds(Long[] ids) {
  178. if (ids == null || ids.length == 0) {
  179. return AjaxResult.error("taskIds null!");
  180. }
  181. for (Long tId : ids) {
  182. taskMapper.deleteTaskByTaskId(tId);
  183. taskFaultService.deleteTaskFaultByTaskId(tId);
  184. }
  185. return AjaxResult.success();
  186. }
  187. /**
  188. * 删除任务信息
  189. *
  190. * @param taskId 任务主键
  191. * @return 结果
  192. */
  193. public int deleteTaskByTaskId(Long taskId) {
  194. return taskMapper.deleteTaskByTaskId(taskId);
  195. }
  196. /**
  197. * 新增任务
  198. *
  199. * @param tv 任务
  200. * @return 结果
  201. * todo:事务有问题。
  202. */
  203. @Transactional
  204. public AjaxResult insertTaskByTeacher(TaskVo tv) {
  205. // check
  206. if (tv == null) {
  207. return AjaxResult.error("TaskVo empty!");
  208. }
  209. if (!simService.checkSimTypeOk(tv.getSimType())) {
  210. return AjaxResult.error("getSimType error!");
  211. }
  212. if (!Task.Type.TEACHER_ADD.equals(tv.getTaskType())) {
  213. return AjaxResult.error("getTaskType value error!");
  214. }
  215. if (StringUtils.isEmpty(tv.getName())) {
  216. return AjaxResult.error("getName isEmpty!");
  217. }
  218. //
  219. tv.setCreateByUserId(SecurityUtils.getUserId());
  220. tv.setCreateBy(SecurityUtils.getUsername());
  221. tv.setCreateTime(DateUtils.getNowDate());
  222. tv.setUpdateBy(SecurityUtils.getUsername());
  223. tv.setUpdateTime(DateUtils.getNowDate());
  224. taskMapper.insertTask(tv);
  225. List<FaultTreeVo> selectedData = tv.getSelectedData();
  226. if (selectedData != null) {
  227. List<FaultTreeVo> list = FaultService.flatten(selectedData);
  228. for (FaultTreeVo ftv : list) {
  229. if (ftv == null) {
  230. continue;
  231. }
  232. if (!Fault.Type.TYPE_GZBW.equals(ftv.getFaultType())) {
  233. continue;
  234. }
  235. TaskFault tf = ftv.getTaskFault();
  236. // check
  237. if (tf == null) {
  238. return AjaxResult.error("TaskFault empty!");
  239. }
  240. String flag = tf.getFlag();
  241. if (StringUtils.isEmpty(flag)) {
  242. return AjaxResult.error("flag empty!");
  243. }
  244. if (TaskFault.UNKNOWN.equals(flag)) {
  245. return AjaxResult.error("flag UNKNOWN!");
  246. }
  247. if (!TaskFault.YES.equals(flag) && !TaskFault.NO.equals(flag)) {
  248. return AjaxResult.error("flag must YES or NO!");
  249. }
  250. // todo:选中数量限制
  251. // 故障部位冲突 暂时没有故障部位冲突。
  252. tf.setTaskId(tv.getTaskId());
  253. taskFaultService.insertOrUpdateTaskFault(tf);
  254. }
  255. }
  256. return AjaxResult.success();
  257. }
  258. /**
  259. * 为了新建Task,返回一个带tree结构的任务详细信息。
  260. *
  261. * @return 任务
  262. */
  263. public AjaxResult getInfoForAdd(final String simType) {
  264. // check
  265. if (StringUtils.isEmpty(simType)) {
  266. return AjaxResult.error("simType empty!");
  267. }
  268. if (!simService.checkSimTypeOk(simType)) {
  269. return AjaxResult.error("simType value error!");
  270. }
  271. // query obj.
  272. List<FaultTreeVo> ftList = faultService.listAllListTreeStyleBySimType(simType);
  273. TaskVo t = new TaskVo();
  274. t.setTaskId(Task.EMPTY_TASK_ID);
  275. t.setSimType(simType);
  276. t.setSelectedData(ftList);
  277. return AjaxResult.success(t);
  278. }
  279. /**
  280. * 新增任务附带选择故障
  281. *
  282. * @param task 任务
  283. * @return 结果
  284. */
  285. @Transactional
  286. public int insertTaskWithFault(Task task) {
  287. Long taskId = task.getTaskId();
  288. if (selectTaskByTaskId(taskId) != null) {
  289. // 已经存在
  290. }
  291. task.setCreateTime(DateUtils.getNowDate());
  292. task.setUpdateTime(DateUtils.getNowDate());
  293. return taskMapper.insertTask(task);
  294. }
  295. }