|
@@ -0,0 +1,120 @@
|
|
|
+package com.diagbot.service.impl;
|
|
|
+
|
|
|
+import com.diagbot.idc.VisibleIdCreater;
|
|
|
+import com.diagbot.service.MrV2Service;
|
|
|
+import com.diagbot.util.DateUtil;
|
|
|
+import com.diagbot.vo.PushJoinV2VO;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
+import org.springframework.dao.DataAccessException;
|
|
|
+import org.springframework.data.redis.connection.RedisConnection;
|
|
|
+import org.springframework.data.redis.core.RedisCallback;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.Date;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Description: 病历保存到redis接口实现
|
|
|
+ * @author: gaodm
|
|
|
+ * @time: 2019/8/28 16:25
|
|
|
+ */
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class MrV2ServiceImpl implements MrV2Service {
|
|
|
+ @Autowired
|
|
|
+ @Qualifier("redisTemplateForMr")
|
|
|
+ private RedisTemplate redisForMr;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private VisibleIdCreater visibleIdCreater;
|
|
|
+
|
|
|
+ private byte[] serializeKey(Object o) {
|
|
|
+ return redisForMr.getKeySerializer().serialize(o);
|
|
|
+ }
|
|
|
+
|
|
|
+ private byte[] serializeValue(Object o) {
|
|
|
+ return redisForMr.getValueSerializer().serialize(o);
|
|
|
+ }
|
|
|
+
|
|
|
+ private Object deserializeValue(byte[] b) {
|
|
|
+ return redisForMr.getValueSerializer().deserialize(b);
|
|
|
+ }
|
|
|
+
|
|
|
+ private byte[] getMrKey(String mrId) {
|
|
|
+ String mrFormat = "mrv2_%s";
|
|
|
+ return serializeKey(String.format(mrFormat, mrId));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建病历信息
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public String createMr(PushJoinV2VO pushJoinV2VO) {
|
|
|
+ Date now = DateUtil.now();
|
|
|
+ final Date expireDate = DateUtil.addMinutes(now, 3);
|
|
|
+ pushJoinV2VO.setCreateTime(now);
|
|
|
+ pushJoinV2VO.setExpireTime(expireDate);
|
|
|
+ pushJoinV2VO.setExpireTimeStr(DateUtil.format(expireDate, "yyyy-MM-dd HH:mm:ss"));
|
|
|
+ String mrId = visibleIdCreater.getNextId(5).toString();
|
|
|
+ pushJoinV2VO.setMrId(mrId);
|
|
|
+ final byte[] redis_key = getMrKey(mrId);
|
|
|
+ redisForMr.execute(new RedisCallback<Object>() {
|
|
|
+ @Override
|
|
|
+ public Object doInRedis(RedisConnection connection) throws DataAccessException {
|
|
|
+ //获取旧的
|
|
|
+ byte[] bytes = connection.get(redis_key);
|
|
|
+ //删除旧的
|
|
|
+ if (bytes != null) {
|
|
|
+ connection.del(bytes);
|
|
|
+ }
|
|
|
+ //设置新的
|
|
|
+ connection.setEx(
|
|
|
+ redis_key,
|
|
|
+ (expireDate.getTime() - DateUtil.now().getTime()) / 1000,
|
|
|
+ serializeValue(pushJoinV2VO)
|
|
|
+ );
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return mrId;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取病历信息
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public PushJoinV2VO getMr(String mrId) {
|
|
|
+ return (PushJoinV2VO) redisForMr.execute(new RedisCallback<Object>() {
|
|
|
+ @Override
|
|
|
+ public Object doInRedis(RedisConnection connection) throws DataAccessException {
|
|
|
+ byte[] redis_key = getMrKey(mrId);
|
|
|
+ byte[] bytes = connection.get(redis_key);
|
|
|
+ if (bytes == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return deserializeValue(bytes);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除用户短信验证码信息
|
|
|
+ */
|
|
|
+ /**
|
|
|
+ * 删除病历信息
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public Boolean deleteMr(String mrId) {
|
|
|
+ final byte[] redis_key = getMrKey(mrId);
|
|
|
+ Long l = (Long) redisForMr.execute(new RedisCallback<Long>() {
|
|
|
+ @Override
|
|
|
+ public Long doInRedis(RedisConnection connection) throws DataAccessException {
|
|
|
+ return connection.del(redis_key);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return l > 0;
|
|
|
+ }
|
|
|
+}
|