package cn.jfinalbbs.section;
import cn.jfinalbbs.common.Constants;
import cn.jfinalbbs.utils.StrUtil;
import com.jfinal.plugin.activerecord.Model;
import java.io.Serializable;
import java.util.List;
/**
* Created by liuyang on 15/4/25.
*/
public class Section extends Model<Section> implements Serializable {
private static final long serialVersionUID = 4351698554467528103L;
public final static Section me = new Section();
public List<Section> findAll() {
return super.findByCache(Constants.CacheName.SECTIONLIST, Constants.CacheKey.SECTIONLISTKEY, "select * from section order by display_index");
}
public List<Section> findShow() {
return super.findByCache(Constants.CacheName.SECTIONSHOWLIST, Constants.CacheKey.SECTIONSHOWLISTKEY, "select * from section where show_status = 1 order by display_index");
}
public Section findByTab(String tab) {
return super.findFirst("select * from section where tab = ?", tab);
}
public Section findDefault() {
List<Section> sections = super.findByCache(Constants.CacheName.DEFAULTSECTION, Constants.CacheKey.DEFAULTSECTIONKEY, "select * from section where default_show = 1");
if(sections.size() > 0) return sections.get(0);
return null;
}
/**
* 查看这个版块是否存在
* @param name
* @param tab
* @return
*/
public Boolean exist(String name,String tab){
StringBuffer stringBuffer=new StringBuffer();
stringBuffer.append("select count(*) as count from section as s where ");
if(StrUtil.notBlank(name)&&StrUtil.notBlank(tab)){
stringBuffer.append(" s.name='" + name + "' or s.tab='" + tab + "'");
}else if(StrUtil.notBlank(name)){
stringBuffer.append(" s.name='" + name + "'");
}else if(StrUtil.notBlank(tab)){
stringBuffer.append(" s.tab='" + tab + "'");
}
Section section=super.findFirst(stringBuffer.toString());
if(null!=section&&0!=section.getLong("count")){
return true;
}else{
return false;
}
}
}