yonge 5 年之前
父节点
当前提交
86e823e32a

+ 11 - 1
mec-biz/src/main/java/com/ym/mec/biz/service/impl/StudentRegistrationServiceImpl.java

@@ -21,6 +21,7 @@ import com.ym.mec.common.entity.ImResult;
 import com.ym.mec.common.entity.ImUserModel;
 import com.ym.mec.common.exception.BizException;
 import com.ym.mec.common.page.PageInfo;
+import com.ym.mec.common.redis.service.RedisCache;
 import com.ym.mec.common.service.impl.BaseServiceImpl;
 import com.ym.mec.im.ImFeignService;
 import com.ym.mec.thirdparty.message.MessageSenderPluginContext;
@@ -93,6 +94,8 @@ public class StudentRegistrationServiceImpl extends BaseServiceImpl<Long, Studen
     private ClassGroupStudentMapperService classGroupStudentMapperService;
     @Autowired
     private SysUserFeignService sysUserFeignService;
+    @Autowired
+    private RedisCache<String, Object> redisCache;
 
     @Override
     public BaseDAO<Long, StudentRegistration> getDAO() {
@@ -190,6 +193,11 @@ public class StudentRegistrationServiceImpl extends BaseServiceImpl<Long, Studen
     @Override
     @Transactional(rollbackFor = Exception.class)
     public StudentRegistration addStudent(StudentRegistration studentRegistration) throws IOException {
+    	String key = "_student_registration";
+    	long threadId = Thread.currentThread().getId();
+    	if(!redisCache.getLocked(key, threadId, 10)){
+    		throw new BizException("系统繁忙,请稍后再试");
+    	}
         Date date = new Date();
         Integer userId = 0;
         SysUser sysUser = studentRegistrationDao.getSysUserByPhone(studentRegistration.getParentsPhone());
@@ -224,8 +232,8 @@ public class StudentRegistrationServiceImpl extends BaseServiceImpl<Long, Studen
         studentRegistration.setCreateTime(date);
         studentRegistration.setUpdateTime(date);
         studentRegistration.setUserId(sysUser.getId());
-        studentRegistrationDao.updateCurrentClass(studentRegistration);
         studentRegistrationDao.insert(studentRegistration);
+        studentRegistrationDao.updateCurrentClass(studentRegistration);
         //增加报名学生数
         musicGroupSubjectPlanService.addApplyStudentNum(studentRegistration.getMusicGroupId(), studentRegistration.getSubjectId(), 1);
         //报名成功后,发送短信
@@ -240,6 +248,8 @@ public class StudentRegistrationServiceImpl extends BaseServiceImpl<Long, Studen
                 MessageTypeEnum.SMS_APPLY_MESSAGE, map, null, 0, "",
                 studentRegistration.getParentsName(), subject.getName(), HttpUtil.getSortUrl(studentApplyUrl),
                 DateUtil.format(musicGroup.getApplyExpireDate(), DateUtil.DATE_FORMAT_MIN), serverPhone);
+        
+        redisCache.releaseLocked(key, threadId);
         return studentRegistration;
     }
 

+ 16 - 0
mec-common/common-core/src/main/java/com/ym/mec/common/cache/Cache.java

@@ -70,4 +70,20 @@ public interface Cache<K, V> {
 	 */
 	void expire(K key, int expireTimes);
 
+	/**
+	 * 获取分布式锁
+	 * @param key 业务唯一标识
+	 * @param threadId 线程编号
+	 * @param seconds 锁的有效期
+	 * @return
+	 */
+	boolean getLocked(K key, V threadId, int seconds);
+
+	/**
+	 * 释放分布式锁
+	 * @param key
+	 * @param threadId
+	 * @return
+	 */
+	boolean releaseLocked(K key, V threadId);
 }

+ 23 - 0
mec-common/common-core/src/main/java/com/ym/mec/common/redis/service/RedisCache.java

@@ -72,4 +72,27 @@ public class RedisCache<K, V> implements Cache<K, V> {
 	public RedisTemplate<K, V> getRedisTemplate() {
 		return redisTemplate;
 	}
+
+	@Override
+	public boolean getLocked(K key, V threadId, int seconds) {
+		if (exists(key)) {
+			return false;
+		}
+
+		put(key, threadId, seconds);
+
+		return true;
+	}
+
+	@Override
+	public boolean releaseLocked(K key, V threadId) {
+		if (exists(key)) {
+			V v = get(key);
+			if (v.toString().equals(threadId.toString())) {
+				delete(key);
+				return true;
+			}
+		}
+		return false;
+	}
 }