UserIncrementServiceImpl.java 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. @Override
  42. public Long getValidCountByUserId(Long userId) {
  43. LambdaQueryWrapper<UserIncrement> wrapper = new LambdaQueryWrapper<>();
  44. wrapper.eq(UserIncrement::getUserId,userId);
  45. wrapper.gt(UserIncrement::getIncrementEndTime, DateUtil.date2String(new Date(),DateUtil.DEFAULT_DATE_FORMAT));
  46. return this.count(wrapper);
  47. }
  48. @Override
  49. public HashMap<Long, Long> getValidCountGroupByUserId(List<Long> userIdList) {
  50. HashMap<Long,Long> map = new HashMap<>();
  51. List<GroupByCount> result = this.getBaseMapper().getValidCountGroupByUserId(userIdList);
  52. result.forEach(entity ->map.put(entity.getId(),entity.getCount()));
  53. return map;
  54. }
  55. @Override
  56. public PageInfo pageList(Long userId, Integer pageNum, Integer pageSize) {
  57. if(userId == null){
  58. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  59. }
  60. LambdaQueryWrapper<UserIncrement> wrapper = new LambdaQueryWrapper<>();
  61. wrapper.eq(UserIncrement::getUserId,userId);
  62. wrapper.and(i ->i.eq(UserIncrement::getIsExpired,0).or().isNotNull(UserIncrement::getCameraId));
  63. wrapper.orderByDesc(UserIncrement::getCreateTime);
  64. Page<UserIncrement> page = this.page(new Page<>(pageNum, pageSize), wrapper);
  65. List<Long> cameraIdList = page.getRecords().parallelStream()
  66. .filter(camera ->camera.getCameraId()!=null)
  67. .map(UserIncrement::getCameraId)
  68. .collect(Collectors.toList());
  69. HashMap<Long,Camera> cameraMap = new HashMap<>();
  70. if(cameraIdList.size() >0){
  71. List<Camera> details = cameraService.getListByCameraIdList(cameraIdList);
  72. for (Camera detail : details) {
  73. cameraMap.put(detail.getId(),detail);
  74. }
  75. }
  76. List<UserIncrementVo> voList = new ArrayList<>();
  77. for (UserIncrement record : page.getRecords()) {
  78. UserIncrementVo vo = new UserIncrementVo();
  79. BeanUtils.copyProperties(record,vo);
  80. if(record.getCameraId() != null){
  81. Camera camera = cameraMap.get(record.getCameraId());
  82. if(camera != null){
  83. vo.setSnCode(camera.getSnCode());
  84. }
  85. }
  86. if(record.getIncrementTypeId()!= null){
  87. IncrementType incrementType = incrementTypeService.getById(record.getIncrementTypeId());
  88. vo.setValidTimeType(incrementType.getValidTimeType());
  89. }
  90. voList.add(vo);
  91. }
  92. Page<UserIncrementVo> voPage = new Page<>(pageNum, pageSize);
  93. voPage.setRecords(voList);
  94. voPage.setTotal(page.getTotal());
  95. return PageInfo.PageInfo(voPage);
  96. }
  97. @Override
  98. public void delayById(Long id, Integer year) {
  99. UserIncrement userIncrement = this.getById(id);
  100. if(userIncrement == null){
  101. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  102. }
  103. IncrementType incrementType = incrementTypeService.getById(userIncrement.getIncrementTypeId());
  104. userIncrement.setIsExpired(0);
  105. Date date = DateUtil.string2Date(userIncrement.getIncrementEndTime(), DateUtil.DEFAULT_DATE_FORMAT);
  106. Date delay = null;
  107. switch (incrementType.getValidTimeType()){
  108. case 0 : delay = DateUtil.delay(date, 1, 1); break;
  109. case 1 : delay = DateUtil.delay(date, 1, 2); break;
  110. case 2 : delay = DateUtil.delay(date, 1, 5); break;
  111. default: throw new BusinessException(ResultCode.INCREMENT_TYPE_ERROR);
  112. }
  113. userIncrement.setIncrementEndTime(DateUtil.date2String(delay,DateUtil.DEFAULT_DATE_FORMAT));
  114. userIncrement.setUpdateTime(null);
  115. this.updateById(userIncrement);
  116. agentNewLogService.addByUserIncrement(userIncrement);
  117. }
  118. @Override
  119. public void add(UserIncrementParam param) {
  120. List<UserIncrement> userIncrementList = new ArrayList<>();
  121. IncrementType incrementType = incrementTypeService.getById(param.getIncrementTypeId());
  122. if(incrementType == null){
  123. throw new BusinessException(ResultCode.INCREMENT_TYPE_EMPTY);
  124. }
  125. for (int i = 0 ; i<param.getCount() ;i++) {
  126. UserIncrement userIncrement = new UserIncrement();
  127. String date = DateUtil.date2String(new Date(), DateUtil.DEFAULT_DATE_FORMAT);
  128. userIncrement.setUserId(param.getUserId());
  129. userIncrement.setKeyWord(UUID.randomUUID().toString().replace("-", ""));
  130. userIncrement.setIsExpired(0);
  131. userIncrement.setCreateTime(date);
  132. userIncrement.setUpdateTime(date);
  133. userIncrement.setIncrementStartTime(date);
  134. userIncrement.setIncrementEndTime(param.getIncrementEndTime());
  135. userIncrement.setIncrementTypeId(param.getIncrementTypeId());
  136. if(incrementType.getValidTimeType() == 0){
  137. userIncrement.setMemberLevels("PR");
  138. }
  139. if(incrementType.getValidTimeType() == 1){
  140. userIncrement.setMemberLevels("SE");
  141. }
  142. if(param.getMonthQy() != null){
  143. userIncrement.setMonthQy(param.getMonthQy());
  144. }
  145. userIncrementList.add(userIncrement);
  146. }
  147. if(userIncrementList.size() >0){
  148. this.saveBatch(userIncrementList);
  149. User user = userService.getById(param.getUserId());
  150. if(user == null){
  151. throw new BusinessException(ResultCode.USER_NOT_EXIST);
  152. }
  153. user.setDownloadNumTotal(user.getDownloadNumTotal() + param.getCount() * incrementType.getDownloadNum());
  154. userService.updateById(user);
  155. }
  156. }
  157. @Override
  158. public void unbindCamera(Long cameraId) {
  159. LambdaUpdateWrapper<UserIncrement> wrapper = new LambdaUpdateWrapper<>();
  160. wrapper.eq(UserIncrement::getCameraId,cameraId);
  161. if("local".equals(CacheUtil.uploadType)){
  162. this.remove(wrapper);
  163. }else {
  164. wrapper.set(UserIncrement::getCameraId,null);
  165. this.update(wrapper);
  166. }
  167. }
  168. @Override
  169. public Long getValidCountByCameraId(Long cameraId) {
  170. LambdaQueryWrapper<UserIncrement> wrapper = new LambdaQueryWrapper<>();
  171. wrapper.eq(UserIncrement::getCameraId,cameraId);
  172. wrapper.gt(UserIncrement::getIncrementEndTime, DateUtil.date2String(new Date(),DateUtil.DEFAULT_DATE_FORMAT));
  173. return this.count(wrapper);
  174. }
  175. @Override
  176. public UserIncrement getByCameraId(Long cameraId) {
  177. LambdaQueryWrapper<UserIncrement> wrapper = new LambdaQueryWrapper<>();
  178. wrapper.eq(UserIncrement::getCameraId,cameraId);
  179. List<UserIncrement> list = this.list(wrapper);
  180. if(list !=null && list.size() >0){
  181. return list.get(0);
  182. }
  183. return null;
  184. }
  185. @Override
  186. public void delAgentId(Integer agentId) {
  187. LambdaUpdateWrapper<UserIncrement> wrapper = new LambdaUpdateWrapper<>();
  188. wrapper.eq(UserIncrement::getAgentId,agentId);
  189. wrapper.set(UserIncrement::getAgentId,null);
  190. this.update(wrapper);
  191. }
  192. }