UserIncrementServiceImpl.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. package com.fdkankan.ucenter.service.impl;
  2. import cn.hutool.core.util.StrUtil;
  3. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  4. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  5. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  6. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  7. import com.fdkankan.common.exception.BusinessException;
  8. import com.fdkankan.common.util.PatternUtils;
  9. import com.fdkankan.common.util.SecurityUtil;
  10. import com.fdkankan.sms.SendMailAcceUtils;
  11. import com.fdkankan.sms.SmsService;
  12. import com.fdkankan.ucenter.common.MailUtil;
  13. import com.fdkankan.ucenter.common.PageInfo;
  14. import com.fdkankan.common.util.DateUtil;
  15. import com.fdkankan.ucenter.common.constants.NacosProperty;
  16. import com.fdkankan.ucenter.constant.CameraConstant;
  17. import com.fdkankan.ucenter.constant.LoginConstant;
  18. import com.fdkankan.ucenter.entity.Camera;
  19. import com.fdkankan.ucenter.entity.CameraDetail;
  20. import com.fdkankan.ucenter.entity.User;
  21. import com.fdkankan.ucenter.entity.UserIncrement;
  22. import com.fdkankan.ucenter.mapper.IUserIncrementMapper;
  23. import com.fdkankan.ucenter.service.*;
  24. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  25. import com.fdkankan.ucenter.util.DateUserUtil;
  26. import com.fdkankan.ucenter.vo.request.IncrementParam;
  27. import com.fdkankan.ucenter.vo.response.UserIncrementVo;
  28. import jodd.util.StringUtil;
  29. import lombok.extern.slf4j.Slf4j;
  30. import org.apache.commons.lang3.StringUtils;
  31. import org.springframework.beans.BeanUtils;
  32. import org.springframework.beans.factory.annotation.Autowired;
  33. import org.springframework.stereotype.Service;
  34. import org.springframework.util.ObjectUtils;
  35. import java.security.GeneralSecurityException;
  36. import java.util.*;
  37. import java.util.stream.Collectors;
  38. /**
  39. * <p>
  40. * 用户增值权益表 服务实现类
  41. * </p>
  42. *
  43. * @author
  44. * @since 2022-07-04
  45. */
  46. @Service
  47. public class UserIncrementServiceImpl extends ServiceImpl<IUserIncrementMapper, UserIncrement> implements IUserIncrementService {
  48. @Autowired
  49. ICameraService cameraService;
  50. @Autowired
  51. IUserService userService;
  52. @Autowired
  53. ICameraDetailService cameraDetailService;
  54. @Autowired
  55. ISceneProService sceneProService;
  56. @Autowired
  57. SmsService smsService;
  58. @Autowired
  59. IMailTemplateService mailTemplateService;
  60. @Autowired
  61. ICameraIncrementLogService cameraIncrementLogService;
  62. @Override
  63. public Long getCountByUserId(Long userId, int type) {
  64. LambdaQueryWrapper<UserIncrement> wrapper = new LambdaQueryWrapper<>();
  65. wrapper.eq(UserIncrement::getUserId,userId);
  66. if(type == 0){
  67. wrapper.eq(UserIncrement::getIsExpired,0);
  68. }else if(type == 1){
  69. wrapper.isNotNull(UserIncrement::getCameraId);
  70. }
  71. return this.count(wrapper);
  72. }
  73. @Override
  74. public PageInfo pageList(IncrementParam param) {
  75. Page<UserIncrementVo> pageVo = new Page<>(param.getPageNum(), param.getPageSize());
  76. User user = userService.getByUserName(param.getUserName());
  77. List<Long> cameraIdList = null;
  78. if(StrUtil.isNotEmpty(param.getSnCode()) && StrUtil.isNotEmpty(param.getSnCode().trim())){
  79. List<Camera> cameraEntityList = cameraService.getCameraLikeSnCode(param.getSnCode().trim());
  80. if(cameraEntityList == null || cameraEntityList.size()<=0){
  81. return PageInfo.PageInfo(pageVo);
  82. }
  83. cameraIdList = cameraEntityList.stream().map(Camera::getId)
  84. .collect(Collectors.toList());
  85. }
  86. Page<UserIncrement> page = new Page<>(param.getPageNum(), param.getPageSize());
  87. LambdaQueryWrapper<UserIncrement> queryWrapper = new LambdaQueryWrapper<>();
  88. queryWrapper.eq(UserIncrement::getUserId,user.getId());
  89. queryWrapper.and(i ->i.eq(UserIncrement::getIsExpired,0).or().isNotNull(UserIncrement::getCameraId));
  90. if(cameraIdList!=null ){
  91. queryWrapper.in(UserIncrement::getCameraId,cameraIdList);
  92. }
  93. queryWrapper.orderByDesc(UserIncrement::getId);
  94. Page<UserIncrement> pageEntity = this.page(page, queryWrapper);
  95. List<UserIncrementVo> responseList = convert(pageEntity.getRecords());
  96. pageVo.setTotal(pageEntity.getTotal());
  97. pageVo.setRecords(responseList);
  98. return PageInfo.PageInfo(pageVo);
  99. }
  100. public List<UserIncrementVo> convert(List<UserIncrement> list) {
  101. List<UserIncrementVo> result = new ArrayList<>();
  102. if(list == null){
  103. return result;
  104. }
  105. UserIncrementVo responseUserIncrement = new UserIncrementVo();
  106. for (UserIncrement userIncrementEntity : list) {
  107. responseUserIncrement = new UserIncrementVo();
  108. BeanUtils.copyProperties(userIncrementEntity, responseUserIncrement);
  109. if(responseUserIncrement.getCameraId() != null){
  110. Camera cameraEntity = cameraService.getById(responseUserIncrement.getCameraId());
  111. responseUserIncrement.setSnCode(cameraEntity != null ? cameraEntity.getSnCode() : null);
  112. }
  113. responseUserIncrement.setIsExpire(userIncrementEntity.getIsExpired() != 0);
  114. result.add(responseUserIncrement);
  115. }
  116. return result;
  117. }
  118. @Override
  119. public HashMap<Long, UserIncrement> findByCameraIds(List<Long> cameraIdList) {
  120. HashMap<Long, UserIncrement> map = new HashMap<>();
  121. LambdaQueryWrapper<UserIncrement> wrapper = new LambdaQueryWrapper<>();
  122. wrapper.in(UserIncrement::getCameraId,cameraIdList);
  123. List<UserIncrement> list = this.list(wrapper);
  124. list.forEach(entity -> map.put(entity.getCameraId(),entity));
  125. return map;
  126. }
  127. @Override
  128. public Long getValidCountByCameraId(Long cameraId) {
  129. LambdaQueryWrapper<UserIncrement> wrapper = new LambdaQueryWrapper<>();
  130. wrapper.eq(UserIncrement::getCameraId,cameraId);
  131. wrapper.gt(UserIncrement::getIncrementEndTime, DateUtil.date2String(new Date(),DateUtil.DEFAULT_DATE_FORMAT));
  132. return this.count(wrapper);
  133. }
  134. @Override
  135. public void unbindCamera(List<Long> cameraIds) {
  136. if(cameraIds.size() >0){
  137. LambdaUpdateWrapper<UserIncrement> wrapper = new LambdaUpdateWrapper<>();
  138. wrapper.in(UserIncrement::getCameraId,cameraIds);
  139. if("local".equals(NacosProperty.uploadType)){
  140. this.remove(wrapper);
  141. }else {
  142. wrapper.set(UserIncrement::getCameraId,null);
  143. this.update(wrapper);
  144. }
  145. }
  146. }
  147. @Override
  148. public UserIncrement getByCameraId(Long cameraId) {
  149. LambdaQueryWrapper<UserIncrement> wrapper = new LambdaQueryWrapper<>();
  150. wrapper.eq(UserIncrement::getCameraId,cameraId);
  151. List<UserIncrement> list = this.list(wrapper);
  152. if(list !=null && list.size() >0){
  153. return list.get(0);
  154. }
  155. return null;
  156. }
  157. @Override
  158. public void bindCamera(IncrementParam param) {
  159. if(param.getId() == null || param.getSnCode() == null){
  160. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  161. }
  162. UserIncrement userIncrement = this.getById(param.getId());
  163. if(userIncrement == null){
  164. throw new BusinessException(LoginConstant.FAILURE_CODE_3030, LoginConstant.FAILURE_MSG_3030);
  165. }
  166. if(userIncrement.getIsExpired() == 1){
  167. throw new BusinessException(LoginConstant.FAILURE_CODE_3030, LoginConstant.FAILURE_MSG_3030);
  168. }
  169. Camera cameraEntity = cameraService.getBySnCode(param.getSnCode());
  170. if(cameraEntity == null){
  171. throw new BusinessException(CameraConstant.FAILURE_CODE_6003, CameraConstant.FAILURE_MSG_6003);
  172. }
  173. UserIncrement byCameraId = this.getByCameraId(cameraEntity.getId());
  174. if(byCameraId != null){
  175. throw new BusinessException(LoginConstant.FAILURE_CODE_3032, LoginConstant.FAILURE_MSG_3032);
  176. }
  177. CameraDetail cameraDetailEntity = cameraDetailService.getByCameraId(cameraEntity.getId());
  178. if(cameraDetailEntity == null){
  179. throw new BusinessException(CameraConstant.FAILURE_CODE_6003, CameraConstant.FAILURE_MSG_6003);
  180. }
  181. User user = userService.getByUserName(param.getUserName());
  182. if(user == null){
  183. throw new BusinessException(CameraConstant.FAILURE_CODE_6003, CameraConstant.FAILURE_MSG_6003);
  184. }
  185. if(cameraDetailEntity.getUserId() == null || !cameraDetailEntity.getUserId().equals(user.getId())){
  186. throw new BusinessException(CameraConstant.FAILURE_CODE_6005, CameraConstant.FAILURE_MSG_6005);
  187. }
  188. userIncrement.setCameraId(cameraEntity.getId());
  189. userIncrement.setUpdateTime(DateUserUtil.getDate(new Date()));
  190. this.updateById(userIncrement);
  191. cameraIncrementLogService.saveLog(cameraEntity.getId(),userIncrement.getId(),user.getId(),0);
  192. sceneProService.lockOrUnLockBySpace(cameraDetailEntity,cameraEntity.getId());
  193. }
  194. @Override
  195. public void unbindCamera(IncrementParam param) {
  196. if(param.getId() == null){
  197. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  198. }
  199. UserIncrement userIncrement = this.getById(param.getId());
  200. if(userIncrement == null){
  201. throw new BusinessException(LoginConstant.FAILURE_CODE_3030, LoginConstant.FAILURE_MSG_3030);
  202. }
  203. if(userIncrement.getCameraId() == null){
  204. return;
  205. }
  206. CameraDetail cameraDetail = cameraDetailService.getByCameraId(userIncrement.getCameraId());
  207. if(cameraDetail == null){
  208. throw new BusinessException(CameraConstant.FAILURE_CODE_6003, CameraConstant.FAILURE_MSG_6003);
  209. }
  210. User user = userService.getByUserName(param.getUserName());
  211. if(user == null){
  212. throw new BusinessException(CameraConstant.FAILURE_CODE_6003, CameraConstant.FAILURE_MSG_6003);
  213. }
  214. cameraIncrementLogService.saveLog(userIncrement.getCameraId(),userIncrement.getId(),user.getId(),1);
  215. LambdaUpdateWrapper<UserIncrement> wrapper = new LambdaUpdateWrapper<>();
  216. wrapper.eq(UserIncrement::getId,param.getId());
  217. wrapper.set(UserIncrement::getCameraId,null);
  218. this.update(wrapper);
  219. if(cameraDetail.getType() !=10){
  220. sceneProService.lockOrUnLockBySpace(cameraDetail,cameraDetail.getCameraId());
  221. }
  222. }
  223. @Override
  224. public void incrementExpire() {
  225. //查找所有刚过期的会员权益
  226. LambdaQueryWrapper<UserIncrement> wrapper = new LambdaQueryWrapper<>();
  227. wrapper.eq(UserIncrement::getIsExpired,0);
  228. wrapper.lt(UserIncrement::getIncrementEndTime,DateUserUtil.getDate(new Date()));
  229. List<UserIncrement> list = this.list(wrapper);
  230. lockScene(list);
  231. }
  232. public void lockScene(List<UserIncrement> list){
  233. for (UserIncrement userIncrement : list) {
  234. if(DateUserUtil.getDate(userIncrement.getIncrementEndTime()).getTime() > new Date().getTime()){
  235. userIncrement.setIsExpired(0);
  236. }else {
  237. userIncrement.setIsExpired(1);
  238. }
  239. userIncrement.setUpdateTime(null);
  240. this.updateById(userIncrement);
  241. //解除相机权益
  242. if(userIncrement.getCameraId() != null){
  243. CameraDetail cameraDetail = cameraDetailService.getByCameraId(userIncrement.getCameraId());
  244. if(cameraDetail == null){
  245. continue;
  246. }
  247. if(cameraDetail.getType() !=10){
  248. sceneProService.lockOrUnLockBySpace(cameraDetail,cameraDetail.getCameraId());
  249. }
  250. }
  251. }
  252. }
  253. @Override
  254. public void incrementExpireSendSms() throws Exception {
  255. //查找所有即将到期的会员权益
  256. List<UserIncrement> expireData30 = this.getBaseMapper().findReadyExpire(30, 0);
  257. List<UserIncrement> expireData15 = this.getBaseMapper().findReadyExpire(15, 0);
  258. List<UserIncrement> expireData5 = this.getBaseMapper().findReadyExpire(5, 0);
  259. List<UserIncrement> expireData3 = this.getBaseMapper().findReadyExpire(3, 0);
  260. List<UserIncrement> expireData0 = this.getBaseMapper().findReadyExpire(0, 0);
  261. List<UserIncrement> expireData = this.getBaseMapper().findReadyExpire(-1, 1);
  262. Map<Long, Integer> userIdsRP = new HashMap<>();
  263. Map<Long, Integer> userIdsSE = new HashMap<>();
  264. for (UserIncrement userIncrementEntity : expireData30) {
  265. if(userIncrementEntity.getUserId() != null){
  266. if(StringUtils.isBlank(userIncrementEntity.getMemberLevels() ) || "PR".equals(userIncrementEntity.getMemberLevels())){
  267. userIdsRP.put(userIncrementEntity.getUserId(), 30);
  268. }
  269. }
  270. }
  271. for (UserIncrement userIncrementEntity : expireData15) {
  272. if(userIncrementEntity.getUserId() != null){
  273. if(StringUtils.isBlank(userIncrementEntity.getMemberLevels() ) ||"PR".equals(userIncrementEntity.getMemberLevels())){
  274. userIdsRP.put(userIncrementEntity.getUserId(), 15);
  275. }
  276. }
  277. }
  278. for (UserIncrement userIncrementEntity : expireData5) {
  279. if(userIncrementEntity.getUserId() != null){
  280. if(StringUtils.isBlank(userIncrementEntity.getMemberLevels() ) ||"PR".equals(userIncrementEntity.getMemberLevels())){
  281. userIdsRP.put(userIncrementEntity.getUserId(), 5);
  282. }
  283. if("SE".equals(userIncrementEntity.getMemberLevels())){
  284. userIdsSE.put(userIncrementEntity.getUserId(), 5);
  285. }
  286. }
  287. }
  288. for (UserIncrement userIncrementEntity : expireData3) {
  289. if(userIncrementEntity.getUserId() != null){
  290. if("SE".equals(userIncrementEntity.getMemberLevels())){
  291. userIdsSE.put(userIncrementEntity.getUserId(), 3);
  292. }
  293. }
  294. }
  295. for (UserIncrement userIncrementEntity : expireData0) {
  296. if(userIncrementEntity.getUserId() != null){
  297. if(StringUtils.isBlank(userIncrementEntity.getMemberLevels() ) ||"PR".equals(userIncrementEntity.getMemberLevels())){
  298. userIdsRP.put(userIncrementEntity.getUserId(), 0);
  299. }
  300. if("SE".equals(userIncrementEntity.getMemberLevels())){
  301. userIdsSE.put(userIncrementEntity.getUserId(), 0);
  302. }
  303. }
  304. }
  305. for (UserIncrement userIncrementEntity : expireData) {
  306. if(userIncrementEntity.getUserId() != null){
  307. if(StringUtils.isBlank(userIncrementEntity.getMemberLevels() ) ||"PR".equals(userIncrementEntity.getMemberLevels())){
  308. userIdsRP.put(userIncrementEntity.getUserId(), -1);
  309. }
  310. if("SE".equals(userIncrementEntity.getMemberLevels())){
  311. userIdsSE.put(userIncrementEntity.getUserId(), -1);
  312. }
  313. }
  314. }
  315. this.sendMsg(userIdsRP,"premium");
  316. this.sendMsg(userIdsSE,"senior");
  317. }
  318. private void sendMsg(Map<Long, Integer> userIds,String msgType) throws Exception {
  319. String cnCode = "SMS_216275556";
  320. String expireCode = "SMS_216425565";
  321. for (Long userId : userIds.keySet()) {
  322. User userEntity = userService.getById(userId);
  323. if(userEntity != null){
  324. if("oss".equals(NacosProperty.uploadType) && StringUtil.isNotBlank(userEntity.getUserName()) && StringUtils.isNumeric(userEntity.getUserName())){
  325. if(userIds.get(userId) == -1){
  326. smsService.sendSms(userEntity.getUserName(), "{\"time\":\"" + userIds.get(userId) + "\"}", expireCode);
  327. continue;
  328. }
  329. smsService.sendSms(userEntity.getUserName(), "{\"time\":\"" + userIds.get(userId) + "\"}", cnCode);
  330. }
  331. if("aws".equals(NacosProperty.uploadType) && StringUtil.isNotBlank(userEntity.getUserName())){
  332. Integer days = userIds.get(userId);
  333. if(days == null || days<0){
  334. mailTemplateService.sendPeExMail(userEntity.getUserName());
  335. continue;
  336. }
  337. if(days > 0){
  338. mailTemplateService.sendPeNoExMail(userEntity.getUserName(),userIds.get(userId));
  339. continue;
  340. }
  341. mailTemplateService.sendPeTodayExMail(userEntity.getUserName());
  342. }
  343. }
  344. }
  345. }
  346. @Override
  347. public void addByCameraAndUser(List<Long> cameraIds, Long userId) {
  348. this.delByCameraId(cameraIds);
  349. for (Long cameraId : cameraIds) {
  350. UserIncrement userIncrement = new UserIncrement();
  351. userIncrement.setKeyWord(UUID.randomUUID().toString().replace("-", ""));
  352. userIncrement.setIsExpired(0);
  353. userIncrement.setIncrementStartTime(DateUserUtil.getDate(new Date()));
  354. userIncrement.setCameraId(cameraId);
  355. userIncrement.setIncrementTypeId(1);
  356. userIncrement.setIncrementEndTime("2199-01-01 00:00:00");
  357. userIncrement.setUserId(userId);
  358. this.save(userIncrement);
  359. }
  360. }
  361. @Override
  362. public void delByCameraId(List<Long> cameraIds) {
  363. if(cameraIds.size() >0){
  364. LambdaQueryWrapper<UserIncrement> wrapper = new LambdaQueryWrapper<>();
  365. wrapper.in(UserIncrement::getCameraId,cameraIds);
  366. this.remove(wrapper);
  367. }
  368. }
  369. @Override
  370. public HashMap<String, List<Long>> getByOrderSnList(Set<String> orderSns) {
  371. HashMap<String, List<Long>> map = new HashMap<>();
  372. if(orderSns.size() >0){
  373. for (String orderSn : orderSns) {
  374. LambdaQueryWrapper<UserIncrement> wrapper = new LambdaQueryWrapper<>();
  375. wrapper.like(UserIncrement::getOrderSn,orderSn);
  376. List<UserIncrement> list = this.list(wrapper);
  377. for (UserIncrement userIncrement : list) {
  378. map.computeIfAbsent(userIncrement.getOrderSn(), k -> new ArrayList<>());
  379. map.get(userIncrement.getOrderSn()).add(userIncrement.getId());
  380. }
  381. }
  382. }
  383. return map;
  384. }
  385. }