Commit 11b26e11 authored by liwei's avatar liwei

修改了发表动态接口

parent 30db737f
...@@ -231,5 +231,11 @@ public class OpmArticle extends CyIdIncreEntity<OpmArticle> { ...@@ -231,5 +231,11 @@ public class OpmArticle extends CyIdIncreEntity<OpmArticle> {
*/ */
@TableField(exist = false) @TableField(exist = false)
private String wxOpenid; private String wxOpenid;
/**
* 是否付费 1:付费 0:不付费
*/
@TableField(exist = false)
private String isPay;
} }
...@@ -31,6 +31,7 @@ import org.rcisoft.sys.dictionary.service.DictionaryService; ...@@ -31,6 +31,7 @@ import org.rcisoft.sys.dictionary.service.DictionaryService;
import org.redisson.api.RLock; import org.redisson.api.RLock;
import org.redisson.api.RedissonClient; import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Propagation;
...@@ -67,6 +68,8 @@ public class OpmArticleServiceImpl extends ServiceImpl<OpmArticleRepository, Opm ...@@ -67,6 +68,8 @@ public class OpmArticleServiceImpl extends ServiceImpl<OpmArticleRepository, Opm
private CyRedisServiceImpl cyRedisServiceImpl; private CyRedisServiceImpl cyRedisServiceImpl;
@Autowired @Autowired
private RedissonClient redissonClient; private RedissonClient redissonClient;
@Autowired
private StringRedisTemplate redisTemplate;
/** /**
* 保存 opmArticle管理 * 保存 opmArticle管理
* @param opmArticle * @param opmArticle
...@@ -100,7 +103,7 @@ public class OpmArticleServiceImpl extends ServiceImpl<OpmArticleRepository, Opm ...@@ -100,7 +103,7 @@ public class OpmArticleServiceImpl extends ServiceImpl<OpmArticleRepository, Opm
Object redisCount = cyRedisServiceImpl.get(OpmArticleRedisBean.USER_ARTICLE_COUNT + date + ":" + userId); Object redisCount = cyRedisServiceImpl.get(OpmArticleRedisBean.USER_ARTICLE_COUNT + date + ":" + userId);
if (redisCount != null && (int)redisCount >= publishCountAll){ if (redisCount != null && (int)redisCount >= publishCountAll){
//超出最大数量 //超出最大数量
throw new CyServiceException(1001,"发布动态已达上限"); throw new CyServiceException(1001,"今日发布动态总次数已达上限");
} else if (redisCount == null){ } else if (redisCount == null){
//当天第一次发布动态 //当天第一次发布动态
cyRedisServiceImpl.set(OpmArticleRedisBean.USER_ARTICLE_COUNT + date + ":" + userId,1,48*60*60L); cyRedisServiceImpl.set(OpmArticleRedisBean.USER_ARTICLE_COUNT + date + ":" + userId,1,48*60*60L);
...@@ -109,28 +112,39 @@ public class OpmArticleServiceImpl extends ServiceImpl<OpmArticleRepository, Opm ...@@ -109,28 +112,39 @@ public class OpmArticleServiceImpl extends ServiceImpl<OpmArticleRepository, Opm
//判断免费和付费数量 //判断免费和付费数量
if ((int)redisCount >= publishCount){ if ((int)redisCount >= publishCount){
//超过免费限制次数 需要付费 //超过免费限制次数 需要付费
//扣除该用户的金币数量 if (opmArticle.getIsPay().equals("1")){
int line = 0; //扣除该用户的金币数量
boolean isGetLock = false; int line = 0;
RLock lock = redissonClient.getLock(OpmArticleRedisBean.USER_ARTICLE_LOCK + userId); boolean isGetLock = false;
try { RLock lock = redissonClient.getLock(OpmArticleRedisBean.USER_ARTICLE_LOCK + userId);
isGetLock = lock.tryLock(Long.parseLong(OpmArticleRedisBean.USER_ARTICLE_LOCK_WAIT_TIME), try {
Long.parseLong(OpmArticleRedisBean.USER_ARTICLE_LOCK_LEASE_TIME), TimeUnit.SECONDS); isGetLock = lock.tryLock(Long.parseLong(OpmArticleRedisBean.USER_ARTICLE_LOCK_WAIT_TIME),
if (isGetLock) { Long.parseLong(OpmArticleRedisBean.USER_ARTICLE_LOCK_LEASE_TIME), TimeUnit.SECONDS);
MemInfo memInfo = memInfoRepository.selectByOpenId(opmArticle.getWxOpenid()); if (isGetLock) {
if (memInfo.getGoldCoinsCount() - payCount < 0){ MemInfo memInfo = memInfoRepository.selectByOpenId(opmArticle.getWxOpenid());
throw new CyServiceException(1003,"金币余额不足,请先充值"); if (memInfo.getGoldCoinsCount() - payCount < 0){
throw new CyServiceException(1003,"金币余额不足,请先充值");
}
memInfo.setGoldCoinsCount(memInfo.getGoldCoinsCount() - payCount);
memInfoRepository.updateById(memInfo);
//增加操作
redisTemplate.opsForValue().increment(OpmArticleRedisBean.USER_ARTICLE_COUNT + date + ":" + userId,1L);
} }
memInfo.setGoldCoinsCount(memInfo.getGoldCoinsCount() - payCount); lock.unlock();
memInfoRepository.updateById(memInfo); } catch (InterruptedException e) {
Thread.currentThread().interrupt();
lock.unlock();
isGetLock = false;
throw new CyServiceException(1004,"服务器繁忙,请稍后再试");
} }
lock.unlock(); } else {
} catch (InterruptedException e) { //不进行发表动态
Thread.currentThread().interrupt(); throw new CyServiceException(1002,"今日发表动态免费次数超出限制");
lock.unlock();
isGetLock = false;
throw new CyServiceException(1004,"服务器繁忙,请稍后再试");
} }
} else {
//没超过免费次数限制
//增加操作
redisTemplate.opsForValue().increment(OpmArticleRedisBean.USER_ARTICLE_COUNT + date + ":" + userId,1L);
} }
} }
//3、进行审核 从redis获取动态审核配置 0:自动审核,1:人工审核 //3、进行审核 从redis获取动态审核配置 0:自动审核,1:人工审核
...@@ -164,8 +178,6 @@ public class OpmArticleServiceImpl extends ServiceImpl<OpmArticleRepository, Opm ...@@ -164,8 +178,6 @@ public class OpmArticleServiceImpl extends ServiceImpl<OpmArticleRepository, Opm
} }
} }
//增加操作
cyRedisServiceImpl.incr(OpmArticleRedisBean.USER_ARTICLE_COUNT + date + ":" + userId,1L);
int line = baseMapper.insert(opmArticle); int line = baseMapper.insert(opmArticle);
log.debug(CyUserUtil.getAuthenUsername()+"新增了ID为"+ log.debug(CyUserUtil.getAuthenUsername()+"新增了ID为"+
opmArticle.getBusinessId()+"的opmArticle管理信息"); opmArticle.getBusinessId()+"的opmArticle管理信息");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment