点赞功能详情

数据库表

在这里插入图片描述

代码实现

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<NiceDetail> {
    int deleteByPrimaryKey(Integer id);
    NiceDetail findNiceDetail(NiceDetail niceDetail);
    int insertSelective(@Param("record") NiceDetail record);
    Integer insertGiveUp(Integer GiveUp);
}

Mapper

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.wwtx.chinesemedicine.service.dao.NiceDetailDao" >

    <resultMap id="BaseResultMap" type="com.wwtx.chinesemedicine.service.models.domain.NiceDetail" >
        <id column="id" property="id" jdbcType="INTEGER" />
        <result property="consumerId" column="consumer_id"  />
        <result property="videoId" column="video_id" jdbcType="INTEGER" />
    </resultMap>

    <sql id="Base_Column_List" >
        id, consumer_id, video_id
    </sql>

    <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
        delete from t_nice_detail
         where id = #{id,jdbcType=INTEGER}
     </delete>
    <select id="findNiceDetail" resultMap="BaseResultMap" parameterType="com.wwtx.chinesemedicine.service.models.domain.NiceDetail" >
        select <include refid="Base_Column_List"/>
        from t_nice_detail where consumer_id = #{consumerId,jdbcType=VARCHAR} AND video_id = #{videoId,jdbcType=INTEGER}
    </select>
    <insert id="insertSelective" parameterType="com.wwtx.chinesemedicine.service.models.domain.NiceDetail" >
        insert into t_nice_detail
        <trim prefix="(" suffix=")" suffixOverrides="," >
            <if test="record !=null " >
                id,
            </if>
            <if test="record !=null " >
                consumer_id,
            </if>
            <if test="record !=null " >
                video_id,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides="," >
            <if test="record !=null " >
                #{record.id,jdbcType=INTEGER},
            </if>
            <if test="record !=null " >
                #{record.consumerId,jdbcType=VARCHAR},
            </if>
            <if test="record !=null " >
                #{record.videoId,jdbcType=INTEGER},
            </if>
        </trim>
    </insert>
    <update id="insertGiveUp" parameterType="com.wwtx.chinesemedicine.service.models.domain.Video">
      update t_video set give_up = give_up+1 where id=#{param2}
    </update>
    <!--[arg1, arg0, param1, param2]-->
</mapper>

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<String> giveUp);
 @Override
    public List selectByIds(List<String> giveUp) {
        return videoDao.selectByIds(giveUp);
    }
  <resultMap id="videoIdList" type="java.lang.Integer">
    <result column="give_Up" property="giveUp"/>
  </resultMap>
<!--根据id列表批量查询点赞-->
  <select id="selectByIds" resultMap="videoIdList" parameterType="java.util.List">
    select give_up from t_video  where id in
    <foreach collection="giveUp" item="id" open="(" close=")" separator=",">
      #{id}
    </foreach>
  </select>
  @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;
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值