UserIncrementServiceImpl.java 20 KB

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