FaultService.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. package com.ruoyi.sim.service.impl;
  2. import java.util.*;
  3. import java.util.stream.Collectors;
  4. import com.ruoyi.common.core.domain.AjaxResult;
  5. import com.ruoyi.common.utils.DateUtils;
  6. import com.ruoyi.sim.domain.TaskFault;
  7. import com.ruoyi.sim.domain.vo.FaultTreeVo;
  8. import org.apache.commons.lang3.RandomUtils;
  9. import org.apache.commons.lang3.StringUtils;
  10. import org.slf4j.Logger;
  11. import org.slf4j.LoggerFactory;
  12. import org.springframework.beans.BeanUtils;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.stereotype.Service;
  15. import com.ruoyi.sim.mapper.FaultMapper;
  16. import com.ruoyi.sim.domain.Fault;
  17. /**
  18. * 故障Service业务层处理
  19. *
  20. * @author tom
  21. * @date 2024-12-15
  22. */
  23. @Service
  24. public class FaultService {
  25. @Autowired
  26. private FaultMapper faultMapper;
  27. /**
  28. * 查询故障
  29. *
  30. * @param faultId 故障主键
  31. * @return 故障
  32. */
  33. public Fault selectFaultByFaultId(String faultId) {
  34. return faultMapper.selectFaultByFaultId(faultId);
  35. }
  36. /**
  37. * 查询故障列表
  38. *
  39. * @param fault 故障
  40. * @return 故障
  41. */
  42. public List<Fault> selectFaultList(Fault fault) {
  43. return faultMapper.selectFaultList(fault);
  44. }
  45. /**
  46. * 新增故障
  47. *
  48. * @param fault 故障
  49. * @return 结果
  50. */
  51. public int insertFault(Fault fault) {
  52. fault.setCreateTime(DateUtils.getNowDate());
  53. return faultMapper.insertFault(fault);
  54. }
  55. /**
  56. * 修改故障
  57. *
  58. * @param fault 故障
  59. * @return 结果
  60. */
  61. public int updateFault(Fault fault) {
  62. fault.setUpdateTime(DateUtils.getNowDate());
  63. return faultMapper.updateFault(fault);
  64. }
  65. /**
  66. * 批量删除故障
  67. *
  68. * @param faultIds 需要删除的故障主键
  69. * @return 结果
  70. */
  71. public int deleteFaultByFaultIds(String[] faultIds) {
  72. return faultMapper.deleteFaultByFaultIds(faultIds);
  73. }
  74. /**
  75. * 删除故障信息
  76. *
  77. * @param faultId 故障主键
  78. * @return 结果
  79. */
  80. public int deleteFaultByFaultId(String faultId) {
  81. return faultMapper.deleteFaultByFaultId(faultId);
  82. }
  83. // -------------------------------- tom add --------------------------------
  84. private static final Logger l = LoggerFactory.getLogger(FaultService.class);
  85. @Autowired
  86. private SimService simService;
  87. public boolean isEnable(String faultId) {
  88. Fault q = new Fault();
  89. q.setFaultId(faultId);
  90. Fault f = selectFaultByFaultId(faultId);
  91. if (f == null) {
  92. return false;
  93. }
  94. return (Fault.State.ENABLE.equals(f.getFaultState()));
  95. }
  96. public boolean isDisable(String faultId) {
  97. return !isEnable(faultId);
  98. }
  99. public boolean isExist(final String simType, final String faultId) {
  100. Fault f = selectFaultByFaultId(faultId);
  101. if (f == null) {
  102. return false;
  103. }
  104. return f.getSimType().equals(simType);
  105. }
  106. public boolean isExist(final String simType, final String[] faultIds) {
  107. if (faultIds == null || faultIds.length == 0) {
  108. return false;
  109. }
  110. for (String faultId : faultIds) {
  111. if (!isExist(simType, faultId)) {
  112. return false;
  113. }
  114. }
  115. return true;
  116. }
  117. public List<Fault> listAllEnable(final String simType) {
  118. Fault q = new Fault();
  119. q.setSimType(simType);
  120. q.setFaultState(Fault.State.ENABLE);
  121. return selectFaultList(q);
  122. }
  123. public List<Fault> listAllType3EnableBySimType(final String simType) {
  124. Fault q = new Fault();
  125. q.setSimType(simType);
  126. q.setFaultType(Fault.Type.TYPE_GZBW);
  127. q.setFaultState(Fault.State.ENABLE);
  128. return selectFaultList(q);
  129. }
  130. public List<Fault> listAllType1And3EnableBySimType(final String simType) {
  131. List<Fault> list = new ArrayList<>();
  132. listAllEnable(simType)
  133. .stream()
  134. .filter(Objects::nonNull)
  135. .filter(f ->
  136. (Fault.State.ENABLE.equals(f.getFaultState()))
  137. )
  138. .filter(f ->
  139. (Fault.Type.TYPE_GZXX.equals(f.getFaultType()) || Fault.Type.TYPE_GZBW.equals(f.getFaultType()))
  140. )
  141. .forEach(list::add);
  142. return list;
  143. }
  144. /**
  145. * 根据模拟器型号,查询故障列表tree。
  146. *
  147. * @param simType
  148. */
  149. public AjaxResult listAllARTreeStyleBySimType(final String simType) {
  150. // check
  151. if (StringUtils.isEmpty(simType)) {
  152. return AjaxResult.error("simType empty!");
  153. }
  154. if (!simService.checkSimTypeOk(simType)) {
  155. return AjaxResult.error("simType value error!");
  156. }
  157. // do.
  158. return AjaxResult.success(listAllListTreeStyleBySimType(simType));
  159. }
  160. /**
  161. * 根据模拟器型号,查询故障列表tree。
  162. *
  163. * @param simType
  164. */
  165. public List<FaultTreeVo> listAllListTreeStyleBySimType(final String simType) {
  166. List<Fault> list = listAllType1And3EnableBySimType(simType);
  167. // 排序。按照faultId升序排序
  168. Collections.sort(list);
  169. List<FaultTreeVo> tempListNode = new ArrayList<>();
  170. for (Fault s : list) {
  171. FaultTreeVo t = new FaultTreeVo();
  172. BeanUtils.copyProperties(s, t);
  173. tempListNode.add(t);
  174. }
  175. // l.info(Objects.requireNonNull(tempListNode).toString());
  176. List<FaultTreeVo> tree = toTree(tempListNode, Fault.ROOT_FAULT_ID);
  177. // l.info(Objects.requireNonNull(tree).toString());
  178. return tree;
  179. }
  180. /**
  181. * 通过Type3 List 获取到不重复的Type1 List。·
  182. *
  183. * @param list
  184. * @return
  185. */
  186. public List<Fault> listType1ByType3(List<Fault> list) {
  187. if (list == null || list.isEmpty()) {
  188. return Collections.emptyList();
  189. }
  190. String[] faultIds = new String[list.size()];
  191. for (int i = 0; i < list.size(); i++) {
  192. faultIds[i] = list.get(i).getFaultId();
  193. }
  194. return setType1ByType3(faultIds).stream().toList();
  195. }
  196. public int countType1ByType3(List<Fault> list) {
  197. if (list == null || list.isEmpty()) {
  198. return 0;
  199. }
  200. return listType1ByType3(list).size();
  201. }
  202. public Set<Fault> setType1ByType3(String[] faultIdsType3) {
  203. if (faultIdsType3 == null || faultIdsType3.length == 0) {
  204. return Collections.emptySet();
  205. }
  206. Set<Fault> set = new HashSet<>();
  207. for (String id : faultIdsType3) {
  208. Fault t3 = selectFaultByFaultId(id);
  209. Fault t1 = selectFaultByFaultId(t3.getParentFaultId());
  210. set.add(t1);
  211. }
  212. return set;
  213. }
  214. public boolean isType(String faultId, String type) {
  215. if (StringUtils.isBlank(type)) {
  216. return false;
  217. }
  218. Fault f = selectFaultByFaultId(faultId);
  219. if (f == null) {
  220. return false;
  221. }
  222. return type.equals(f.getFaultType());
  223. }
  224. public boolean isType3(String faultId) {
  225. return isType(faultId, Fault.Type.TYPE_GZBW);
  226. }
  227. public static int COUNT_3 = 3;
  228. public List<Fault> randomEnable3(final String simType) {
  229. return randomEnable(simType, COUNT_3);
  230. }
  231. /**
  232. * 随机出题。
  233. *
  234. * @param simType
  235. * @param selectCount 选中题目数量。
  236. * @return
  237. */
  238. public List<Fault> randomEnable(final String simType, final int selectCount) {
  239. List<Fault> list1 = listAllType3EnableBySimType(simType);
  240. Set<Fault> set = new HashSet<>();
  241. for (int i = 0; i < selectCount; i++) {
  242. int index = RandomUtils.nextInt(0, list1.size() - i);
  243. set.add(list1.get(index));
  244. list1.remove(index);
  245. }
  246. if (set.size() != selectCount) {
  247. throw new RuntimeException("randomEnable fail!");
  248. }
  249. return set.stream().toList();
  250. }
  251. // private static List<FaultNode> toTree2(List<FaultNode> list) {
  252. // List<FaultNode> tree = new ArrayList<>();
  253. // for (FaultNode fn1 : list) {
  254. // if (fn1.getParentFaultId().equals(ROOT_FAULT_ID)) {
  255. // tree.add(fn1);
  256. // }
  257. // for (FaultNode fn2 : list) {
  258. // if (fn2.getParentFaultId().equals(fn1.getParentFaultId())) {
  259. // if (fn1.getChildren() == null) {
  260. // fn1.setChildren(new ArrayList<>());
  261. // }
  262. // fn1.getChildren().add(fn2);
  263. // }
  264. // }
  265. // }
  266. // return tree;
  267. // }
  268. public static List<FaultTreeVo> toTree(List<FaultTreeVo> list, String parentFaultId) {
  269. // todo:sort
  270. List<FaultTreeVo> tree = list
  271. .stream()
  272. .filter(
  273. parent ->
  274. parent.getParentFaultId().equals(parentFaultId))
  275. .map(child -> {
  276. if (child.getTaskFault() == null) {
  277. child.setTaskFault(new TaskFault(0L, 0L, child.getFaultId(), TaskFault.Flag.UNKNOWN));
  278. }
  279. child.setChildren(toTree(list, child.getFaultId()));
  280. return child;
  281. })
  282. .collect(Collectors.toList());
  283. return tree;
  284. }
  285. public static List<FaultTreeVo> flatten(List<FaultTreeVo> list) {
  286. return list
  287. // todo:stream filter
  288. // .stream()
  289. // .filter(x -> {
  290. // return StringUtils.isEmpty(x.getParentFaultId());
  291. // })
  292. // .collect(Collectors.toList())
  293. .stream()
  294. .map(x -> {
  295. return flatten(x, list);
  296. })
  297. .flatMap(Collection::stream)
  298. .distinct()
  299. .collect(Collectors.toList());
  300. }
  301. public static List<FaultTreeVo> flatten(FaultTreeVo node, List<FaultTreeVo> list2222) {
  302. List<FaultTreeVo> results = new ArrayList<>();
  303. if (node != null) {
  304. // get rid of children & parent references
  305. FaultTreeVo target = new FaultTreeVo();
  306. BeanUtils.copyProperties(node, target);
  307. results.add(target);
  308. }
  309. List<FaultTreeVo> children = node.getChildren();
  310. for (FaultTreeVo child : children) {
  311. if (child.getChildren() != null) {
  312. // Recursive call - Keep flattening until no more children
  313. List<FaultTreeVo> flatten = flatten(child, list2222);
  314. results.addAll(flatten);
  315. }
  316. }
  317. // stop or exit condition
  318. return results;
  319. }
  320. }