domain
@Data
@TableName("t_nice_detail")
public class NiceDetail {
@TableId(value ="id",type = IdType.AUTO)
@JsonIgnore
@ApiModelProperty(hidden=true)
private Integer id;
private Long consumerId;
private Integer videoId;
}
dao
@Repository
public interface NiceDetailDao extends BaseMapper {
int deleteByPrimaryKey(Integer id);
NiceDetail findNiceDetail(NiceDetail niceDetail);
int insertSelective(@Param("record") NiceDetail record);
Integer insertGiveUp(Integer GiveUp);
}
Mapper
id, consumer_id, video_id
delete from t_nice_detail
where id = #{id,jdbcType=INTEGER}
insert into t_nice_detail
id,
consumer_id,
video_id,
#{record.id,jdbcType=INTEGER},
#{record.consumerId,jdbcType=VARCHAR},
#{record.videoId,jdbcType=INTEGER},
update t_video set give_up = give_up+1 where id=#{param2}
Service
public interface NiceDetailService {
/**
* 插入点赞记录
* @param niceDetail
* @return
*/
Integer insertNiceDetail(NiceDetail niceDetail);
/**
* 删除点赞记录
* @param id
* @return
*/
Integer deleteNiceDetail(Integer id);
/**
* 根据用户id和视频id信息查询点赞记录
* @param niceDetail
* @return
*/
NiceDetail findNiceDetail(NiceDetail niceDetail);
/**
* @param niceDetail
* @return
*/
JsonResult niceDetail(NiceDetail niceDetail);
/**
* @param GiveUp 游客点赞数加一
* @return
*/
Integer addGiveUp(Integer GiveUp);
}
Impi
@Service("nicedetailService")
public class NiceDetailServiceImpl implements NiceDetailService {
@Autowired
private NiceDetailDao nicedetailDao;
@Autowired
private VideoDao videoDao;
@Override
public Integer insertNiceDetail(NiceDetail niceDetail) {
System.out.println("impl:"+niceDetail.toString());
return nicedetailDao.insertSelective(niceDetail);
}
@Override
public Integer deleteNiceDetail(Integer id) {
return nicedetailDao.deleteByPrimaryKey(id);
}
@Override
public NiceDetail findNiceDetail(NiceDetail niceDetail) {
return nicedetailDao.findNiceDetail(niceDetail);
}
@Override
public JsonResult niceDetail(NiceDetail niceDetail) {
System.out.println("点赞模块:"+niceDetail.toString());
//查询是否有该用户的点赞记录
NiceDetail niceDetail1 = nicedetailDao.findNiceDetail(niceDetail);;
int result=0;
if (niceDetail1!=null){
System.out.println("有该记录");
//如果找到这条记录,删除该记录,同时视频的点赞数减一
//删除记录
nicedetailDao.deleteByPrimaryKey(niceDetail1.getId());
//根据点赞id找到视频
Video byId = videoDao.selectById(niceDetail1.getVideoId());
//视频点赞数减一
byId.setGiveUp(byId.getGiveUp()-1);
result= Math.toIntExact((byId.getGiveUp()));
System.out.println("res:"+result);
//更新视频点赞数
videoDao.updateById(byId);
}else{
//如果没有找到这条记录,则添加这条记录,同时视频数加一;
//添加记录
System.out.println("没有记录:");
nicedetailDao.insertSelective(niceDetail);
NiceDetail niceDetail2 = nicedetailDao.findNiceDetail(niceDetail);;
Video byId = videoDao.selectById(niceDetail2.getVideoId());
//视频点赞数加一
byId.setGiveUp(byId.getGiveUp()+1);
System.out.println("点赞加"+byId.getGiveUp()+1);
result= Math.toIntExact(byId.getGiveUp());
System.out.println("res:"+result);
//更新视频点赞数
videoDao.updateById(byId);
}
return JsonResult.success();
}
批量查询点赞
@Override
public Integer addGiveUp(Integer GiveUp) {
return nicedetailDao.insertGiveUp(GiveUp);
}
}
点赞做了限制,一个用户只能点30次
@RestController
@Api(tags = "点赞")
@RequestMapping("api/nicedetail")
@Validated
@CrossOrigin
public class NiceDetailController {
@Autowired
private NiceDetailService niceDetailService;
@Autowired
private HttpServletRequest httprequest;
@Autowired
private VideoService videoService;
/**
* 处理用户点赞行为
* @param niceDetail
* @return
*/
@PostMapping("/niceDetail{userid,consumerId}")
@ResponseBody
@ApiOperation("点赞")
@Limit(limitType = LimitType.SECONDS,period = 3600,limitParamTypes = {LimitParamType.PARAM,LimitParamType.IP},count =30,params = "niceDetail.videoId")
public Serializable niceDetail(NiceDetail niceDetail){
JSONObject json = new JSONObject();
//获取请求头Authorization信息令牌
String authorization = httprequest.getHeader("Authorization");
json.put("Authorization",authorization);
Long uid = null ;
//判断是否有token,有才执行解析操作
if(authorization!=null){
uid = JWTUtil.getAppUID(authorization);
}
System.out.println(uid);
//判断用户是否有令牌
if (uid != null) {
return niceDetailService.niceDetail(niceDetail);
}else {
Video video = new Video();
Integer videoId = niceDetail.getVideoId();
video.setId(Long.valueOf(videoId));
Long id = video.getId();
//游客加一
return niceDetailService.addGiveUp(Integer.valueOf(Math.toIntExact(id)));
}
}
//限制用户频繁点赞注解,
@Limit(limitType = LimitType.SECONDS,period = 3600,limitParamTypes = {LimitParamType.PARAM,LimitParamType.IP},count = 30,params = "id")
@ApiOperation("游客取消点赞")
@DeleteMapping("unlikeVideoGiveUp/{id}")
public JsonResult unlikeVideo(Long id) {
videoService.guest(id);
return JsonResult.success();
}
}
批量查询点赞
List selectByIds(@Param("giveUp")List giveUp);
@Override
public List selectByIds(List giveUp) {
return videoDao.selectByIds(giveUp);
}
@ApiOperation("批量查询点赞数")
@RequestMapping(value="/getVideoList/{id}",method= RequestMethod.POST)
public String getVideoList(@RequestParam(value ="id",required = false)String id) {
videoService.selectByIds(Arrays.asList(id.split(",")));
return "id:"+id;
}