IdUtils.java 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package com.boman.system.utils;
  2. import com.boman.common.core.utils.SpringUtils;
  3. import com.boman.common.redis.RedisKey;
  4. import com.boman.common.redis.service.RedisService;
  5. import com.boman.gen.domain.GenTableColumn;
  6. import com.boman.system.mapper.StandardlyMapper;
  7. import org.apache.commons.collections.CollectionUtils;
  8. import java.util.List;
  9. /**
  10. * @author shiqian
  11. * @description
  12. * @date 2021年03月23日 16:04
  13. **/
  14. public class IdUtils {
  15. public static Long getMaxId(String tableName, String pkName) {
  16. RedisService redisService = SpringUtils.getBean(RedisService.class);
  17. String sequencesKey = RedisKey.SEQ + tableName.toUpperCase();
  18. boolean isExist = redisService.exists(sequencesKey);
  19. if (isExist) {
  20. return redisService.increment(sequencesKey);
  21. }
  22. // 查数据库
  23. StandardlyMapper mapper = SpringUtils.getBean(StandardlyMapper.class);
  24. Long maxId = mapper.selectMaxId(tableName, pkName);
  25. if (null == maxId || maxId <= 0) {
  26. maxId = 1L;
  27. redisService.setCacheObject(sequencesKey, maxId);
  28. } else {
  29. redisService.increment(sequencesKey, ++maxId);
  30. }
  31. return maxId;
  32. }
  33. public static String getPkName(List<GenTableColumn> columnList) {
  34. if (CollectionUtils.isEmpty(columnList)) {
  35. throw new IllegalArgumentException("获取主键失败");
  36. }
  37. for (GenTableColumn tableColumn : columnList) {
  38. if ("1".equalsIgnoreCase(tableColumn.getIsPk())) {
  39. return tableColumn.getColumnName();
  40. }
  41. }
  42. throw new IllegalArgumentException("获取主键失败");
  43. }
  44. }