UserServiceService.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package com.qxgmat.service;
  2. import com.github.pagehelper.Page;
  3. import com.nuliji.tools.AbstractService;
  4. import com.nuliji.tools.exception.ParameterException;
  5. import com.nuliji.tools.exception.SystemException;
  6. import com.nuliji.tools.mybatis.Example;
  7. import com.qxgmat.data.constants.enums.ServiceKey;
  8. import com.qxgmat.data.dao.UserServiceMapper;
  9. import com.qxgmat.data.dao.entity.UserService;
  10. import com.qxgmat.service.inline.UserOrderRecordService;
  11. import org.slf4j.Logger;
  12. import org.slf4j.LoggerFactory;
  13. import org.springframework.stereotype.Service;
  14. import javax.annotation.Resource;
  15. import java.util.*;
  16. @Service
  17. public class UserServiceService extends AbstractService {
  18. private static final Logger logger = LoggerFactory.getLogger(UserServiceService.class);
  19. @Resource
  20. private UserServiceMapper userServiceMapper;
  21. @Resource
  22. private UserOrderRecordService userOrderRecordService;
  23. /**
  24. * 判断是否有权限
  25. * @param userId
  26. * @param key
  27. * @return
  28. */
  29. public boolean hasService(Integer userId, ServiceKey key){
  30. if (key == null) return false;
  31. Example example = new Example(UserService.class);
  32. example.and(
  33. example.createCriteria()
  34. .andEqualTo("userId", userId)
  35. .andEqualTo("service", key.key)
  36. .andGreaterThanOrEqualTo("startTime", new Date())
  37. .andLessThan("expireTime", new Date())
  38. );
  39. UserService service = one(userServiceMapper, example);
  40. return service != null;
  41. }
  42. /**
  43. * 获取用户服务
  44. * @param userId
  45. * @param key
  46. * @return
  47. */
  48. public UserService getService(Integer userId, ServiceKey key){
  49. Example example = new Example(UserService.class);
  50. example.and(
  51. example.createCriteria()
  52. .andEqualTo("userId", userId)
  53. .andEqualTo("service", key.key)
  54. .andGreaterThanOrEqualTo("startTime", new Date())
  55. .andLessThan("expireTime", new Date())
  56. );
  57. return one(userServiceMapper, example);
  58. }
  59. /**
  60. * 获取用户服务
  61. * @param userId
  62. * @param key
  63. * @return
  64. */
  65. public UserService getServiceBase(Integer userId, ServiceKey key){
  66. Example example = new Example(UserService.class);
  67. example.and(
  68. example.createCriteria()
  69. .andEqualTo("userId", userId)
  70. .andEqualTo("service", key.key)
  71. );
  72. return one(userServiceMapper, example);
  73. }
  74. /**
  75. * 合并用户信息,将old转移至new
  76. * @param oldUserId
  77. * @param newUserId
  78. */
  79. public void mergeUser(Number oldUserId, Integer newUserId){
  80. Example example = new Example(UserService.class);
  81. example.and(
  82. example.createCriteria().andEqualTo("userId", oldUserId)
  83. );
  84. update(userServiceMapper, example, UserService.builder().userId(newUserId).build());
  85. }
  86. public Collection<UserService> getByUser(Number userId){
  87. Example example = new Example(UserService.class);
  88. example.and(
  89. example.createCriteria()
  90. .andEqualTo("userId", userId)
  91. .andGreaterThanOrEqualTo("startTime", new Date())
  92. .andLessThan("expireTime", new Date())
  93. );
  94. return select(userServiceMapper, example);
  95. }
  96. public Map<Object, Collection<UserService>> mapByUser(Collection userIds){
  97. Map<Object, Collection<UserService>> relationMap = new HashMap<>();
  98. if(userIds.size() == 0) return relationMap;
  99. Example example = new Example(UserService.class);
  100. example.and(
  101. example.createCriteria()
  102. .andIn("userId", userIds)
  103. .andGreaterThanOrEqualTo("startTime", new Date())
  104. .andLessThan("expireTime", new Date())
  105. );
  106. List<UserService> userServiceList = select(userServiceMapper, example);
  107. if(userServiceList.size() == 0) return relationMap;
  108. for(UserService row: userServiceList){
  109. List<UserService> l;
  110. Number userId = row.getUserId();
  111. if(!relationMap.containsKey(userId)){
  112. l = new ArrayList<>();
  113. relationMap.put(userId, l);
  114. }else{
  115. l = (List<UserService>)relationMap.get(userId);
  116. }
  117. l.add(row);
  118. }
  119. return relationMap;
  120. }
  121. public UserService add(UserService service){
  122. int result = insert(userServiceMapper, service);
  123. service = one(userServiceMapper, service.getId());
  124. if(service == null){
  125. throw new SystemException("服务记录添加失败");
  126. }
  127. return service;
  128. }
  129. public UserService edit(UserService service){
  130. UserService in = one(userServiceMapper, service.getId());
  131. if(in == null){
  132. throw new ParameterException("服务记录不存在");
  133. }
  134. int result = update(userServiceMapper, service);
  135. return service;
  136. }
  137. public boolean delete(Number id){
  138. UserService in = one(userServiceMapper, id);
  139. if(in == null){
  140. throw new ParameterException("服务记录不存在");
  141. }
  142. int result = delete(userServiceMapper, id);
  143. return result > 0;
  144. }
  145. public UserService get(Number id){
  146. UserService in = one(userServiceMapper, id);
  147. if(in == null){
  148. throw new ParameterException("服务记录不存在");
  149. }
  150. return in;
  151. }
  152. public Page<UserService> select(int page, int pageSize){
  153. return select(userServiceMapper, page, pageSize);
  154. }
  155. public Page<UserService> select(Integer[] ids){
  156. return page(()->select(userServiceMapper, ids), 1, ids.length);
  157. }
  158. public List<UserService> select(Collection ids){
  159. return select(userServiceMapper, ids);
  160. }
  161. }