发表于: 2019-11-08 19:52:27

1 1082


今天完成的事情:

在昨天的基础上,把收藏和浏览接口写完,都是一对一查询。未一麻烦的地方是,要统计的问题。


在学习对象表的xml映射文件里,弄一个动态修改统计字段的SQL

<!--动态修改统计值-->
<update id="updateCount" parameterType="com.happynewyear.user.pojo.StudyObject">
   update study_object
<set>
       <if test="likeCount != null">
           like_count = like_count+#{likeCount,jdbcType=INTEGER},
</if>
       <if test="buyCount != null">
           buy_count = buy_count+#{buyCount,jdbcType=INTEGER},
</if>
       <if test="collectionCount != null">
           collection_count = collection_count+#{collectionCount,jdbcType=INTEGER},
</if>
       <if test="commentCount != null">
           comment_count = comment_count+#{commentCount,jdbcType=INTEGER},
</if>
       <if test="browseCount != null">
           browse_count = browse_count+#{browseCount,jdbcType=INTEGER},
</if>
       <if test="praiseCount != null">
           praise_count = praise_count+#{praiseCount,jdbcType=INTEGER},
</if>
   </set>
   where id = #{id,jdbcType=BIGINT}
</update>

这里贴一个详细查询学习对象后,添加浏览量与浏览记录的接口,这里获取当前用户还没要,先写个假数据。

完整的逻辑是

传入学习对象id返回学习对象内容,学习对象浏览统计加1,查看浏览表是否有当前用户查看此学习对象记录,有既修改浏览时间,无则新增对应的浏览记录。

/**
* 详细查学习对象
*/
@GetMapping("/a/u/study")
public Map<String, Object> studySelect(Long id) {
   HashMap<String, Object> result = new HashMap<>(16);
   try {
       log.info("要查询的学习对象ID:{}", id);
       StudyObject studyObject = studyObjectServer.selectByPrimaryKey(id);
       /*查看一次,浏览统计加一*/
       StudyObject studyObjectCount=new StudyObject();
       studyObjectCount.setId(id);
       studyObjectCount.setBrowseCount(+1);
       studyObjectServer.updateCount(studyObjectCount);

       long userId=1L;
       /*判断浏览表是否有此用户对应的当前学习对象的浏览记录*/
       History history = new History();
       history.setStudyObjectId(id);
       history.setCreateBy(userId);
       List<History> historyList = historyServer.selectSelective(history);
       if (!historyList.isEmpty()) {
           history.setId(historyList.get(0).getId());
           /*如果不为空就更新这条浏览记录*/
           historyServer.updateByPrimaryKeySelective(history);
       } else {
           /*如果为空就新增一条浏览记录*/
           history.setUpdateBy(userId);
           historyServer.insertSelective(history);
       }
       result.put("code", SUCCESS.getCode());
       result.put("msg", SUCCESS.getMsg());
       result.put("data", studyObject);
       return result;
   } catch (Exception e) {
       e.printStackTrace();
       result.put("code", REQUEST_FAILED.getCode());
       result.put("msg", REQUEST_FAILED.getMsg());
       return result;
   }
}

收藏会简单点

贴个删除收藏的代码

/**
* 收藏删除
*/
@DeleteMapping("/a/u/collection")
public Map<String, Object> collectionDelete(long id) {
   HashMap<String, Object> result = new HashMap<>(16);
   log.info("要删除的收藏{}",id);
   try {
       /*查询要删除收藏的对应学习对象,进行统计删除准备*/
       StudyObject studyObject=new StudyObject();
       studyObject.setId(studyCollectionServer.selectByPrimaryKey(id).getId());
       studyObject.setCollectionCount(-1);
       int collectionDeleteSuccess=studyCollectionServer.deleteByPrimaryKey(id);
       /*收藏删除成功后,对应学习对象收藏统计减1*/
       if(collectionDeleteSuccess==1){
           studyObjectServer.updateCount(studyObject);
       }
       result.put("code", SUCCESS.getCode());
       result.put("msg", SUCCESS.getMsg());
       return result;
   } catch (Exception e) {
       e.printStackTrace();
       result.put("code", REQUEST_FAILED.getCode());
       result.put("msg", REQUEST_FAILED.getMsg());
       return result;
   }
}


明天计划的事情:

最后一个留言接口写完,和前端对接口,给数据库添点假数据。


遇到的问题:

问题都是一些粗心的问题,都是SQL语句里的,动态查询是我自己写的,然后贴的时候没看仔细。表名写错了。剩下就是增加,删除上的逻辑有点烦。



收获:




返回列表 返回列表
评论

    分享到