package com.service.schoolhome;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.model.Notice;
import com.model.Org;
import com.utils.CommonUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class NewsBulletinService {
    public static final NewsBulletinService me = new NewsBulletinService();

    public Page<Record> schoolNotice(int pageNumber, int pagesize, Integer orgid) {
        String select = "SELECT a.id,a.status,a.flag,a.title,a.content,DATE_FORMAT(a.createtime,'%Y-%m-%d') AS createtime,c.orgname,a.filsurl ";
        StringBuilder where = new StringBuilder(" FROM t_notice a LEFT JOIN t_org c ON a.orgid = c.id WHERE a.`status` = 1 ");
        List list = new ArrayList();
        //待定 获取数据源
        if(orgid > 0){
            where.append(" AND a.orgid = ? ");
            list.add(orgid);
        }
        where.append(" ORDER BY a.createtime DESC");
        return Db.paginate(pageNumber, pagesize,select,where.toString(),CommonUtils.listToArray(list));
    }

    public Page<Record> schoolNews(int pageNumber, int pagesize, Integer orgid) {
        String select = "SELECT DATE_FORMAT(createtime,'%Y-%m-%d') AS createtime,content,id,orgid,status,title ";
        StringBuilder where = new StringBuilder(" FROM t_news WHERE `status` = 1 ");
        List list = new ArrayList();
        if(orgid > 0){
            where.append(" AND orgid = ? ");
            list.add(orgid);
        }
        where.append(" ORDER BY createtime DESC");
        return Db.paginate(pageNumber, pagesize,select,where.toString(),CommonUtils.listToArray(list));
    }

    public Record newsNoticeInfo(Integer id,Integer flag){
        if(flag == 2){
            Record r = Db.findFirst("SELECT a.id,a.status,a.flag,a.title,a.content,a.createtime,a.filsurl,c.orgname" +
                    " FROM t_notice a LEFT JOIN t_org c ON a.orgid = c.id WHERE a.id = ?",id);
            return r;
        }else {
            Record r = Db.findFirst("SELECT a.*,b.orgname FROM t_news a LEFT JOIN t_org b ON a.orgid = b.id WHERE a.id = ?",id);
            return r;
        }
    }

    public Map infoUpperOrLower(Integer id,Integer flag){
        Map map = new HashMap();
        if(flag == 2){
            Record rUpper = Db.findFirst("select * from t_notice where id = (select id from t_notice where id < ? AND `status` = 1 order by createtime DESC limit 1)",id);
            Record rLower = Db.findFirst("select * from t_notice where id = (select id from t_notice where id > ? AND `status` = 1 order by createtime ASC limit 1)",id);
            map.put("rUpper",rUpper);
            map.put("rLower",rLower);
            return map;
        }else {
            Record rUpper = Db.findFirst("select * from t_news where id = (select id from t_news where id < ? AND `status` = 1 order by createtime DESC limit 1)",id);
            Record rLower = Db.findFirst("select * from t_news where id = (select id from t_news where id > ? AND `status` = 1 order by createtime ASC limit 1)",id);
            map.put("rUpper",rUpper);
            map.put("rLower",rLower);
            return map;
        }

    }
}
