package com.controller;

import com.config.BaseController;
import com.config.ControllerInterface;
import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.plugin.activerecord.Page;
import com.model.Org;
import com.model.OrgDept;
import com.service.OrgDeptService;
import com.service.OrgService;
import com.tools.JwtUtils;
import com.validator.OrgDeptValidator;
import com.validator.OrgValidator;

/**
 * 部门
 * 2018-10-17
 * zj
 */
@ControllerInterface(path="/orgdept")
public class OrgDeptController extends BaseController {

    OrgDeptService service = OrgDeptService.me;

    public void index() {
        render("/view/reader/readerdept.html");
    }
    public void importFace() {
        int id = getParaToInt("id");
        render("/view/reader/importFace.html");
    }

    //查询列表
    public void list(){
        try{
            OrgDept bean = getBean(OrgDept.class,"bean");
            success(service.list(bean));
        }catch(Exception e){
            e.printStackTrace();
            fail();
        }
    }
    public void getById(){
        int id =this.getParaToInt("bean.id", 0);
        if(id == 0){
            fail();
        }
        success(service.findById(id));
    }
    //增加或修改
    @Before(OrgDeptValidator.class)
    public void addOrUpdate(){
        OrgDept bean = getBean(OrgDept.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("bean.id");
        boolean b = service.isJunior(id);
        if(b){
            fail(0,"请确保该部门已无下级部门和读者");
            return;
        }else {
            service.deleteById(id);
            success();
        }
    }

    /**
     * 部门下拉选
     */
    public void deptList(){
        try{
            int orgid = this.getParaToInt("orgid");
            success(service.deptList(orgid));
        }catch(Exception e){
            e.printStackTrace();
            fail();
        }
    }

}
