UserIncrementServiceImpl.java 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. package com.fdkankan.manage.service.impl;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  4. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  5. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  6. import com.fdkankan.manage.common.CacheUtil;
  7. import com.fdkankan.manage.common.ResultCode;
  8. import com.fdkankan.manage.entity.*;
  9. import com.fdkankan.manage.exception.BusinessException;
  10. import com.fdkankan.manage.common.PageInfo;
  11. import com.fdkankan.common.util.DateUtil;
  12. import com.fdkankan.manage.mapper.IUserIncrementMapper;
  13. import com.fdkankan.manage.service.*;
  14. import com.fdkankan.manage.util.Dateutils;
  15. import com.fdkankan.manage.vo.request.UserIncrementParam;
  16. import com.fdkankan.manage.vo.response.GroupByCount;
  17. import com.fdkankan.manage.vo.response.UserIncrementVo;
  18. import org.springframework.beans.BeanUtils;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.stereotype.Service;
  21. import java.util.*;
  22. import java.util.stream.Collectors;
  23. /**
  24. * <p>
  25. * 用户增值权益表 服务实现类
  26. * </p>
  27. *
  28. * @author
  29. * @since 2022-06-16
  30. */
  31. @Service
  32. public class UserIncrementServiceImpl extends ServiceImpl<IUserIncrementMapper, UserIncrement> implements IUserIncrementService {
  33. @Autowired
  34. private ICameraService cameraService;
  35. @Autowired
  36. IIncrementTypeService incrementTypeService;
  37. @Autowired
  38. IUserService userService;
  39. @Autowired
  40. IAgentNewLogService agentNewLogService;
  41. @Autowired
  42. ICameraIncrementLogService cameraIncrementLogService;
  43. @Autowired
  44. ISceneProService sceneProService;
  45. @Autowired
  46. IIncrementOrderMgService iIncrementOrderMgService;
  47. @Override
  48. public Long getValidCountByUserId(Long userId) {
  49. LambdaQueryWrapper<UserIncrement> wrapper = new LambdaQueryWrapper<>();
  50. wrapper.eq(UserIncrement::getUserId,userId);
  51. wrapper.gt(UserIncrement::getIncrementEndTime, DateUtil.date2String(new Date(),DateUtil.DEFAULT_DATE_FORMAT));
  52. return this.count(wrapper);
  53. }
  54. @Override
  55. public HashMap<Long, Long> getValidCountGroupByUserId(List<Long> userIdList) {
  56. HashMap<Long,Long> map = new HashMap<>();
  57. List<GroupByCount> result = this.getBaseMapper().getValidCountGroupByUserId(userIdList);
  58. result.forEach(entity ->map.put(entity.getId(),entity.getCount()));
  59. return map;
  60. }
  61. @Override
  62. public PageInfo pageList(Long userId, Integer pageNum, Integer pageSize) {
  63. if(userId == null){
  64. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  65. }
  66. LambdaQueryWrapper<UserIncrement> wrapper = new LambdaQueryWrapper<>();
  67. wrapper.eq(UserIncrement::getUserId,userId);
  68. wrapper.and(i ->i.eq(UserIncrement::getIsExpired,0).or().isNotNull(UserIncrement::getCameraId));
  69. wrapper.orderByDesc(UserIncrement::getCreateTime);
  70. Page<UserIncrement> page = this.page(new Page<>(pageNum, pageSize), wrapper);
  71. List<Long> cameraIdList = page.getRecords().parallelStream()
  72. .filter(camera ->camera.getCameraId()!=null)
  73. .map(UserIncrement::getCameraId)
  74. .collect(Collectors.toList());
  75. HashMap<Long,Camera> cameraMap = new HashMap<>();
  76. if(cameraIdList.size() >0){
  77. List<Camera> details = cameraService.getListByCameraIdList(cameraIdList);
  78. for (Camera detail : details) {
  79. cameraMap.put(detail.getId(),detail);
  80. }
  81. }
  82. List<UserIncrementVo> voList = new ArrayList<>();
  83. for (UserIncrement record : page.getRecords()) {
  84. UserIncrementVo vo = new UserIncrementVo();
  85. BeanUtils.copyProperties(record,vo);
  86. if(record.getCameraId() != null){
  87. Camera camera = cameraMap.get(record.getCameraId());
  88. if(camera != null){
  89. vo.setSnCode(camera.getSnCode());
  90. }
  91. }
  92. if(record.getIncrementTypeId()!= null){
  93. IncrementType incrementType = incrementTypeService.getById(record.getIncrementTypeId());
  94. vo.setValidTimeType(incrementType.getValidTimeType());
  95. }
  96. voList.add(vo);
  97. }
  98. Page<UserIncrementVo> voPage = new Page<>(pageNum, pageSize);
  99. voPage.setRecords(voList);
  100. voPage.setTotal(page.getTotal());
  101. return PageInfo.PageInfo(voPage);
  102. }
  103. @Override
  104. public void delayById(Long id, Integer year) {
  105. UserIncrement userIncrement = this.getById(id);
  106. if(userIncrement == null){
  107. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  108. }
  109. IncrementType incrementType = incrementTypeService.getById(userIncrement.getIncrementTypeId());
  110. String incrementEndTime = userIncrement.getIncrementEndTime();
  111. if(userIncrement.getIsExpired() == 1){
  112. incrementEndTime = Dateutils.getDate(new Date());
  113. }
  114. userIncrement.setIsExpired(0);
  115. Date date = DateUtil.string2Date(incrementEndTime, DateUtil.DEFAULT_DATE_FORMAT);
  116. Date delay = null;
  117. switch (incrementType.getValidTimeType()){
  118. case 0 : delay = DateUtil.delay(date, 1, 1); break;
  119. case 1 : delay = DateUtil.delay(date, 1, 2); break;
  120. case 2 : delay = DateUtil.delay(date, 1, 5); break;
  121. default: throw new BusinessException(ResultCode.INCREMENT_TYPE_ERROR);
  122. }
  123. userIncrement.setIncrementEndTime(DateUtil.date2String(delay,DateUtil.DEFAULT_DATE_FORMAT));
  124. userIncrement.setUpdateTime(null);
  125. this.updateById(userIncrement);
  126. UserIncrementParam param = new UserIncrementParam();
  127. param.setUserId(userIncrement.getUserId());
  128. param.setCount(incrementType.getDownloadNum());
  129. userService.addDownNum(param);
  130. agentNewLogService.addByUserIncrement(userIncrement);
  131. if(userIncrement.getCameraId() != null){
  132. sceneProService.lockOrUnLockBySpace(null,userIncrement.getCameraId(),1);
  133. }
  134. }
  135. @Override
  136. public void add(UserIncrementParam param) {
  137. List<UserIncrement> userIncrementList = new ArrayList<>();
  138. IncrementType incrementType = incrementTypeService.getById(param.getIncrementTypeId());
  139. if(incrementType == null){
  140. throw new BusinessException(ResultCode.INCREMENT_TYPE_EMPTY);
  141. }
  142. iIncrementOrderMgService.addOrder(param);
  143. for (int i = 0 ; i<param.getCount() ;i++) {
  144. UserIncrement userIncrement = new UserIncrement();
  145. String date = DateUtil.date2String(new Date(), DateUtil.DEFAULT_DATE_FORMAT);
  146. userIncrement.setUserId(param.getUserId());
  147. userIncrement.setKeyWord(UUID.randomUUID().toString().replace("-", ""));
  148. userIncrement.setIsExpired(0);
  149. userIncrement.setCreateTime(date);
  150. userIncrement.setUpdateTime(date);
  151. userIncrement.setIncrementStartTime(date);
  152. userIncrement.setIncrementEndTime(param.getIncrementEndTime());
  153. userIncrement.setIncrementTypeId(param.getIncrementTypeId());
  154. if(incrementType.getValidTimeType() == 0){
  155. userIncrement.setMemberLevels("PR");
  156. }
  157. if(incrementType.getValidTimeType() == 1){
  158. userIncrement.setMemberLevels("SE");
  159. }
  160. if(param.getMonthQy() != null){
  161. userIncrement.setMonthQy(param.getMonthQy());
  162. }
  163. userIncrementList.add(userIncrement);
  164. }
  165. if(userIncrementList.size() >0){
  166. this.saveBatch(userIncrementList);
  167. User user = userService.getById(param.getUserId());
  168. if(user == null){
  169. throw new BusinessException(ResultCode.USER_NOT_EXIST);
  170. }
  171. user.setDownloadNumTotal(user.getDownloadNumTotal() + param.getCount() * incrementType.getDownloadNum());
  172. userService.updateById(user);
  173. }
  174. }
  175. @Override
  176. public void unbindCamera(Long cameraId) {
  177. cameraIncrementLogService.saveUnbindLog(cameraId);
  178. LambdaUpdateWrapper<UserIncrement> wrapper = new LambdaUpdateWrapper<>();
  179. wrapper.eq(UserIncrement::getCameraId,cameraId);
  180. if("local".equals(CacheUtil.uploadType)){
  181. this.remove(wrapper);
  182. }else {
  183. wrapper.set(UserIncrement::getCameraId,null);
  184. this.update(wrapper);
  185. }
  186. }
  187. @Override
  188. public Long getValidCountByCameraId(Long cameraId) {
  189. LambdaQueryWrapper<UserIncrement> wrapper = new LambdaQueryWrapper<>();
  190. wrapper.eq(UserIncrement::getCameraId,cameraId);
  191. wrapper.gt(UserIncrement::getIncrementEndTime, DateUtil.date2String(new Date(),DateUtil.DEFAULT_DATE_FORMAT));
  192. return this.count(wrapper);
  193. }
  194. @Override
  195. public UserIncrement getByCameraId(Long cameraId) {
  196. LambdaQueryWrapper<UserIncrement> wrapper = new LambdaQueryWrapper<>();
  197. wrapper.eq(UserIncrement::getCameraId,cameraId);
  198. List<UserIncrement> list = this.list(wrapper);
  199. if(list !=null && list.size() >0){
  200. return list.get(0);
  201. }
  202. return null;
  203. }
  204. @Override
  205. public void delAgentId(Integer agentId) {
  206. LambdaUpdateWrapper<UserIncrement> wrapper = new LambdaUpdateWrapper<>();
  207. wrapper.eq(UserIncrement::getAgentId,agentId);
  208. wrapper.set(UserIncrement::getAgentId,null);
  209. this.update(wrapper);
  210. }
  211. }