Commit 2dcb8e1b authored by shiyu's avatar shiyu

五分钟内重复关注不会发送消息

parent 0d6e0272
......@@ -11,11 +11,20 @@ public interface FollowFansRecordDao {
/**
* 取消关注
* 根据关注记录的id来删除
* @param record
* @return
*/
boolean delete(FollowFansRecord record);
/**
* 取消关注
* @param fansId
* @param followerId
* @return
*/
boolean delete(long fansId, long followerId);
List<FollowFansRecord> findList(FollowFansRecordRequestDto dto);
......
......@@ -39,4 +39,10 @@ public class FollowFansRecordRequestDto extends BaseRequestDto implements Entity
* 状态
*/
private Integer state;
/**
* 当前查看的用户id
* 进入别人的主页时需要传
*/
private Long currentUserId;
}
......@@ -29,6 +29,19 @@ public class FollowFansRecordDaoImpl implements FollowFansRecordDao {
return followFansRecordMapper.updateByPrimaryKeySelective(record) > 0;
}
@Override
public boolean delete(long fansId, long followerId) {
FollowFansRecordExample example = new FollowFansRecordExample();
FollowFansRecordExample.Criteria criteria = example.createCriteria();
criteria.andFollowerIdEqualTo(followerId);
criteria.andFansIdEqualTo(fansId);
criteria.andStateEqualTo(1);
FollowFansRecord followFansRecord = new FollowFansRecord();
followFansRecord.setState(0);
return followFansRecordMapper.updateByExampleSelective(followFansRecord, example) > 0;
}
@Override
public List<FollowFansRecord> findList(FollowFansRecordRequestDto dto) {
FollowFansRecordExample example = new FollowFansRecordExample();
......
......@@ -50,9 +50,9 @@
<!--数据库链接地址账号密码-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://pc-bp1l2k9j6ck1gr923.mysql.polardb.rds.aliyuncs.com:3306/quanku?useUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=UTC&amp;verifyServerCertificate=false&amp;useSSL=false"
userId="quanku"
password="3N0W8i0b1Wm35MJ5"/>
connectionURL="jdbc:mysql://pc-bp18q94o1mukb67hy.mysql.polardb.rds.aliyuncs.com:3306/quanku_dev?useUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=UTC&amp;verifyServerCertificate=false&amp;useSSL=false"
userId="owner"
password="VS1xaA37hyu1wJSw"/>
<!-- 类型转换 -->
<javaTypeResolver>
......@@ -72,7 +72,7 @@
<javaClientGenerator type="XMLMAPPER" targetPackage="com.wwdz.ch.db.mapper"
targetProject="ch-dao/src/main/java"/>
<table tableName="category_temp" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true">
<table tableName="message_record" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true">
<generatedKey column="id" sqlStatement="Mysql" identity="true" />
</table>
......
......@@ -4,6 +4,7 @@ import com.wwdz.ch.core.consts.MessageEnum;
import com.wwdz.ch.core.consts.ResultCode;
import com.wwdz.ch.core.type.Result;
import com.wwdz.ch.core.util.CacheUtil;
import com.wwdz.ch.core.util.RedisUtils;
import com.wwdz.ch.db.dao.FollowFansRecordDao;
import com.wwdz.ch.db.domain.FollowFansRecord;
import com.wwdz.ch.db.dto.request.FollowFansRecordRequestDto;
......@@ -11,6 +12,8 @@ import com.wwdz.ch.wx.entity.request.MessageRequestDto;
import com.wwdz.ch.wx.entity.vo.FollowFansRecordVo;
import com.wwdz.ch.wx.service.FollowFansRecordService;
import com.wwdz.ch.wx.service.MessageService;
import com.wwdz.ch.wx.util.StringUtil;
import com.xxdxxs.utils.DateUtils;
import com.xxdxxs.utils.EntityMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -19,6 +22,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
......@@ -37,6 +41,9 @@ public class FollowFansRecordServiceImpl implements FollowFansRecordService {
@Autowired
CacheUtil cacheUtil;
@Autowired
RedisUtils redisUtils;
@Override
@Transactional(rollbackFor = Exception.class)
public Result follow(FollowFansRecordRequestDto dto) {
......@@ -54,6 +61,10 @@ public class FollowFansRecordServiceImpl implements FollowFansRecordService {
Map<String, Object> map = new HashMap<>();
map.put("recordId", id);
String messageKey = StringUtil.formatMessageKey(dto.getFansId(), MessageEnum.TypeEnum.FOLLOW.getCode(), dto.getFollowerId());
if (redisUtils.hasKey(messageKey)) {
return Result.success(map);
}
//发送关注的消息
MessageRequestDto messageRequestDto = new MessageRequestDto();
messageRequestDto.setSenderId(dto.getFansId());
......@@ -61,6 +72,8 @@ public class FollowFansRecordServiceImpl implements FollowFansRecordService {
messageRequestDto.setType(MessageEnum.TypeEnum.FOLLOW.getCode());
messageRequestDto.setContent("关注了您");
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);
......@@ -86,6 +99,22 @@ public class FollowFansRecordServiceImpl implements FollowFansRecordService {
return Result.failed();
}
@Override
public Result cancelFollowByBothId(FollowFansRecordRequestDto dto) {
try {
if (dto.getFollowerId() == null || dto.getFansId() == null) {
return Result.failed(ResultCode.PARAM_ERROR);
}
followFansRecordDao.delete(dto.getFansId(), dto.getFollowerId());
return Result.success();
} catch (Exception e) {
logger.error("{} 取消关注error {}", dto.getId(), e);
}
return Result.failed();
}
@Override
public Result countFollowFans(FollowFansRecordRequestDto dto) {
try {
......@@ -135,7 +164,8 @@ public class FollowFansRecordServiceImpl implements FollowFansRecordService {
}
followFansRecordVos.add(followFansRecordVo);
});
return Result.success(followFansRecordVos);
List<FollowFansRecordVo> followFansRecordVoList = getCurrentFollowRelation(dto, followFansRecordVos);
return Result.success(followFansRecordVoList);
} catch (Exception e) {
logger.error("{} 查询关注列表 {}", dto.getId(), e);
}
......@@ -170,7 +200,8 @@ public class FollowFansRecordServiceImpl implements FollowFansRecordService {
}
followFansRecordVos.add(followFansRecordVo);
});
return Result.success(followFansRecordVos);
List<FollowFansRecordVo> followFansRecordVoList = getCurrentFollowRelation(dto, followFansRecordVos);
return Result.success(followFansRecordVoList);
} catch (Exception e) {
logger.error("{} 查询粉丝列表 {}", dto.getId(), e);
}
......@@ -178,6 +209,36 @@ public class FollowFansRecordServiceImpl implements FollowFansRecordService {
}
public List<FollowFansRecordVo> getCurrentFollowRelation(FollowFansRecordRequestDto dto, List<FollowFansRecordVo> followFansRecordVos) {
if (dto.getCurrentUserId() != null) {
//查询当前用户关注的人
FollowFansRecordRequestDto currentFollowDto = new FollowFansRecordRequestDto();
dto.setFansId(dto.getCurrentUserId());
List<FollowFansRecord> currentFollowFansRecordList = followFansRecordDao.findList(currentFollowDto);
List<Long> currentFollowIdList = currentFollowFansRecordList.stream().map(FollowFansRecord::getFollowerId).collect(Collectors.toList());
//查询当前用户的粉丝
FollowFansRecordRequestDto currentFansDto = new FollowFansRecordRequestDto();
dto.setFollowerId(dto.getCurrentUserId());
List<FollowFansRecord> currentFansRecordList = followFansRecordDao.findList(currentFansDto);
List<Long> currentFansIdList = currentFansRecordList.stream().map(FollowFansRecord::getFansId).collect(Collectors.toList());
followFansRecordVos.forEach(followFansRecordVo -> {
long followerId = followFansRecordVo.getFollowerId();
if (currentFollowIdList.contains(followerId) && currentFansIdList.contains(followerId)) {
followFansRecordVo.setRelation("互相关注");
} else if (currentFollowIdList.contains(followerId) && !currentFansIdList.contains(followerId)) {
followFansRecordVo.setRelation("已关注");
} else if (!currentFollowIdList.contains(followerId) && currentFansIdList.contains(followerId)) {
followFansRecordVo.setRelation("回关");
} else {
followFansRecordVo.setRelation("");
}
});
}
return followFansRecordVos;
}
@Override
public Result findFollowRelation(FollowFansRecordRequestDto dto) {
try {
......
......@@ -5,10 +5,7 @@ import com.wwdz.ch.core.consts.MessageEnum;
import com.wwdz.ch.core.consts.ResultCode;
import com.wwdz.ch.core.type.PageSearchResult;
import com.wwdz.ch.core.type.Result;
import com.wwdz.ch.core.util.CacheUtil;
import com.wwdz.ch.core.util.DateTimeUtil;
import com.wwdz.ch.core.util.MediaUtil;
import com.wwdz.ch.core.util.StringUtil;
import com.wwdz.ch.core.util.*;
import com.wwdz.ch.db.dao.FollowFansRecordDao;
import com.wwdz.ch.db.dao.ItemDao;
import com.wwdz.ch.db.dao.MessageContentDao;
......@@ -61,6 +58,9 @@ public class MessageServiceImpl implements MessageService {
@Autowired
CacheUtil cacheUtil;
@Autowired
RedisUtils redisUtils;
@Transactional(rollbackFor = Exception.class)
@Override
......
......@@ -21,6 +21,14 @@ public interface FollowFansRecordService {
Result cancelFollow(FollowFansRecordRequestDto dto);
/**
* 更具双方id取消关注某人
* @param dto
* @return
*/
Result cancelFollowByBothId(FollowFansRecordRequestDto dto);
/**
* 查询关注和粉丝人数
* @param dto
......
......@@ -65,4 +65,9 @@ public class StringUtil {
}
return countString;
}
public static String formatMessageKey(long sendId, int type, long receiverId) {
return "" + sendId + ":" + type + ":" + receiverId;
}
}
......@@ -46,6 +46,17 @@ public class FollowFansRecordController {
return followFansRecordService.cancelFollow(dto);
}
@ApiOperation(value = "取消关注")
@PostMapping("/cancelFollowByBothId")
public Result cancelFollowByBothId(@RequestBody FollowFansRecordRequestDto dto) {
logger.info("取消关注,请求参数:{}", JSON.toJSONString(dto));
if (dto.getFollowerId() == null || dto.getFansId() == null) {
return Result.failed(ResultCode.PARAM_ERROR);
}
return followFansRecordService.cancelFollowByBothId(dto);
}
@ApiOperation(value = "统计关注和粉丝数量")
@PostMapping("/countFollowFans")
public Result countFollowFans(@RequestBody FollowFansRecordRequestDto dto) {
......
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