123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- package com.qxgmat.service;
- import com.github.pagehelper.Page;
- import com.nuliji.tools.AbstractService;
- import com.nuliji.tools.exception.ParameterException;
- import com.nuliji.tools.exception.SystemException;
- import com.nuliji.tools.mybatis.Example;
- import com.qxgmat.data.constants.enums.ServiceKey;
- import com.qxgmat.data.dao.UserServiceMapper;
- import com.qxgmat.data.dao.entity.UserService;
- import com.qxgmat.service.inline.UserOrderRecordService;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.stereotype.Service;
- import javax.annotation.Resource;
- import java.util.*;
- @Service
- public class UserServiceService extends AbstractService {
- private static final Logger logger = LoggerFactory.getLogger(UserServiceService.class);
- @Resource
- private UserServiceMapper userServiceMapper;
- @Resource
- private UserOrderRecordService userOrderRecordService;
- /**
- * 判断是否有权限
- * @param userId
- * @param key
- * @return
- */
- public boolean hasService(Integer userId, ServiceKey key){
- if (key == null) return false;
- Example example = new Example(UserService.class);
- example.and(
- example.createCriteria()
- .andEqualTo("userId", userId)
- .andEqualTo("service", key.key)
- .andLessThanOrEqualTo("startTime", new Date())
- .andGreaterThan("expireTime", new Date())
- );
- UserService service = one(userServiceMapper, example);
- return service != null;
- }
- /**
- * 权限到期时间
- * @param userId
- * @param key
- * @return
- */
- public Date timeService(Integer userId, ServiceKey key){
- if (key == null) return null;
- Example example = new Example(UserService.class);
- example.and(
- example.createCriteria()
- .andEqualTo("userId", userId)
- .andEqualTo("service", key.key)
- .andLessThanOrEqualTo("startTime", new Date())
- .andGreaterThan("expireTime", new Date())
- );
- UserService service = one(userServiceMapper, example);
- return service != null ? service.getExpireTime() : null;
- }
- /**
- * 获取当前有权限的用户信息
- * @param key
- * @return
- */
- public Page<UserService> listByService(int page, int size, ServiceKey key, Boolean isSubscribe){
- Example example = new Example(UserService.class);
- example.and(
- example.createCriteria()
- .andEqualTo("service", key.key)
- .andLessThanOrEqualTo("startTime", new Date())
- .andGreaterThan("expireTime", new Date())
- );
- if (isSubscribe != null){
- example.and(
- example.createCriteria()
- .andEqualTo("isSubscribe", isSubscribe ? 1:0)
- );
- }
- return select(userServiceMapper, example, page, size);
- }
- /**
- * 获取用户服务
- * @param userId
- * @param key
- * @return
- */
- public UserService getService(Integer userId, ServiceKey key){
- Example example = new Example(UserService.class);
- example.and(
- example.createCriteria()
- .andEqualTo("userId", userId)
- .andEqualTo("service", key.key)
- .andLessThanOrEqualTo("startTime", new Date())
- .andGreaterThan("expireTime", new Date())
- );
- return one(userServiceMapper, example);
- }
- /**
- * 获取用户服务
- * @param userId
- * @param key
- * @return
- */
- public UserService getServiceBase(Integer userId, ServiceKey key){
- Example example = new Example(UserService.class);
- example.and(
- example.createCriteria()
- .andEqualTo("userId", userId)
- .andEqualTo("service", key.key)
- );
- return one(userServiceMapper, example);
- }
- /**
- * 合并用户信息,将old转移至new
- * @param oldUserId
- * @param newUserId
- */
- public void mergeUser(Number oldUserId, Integer newUserId){
- Example example = new Example(UserService.class);
- example.and(
- example.createCriteria().andEqualTo("userId", oldUserId)
- );
- update(userServiceMapper, example, UserService.builder().userId(newUserId).build());
- }
- public Collection<UserService> getByUser(Number userId){
- Example example = new Example(UserService.class);
- example.and(
- example.createCriteria()
- .andEqualTo("userId", userId)
- .andLessThanOrEqualTo("startTime", new Date())
- .andGreaterThan("expireTime", new Date())
- );
- return select(userServiceMapper, example);
- }
- public Map<Object, Collection<UserService>> mapByUser(Collection userIds){
- Map<Object, Collection<UserService>> relationMap = new HashMap<>();
- if(userIds.size() == 0) return relationMap;
- Example example = new Example(UserService.class);
- example.and(
- example.createCriteria()
- .andIn("userId", userIds)
- .andLessThanOrEqualTo("startTime", new Date())
- .andGreaterThan("expireTime", new Date())
- );
- List<UserService> userServiceList = select(userServiceMapper, example);
- if(userServiceList.size() == 0) return relationMap;
- for(UserService row: userServiceList){
- List<UserService> l;
- Number userId = row.getUserId();
- if(!relationMap.containsKey(userId)){
- l = new ArrayList<>();
- relationMap.put(userId, l);
- }else{
- l = (List<UserService>)relationMap.get(userId);
- }
- l.add(row);
- }
- return relationMap;
- }
- public UserService add(UserService service){
- int result = insert(userServiceMapper, service);
- service = one(userServiceMapper, service.getId());
- if(service == null){
- throw new SystemException("服务记录添加失败");
- }
- return service;
- }
- public UserService edit(UserService service){
- UserService in = one(userServiceMapper, service.getId());
- if(in == null){
- throw new ParameterException("服务记录不存在");
- }
- int result = update(userServiceMapper, service);
- return service;
- }
- public boolean delete(Number id){
- UserService in = one(userServiceMapper, id);
- if(in == null){
- throw new ParameterException("服务记录不存在");
- }
- int result = delete(userServiceMapper, id);
- return result > 0;
- }
- public UserService get(Number id){
- UserService in = one(userServiceMapper, id);
- if(in == null){
- throw new ParameterException("服务记录不存在");
- }
- return in;
- }
- public Page<UserService> select(int page, int pageSize){
- return select(userServiceMapper, page, pageSize);
- }
- public Page<UserService> select(Integer[] ids){
- return page(()->select(userServiceMapper, ids), 1, ids.length);
- }
- public List<UserService> select(Collection ids){
- return select(userServiceMapper, ids);
- }
- }
|