package com.controller;

import com.config.BaseController;
import com.config.ControllerInterface;
import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.model.*;
import com.service.LibraryService;
import com.service.OrgService;
import com.tools.JwtUtils;
import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.BorderStyle;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 藏书室信息表
 * 2018-10-19
 * zj
 */
@ControllerInterface(path="/library")
public class LibraryController extends BaseController {
    LibraryService service = LibraryService.me;
    OrgService orgService = OrgService.me;
    public void index() {
        render("/view/sysconfig/library.html");
    }








    public void list(){
        String token = this.getPara("token");
        try {
            int operatorid = JwtUtils.validateJWTForUserId(token);
            if(operatorid == 0){
                fail(3,"token错误");
                return;
            }
            Library bean = getBean(Library.class,"bean");
            success(service.paginate(this.getParaToInt("pageNumber"),this.getParaToInt("pageSize"),bean));
        }catch(Exception e){
            e.printStackTrace();
            fail();
        }

    }
    public void getById(){
        int id =this.getParaToInt("bean.id", 0);
        if(id == 0){
            fail();
            return;
        }
        success(service.findById(id));
    }
    //增加或修改
    public void addOrUpdate(){
        Library bean = getBean(Library.class,"bean");
        String token = this.getPara("token");
        try {
            int operatorid = JwtUtils.validateJWTForUserId(token);
            if(operatorid == 0){
                fail(3,"token错误");
                return;
            }
            service.addOrUpdate(bean,operatorid);
            success();
        }catch(Exception e){
            e.printStackTrace();
            fail();
        }
    }
    //删除
    public void delete(){
        int id =this.getParaToInt("id");
        try{
            service.deleteById(id);
        }catch (Exception e){
            fail(0,e.getMessage());
            return;
        }
        success();
    }
    public void ajaxlibrarylist(){
        int orgid = ((User) this.getSessionAttr("userInfo")).getOrgid();
        success(service.librarylistforselect(orgid));
    }





    public void findLibraryListByLibraryType(){
        String token = this.getPara("token");
        int operatorid = JwtUtils.validateJWTForUserId(token);
        if(operatorid == 0){
            fail(3,"token错误");
            return;
        }
        Map userinfo = JwtUtils.validateJWTForMap(token);
        int orgid = (Integer) userinfo.get("orgid");
        Integer libraryType = getParaToInt("libraryType",0);
        List list = Db.find("select * from t_library where orgid = ? and libraryType = ?",orgid,libraryType);
        success(list);
    }
    public void librayclocklist(){
        String token = this.getPara("token");
        int operatorid = JwtUtils.validateJWTForUserId(token);
        if(operatorid == 0){
            fail(3,"token错误");
            return;
        }
        int libraryid = this.getParaToInt("libraryid");
        Page<Record> page = Db.paginate(getParaToInt("pageNumber",1),getParaToInt("pageSize", 10),
                "select a.createtime,b.cardnumber,b.realname,l.library ","from t_library_clock a " +
                        "inner join t_reader b on a.readerid = b.id " +
                        "inner join t_library l on l.id=a.libraryid where a.libraryid =? order by createtime desc",libraryid);
        success(page);
    }

    public void addlibrayclock(){
        String token = this.getPara("token");
        int operatorid = JwtUtils.validateJWTForUserId(token);
        if(operatorid == 0){
            fail(3,"token错误");
            return;
        }
        String cardno = this.getPara("cardno");
        int libraryid = getParaToInt("libraryid");
        Record reader = Db.findFirst("select * from t_reader where cardnumber=? or readcardcode=?", cardno,cardno);
        if(reader == null){
            fail(1,"读者不存在！");
            return;
        }
        LibraryClock lc = new LibraryClock();
        lc.setOrgid(reader.get("orgid")).setReaderid(reader.getInt("id")).setCreatetime(new Date()).setLibraryid(libraryid).save();
        success();
    }
}
