UserIncrementServiceImpl.java 12 KB

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