Commit a38b63224440bf824fdbcfdc6d7f946f90714e08

Authored by litao
1 parent 944c7af5d9

add id

Showing 5 changed files with 274 additions and 0 deletions

platform-biz-patient-service/src/main/java/com/lyms/platform/biz/SequenceConstant.java View file @ a38b632
  1 +package com.lyms.platform.biz;
  2 +
  3 +public class SequenceConstant {
  4 + public static final String ID_KEY = "SEQUENCE_ID_KEY";
  5 + public static final String KEY_TYPE = "SEQUENCE_KEY";
  6 + public static final String KEY_REMARK = "SEQUENCE_INCREMENT_VALUE";
  7 + public static final String QUEUE = "SEQUENCE_QUEUE";
  8 + public static final String QUEUE_TYPE = "SEQUENCE_QUEUE";
  9 + public static final String QUEUE_REMARK = "SEQUENCE_QUEUE";
  10 +}
platform-biz-patient-service/src/main/java/com/lyms/platform/biz/dal/IGenSequenceIdDao.java View file @ a38b632
  1 +package com.lyms.platform.biz.dal;
  2 +
  3 +import java.util.List;
  4 +
  5 +public interface IGenSequenceIdDao {
  6 +
  7 + /**
  8 + * 弹出八位随机数
  9 + * @return
  10 + */
  11 + String poll();
  12 +
  13 + /**
  14 + * 添加数据
  15 + * @param list
  16 + */
  17 + void add(List<String> list);
  18 +
  19 + /**
  20 + * 原子性的返回当前id 并做自增操作
  21 + * @return
  22 + */
  23 + String next();
  24 +
  25 + /**
  26 + * 是否生成数据
  27 + * 定时检查剩余数据
  28 + * 不够就生成一批数据添加
  29 + * @return
  30 + */
  31 + boolean check();
  32 +
  33 + /**
  34 + * 初始检查
  35 + */
  36 + void init();
  37 +
  38 +}
platform-biz-patient-service/src/main/java/com/lyms/platform/biz/dal/impl/GenMongoSequenceIdDaoImpl.java View file @ a38b632
  1 +package com.lyms.platform.biz.dal.impl;
  2 +
  3 +import java.util.ArrayList;
  4 +import java.util.Date;
  5 +import java.util.List;
  6 +
  7 +import javax.annotation.PostConstruct;
  8 +
  9 +import org.apache.commons.collections.CollectionUtils;
  10 +import org.apache.commons.lang.StringUtils;
  11 +import org.slf4j.Logger;
  12 +import org.slf4j.LoggerFactory;
  13 +import org.springframework.beans.factory.annotation.Autowired;
  14 +import org.springframework.beans.factory.annotation.Value;
  15 +import org.springframework.data.mongodb.core.MongoTemplate;
  16 +import org.springframework.data.mongodb.core.query.Criteria;
  17 +import org.springframework.data.mongodb.core.query.Query;
  18 +import org.springframework.data.mongodb.core.query.Update;
  19 +import org.springframework.stereotype.Repository;
  20 +
  21 +import com.lyms.platform.biz.SequenceConstant;
  22 +import com.lyms.platform.biz.dal.IGenSequenceIdDao;
  23 +import com.lyms.platform.pojo.PlantformConfigModel;
  24 +
  25 +@Repository("genMongoSequenceIdDao")
  26 +public class GenMongoSequenceIdDaoImpl implements IGenSequenceIdDao {
  27 +
  28 + private Logger logger = LoggerFactory.getLogger(GenMongoSequenceIdDaoImpl.class);
  29 +
  30 + @Value("${sequence.index}")
  31 + private long index;
  32 +
  33 + @Value("${sequence.create.size}")
  34 + private long createSize;
  35 +
  36 + @Value("${sequence.min.size}")
  37 + private int minSize;
  38 +
  39 + @Autowired
  40 + private MongoTemplate mongoTemplate;
  41 +
  42 + @Override
  43 + @PostConstruct
  44 + public void init() {
  45 + PlantformConfigModel config = mongoTemplate.findOne(Query.query(Criteria.where("key").is(SequenceConstant.ID_KEY)), PlantformConfigModel.class);
  46 + if(config == null) {
  47 + config = new PlantformConfigModel();
  48 + config.setCreateDate(new Date());
  49 + config.setKey(SequenceConstant.ID_KEY);
  50 + config.setIncrementValue(index);
  51 + config.setRemark(SequenceConstant.KEY_REMARK);
  52 + config.setType(SequenceConstant.KEY_TYPE);
  53 + mongoTemplate.save(config);
  54 + logger.info("init sequence key");
  55 + }
  56 + }
  57 +
  58 + @Override
  59 + public String poll() {
  60 + PlantformConfigModel config = mongoTemplate.findAndRemove(Query.query(
  61 + Criteria.where("key").is(SequenceConstant.QUEUE)
  62 + .and("type").is(SequenceConstant.QUEUE)).limit(1),
  63 + PlantformConfigModel.class);
  64 + if(config != null && StringUtils.isNotBlank(config.getValue())) {
  65 + return config.getValue();
  66 + }
  67 + return null;
  68 + }
  69 +
  70 + @Override
  71 + public void add(List<String> list) {
  72 + List<PlantformConfigModel> configModels = new ArrayList<>();
  73 + for (String value : list) {
  74 + PlantformConfigModel model = new PlantformConfigModel();
  75 + model.setCreateDate(new Date());
  76 + model.setKey(SequenceConstant.QUEUE);
  77 + model.setType(SequenceConstant.QUEUE_TYPE);
  78 + model.setRemark(SequenceConstant.QUEUE_REMARK);
  79 + model.setValue(value);
  80 + configModels.add(model);
  81 + }
  82 + mongoTemplate.insertAll(configModels);
  83 + }
  84 +
  85 + @Override
  86 + public synchronized String next() {
  87 + PlantformConfigModel plantformConfig = mongoTemplate.findAndModify(Query.query(Criteria.where("key").is(SequenceConstant.ID_KEY)),
  88 + new Update().inc("incrementValue", createSize), PlantformConfigModel.class);
  89 + if(plantformConfig != null) {
  90 + return String.valueOf(plantformConfig.getIncrementValue());
  91 + }
  92 + return null;
  93 + }
  94 +
  95 + @Override
  96 + public boolean check() {
  97 + List<PlantformConfigModel> datas = mongoTemplate.find(Query.query(Criteria.where("key").is(SequenceConstant.QUEUE)
  98 + .and("type").is(SequenceConstant.QUEUE)), PlantformConfigModel.class);
  99 + return CollectionUtils.isEmpty(datas) ? true : datas.size() > minSize ? false : true;
  100 + }
  101 +
  102 +}
