package com.controller.screendisplay;

import com.config.AuthInterceptor;
import com.config.BaseController;
import com.config.ControllerInterface;
import com.jfinal.aop.Clear;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.service.screendisplay.ScreenHomeService;
import com.utils.CommonUtils;

import java.util.List;

/**
 * 大屏幕首页
 * 2019-05-05
 * chenworgTypeNumber
 */
@ControllerInterface(path="/screen")
@Clear(AuthInterceptor.class)
public class ScreenHomeController extends BaseController {
    ScreenHomeService service = ScreenHomeService.me;

    public void teach() {
        render("/screen/teach.html");
    }

    public void school() {
        render("/screen/school.html");
    }

    public void modular() {
        render("/screen/modular.html");
    }

    /**
     * 学校书刊数量展示
     */
    public void schoolHomeCount(){
        try{
            Integer orgid = this.getParaToInt("orgid");
            success(service.schoolHomeCount(orgid));
        }catch(Exception e){
            e.printStackTrace();
            fail();
        }
    }

    /**
     * 学校今日简报
     */
    public void schoolTodayReport(){
        try{
            Integer orgid = this.getParaToInt("orgid");
            success(service.schoolTodayReport(orgid));
        }catch(Exception e){
            e.printStackTrace();
            fail();
        }
    }

    /**
     * 学校新书、好书推荐
     */
    public void schoolBookRecommend(){
        try{
            Integer orgid = this.getParaToInt("orgid");
            Integer flag = this.getParaToInt("flag",0);
            success(service.schoolBookRecommend(orgid,flag));
        }catch(Exception e){
            e.printStackTrace();
            fail();
        }
    }

    /**
     * 超期催还列表
     */
    public void overdueList(){
        try{
            Integer orgid = this.getParaToInt("orgid");
            success(service.overdueList(orgid));
        }catch(Exception e){
            e.printStackTrace();
            fail();
        }
    }

    /**
     * 图书借阅排行
     */
    public void borrowRank(){
        try{
            Integer orgid = this.getParaToInt("orgid");
            success(service.borrowRank(orgid,this.getParaToInt("pageNumber"),this.getParaToInt("pageSize")));
        }catch(Exception e){
            e.printStackTrace();
            fail();
        }
    }

    /**
     * 读者借阅排行
     */
    public void readerBorrowRank(){
        try{
            Integer orgid = this.getParaToInt("orgid");
            success(service.readerBorrowRank(orgid));
        }catch(Exception e){
            e.printStackTrace();
            fail();
        }
    }

    /**
     * 学校优秀集体
     */
    public void excellentDept(){
        try{
            Integer orgid = this.getParaToInt("orgid");
            success(service.excellentDept(orgid));
        }catch(Exception e){
            e.printStackTrace();
            fail();
        }
    }

    /**
     * 学校智能设备监控
     */
    public void schoolWisdomMonitor(){
        try{
            Integer orgid = this.getParaToInt("orgid");
            success(service.schoolWisdomMonitor(orgid));
        }catch(Exception e){
            e.printStackTrace();
            fail();
        }
    }

    /**
     * 教育局首页数量
     */
    public void orgHomeCount(){
        try{
            Integer orgid = this.getParaToInt("orgid");
            success(service.orgHomeCount(orgid));
        }catch(Exception e){
            e.printStackTrace();
            fail();
        }
    }

    /**
     * 教育局今日简报
     */
    public void orgTodayReport(){
        try{
            Integer orgid = this.getParaToInt("orgid");
            success(service.orgTodayReport(orgid));
        }catch(Exception e){
            e.printStackTrace();
            fail();
        }
    }

    /**
     * 优秀读者
     */
    public void excellentReaders(){
        try{
            Integer orgid = this.getParaToInt("orgid");
            success(service.excellentReaders(orgid));
        }catch(Exception e){
            e.printStackTrace();
            fail();
        }
    }

    /**
     * 教育局图书借阅排行
     */
    public void orgBorrowRank(){
        try{
            Integer orgid = this.getParaToInt("orgid");
            success(service.orgBorrowRank(orgid,this.getParaToInt("pageNumber"),this.getParaToInt("pageSize")));
        }catch(Exception e){
            e.printStackTrace();
            fail();
        }
    }

    /**
     * 教育局流通排行
     */
    public void circulateRank(){
        try{
            Integer orgid = this.getParaToInt("orgid");
            success(service.circulateRank(orgid));
        }catch(Exception e){
            e.printStackTrace();
            fail();
        }
    }

    /**
     * 教育局智能设备排行
     */
    public void orgWisdomRank(){
        try{
            Integer orgid = this.getParaToInt("orgid");
            success(service.orgWisdomRank(orgid));
        }catch(Exception e){
            e.printStackTrace();
            fail();
        }
    }

    /**
     * 教育局分类数量统计
     */
    public void orgTypeNumber(){
        try{
            Integer orgid = this.getParaToInt("orgid");
            success(service.orgTypeNumber(orgid));
        }catch(Exception e){
            e.printStackTrace();
            fail();
        }
    }

    /**
     * 查询机构展示的模块列表
     */
    public void showModular(){
        try{
            Integer orgid = this.getParaToInt("orgid");
            List<Record> list = Db.find("select div_id divId from t_modular_org o,t_screen_modular m where o.modular_id = m.id and o.org_id = ?",orgid);
            success(list);
        }catch(Exception e){
            e.printStackTrace();
            fail();
        }
    }

    /**
     * 查询模块列表
     */
    public void findModulars(){
        try{
            Integer orgid = this.getParaToInt("orgid");
            Record org = Db.findFirst("select orgtypeid from t_org o where o.id = ?",orgid);
            //查询教育局
            Integer type = 1;
            if (5 == org.getInt("orgtypeid")){
                //查询学校
                type = 2;
            }
            List<Record> list = Db.find("select m.*,(SELECT COUNT(1) FROM t_modular_org o WHERE o.modular_id = m.id AND o.org_id = ?) isShow from t_screen_modular m where m.type = ?",orgid,type);
            success(list);
        }catch(Exception e){
            e.printStackTrace();
            fail();
        }
    }

    public void bindModulars(){
        try{
            Integer orgid = this.getParaToInt("orgid");
            Db.delete("delete from t_modular_org where org_id = ?",orgid);
            String ids[] = this.getParaValues("ids[]");
            if (CommonUtils.isNotNullOrNotEmpty(ids)){
                for (String id : ids){
                    Record record = new Record();
                    record.set("modular_id",id);
                    record.set("org_id",orgid);
                    Db.save("t_modular_org","id",record);
                }
            }
            success();
        }catch(Exception e){
            e.printStackTrace();
            fail();
        }
    }
}
