TaskService.java 9.3 KB

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