SimService.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. package com.ruoyi.sim.service.impl;
  2. import java.util.ArrayList;
  3. import java.util.Date;
  4. import java.util.List;
  5. import java.util.Objects;
  6. import com.ruoyi.common.core.domain.AjaxResult;
  7. import com.ruoyi.common.utils.DateUtils;
  8. import org.apache.commons.lang3.StringUtils;
  9. import org.slf4j.Logger;
  10. import org.slf4j.LoggerFactory;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.scheduling.annotation.Async;
  13. import org.springframework.stereotype.Service;
  14. import com.ruoyi.sim.mapper.SimMapper;
  15. import com.ruoyi.sim.domain.Sim;
  16. import org.springframework.transaction.annotation.Isolation;
  17. import org.springframework.transaction.annotation.Propagation;
  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 SimService {
  27. @Autowired
  28. private SimMapper simMapper;
  29. /**
  30. * 查询模拟器
  31. *
  32. * @param simId 模拟器主键
  33. * @return 模拟器
  34. */
  35. public Sim selectSimBySimId(Long simId) {
  36. return simMapper.selectSimBySimId(simId);
  37. }
  38. /**
  39. * 查询模拟器列表
  40. *
  41. * @param sim 模拟器
  42. * @return 模拟器
  43. */
  44. public List<Sim> selectSimList(Sim sim) {
  45. return simMapper.selectSimList(sim);
  46. }
  47. /**
  48. * 新增模拟器
  49. *
  50. * @param sim 模拟器
  51. * @return 结果
  52. */
  53. public int insertSim(Sim sim) {
  54. sim.setCreateTime(DateUtils.getNowDate());
  55. return simMapper.insertSim(sim);
  56. }
  57. /**
  58. * 修改模拟器
  59. *
  60. * @param sim 模拟器
  61. * @return 结果
  62. */
  63. public int updateSim(Sim sim) {
  64. sim.setUpdateTime(DateUtils.getNowDate());
  65. return simMapper.updateSim(sim);
  66. }
  67. /**
  68. * 批量删除模拟器
  69. *
  70. * @param simIds 需要删除的模拟器主键
  71. * @return 结果
  72. */
  73. public int deleteSimBySimIds(Long[] simIds) {
  74. return simMapper.deleteSimBySimIds(simIds);
  75. }
  76. /**
  77. * 删除模拟器信息
  78. *
  79. * @param simId 模拟器主键
  80. * @return 结果
  81. */
  82. public int deleteSimBySimId(Long simId) {
  83. return simMapper.deleteSimBySimId(simId);
  84. }
  85. // -------------------------------- tom add --------------------------------
  86. private static final Logger l = LoggerFactory.getLogger(SimService.class);
  87. /**
  88. * @param simId
  89. * @return true:存在,false:不存在
  90. */
  91. public boolean existBySimId(final Long simId) {
  92. if (simId == null) {
  93. return false;
  94. }
  95. if (simId == 0) {
  96. return false;
  97. }
  98. Sim s = selectSimBySimId(simId);
  99. if (s == null) {
  100. return false;
  101. }
  102. return true;
  103. }
  104. /**
  105. * @param simNum
  106. * @return true:存在,false:不存在
  107. */
  108. public boolean existBySimNum(final String simNum) {
  109. if (StringUtils.isEmpty(simNum)) {
  110. return false;
  111. }
  112. Sim s = uniqueBySimNum(simNum);
  113. return s != null;
  114. }
  115. /**
  116. * 通过sim_num获取唯一模拟器Sim对象。
  117. *
  118. * @param simNum
  119. * @return
  120. */
  121. public Sim uniqueBySimNum(final String simNum) {
  122. Sim q = new Sim();
  123. q.setSimNum(simNum);
  124. List<Sim> list = simMapper.selectSimList(q);
  125. if (list.isEmpty()) {
  126. return null;
  127. } else if (list.size() == 1) {
  128. return list.get(0);
  129. } else {
  130. throw new IllegalArgumentException("Sim数据错误。");
  131. }
  132. }
  133. public List<Sim> listAll() {
  134. return selectSimList(new Sim());
  135. }
  136. /**
  137. * 获取所有没有被禁用的模拟器列表
  138. *
  139. * @return
  140. */
  141. public AjaxResult listAllEnableAj() {
  142. return AjaxResult.success(listAllEnable());
  143. }
  144. public List<Sim> listAllEnable() {
  145. List<Sim> list = new ArrayList<>();
  146. listAll()
  147. .stream()
  148. .filter(Objects::nonNull)
  149. .filter(s -> !StringUtils.equals(Sim.State.DISABLE, s.getSimState()))
  150. .forEach(list::add);
  151. return list;
  152. }
  153. /**
  154. * 获取所有在线的模拟器列表
  155. *
  156. * @return
  157. */
  158. public AjaxResult listAllOnlineAj() {
  159. return AjaxResult.success(listAllOnline());
  160. }
  161. public List<Sim> listAllOnline() {
  162. List<Sim> list = new ArrayList<>();
  163. listAll()
  164. .stream()
  165. .filter(Objects::nonNull)
  166. .filter(s -> Sim.State.ONLINE.equals(s.getSimState()))
  167. .forEach(list::add);
  168. return list;
  169. }
  170. public List<Sim> listAllEnableBySimType(String simType) {
  171. List<Sim> list = new ArrayList<>();
  172. listAll()
  173. .stream()
  174. .filter(Objects::nonNull)
  175. .filter(s -> !Sim.State.DISABLE.equals(s.getSimState()))
  176. .filter(s -> s.getSimType().equals(simType))
  177. .forEach(list::add);
  178. return list;
  179. }
  180. /**
  181. * 修改所有启用的模拟器 在线/离线 状态。
  182. *
  183. * @param simState
  184. * @return 修改模拟器状态的数量。
  185. */
  186. public int updateAllEnableState(final String simState) {
  187. List<Sim> list = listAllEnable();
  188. for (Sim sim : list) {
  189. sim.setSimState(simState);
  190. simMapper.updateSim(sim);
  191. }
  192. return list.size();
  193. }
  194. public List<String> listSimTypes() {
  195. return Sim.TYPE_SET.stream().toList();
  196. }
  197. public boolean checkSimTypeOk(String simType) {
  198. return Sim.TYPE_SET.contains(simType);
  199. }
  200. public boolean checkState(String simState) {
  201. return !Sim.STATE_SET.contains(simState);
  202. }
  203. /**
  204. * 修改模拟器状态,并重置充电计数。
  205. *
  206. * @param simId
  207. * @param simState
  208. * @return
  209. */
  210. @Transactional(propagation = Propagation.REQUIRES_NEW, isolation = Isolation.SERIALIZABLE)
  211. public int updateSimStateBySimId(final Long simId, final String simState) {
  212. // check
  213. if (checkState(simState)) {
  214. throw new IllegalArgumentException("simState wrong!");
  215. }
  216. //
  217. Sim q = selectSimBySimId(simId);
  218. if (Objects.isNull(q)) {
  219. return 0;
  220. }
  221. // 离线强制重置 chargingCount = 0,归零。
  222. if (StringUtils.equals(simState, Sim.State.OFFLINE)) {
  223. updateChargingCountResetBySimId(simId);
  224. }
  225. q.setSimState(simState);
  226. return updateSim(q);
  227. }
  228. @Transactional(propagation = Propagation.REQUIRES_NEW, isolation = Isolation.SERIALIZABLE)
  229. public int updateSimStateBySimNum(final String simNum, final String simState) {
  230. return updateSimStateBySimId(uniqueBySimNum(simNum).getSimId(), simState);
  231. }
  232. /**
  233. * 充满量
  234. * 单位:s
  235. */
  236. public static final Integer CHARGING_TOTAL = 120;
  237. /**
  238. * 充电步长量
  239. * 单位:s
  240. *
  241. * @param simId
  242. */
  243. public static final Integer CHARGING_STEP = 30;
  244. /**
  245. * 充电起始量。
  246. */
  247. public static final Integer CHARGING_START = 0;
  248. /**
  249. * 充电count步长+1
  250. *
  251. * @param simId
  252. * @return new value.
  253. */
  254. @Transactional(propagation = Propagation.REQUIRES_NEW, isolation = Isolation.SERIALIZABLE)
  255. public Integer updateChargingCountPlusBySimId(final Long simId) {
  256. l.info("updateChargingCountPlusBySimId simId = {}", simId);
  257. Sim q = selectSimBySimId(simId);
  258. if (Objects.isNull(q)) {
  259. throw new IllegalArgumentException("simId");
  260. }
  261. if (q.getChargingCount() >= CHARGING_TOTAL) {// 超过上限。
  262. return q.getChargingCount();
  263. }
  264. Integer countNew = q.getChargingCount() + CHARGING_STEP;
  265. q.setChargingCount(countNew);
  266. updateSim(q);
  267. return countNew;
  268. }
  269. /**
  270. * 充电count归零
  271. *
  272. * @param simId
  273. * @return new value.
  274. */
  275. @Transactional(propagation = Propagation.REQUIRES_NEW, isolation = Isolation.SERIALIZABLE)
  276. public Integer updateChargingCountResetBySimId(final Long simId) {
  277. Sim q = selectSimBySimId(simId);
  278. if (Objects.isNull(q)) {
  279. throw new IllegalArgumentException("simId");
  280. }
  281. q.setChargingCount(CHARGING_START);
  282. updateSim(q);
  283. return CHARGING_START;
  284. }
  285. /**
  286. * 是否充电完成
  287. *
  288. * @param simId
  289. * @return
  290. */
  291. public Boolean isChargingCountFullBySimId(final Long simId) {
  292. Sim q = selectSimBySimId(simId);
  293. if (Objects.isNull(q)) {
  294. throw new IllegalArgumentException("simId");
  295. }
  296. return (q.getChargingCount() >= CHARGING_TOTAL);
  297. }
  298. public Long getChargingCountPercentage(final Long simId) {
  299. Sim q = selectSimBySimId(simId);
  300. if (Objects.isNull(q)) {
  301. throw new IllegalArgumentException("simId");
  302. }
  303. double p = q.getChargingCount() / ((double) CHARGING_TOTAL);
  304. return Math.round(p * 100);
  305. }
  306. public boolean isSimStateBySimId(Long simId, String simState) {
  307. // check
  308. if (checkState(simState)) {
  309. }
  310. Sim q = selectSimBySimId(simId);
  311. if (Objects.isNull(q)) {
  312. return false;
  313. }
  314. return (q.getSimState().equals(simState));
  315. }
  316. public boolean isSimDisable(Long simId) {
  317. return isSimStateBySimId(simId, Sim.State.DISABLE);
  318. }
  319. /**
  320. * 更新模拟器序列号。
  321. *
  322. * @param simId
  323. * @param simSn
  324. * @return
  325. */
  326. public int updateSimSnBySimId(Long simId, String simSn) {
  327. // check
  328. if (StringUtils.isEmpty(simSn)) {
  329. return 0;
  330. }
  331. //
  332. Sim q = selectSimBySimId(simId);
  333. if (Objects.isNull(q)) {
  334. return 0;
  335. }
  336. q.setSimSn(simSn);
  337. q.setUpdateTime(DateUtils.getNowDate());
  338. return updateSim(q);
  339. }
  340. public int updateAllEnableStateInit() {
  341. List<Sim> list = simMapper.selectSimList(new Sim());
  342. int count = 0;
  343. for (Sim o : list) {
  344. if (o == null) {
  345. continue;
  346. }
  347. if (!Objects.equals(o.getSimState(), Sim.State.DISABLE)) {
  348. o.setSimState(Sim.State.ENABLE_INIT);
  349. }
  350. updateSim(o);
  351. count = count + 1;
  352. }
  353. return count;
  354. }
  355. @Async("tp-log")
  356. public void updateLastSentTime(Sim s) {
  357. if (s == null) {
  358. return;
  359. }
  360. s.setLastSentTime(DateUtils.getNowDate());
  361. updateSim(s);
  362. }
  363. @Async("tp-log")
  364. public void updateLastReceivedTime(Sim s) {
  365. if (s == null) {
  366. return;
  367. }
  368. s.setLastReceivedTime(DateUtils.getNowDate());
  369. updateSim(s);
  370. }
  371. }