platform-biz-patient-service/src/main/java/com/lyms/platform/biz/dal/impl/GenRedisSequenceIdDaoImpl.java View file @ a38b632
  1 +package com.lyms.platform.biz.dal.impl;
  2 +
  3 +import java.util.List;
  4 +
  5 +import javax.annotation.PostConstruct;
  6 +
  7 +import org.springframework.stereotype.Repository;
  8 +
  9 +import com.lyms.platform.biz.dal.IGenSequenceIdDao;
  10 +
  11 +@Repository("genRedisSequenceIdDao")
  12 +public abstract class GenRedisSequenceIdDaoImpl implements IGenSequenceIdDao {
  13 +
  14 + @Override
  15 + public String poll() {
  16 + return null;
  17 + }
  18 +
  19 + @Override
  20 + public void add(List<String> list) {
  21 +
  22 + }
  23 +
  24 + @Override
  25 + public String next() {
  26 + return null;
  27 + }
  28 +
  29 + @Override
  30 + public boolean check() {
  31 + return false;
  32 + }
  33 +
  34 + @Override
  35 + @PostConstruct
  36 + public void init() {
  37 +
  38 + }
  39 +
  40 +}
platform-biz-patient-service/src/main/java/com/lyms/platform/biz/service/GenSequenceIdService.java View file @ a38b632
  1 +package com.lyms.platform.biz.service;
  2 +
  3 +import java.util.ArrayList;
  4 +import java.util.Collections;
  5 +import java.util.Date;
  6 +import java.util.List;
  7 +
  8 +import org.apache.commons.collections.CollectionUtils;
  9 +import org.apache.commons.lang.StringUtils;
  10 +import org.slf4j.Logger;
  11 +import org.slf4j.LoggerFactory;
  12 +import org.springframework.beans.factory.annotation.Autowired;
  13 +import org.springframework.beans.factory.annotation.Qualifier;
  14 +import org.springframework.beans.factory.annotation.Value;
  15 +import org.springframework.stereotype.Service;
  16 +
  17 +import com.lyms.platform.biz.dal.IGenSequenceIdDao;
  18 +
  19 +@Service
  20 +public class GenSequenceIdService {
  21 +
  22 + private Logger logger = LoggerFactory.getLogger(GenSequenceIdService.class);
  23 +
  24 + @Autowired
  25 + @Qualifier("genMongoSequenceIdDao")
  26 + private IGenSequenceIdDao genSequenceIdDao;
  27 +
  28 + @Value("${sequence.create.size}")
  29 + private int createSize;
  30 +
  31 +
  32 + public String poll(String areaCode) {
  33 + String s = genSequenceIdDao.poll();
  34 + if(StringUtils.isBlank(s)) {
  35 + generateData();
  36 + s = genSequenceIdDao.poll();
  37 + }
  38 + return areaCode + s;
  39 + }
  40 +
  41 + /**
  42 + * 定时扫描数据 当数据不足时
  43 + * 自动生成一定量的数据进去
  44 + */
  45 + public void autoInsertId() {
  46 + if(genSequenceIdDao.check()) {
  47 + generateData();
  48 + logger.info("generate sequence id.....");
  49 + }
  50 + }
  51 +
  52 + private synchronized void generateData() {
  53 + if(genSequenceIdDao.check()) {
  54 + List<String> list = new ArrayList<>();
  55 + Integer startValue = Integer.valueOf(genSequenceIdDao.next());
  56 + Integer endValue = startValue + createSize;
  57 + for(int i = startValue; i < endValue; i++) {
  58 + list.add(String.valueOf(i));
  59 + }
  60 + Collections.shuffle(list);
  61 + batchInsert(list, 1000);
  62 + }
  63 + }
  64 +
  65 + /**
  66 + * 批量保存数据
  67 + * @param list 数据列表
  68 + * @param size commit size
  69 + */
  70 + private void batchInsert(List<String> list, int size) {
  71 + List<String> commitList = new ArrayList<>();
  72 + for (int i = 0; i < list.size(); i++) {
  73 + commitList.add(list.get(i));
  74 + if(i != 0 && commitList.size() % size == 0) {
  75 + genSequenceIdDao.add(commitList);
  76 + commitList.clear();
  77 + }
  78 + }
  79 + if(CollectionUtils.isNotEmpty(commitList)) {
  80 + genSequenceIdDao.add(commitList);
  81 + }
  82 + }
  83 +
  84 +}