package com.sys.bus.jx.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Enhancer;
import com.jfinal.core.ActionKey;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.sys.bus.hr.model.Person;
import com.sys.bus.jx.service.JxCommonService;
import com.sys.bus.jx.service.KpilibService;
import com.sys.bus.jx.service.OrgService;
import com.sys.bus.model.Kpilib;
import com.sys.core.model.DataBean;
import com.sys.core.model.PageBean;
import com.sys.core.model.Response;
import com.sys.core.utils.Tool;

import java.math.BigDecimal;
import java.util.List;


/**
 * 组织考核控制器
 */
public class KpilibController extends Controller {
    static KpilibService kpilibService = Enhancer.enhance(KpilibService.class);
    static OrgService orgService =  Enhancer.enhance(OrgService.class);
    static JxCommonService jxCommonService =  Enhancer.enhance(JxCommonService.class);


    @ActionKey("/jx/toKpilibList")
    public void toKpilibList(){
        this.getSession().removeAttribute("jxManager_query");
        this.getSession().removeAttribute("jxAllEntry_query");
        //this.setAttr("kpilibList",kpilibService.getKpilibList());
        this.render("kpilibList.html");
    }

    @ActionKey("/jx/kpilibList")
    public void kpilibList(){
        Person person = (Person) this.getSession().getAttribute("person");
        boolean isAdmin = (boolean)this.getSession().getAttribute("admin");
        PageBean pageBean = new PageBean();
        List<Kpilib> list = kpilibService.getKpilibList(person.getId(),isAdmin);
        pageBean.setData(list);
        pageBean.setCount(list.size());
        this.renderJson(pageBean);
    }

    @ActionKey("/jx/getKpilibListNoField")
    public void getKpilibListNoField(){
        String entryid = this.getPara("entryid");
        String type = this.getPara("type");
        String kpiName = this.getPara("kpiName");

        String orgId = jxCommonService.getJxentryById(entryid).getEntryOrgid();
        Integer pageNumber = this.getParaToInt("page");
        Integer pageSize = this.getParaToInt("limit");
        Page<Kpilib> list = kpilibService.getKpilibListNoField(orgId,type,kpiName,pageSize,pageNumber);
        DataBean dBean = new DataBean();
        dBean.setCode(0);
        dBean.setMsg("Ok");
        dBean.setData(list.getList());
        dBean.setCount(list.getTotalRow());
        this.renderJson(dBean);
    }

    @ActionKey("/jx/toAddKpilib")
    public void toAddKpilib(){
        Person person = (Person) this.getSession().getAttribute("person");
        this.setAttr("orgTypeList", orgService.getAllOrgType());
        this.setAttr("orgList",orgService.getEnableOrgList(person.getId()));
        this.render("kpilibAdd.html");
    }
    @ActionKey("/jx/toUpdateKpilib")
    public void toUpdateKpilib(){
        String kpilibId = this.getPara("kpilibId");
        Person person = (Person) this.getSession().getAttribute("person");
        this.setAttr("orgList",orgService.getEnableOrgList(person.getId()));
        this.setAttr("orgTypeList", orgService.getAllOrgType());
        this.setAttr("kpilib",Kpilib.dao.findById(kpilibId));
        this.render("kpilibAdd.html");
    }
    @ActionKey("/jx/toGetKpilib")
    public void toGetKpilib(){
        String kpilibId = this.getPara("kpilibId");
        Person person = (Person) this.getSession().getAttribute("person");
        this.setAttr("orgList",orgService.getEnableOrgList(person.getId()));
        this.setAttr("kpilib",Kpilib.dao.findById(kpilibId));
        this.setAttr("view",true);
        this.render("kpilibAdd.html");
    }

    @ActionKey("/jx/saveKpilib")
    public void saveKpilib()throws Exception {
        Person person = (Person) this.getSession().getAttribute("person");
        JSONObject jsonObj = Tool.getRequestObject(this.getRequest());
        Kpilib kpilib = JSON.toJavaObject(jsonObj, Kpilib.class);

        JSONObject result = new JSONObject();
        result.put("code",0);
        String id = null;
        try{
            id = kpilibService.saveKpilib(kpilib,person);
        }catch (Exception e){
            result.put("code",-1);
            result.put("msg",e.getMessage());
        }

        result.put("id",id);

        this.renderJson(result);
    }

//
    @ActionKey("/jx/disableKpilib")
    public void disableKpilib(){
        Person person = (Person) this.getSession().getAttribute("person");
        String kpilibId = this.getPara("kpilibId");
        int r = kpilibService.saveKpilib(false,kpilibId,person);
        JSONObject result = new JSONObject();
        result.put("code",r);
        this.renderJson(result);
    }
    @ActionKey("/jx/enableKpilib")
    public void enableKpilib(){
        Person person = (Person) this.getSession().getAttribute("person");
        String kpilibId = this.getPara("kpilibId");
        int r = kpilibService.saveKpilib(true,kpilibId,person);
        JSONObject result = new JSONObject();
        result.put("code",r);
        this.renderJson(result);
    }
    @ActionKey("/jx/deleteKpilib")
    public void deleteKpilib(){
        Person person = (Person) this.getSession().getAttribute("person");
        boolean isAdmin = (boolean)this.getSession().getAttribute("admin");
        String kpilibId = this.getPara("kpilibId");
        int r = kpilibService.deleteKpilib(kpilibId,person,isAdmin);
        JSONObject result = new JSONObject();
        result.put("code",r);
        String msg = null;
        if(r==1)msg="已被考核引用！";
        if(r==2)msg="没有权限删除！";
        if(r==3)msg="未找到数据，请刷新页面后重试！";
        result.put("msg",msg);
        this.renderJson(result);
    }


}
