package cn.jfinalbbs.api;
import cn.jfinalbbs.api.vo.SectionVo;
import cn.jfinalbbs.api.vo.TopicVo;
import cn.jfinalbbs.common.BaseController;
import cn.jfinalbbs.reply.Reply;
import cn.jfinalbbs.section.Section;
import cn.jfinalbbs.topic.Topic;
import cn.jfinalbbs.utils.DateUtil;
import com.google.common.collect.Lists;
import java.util.List;
/**
* 版块
* Created by yahui on 2015/11/12.
*/
public class SectionApi extends BaseController {
/**
* 获取所有的版块 主题数量;帖子数量(包括回复);包括最后一个帖子的最后发表帖子的标题、时间、作者
*/
public void findAllAndTopic() {
List<Section> list = Section.me.findShow();
List<SectionVo> sectionVos = Lists.newArrayList();
for (Section item : list) {
long sectionId = item.getLong("id");
//获取到主题数量
int themeCount = Topic.me.topicCountWithSectionId(sectionId);
List<Topic> topics = Topic.me.findWithSectionId(sectionId);
StringBuffer stringBuffer = new StringBuffer();
int topicCount = 0;
if (null != topics && topics.size() > 0) {
for (Topic item1 : topics) {
stringBuffer.append("'" + item1.getStr("id") + "',");
}
String ids = stringBuffer.substring(0, stringBuffer.length() - 1);
//获取帖子数量(主题和回复的数量)
topicCount = Reply.me.replyCountWithTopicIds(ids);
}
SectionVo sectionVo = new SectionVo();
String name = item.get("name");
String image = item.get("image");
String tab = item.get("tab");
sectionVo.setSectionId(sectionId);
sectionVo.setSectionName(name);
sectionVo.setImage(image);
sectionVo.setTab(tab);
sectionVo.setThemeCount(themeCount);
sectionVo.setTopicCount(topicCount);
Topic finalTopic = Topic.me.findFirst("select t.*,u.nickname from topic as t inner join user as u on t.author_id=u.id where t.s_id='" + sectionId + "'order by in_time desc");
if (null != finalTopic) {
sectionVo.setPublishTime(DateUtil.formatDateTime(finalTopic.getDate("in_time")));
sectionVo.setThemeName(finalTopic.getStr("title"));
sectionVo.setAuthor(finalTopic.getStr("nickname"));
}
sectionVos.add(sectionVo);
}
success(sectionVos);
}
/**
* 获取所有的版块
*/
public void findAllSection(){
List<Section> list=Section.me.findAll();
List<SectionVo> sectionVos= Lists.newArrayList();
if(null!=list&&list.size()>0){
for(Section item:list){
SectionVo sectionVo=new SectionVo();
sectionVo.setSectionId(item.getLong("id"));
sectionVo.setSectionName(item.getStr("name"));
sectionVo.setTab(item.getStr("tab"));
sectionVos.add(sectionVo);
}
}
success(sectionVos);
}
}