RtkAccountServiceImpl.java 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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.dingtalk.DingTalkSendUtils;
  8. import com.fdkankan.manage.common.PageInfo;
  9. import com.fdkankan.manage.common.ResultCode;
  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.SendMailUtils;
  19. import com.fdkankan.manage.vo.request.RtkInfoParam;
  20. import com.fdkankan.redis.util.RedisUtil;
  21. import lombok.extern.slf4j.Slf4j;
  22. import org.apache.commons.lang3.StringUtils;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.beans.factory.annotation.Value;
  25. import org.springframework.scheduling.annotation.Scheduled;
  26. import org.springframework.stereotype.Service;
  27. import java.math.BigDecimal;
  28. import java.math.RoundingMode;
  29. import java.util.Date;
  30. import java.util.HashMap;
  31. import java.util.List;
  32. import java.util.Set;
  33. import java.util.stream.Collectors;
  34. /**
  35. * <p>
  36. * 服务实现类
  37. * </p>
  38. *
  39. * @author
  40. * @since 2024-07-22
  41. */
  42. @Service
  43. @Slf4j
  44. public class RtkAccountServiceImpl extends ServiceImpl<IRtkAccountMapper, RtkAccount> implements IRtkAccountService {
  45. @Autowired
  46. RedisUtil redisUtil;
  47. @Autowired
  48. IRtkUseLogService rtkUseLogService;
  49. @Autowired
  50. ISysUserService sysUserService;
  51. @Autowired
  52. DingdingService dingdingService;
  53. @Autowired
  54. TaskService taskService;
  55. @Autowired
  56. ICameraService cameraService;
  57. @Autowired
  58. ICameraDetailService cameraDetailService;
  59. @Override
  60. public RtkAccount getOneNotUseAccount(String rtkSnCode,String cameraSn) {
  61. String redisKey = "4dkankan:rtk:snCode:"+rtkSnCode;
  62. Long time = 8 * 60 * 60L;
  63. if(StringUtils.isNotBlank(cameraSn)){
  64. Camera camera = cameraService.getBySnCode(cameraSn);
  65. if(camera != null){
  66. CameraDetail cameraDetail = cameraDetailService.getByCameraId(camera.getId());
  67. if(cameraDetail != null && ( cameraDetail.getType() == 10 || cameraDetail.getType() == 11)){
  68. time = 4 * 60 * 60L;
  69. }
  70. }
  71. }
  72. if(redisUtil.hasKey(redisKey)){
  73. String jsonStr = redisUtil.get(redisKey);
  74. redisUtil.expire(redisKey,time);
  75. return JSONObject.parseObject(jsonStr, RtkAccount.class);
  76. }
  77. LambdaQueryWrapper<RtkAccount> wrapper = new LambdaQueryWrapper<>();
  78. wrapper.in(RtkAccount::getStatus,0,1);
  79. wrapper.orderByAsc(RtkAccount::getUpdateTime);
  80. wrapper.orderByAsc(RtkAccount::getId);
  81. List<RtkAccount> list = this.list(wrapper);
  82. if(list == null || list.isEmpty()){
  83. //账号库存不足,钉钉通知
  84. dingdingService.sendDingDingMsg(0);
  85. throw new BusinessException(ResultCode.RTK_ACCOUNT_NOT_EXIT);
  86. }
  87. dingdingService.modelThreshold(list.size() -1,this.count());
  88. RtkAccount rtkAccount = null;
  89. RtkUseLog rtkUseLog = rtkUseLogService.getByRtkSn(rtkSnCode);
  90. if(rtkUseLog != null && rtkUseLog.getRtkAccountId() != null){
  91. RtkAccount rtkAccount2 = this.getById(rtkUseLog.getRtkAccountId());
  92. if(rtkAccount2 != null && rtkAccount2.getStatus() == 1){
  93. rtkAccount = rtkAccount2;
  94. }
  95. }
  96. if(rtkAccount == null){
  97. rtkAccount = list.get(0);
  98. }
  99. updateAccountStatus(rtkAccount.getId(),2);
  100. redisUtil.set(redisKey,JSONObject.toJSONString(rtkAccount),time);
  101. return rtkAccount;
  102. }
  103. @Override
  104. public void updateAccountStatus(Integer id, int status) {
  105. LambdaUpdateWrapper<RtkAccount> wrapper1 = new LambdaUpdateWrapper<>();
  106. wrapper1.eq(RtkAccount::getId,id);
  107. wrapper1.set(RtkAccount::getStatus,status);
  108. this.update(wrapper1);
  109. }
  110. @Override
  111. public void stop(String rtkSnCode) {
  112. String redisKey = "4dkankan:rtk:snCode:"+rtkSnCode;
  113. if(redisUtil.hasKey(redisKey)){
  114. String jsonStr = redisUtil.get(redisKey);
  115. RtkAccount rtkAccount = JSONObject.parseObject(jsonStr, RtkAccount.class);
  116. updateAccountStatus(rtkAccount.getId(),1);
  117. redisUtil.del(redisKey);
  118. }
  119. }
  120. @Override
  121. public Object pageList(RtkInfoParam param) {
  122. LambdaQueryWrapper<RtkAccount> wrapper = new LambdaQueryWrapper<>();
  123. if(StringUtils.isNotBlank(param.getUserName())){
  124. wrapper.like(RtkAccount::getUserName,param.getUserName());
  125. }
  126. if(StringUtils.isNotBlank(param.getStartTime())&& StringUtils.isNotBlank(param.getEndTime()) ) {
  127. wrapper.between(RtkAccount::getCreateTime,param.getStartTime(),param.getEndTime());
  128. }
  129. wrapper.orderByDesc(RtkAccount::getCreateTime);
  130. wrapper.orderByDesc(RtkAccount::getId);
  131. Page<RtkAccount> page = this.page(new Page<>(param.getPageNum(), param.getPageSize()), wrapper);
  132. Set<Long> createUserIds = page.getRecords().stream().map(RtkAccount::getCreateUserId).collect(Collectors.toSet());
  133. HashMap<Long, SysUser> byIds = sysUserService.getByIds(createUserIds);
  134. for (RtkAccount record : page.getRecords()) {
  135. SysUser sysUser = byIds.get(record.getCreateUserId());
  136. if(sysUser != null){
  137. record.setCreateUserName(sysUser.getUserName());
  138. record.setCreateNickName(sysUser.getNickName());
  139. }
  140. }
  141. return PageInfo.PageInfo(page);
  142. }
  143. @Override
  144. public void saveOrEditEntity(RtkAccount param) {
  145. if(StringUtils.isBlank(param.getUserName()) || param.getPassword() == null){
  146. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  147. }
  148. RtkAccount rtkAccount = this.getByUserName(param.getUserName());
  149. if(rtkAccount != null && param.getId() == null){
  150. throw new BusinessException(ResultCode.RTK_SN_EXIST);
  151. }
  152. if(rtkAccount != null && !param.getId().equals(rtkAccount.getId())){
  153. throw new BusinessException(ResultCode.RTK_SN_EXIST);
  154. }
  155. if(param.getId()== null){
  156. param.setCreateUserId(Long.valueOf(StpUtil.getLoginId().toString()));
  157. }
  158. if(param.getId()!= null){
  159. param.setUpdateUserId(Long.valueOf(StpUtil.getLoginId().toString()));
  160. }
  161. this.saveOrUpdate(param);
  162. }
  163. @Override
  164. public void del(RtkAccount param) {
  165. if(param.getId() == null){
  166. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  167. }
  168. RtkAccount rtkAccount = this.getById(param.getId());
  169. if(rtkAccount != null){
  170. LambdaUpdateWrapper<RtkAccount> wrapper = new LambdaUpdateWrapper<>();
  171. wrapper.eq(RtkAccount::getId,param.getId());
  172. wrapper.set(RtkAccount::getDelUserId,Long.valueOf(StpUtil.getLoginId().toString()));
  173. wrapper.set(RtkAccount::getRecStatus,rtkAccount.getId());
  174. this.update(wrapper);
  175. }
  176. }
  177. @Override
  178. public RtkAccount getByUserName(String userName) {
  179. LambdaQueryWrapper<RtkAccount> wrapper = new LambdaQueryWrapper<>();
  180. wrapper.eq(RtkAccount::getUserName,userName);
  181. return this.getOne(wrapper);
  182. }
  183. }