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.BookNew;
import com.model.News;
import com.model.Notice;
import com.model.Rules;

import com.utils.CommonUtils;

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

public class SchoolHomeService {

    private BookNew dao = new BookNew().dao();
    private News newsDao = new News().dao();
    private Notice noticeDao = new Notice().dao();
    private Rules rulesDao = new Rules().dao();

    public List<Record> newBook(Integer orgid){
        String select = "SELECT b.title,b.author,b.imgurl ";
        StringBuilder where = new StringBuilder(" FROM t_schoolbookrecommend a LEFT JOIN t_book b ON a.bookid = b.id " +
                " WHERE a.flag = 0 AND a.deletestatus = 0 ");
        List list = new ArrayList();
        if(orgid > 0){
            where.append(" AND a.orgid = ? ");
            list.add(orgid);
        }
        where.append(" ORDER BY a.createtime DESC LIMIT 0,4");
        return Db.find(select+where.toString(),CommonUtils.listToArray(list));
    }

    public List<News> homeNews(Integer orgid) {
        String select = "SELECT * ";
        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 LIMIT 0,3");
        return newsDao.find(select+where.toString(),CommonUtils.listToArray(list));
    }

    public List<Notice> homeNotice(Integer orgid) {
        String select = "SELECT a.id,a.status,a.flag,a.title,a.content,a.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 LIMIT 0,3");
        return noticeDao.find(select+where.toString(),CommonUtils.listToArray(list));
    }

    public Rules homeRules(Integer orgid) {
        String select = "SELECT title,content ";
        StringBuilder where = new StringBuilder(" FROM t_rules WHERE 1=1 ");
        List list = new ArrayList();
        if(orgid > 0){
            where.append("AND orgid = ?");
            list.add(orgid);
        }else {
            where.append("LIMIT 0,1");
        }
        return rulesDao.findFirst(select+where.toString(),CommonUtils.listToArray(list));
    }
    public Page<Record> homeReaderMsg(int pageNumber, int pageSize, Integer readerid){
        String select = "SELECT * ";
        StringBuilder where = new StringBuilder(" FROM t_reader_msg WHERE readerid = ?");
        List list = new ArrayList();
        list.add(readerid);
        where.append(" ORDER BY createtime DESC");
        return Db.paginate(pageNumber, pageSize,select,where.toString(),CommonUtils.listToArray(list));
    }

    public int readerMsgNum(Integer readerid){
       Record r = Db.findFirst("SELECT COUNT(*) as num FROM t_reader_msg WHERE readerid = ? AND TO_DAYS(createtime) = to_days(now()) AND `status` = 0",readerid);
       if(r != null){
           int num = r.getInt("num");
           return num;
       }
       return 0;
    }

    public Page<Record> yuyueToLibrary(int pageNumber, int pageSize, Integer orgid){
        String select = "SELECT c.cardnumber,b.title,DATE_FORMAT(a.successtime,'%Y-%m-%d') AS successtime,DATE_FORMAT(a.createtime,'%Y-%m-%d') AS createtime ";
        StringBuilder where = new StringBuilder(" FROM t_yuyue a LEFT JOIN t_book b ON a.bookid = b.id " +
                "LEFT JOIN t_reader c ON a.readerid = c.id " +
                "WHERE a.flag = 0 AND a.deletestatus = 0 AND a.`status` = 1 AND c.orgid = ?");
        List list = new ArrayList();
        list.add(orgid);
        return Db.paginate(pageNumber, pageSize,select,where.toString(),CommonUtils.listToArray(list));
    }

    public Page<Record> homeOverdueReminder(int pageNumber, int pageSize, Integer orgid){
        String select = "SELECT d.cardnumber,c.title,DATE_FORMAT(a.duetime,'%Y-%m-%d') AS duetime ";
        StringBuilder where = new StringBuilder(" FROM t_jieyue a " +
                "INNER JOIN t_book_detail b ON a.bookdetailid = b.id INNER JOIN t_book c ON b.bookid = c.id " +
                "INNER JOIN t_reader d ON a.readerid = d.id " +
                "WHERE a.duetime < NOW() AND a.flag = 0 AND a.`status` = 0 AND a.orgid = ?");
        List list = new ArrayList();
        list.add(orgid);
        return Db.paginate(pageNumber, pageSize,select,where.toString(),CommonUtils.listToArray(list));
    }

    public Object findTypeNumber(Integer orgid) {
        String fiveNumSql = "SELECT CASE WHEN a.parentid = 1 THEN '马列主义毛泽东思想' WHEN a.parentid=2 THEN '哲学、宗教' WHEN a.parentid=3 THEN '社会科学' WHEN a.parentid=4 THEN '自然科学' WHEN a.parentid=5 THEN '综合性图书' END AS name,COUNT(c.id) AS value FROM t_booktype a " +
                "LEFT JOIN t_book b ON a.id = b.typeid LEFT JOIN t_book_detail c ON b.id = c.bookid  " +
                "WHERE a.parentid IN (1,2,3,4,5)  AND c.status !=3 AND c.bookstatus = 1 AND b.orgid = ? GROUP BY a.parentid " ;
        Map result = new HashMap();
        List fiveNum = Db.find(fiveNumSql,orgid);
        result.put("fiveNum",fiveNum);

        String select = "(SELECT a.id,a.`name`,COUNT(c.id) as value  \n" +
                "FROM t_booktype a LEFT JOIN t_book b ON a.id = b.typeid LEFT JOIN t_book_detail c ON b.id = c.bookid WHERE c.status !=3 AND c.bookstatus = 1 AND b.orgid = "+orgid+"\n" +
                "GROUP BY a.id ORDER BY value DESC LIMIT 10)\n" +
                "UNION\n" +
                "(select '' as id,'其他' as `name`,SUM(test.`value`) FROM\n" +
                "(SELECT a.id,a.`name`,COUNT(c.id) as value  \n" +
                "FROM t_booktype a LEFT JOIN t_book b ON a.id = b.typeid LEFT JOIN t_book_detail c ON b.id = c.bookid WHERE c.status !=3 AND c.bookstatus = 1 AND b.orgid = "+orgid+" \n" +
                "GROUP BY a.id ORDER BY value DESC LIMIT 10,26) as test)";
        List topTen = Db.find(select);
        result.put("topTen",topTen);
        return result;
    }

    public Object findTypeNumberTeach(Integer orgid) {
        String fiveNumSql = "SELECT CASE WHEN a.parentid = 1 THEN '马列主义毛泽东思想' WHEN a.parentid=2 THEN '哲学、宗教' WHEN a.parentid=3 THEN '社会科学' WHEN a.parentid=4 THEN '自然科学' WHEN a.parentid=5 THEN '综合性图书' END AS name,COUNT(c.id) AS value FROM t_booktype a " +
                "LEFT JOIN t_book b ON a.id = b.typeid LEFT JOIN t_book_detail c ON b.id = c.bookid  LEFT JOIN t_org o ON b.orgid = o.id " +
                "WHERE a.parentid IN (1,2,3,4,5)  AND c.status !=3 AND c.bookstatus = 1 AND o.parentcodes like ? GROUP BY a.parentid " ;
        Map result = new HashMap();
        List fiveNum = Db.find(fiveNumSql,"%"+orgid+"%");
        result.put("fiveNum",fiveNum);
        List topTen = Db.find("SELECT a.name,COUNT(c.id) AS value FROM t_booktype a LEFT JOIN t_book b ON a.id = b.typeid LEFT JOIN t_book_detail c ON b.id = c.bookid LEFT JOIN t_org o ON b.orgid = o.id  WHERE c.status !=3 AND c.bookstatus = 1 AND o.parentcodes like ? GROUP BY a.id ORDER BY value DESC LIMIT 10","%"+orgid+"%");
        result.put("topTen",topTen);
        return result;
    }

    public String orgTitle(Integer orgid){
        Record r = Db.findFirst("SELECT orgname FROM t_org WHERE id = ?",orgid);
        if(r != null){
            String title = r.getStr("orgname");
            return title;
        }
        return null;
    }
}
