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 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 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> mapByUser(Collection userIds){ Map> 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 userServiceList = select(userServiceMapper, example); if(userServiceList.size() == 0) return relationMap; for(UserService row: userServiceList){ List l; Number userId = row.getUserId(); if(!relationMap.containsKey(userId)){ l = new ArrayList<>(); relationMap.put(userId, l); }else{ l = (List)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 select(int page, int pageSize){ return select(userServiceMapper, page, pageSize); } public Page select(Integer[] ids){ return page(()->select(userServiceMapper, ids), 1, ids.length); } public List select(Collection ids){ return select(userServiceMapper, ids); } }