package com.controller;

import com.config.BaseController;
import com.config.ControllerInterface;
import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.ext.interceptor.POST;
import com.jfinal.plugin.activerecord.Db;
import com.model.BookDetail;
import com.model.BookPlus;
import com.model.Bookpici;
import com.service.BookPiciService;
import com.tools.JwtUtils;
import com.validator.BookValidator;

import java.util.Date;

/**
 * 图书批次
 * 2018-10-23
 * zj
 */
@ControllerInterface(path="/bookPici")
public class BookPiciController extends BaseController {
    BookPiciService service = BookPiciService.me;


    public void list(){
        int orgid = this.getParaToInt("orgid",0);
        if(orgid == 0) {
            fail(1,"请求参数不完整!");
            return;
        }
        success(Db.find("SELECT * from t_bookpici where orgid = ? ORDER BY createtime desc",orgid));
    }

    public void picilist(){
        int orgid = this.getParaToInt("orgid",0);
        String batchname = this.getPara("batchname");
        String realname = this.getPara("realname");
        if(orgid == 0) {
            fail(1,"请求参数不完整!");
            return;
        }
        success(service.paginate(this.getParaToInt("pageNumber"),this.getParaToInt("pageSize"),orgid,batchname,realname));
    }

    //增加

    public void addOrUpdate(){
        //必填信息
        Bookpici bean = getBean(Bookpici.class,"bean");
        String token = this.getPara("token");
        int orgid = this.getParaToInt("bean.orgid");
        try {
            int operatorid = JwtUtils.validateJWTForUserId(token);
            if(operatorid == 0){
                fail(3,"token错误");
                return;
            }
            service.addOrUpdate(bean,operatorid,orgid);
            success();
        }catch(Exception e){
            e.printStackTrace();
            fail();
        }
    }
    public void delete(){
        Bookpici bean = getBean(Bookpici.class,"bean");
        try {
            if(service.deleteById(bean)){
                success(200,"操作成功");
            }else {
                success(4,"该批次存在书籍，不能删除");
            }

        }catch(Exception e){
            e.printStackTrace();
            fail();
        }
    }

    /**
     * 根据机构和分类号获取最大书次号
     */
    public void findMaxShucihao(){
        int orgid = this.getParaToInt("orgid",0);
        String typecode = this.getPara("typecode");
        if(orgid == 0) {
            fail(1,"请求参数不完整!");
            return;
        }
        Long maxshucihao = service.findMaxShucihao(orgid,typecode);
        success(maxshucihao);
    }

    /**
     * 根据类型 查询书次号集合
     */
    public void findShucihaoList() {
        int orgid = this.getParaToInt("orgid",0);
        String typecode = this.getPara("typecode");
        if(orgid == 0) {
            fail(1,"请求参数不完整!");
            return;
        }
        success(service.findShucihaoListByTypeCode(orgid,typecode));
    }

}
