UserIncrementServiceImpl.java 20 KB

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