UserIncrementServiceImpl.java 20 KB

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