UserIncrementServiceImpl.java 18 KB

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