发表于: 2017-11-06 23:47:30
2 669
【今日完成】
今天花了一天时间,完成了一个接口:
现在来详细介绍一下:
/**
* 前台-用户查看文档列表
*@param userId
*@author Jeff
*@date 2017-11-6 9:53
**/
@RequestMapping(value = "/a/document/list", method = RequestMethod.GET)
public String getMultiArticleJson(HttpServletRequest request,
HttpServletResponse response, ModelMap model, String userId,Integer startNumber )
throws Exception {
log.info("用户查看文档详情");
//1 初始化参数
if(null ==userId){
model.addAttribute("code", -1);
return "data/json";
}
Integer size =9;
Integer start =startNumber;
try {
/*HashMap hashMap =new HashMap();*/
// 2 通过参数拿条件
Map<String, Object> param=DynamicUtil.getArticleIds();
log.info("文档参数是:" + param);
//3 通过条件拿Ids
List<Long> ids = articleService.getIdsByDynamicCondition(Article.class,param,start,size);
log.info("the ids is " + ids);
// 4 通过Ids拿文章列表
List<Article> articleList = articleService.getObjectsByIds(ids);
log.info("get article data is " + articleList.size());
int i =articleList.size();
log.info(i);
while (i > 0) {
Long articleId =ids.get(ids.size()-i);
log.info(articleId);
//这里开始内部循环,把点赞/收藏状态给出来
Map<String, Object> innerParam = DynamicUtil.getTargetId(userId,articleId,2);
log.info("内部条件是"+innerParam);
List<Long> innerIds = relationService.getIdsByDynamicCondition(Relation.class,innerParam,0,4);
List relationList = relationService.getObjectsByIds(innerIds);
int j =relationList.size();//j最多为2
log.info(j+"?????");
while (j >0) {
log.info(j);
Relation relation = (Relation) relationList.get(j - 1);
if (relation.getRelationType() == 1) {
log.info("0000");
articleList.get(i-1).setIsPraised(2);
log.info("aaaa");
} else if (relation.getRelationType() == 2) {
log.info("0001");
articleList.get(i-1).setIsCollected(2);
log.info("bbbb");
}
j--;
log.info(j);
}
i--;
log.info("循环一次结束i"+i);
}
log.info("breakloop");
int ii=articleList.size();
log.info("getsize"+ii);
while(ii > 0) {
Long ll =articleList.get(ii-1).getUpdateAt();
log.info("1111111111"+ll);
String lt = TimeTransformationUtil.transforFromNow(ll);
log.info("1111111111"+lt);
articleList.get(ii-1).setLastTime(lt);
log.info("222222222");
ii--;
log.info(i+"iiiii");
}
model.addAttribute("articleList", articleList);
model.addAttribute("code", 0);
model.addAttribute("page", 1+startNumber/size);
model.addAttribute("size", size);
/*model.addAttribute("l", hashMap);*/
} catch (Throwable t) {
log.error(t.getMessage());
log.error("get article error" );
model.addAttribute("code", -1);
}
return "article/json/articleListJson";
}
其实这个接口就是获取一个文章列表:
难点有两个,第一是每个用户对每个文章的点赞收藏状态是不同的
第二是那个时间,24小时之内都显示“距离现在有XX小时”,超过24小时则会显示日期
第一个难点,我是准备拿到每个Article的信息时,再查询一次关系表,把用户与相关文章的收藏点赞状态放到Article里面,但是以前Article的model里面没有这个属性,所以我在model里面加了几个属性。
注意,这里要加@Transient才不会报错。
关于第二个难点,我写了个工具类:
public static String transforFromNow(Long updateTime){
Date now = new Date();
Long currentTime =now.getTime();
Long lastTime =currentTime - updateTime;
log.info("进入if判断");
if(lastTime >1000*60*60*24){
return dayTransfor(updateTime);
}else if(lastTime>0){
for(int i=24;i>1;i--){
if( 1000*60*60*(i-1) < lastTime&& lastTime <1000*60*60*i){
return (i-1) +"小时前";
}else if(0 < lastTime&& lastTime <1000*60*60){
return "1小时前";
}
}
}else if(lastTime <0){
return "出错了!时间毫秒为负数了!";
}
return "未知出错情况";
}
这样就能把毫秒数与现在时间对比,通过相减,来得出距离现在有多久远。
【明日计划】
代码写得比较粗糙,只是实现了功能,今天优化一下
【今日收获】
完成了一个逻辑比较麻烦的接口
评论