Commit d93da892 authored by shiyu's avatar shiyu

关注操作加锁

parent c14f1a83
......@@ -26,12 +26,15 @@ import com.wwdz.ch.wx.service.FollowFansRecordService;
import com.wwdz.ch.wx.service.MessageService;
import com.wwdz.ch.core.service.SendMsgService;
import com.xxdxxs.utils.EntityMapper;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
......@@ -42,6 +45,8 @@ public class FollowFansRecordServiceImpl implements FollowFansRecordService {
private static final String TEMPLET_URL = "/pages/followAndFansList/index";
private final static String FOLLOW_AND_FANS_KEY = "FOLLOW_AND_FANS:";
@Autowired
FollowFansRecordDao followFansRecordDao;
......@@ -69,67 +74,62 @@ public class FollowFansRecordServiceImpl implements FollowFansRecordService {
@Autowired
WxAppletApi wxAppletApi;
@Autowired
RedissonClient redissonClient;
@Override
public Result follow(FollowFansRecordRequestDto dto) {
String key = FOLLOW_AND_FANS_KEY + dto.getFollowerId() + ":" + dto.getFansId();
RLock lock = redissonClient.getLock(key);
try {
if (dto.getFollowerId() == null || dto.getFansId() == null) {
return Result.failed(ResultCode.PARAM_ERROR);
}
//校验是否已有关注记录
if(isFollowed(dto)) {
return Result.failed("已经关注,无需重复操作");
}
Date now = new Date();
FollowFansRecord followFansRecord = new FollowFansRecord();
EntityMapper.copyAttribute(dto, followFansRecord);
followFansRecord.setCreateTime(now);
followFansRecord.setUpdateTime(now);
followFansRecord.setState(1);
int id = followFansRecordDao.add(followFansRecord);
Map<String, Object> map = new HashMap<>();
map.put("recordId", id);
String fansName = cacheUtil.appletUserInfoCache.get(dto.getFansId()).get().getNickname();
//发送消息通知对应的分销商有用户出价
String openId = sendMsgService.getOpenIdByUserId(dto.getFollowerId());
if (com.xxdxxs.utils.StringUtils.hasLength(openId)) {
FollowFansMsg followFansMsg = new FollowFansMsg();
followFansMsg.setFansName(fansName + "关注了你");
followFansMsg.setOpenId(openId);
followFansMsg.setContent("由" + cacheUtil.appletUserInfoCache.get(dto.getIntroducerId()).get().getNickname() + "介绍");
Result result = sendMsgService.followNoticeMsg(followFansMsg);
if (!result.getSuccess()) {
String url = wxAppletApi.getUrlLink(TEMPLET_URL,"tabType=2");
String msg = "您新增了一个粉丝" + fansName + ", 请前往"+ url + "查看";
if (lock.tryLock(5, TimeUnit.SECONDS)) {
if (dto.getFollowerId() == null || dto.getFansId() == null) {
return Result.failed(ResultCode.PARAM_ERROR);
}
//校验是否已有关注记录
if (isFollowed(dto)) {
return Result.failed("已经关注,无需重复操作");
}
Date now = new Date();
FollowFansRecord followFansRecord = new FollowFansRecord();
EntityMapper.copyAttribute(dto, followFansRecord);
followFansRecord.setCreateTime(now);
followFansRecord.setUpdateTime(now);
followFansRecord.setState(1);
int id = followFansRecordDao.add(followFansRecord);
Map<String, Object> map = new HashMap<>();
map.put("recordId", id);
String fansName = cacheUtil.appletUserInfoCache.get(dto.getFansId()).get().getNickname();
//发送消息通知对应的分销商有用户出价
String openId = sendMsgService.getOpenIdByUserId(dto.getFollowerId());
if (com.xxdxxs.utils.StringUtils.hasLength(openId)) {
FollowFansMsg followFansMsg = new FollowFansMsg();
followFansMsg.setFansName(fansName + "关注了你");
followFansMsg.setOpenId(openId);
followFansMsg.setContent("由" + cacheUtil.appletUserInfoCache.get(dto.getIntroducerId()).get().getNickname() + "介绍");
Result result = sendMsgService.followNoticeMsg(followFansMsg);
if (!result.getSuccess()) {
String url = wxAppletApi.getUrlLink(TEMPLET_URL, "tabType=2");
String msg = "您新增了一个粉丝" + fansName + ", 请前往" + url + "查看";
aliSmsSender.sendSms(dto.getFollowerId(), msg);
}
} else {
String url = wxAppletApi.getUrlLink(TEMPLET_URL, "tabType=2");
String msg = "您新增了一个粉丝" + fansName + ", 请前往" + url + "查看";
aliSmsSender.sendSms(dto.getFollowerId(), msg);
}
} else {
String url = wxAppletApi.getUrlLink(TEMPLET_URL,"tabType=2");
String msg = "您新增了一个粉丝" + fansName + ", 请前往"+ url + "查看";
aliSmsSender.sendSms(dto.getFollowerId(), msg);
}
/* String messageKey = StringUtil.formatMessageKey(dto.getFansId(), MessageEnum.TypeEnum.FOLLOW.getCode(), dto.getFollowerId());
if (redisUtils.hasKey(messageKey)) {
return Result.success(map);
} else {
return Result.failed("稍后再试");
}
//发送关注的消息
MessageRequestDto messageRequestDto = new MessageRequestDto();
messageRequestDto.setSenderId(dto.getFansId());
messageRequestDto.setReceiverId(dto.getFollowerId());
messageRequestDto.setType(MessageEnum.TypeEnum.FOLLOW.getCode());
MessageContentText messageContentText = new MessageContentText();
messageContentText.setId(dto.getFansId());
messageContentText.setIdType(MessageEnum.ContentIdTypeEnum.USERID.getCode());
messageContentText.setText("关注了您");
messageRequestDto.setContent(JsonUtils.from(messageContentText));
messageService.add(messageRequestDto);
//五分钟内再次关注不会发送消息
redisUtils.set(messageKey, LocalDateTime.now(), 60 * 5);*/
return Result.success(map);
} catch (Exception e) {
logger.error("{} 关注 {} error {}", dto.getFansId(), dto.getFollowerId(), e);
} finally {
if (lock != null && lock.isHeldByCurrentThread()) {
lock.unlock();
logger.info("======================== 关注操作 释放锁成功 ========================", Thread.currentThread().getId());
}
}
return Result.failed();
}
......
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