RtkAccountServiceImpl.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. package com.fdkankan.manage.service.impl;
  2. import cn.dev33.satoken.stp.StpUtil;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  5. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  6. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  7. import com.fdkankan.manage.common.PageInfo;
  8. import com.fdkankan.manage.common.ResultCode;
  9. import com.fdkankan.manage.constant.RtkTypeEnum;
  10. import com.fdkankan.manage.entity.*;
  11. import com.fdkankan.manage.exception.BusinessException;
  12. import com.fdkankan.manage.mapper.ICameraDetailMapper;
  13. import com.fdkankan.manage.mapper.IRtkAccountMapper;
  14. import com.fdkankan.manage.service.*;
  15. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  16. import com.fdkankan.manage.task.DingdingService;
  17. import com.fdkankan.manage.task.TaskService;
  18. import com.fdkankan.manage.util.DateUtils;
  19. import com.fdkankan.manage.util.SendMailUtils;
  20. import com.fdkankan.manage.vo.request.RtkAccountInParam;
  21. import com.fdkankan.manage.vo.request.RtkDeviceInParam;
  22. import com.fdkankan.manage.vo.request.RtkInfoParam;
  23. import com.fdkankan.redis.util.RedisUtil;
  24. import lombok.extern.slf4j.Slf4j;
  25. import org.apache.commons.lang3.StringUtils;
  26. import org.springframework.beans.factory.annotation.Autowired;
  27. import org.springframework.beans.factory.annotation.Value;
  28. import org.springframework.scheduling.annotation.Scheduled;
  29. import org.springframework.stereotype.Service;
  30. import java.math.BigDecimal;
  31. import java.math.RoundingMode;
  32. import java.text.ParseException;
  33. import java.text.SimpleDateFormat;
  34. import java.util.*;
  35. import java.util.stream.Collectors;
  36. /**
  37. * <p>
  38. * 服务实现类
  39. * </p>
  40. *
  41. * @author
  42. * @since 2024-07-22
  43. */
  44. @Service
  45. @Slf4j
  46. public class RtkAccountServiceImpl extends ServiceImpl<IRtkAccountMapper, RtkAccount> implements IRtkAccountService {
  47. @Autowired
  48. RedisUtil redisUtil;
  49. @Autowired
  50. IRtkUseLogService rtkUseLogService;
  51. @Autowired
  52. ISysUserService sysUserService;
  53. @Autowired
  54. DingdingService dingdingService;
  55. @Autowired
  56. TaskService taskService;
  57. @Autowired
  58. ICameraService cameraService;
  59. @Autowired
  60. ICameraDetailService cameraDetailService;
  61. @Autowired
  62. IExcelService excelService;
  63. @Override
  64. public RtkAccount getOneNotUseAccount(String rtkSnCode,String cameraSn) {
  65. String redisKey = "4dkankan:rtk:snCode:"+rtkSnCode;
  66. Long time = 8 * 60 * 60L;
  67. if(StringUtils.isNotBlank(cameraSn)){
  68. Camera camera = cameraService.getBySnCode(cameraSn);
  69. if(camera != null){
  70. CameraDetail cameraDetail = cameraDetailService.getByCameraId(camera.getId());
  71. if(cameraDetail != null && ( cameraDetail.getType() == 10 || cameraDetail.getType() == 11)){
  72. time = 4 * 60 * 60L;
  73. }
  74. }
  75. }
  76. if(redisUtil.hasKey(redisKey)){
  77. String jsonStr = redisUtil.get(redisKey);
  78. RtkAccount rtkAccount = JSONObject.parseObject(jsonStr, RtkAccount.class);
  79. log.info("rtkAccount:{}",jsonStr);
  80. RtkAccount dbRtkAccount = this.getById(rtkAccount.getId());
  81. Boolean flag = checkAccountFailureTime(dbRtkAccount);
  82. if(flag){
  83. redisUtil.set(redisKey,JSONObject.toJSONString(dbRtkAccount),time);
  84. return dbRtkAccount;
  85. }else {
  86. redisUtil.del(redisKey);
  87. }
  88. }
  89. List<RtkAccount> list = this.getByNotUseList();
  90. if(list == null || list.isEmpty()){
  91. //账号库存不足,钉钉通知
  92. dingdingService.sendDingDingMsg(0);
  93. throw new BusinessException(ResultCode.RTK_ACCOUNT_NOT_EXIT);
  94. }
  95. dingdingService.modelThreshold(list.size() -1,this.getByCanUseList().size());
  96. RtkAccount rtkAccount = null;
  97. RtkUseLog rtkUseLog = rtkUseLogService.getByRtkSn(rtkSnCode);
  98. if(rtkUseLog != null && rtkUseLog.getRtkAccountId() != null){
  99. RtkAccount rtkAccount2 = this.getById(rtkUseLog.getRtkAccountId());
  100. if(rtkAccount2 != null && rtkAccount2.getStatus() == 1 && rtkAccount2.getUseStatus() == 0 && rtkAccount2.getFailureTime() !=null && rtkAccount2.getFailureTime().getTime() > new Date().getTime()){
  101. rtkAccount = rtkAccount2;
  102. }
  103. }
  104. if(rtkAccount == null){
  105. rtkAccount = list.get(0);
  106. }
  107. updateAccountStatus(rtkAccount.getId(),2);
  108. redisUtil.set(redisKey,JSONObject.toJSONString(rtkAccount),time);
  109. return rtkAccount;
  110. }
  111. private Boolean checkAccountFailureTime(RtkAccount dbRtkAccount) {
  112. if(dbRtkAccount == null || dbRtkAccount.getUseStatus() == 1){
  113. return false;
  114. }
  115. if(dbRtkAccount.getFailureTime() != null && dbRtkAccount.getFailureTime().getTime() <= new Date().getTime() ){
  116. return false;
  117. }
  118. return true;
  119. }
  120. @Override
  121. public void updateAccountStatus(Integer id, int status) {
  122. LambdaUpdateWrapper<RtkAccount> wrapper1 = new LambdaUpdateWrapper<>();
  123. wrapper1.eq(RtkAccount::getId,id);
  124. wrapper1.set(RtkAccount::getStatus,status);
  125. this.update(wrapper1);
  126. }
  127. @Override
  128. public void stop(RtkDevice rtkDevice,String clientIP,String cameraSn) {
  129. String redisKey = "4dkankan:rtk:snCode:"+rtkDevice.getRtkSnCode();
  130. if(redisUtil.hasKey(redisKey)){
  131. String jsonStr = redisUtil.get(redisKey);
  132. RtkAccount rtkAccount = JSONObject.parseObject(jsonStr, RtkAccount.class);
  133. rtkUseLogService.saveLog(rtkDevice,clientIP,rtkAccount,cameraSn);
  134. updateAccountStatus(rtkAccount.getId(),1);
  135. redisUtil.del(redisKey);
  136. }
  137. }
  138. @Override
  139. public Object pageList(RtkInfoParam param) {
  140. LambdaQueryWrapper<RtkAccount> wrapper = new LambdaQueryWrapper<>();
  141. if(StringUtils.isNotBlank(param.getUserName())){
  142. wrapper.like(RtkAccount::getUserName,param.getUserName());
  143. }
  144. if(StringUtils.isNotBlank(param.getOperator())){
  145. wrapper.like(RtkAccount::getOperator,param.getOperator());
  146. }
  147. if(StringUtils.isNotBlank(param.getStartTime())&& StringUtils.isNotBlank(param.getEndTime()) ) {
  148. wrapper.between(RtkAccount::getCreateTime,param.getStartTime(),param.getEndTime());
  149. }
  150. wrapper.orderByDesc(RtkAccount::getCreateTime);
  151. wrapper.orderByDesc(RtkAccount::getId);
  152. Page<RtkAccount> page = this.page(new Page<>(param.getPageNum(), param.getPageSize()), wrapper);
  153. Set<Long> createUserIds = page.getRecords().stream().map(RtkAccount::getCreateUserId).collect(Collectors.toSet());
  154. HashMap<Long, SysUser> byIds = sysUserService.getByIds(createUserIds);
  155. for (RtkAccount record : page.getRecords()) {
  156. SysUser sysUser = byIds.get(record.getCreateUserId());
  157. if(sysUser != null){
  158. record.setCreateUserName(sysUser.getUserName());
  159. record.setCreateNickName(sysUser.getNickName());
  160. }
  161. }
  162. return PageInfo.PageInfo(page);
  163. }
  164. @Override
  165. public void saveOrEditEntity(RtkAccount param) {
  166. if(StringUtils.isBlank(param.getUserName()) || param.getPassword() == null){
  167. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  168. }
  169. RtkAccount rtkAccount = this.getByUserName(param.getUserName());
  170. if(rtkAccount != null && param.getId() == null){
  171. throw new BusinessException(ResultCode.RTK_USERNAME_EXIST);
  172. }
  173. if(rtkAccount != null && !param.getId().equals(rtkAccount.getId())){
  174. throw new BusinessException(ResultCode.RTK_USERNAME_EXIST);
  175. }
  176. if(param.getId()== null){
  177. param.setCreateUserId(Long.valueOf(StpUtil.getLoginId().toString()));
  178. }
  179. if(param.getId()!= null){
  180. param.setUpdateUserId(Long.valueOf(StpUtil.getLoginId().toString()));
  181. }
  182. this.saveOrUpdate(param);
  183. }
  184. @Override
  185. public void del(RtkAccount param) {
  186. if(param.getId() == null){
  187. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  188. }
  189. RtkAccount rtkAccount = this.getById(param.getId());
  190. if(rtkAccount != null){
  191. LambdaUpdateWrapper<RtkAccount> wrapper = new LambdaUpdateWrapper<>();
  192. wrapper.eq(RtkAccount::getId,param.getId());
  193. wrapper.set(RtkAccount::getDelUserId,Long.valueOf(StpUtil.getLoginId().toString()));
  194. wrapper.set(RtkAccount::getRecStatus,rtkAccount.getId());
  195. this.update(wrapper);
  196. }
  197. }
  198. @Override
  199. public RtkAccount getByUserName(String userName) {
  200. LambdaQueryWrapper<RtkAccount> wrapper = new LambdaQueryWrapper<>();
  201. wrapper.eq(RtkAccount::getUserName,userName);
  202. return this.getOne(wrapper);
  203. }
  204. @Override
  205. public Integer insExcelList(List<HashMap<Integer, String>> excelRowList) {
  206. List<RtkAccountInParam> params = new ArrayList<>();
  207. List<Integer> errorIndex = new ArrayList<>();
  208. Integer index = 0;
  209. for (HashMap<Integer, String> map : excelRowList) {
  210. index ++;
  211. if(map.isEmpty()){
  212. continue;
  213. }
  214. if(index == 0 && !map.get(0).equals("差分账号批量导入模板")){
  215. throw new BusinessException(ResultCode.TEMPLATE_TYPE_ERROR);
  216. }
  217. if(index <4){
  218. continue;
  219. }
  220. String userName = map.get(0);
  221. String password = map.get(1);
  222. String operator = map.get(2);
  223. String ipAddr = map.get(3);
  224. String mountPoint = map.get(4);
  225. String port = map.get(5);
  226. String failureTimeStr = map.get(6);
  227. if(StringUtils.isBlank(userName) && StringUtils.isBlank(password) && StringUtils.isBlank(operator) && StringUtils.isBlank(ipAddr)
  228. && StringUtils.isBlank(mountPoint)&& StringUtils.isBlank(port)&& StringUtils.isBlank(failureTimeStr)){
  229. continue;
  230. }
  231. log.info("rtkAccount-excel-in--userName:{},password:{},operator:{},ipAddr:{},mountPoint:{},port:{},failureTime:{}",userName,password,operator,ipAddr,mountPoint,port,failureTimeStr);
  232. if(StringUtils.isBlank(userName) || StringUtils.isBlank(password) || StringUtils.isBlank(operator)|| StringUtils.isBlank(ipAddr) || StringUtils.isBlank(mountPoint) || StringUtils.isBlank(port)){
  233. errorIndex.add(index -3);
  234. continue;
  235. }
  236. RtkAccount rtkAccount = this.getByUserName(userName);
  237. if(rtkAccount !=null){
  238. errorIndex.add(index -3);
  239. continue;
  240. }
  241. Date failureTime = null;
  242. if(StringUtils.isNotBlank(failureTimeStr)){
  243. try {
  244. failureTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(failureTimeStr);
  245. } catch (ParseException e) {
  246. errorIndex.add(index -3);
  247. continue;
  248. }
  249. }
  250. RtkAccountInParam param = new RtkAccountInParam(userName,password,operator,ipAddr,mountPoint,port,failureTime);
  251. params.add(param);
  252. }
  253. excelService.toExcelError(errorIndex);
  254. if(params.size() <=0){
  255. throw new BusinessException(ResultCode.RTK_DEVICEIN_TEMPLATE_EMPTY);
  256. }
  257. return this.ins(params);
  258. }
  259. private Integer ins(List<RtkAccountInParam> params) {
  260. List<RtkAccount> dbs = new ArrayList<>();
  261. for (RtkAccountInParam param : params) {
  262. RtkAccount rtkAccount = new RtkAccount();
  263. rtkAccount.setUserName(param.getUserName());
  264. rtkAccount.setPassword(param.getPassword());
  265. rtkAccount.setIpAddr(param.getIpAddr());
  266. rtkAccount.setMountPoint(param.getMountPoint());
  267. rtkAccount.setPort(param.getPort());
  268. rtkAccount.setOperator(param.getOperator());
  269. rtkAccount.setFailureTime(param.getFailureTime());
  270. rtkAccount.setCreateUserId(Long.valueOf( StpUtil.getLoginId().toString()));
  271. dbs.add(rtkAccount);
  272. }
  273. if(dbs.isEmpty()){
  274. return 0;
  275. }
  276. this.saveBatch(dbs);
  277. return dbs.size();
  278. }
  279. @Override
  280. public Integer insFailureTimeExcelList(List<HashMap<Integer, String>> excelRowList) {
  281. List<RtkAccountInParam> params = new ArrayList<>();
  282. List<Integer> errorIndex = new ArrayList<>();
  283. Integer index = 0;
  284. for (HashMap<Integer, String> map : excelRowList) {
  285. index++;
  286. if (map.isEmpty()) {
  287. continue;
  288. }
  289. if (index == 0 && !map.get(0).equals("到期时间批量导入模板")) {
  290. throw new BusinessException(ResultCode.TEMPLATE_TYPE_ERROR);
  291. }
  292. if (index < 4) {
  293. continue;
  294. }
  295. String userName = map.get(0);
  296. String failureTimeStr = map.get(1);
  297. if(StringUtils.isBlank(userName) && StringUtils.isBlank(failureTimeStr)){
  298. continue;
  299. }
  300. log.info("rtkAccount-excel-in--userName:{},failureTime:{}",userName,failureTimeStr);
  301. if(StringUtils.isBlank(userName) || StringUtils.isBlank(failureTimeStr)){
  302. errorIndex.add(index -3);
  303. continue;
  304. }
  305. RtkAccount rtkAccount = this.getByUserName(userName);
  306. if(rtkAccount == null){
  307. continue;
  308. }
  309. Date failureTime = null;
  310. if(StringUtils.isNotBlank(failureTimeStr)){
  311. try {
  312. failureTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(failureTimeStr);
  313. } catch (ParseException e) {
  314. //errorIndex.add(index -3);
  315. continue;
  316. }
  317. }
  318. RtkAccountInParam param = new RtkAccountInParam(userName,failureTime);
  319. params.add(param);
  320. }
  321. excelService.toExcelError(errorIndex);
  322. if(params.size() <=0){
  323. throw new BusinessException(ResultCode.RTK_USERNAME_TEMPLATE_EMPTY);
  324. }
  325. return this.updateFailureTime(params);
  326. }
  327. private Integer updateFailureTime(List<RtkAccountInParam> params) {
  328. for (RtkAccountInParam param : params) {
  329. LambdaUpdateWrapper<RtkAccount> wrapper = new LambdaUpdateWrapper<>();
  330. wrapper.eq(RtkAccount::getUserName,param.getUserName());
  331. wrapper.set(RtkAccount::getFailureTime,param.getFailureTime());
  332. this.update(wrapper);
  333. }
  334. return params.size();
  335. }
  336. @Override
  337. public List<RtkAccount> getByNotUseList() {
  338. return this.getBaseMapper().getByNotUseList();
  339. }
  340. @Override
  341. public List<RtkAccount> getByCanUseList() {
  342. return this.getBaseMapper().getByCanUseList();
  343. }
  344. }