UserServiceService.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. .andLessThanOrEqualTo("startTime", new Date())
  37. .andGreaterThan("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 Date timeService(Integer userId, ServiceKey key){
  49. if (key == null) return null;
  50. Example example = new Example(UserService.class);
  51. example.and(
  52. example.createCriteria()
  53. .andEqualTo("userId", userId)
  54. .andEqualTo("service", key.key)
  55. .andLessThanOrEqualTo("startTime", new Date())
  56. .andGreaterThan("expireTime", new Date())
  57. );
  58. UserService service = one(userServiceMapper, example);
  59. return service != null ? service.getExpireTime() : null;
  60. }
  61. /**
  62. * 获取当前有权限的用户信息
  63. * @param key
  64. * @return
  65. */
  66. public Page<UserService> listByService(int page, int size, ServiceKey key, Boolean isSubscribe){
  67. Example example = new Example(UserService.class);
  68. example.and(
  69. example.createCriteria()
  70. .andEqualTo("service", key.key)
  71. .andLessThanOrEqualTo("startTime", new Date())
  72. .andGreaterThan("expireTime", new Date())
  73. );
  74. if (isSubscribe != null){
  75. example.and(
  76. example.createCriteria()
  77. .andEqualTo("isSubscribe", isSubscribe ? 1:0)
  78. );
  79. }
  80. return select(userServiceMapper, example, page, size);
  81. }
  82. /**
  83. * 获取用户服务
  84. * @param userId
  85. * @param key
  86. * @return
  87. */
  88. public UserService getService(Integer userId, ServiceKey key){
  89. Example example = new Example(UserService.class);
  90. example.and(
  91. example.createCriteria()
  92. .andEqualTo("userId", userId)
  93. .andEqualTo("service", key.key)
  94. .andLessThanOrEqualTo("startTime", new Date())
  95. .andGreaterThan("expireTime", new Date())
  96. );
  97. return one(userServiceMapper, example);
  98. }
  99. /**
  100. * 获取用户服务
  101. * @param userId
  102. * @param key
  103. * @return
  104. */
  105. public UserService getServiceBase(Integer userId, ServiceKey key){
  106. Example example = new Example(UserService.class);
  107. example.and(
  108. example.createCriteria()
  109. .andEqualTo("userId", userId)
  110. .andEqualTo("service", key.key)
  111. );
  112. return one(userServiceMapper, example);
  113. }
  114. /**
  115. * 合并用户信息,将old转移至new
  116. * @param oldUserId
  117. * @param newUserId
  118. */
  119. public void mergeUser(Number oldUserId, Integer newUserId){
  120. Example example = new Example(UserService.class);
  121. example.and(
  122. example.createCriteria().andEqualTo("userId", oldUserId)
  123. );
  124. update(userServiceMapper, example, UserService.builder().userId(newUserId).build());
  125. }
  126. public Collection<UserService> getByUser(Number userId){
  127. Example example = new Example(UserService.class);
  128. example.and(
  129. example.createCriteria()
  130. .andEqualTo("userId", userId)
  131. .andLessThanOrEqualTo("startTime", new Date())
  132. .andGreaterThan("expireTime", new Date())
  133. );
  134. return select(userServiceMapper, example);
  135. }
  136. public Map<Object, Collection<UserService>> mapByUser(Collection userIds){
  137. Map<Object, Collection<UserService>> relationMap = new HashMap<>();
  138. if(userIds.size() == 0) return relationMap;
  139. Example example = new Example(UserService.class);
  140. example.and(
  141. example.createCriteria()
  142. .andIn("userId", userIds)
  143. .andLessThanOrEqualTo("startTime", new Date())
  144. .andGreaterThan("expireTime", new Date())
  145. );
  146. List<UserService> userServiceList = select(userServiceMapper, example);
  147. if(userServiceList.size() == 0) return relationMap;
  148. for(UserService row: userServiceList){
  149. List<UserService> l;
  150. Number userId = row.getUserId();
  151. if(!relationMap.containsKey(userId)){
  152. l = new ArrayList<>();
  153. relationMap.put(userId, l);
  154. }else{
  155. l = (List<UserService>)relationMap.get(userId);
  156. }
  157. l.add(row);
  158. }
  159. return relationMap;
  160. }
  161. public UserService add(UserService service){
  162. int result = insert(userServiceMapper, service);
  163. service = one(userServiceMapper, service.getId());
  164. if(service == null){
  165. throw new SystemException("服务记录添加失败");
  166. }
  167. return service;
  168. }
  169. public UserService edit(UserService service){
  170. UserService in = one(userServiceMapper, service.getId());
  171. if(in == null){
  172. throw new ParameterException("服务记录不存在");
  173. }
  174. int result = update(userServiceMapper, service);
  175. return service;
  176. }
  177. public boolean delete(Number id){
  178. UserService in = one(userServiceMapper, id);
  179. if(in == null){
  180. throw new ParameterException("服务记录不存在");
  181. }
  182. int result = delete(userServiceMapper, id);
  183. return result > 0;
  184. }
  185. public UserService get(Number id){
  186. UserService in = one(userServiceMapper, id);
  187. if(in == null){
  188. throw new ParameterException("服务记录不存在");
  189. }
  190. return in;
  191. }
  192. public Page<UserService> select(int page, int pageSize){
  193. return select(userServiceMapper, page, pageSize);
  194. }
  195. public Page<UserService> select(Integer[] ids){
  196. return page(()->select(userServiceMapper, ids), 1, ids.length);
  197. }
  198. public List<UserService> select(Collection ids){
  199. return select(userServiceMapper, ids);
  200. }
  201. }