package com.sys.bus.jx.controller;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Enhancer;
import com.jfinal.core.ActionKey;
import com.jfinal.core.Controller;
import com.sys.bus.hr.model.Person;
import com.sys.bus.jx.service.JxCycleService;
import com.sys.bus.jx.service.OrgService;
import com.sys.bus.jx.service.UpdateRatingPermissionService;
import com.sys.bus.model.Cycle;
import com.sys.bus.model.Org;
import com.sys.bus.model.OrgPerson;
import com.sys.bus.model.UpdateRatingPermission;
import com.sys.core.model.PageBean;
import com.sys.core.utils.ConstUtil;
import com.sys.core.utils.SessionUtil;

import java.util.List;


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

    static JxCycleService jxCycleService = Enhancer.enhance(JxCycleService.class);

    static UpdateRatingPermissionService updateRatingPermissionService = Enhancer.enhance(UpdateRatingPermissionService.class);

    @ActionKey("/jx/orgSetting")
    public void toOrgList(){

        //this.setAttr("orgList",orgService.getOrgList());
        this.render("orgList.html");
    }
    @ActionKey("/jx/orgPersonSetting")
    public void toOrgPersonList(){
        List<Org> enableOrgList = orgService.getOrgList();
        this.setAttr("enableOrgList",enableOrgList);
        //this.setAttr("orgList",orgService.getOrgList());
        this.render("orgPersonList.html");
    }

    @ActionKey("/jx/updateRatingPermission")
    public void updateRatingPermission(){
        List<Cycle> jxCycleList = jxCycleService.getJxCycleList();
        this.setAttr("jxCycleList", jxCycleList);
        //this.setAttr("orgList",orgService.getOrgList());
        this.render("updateRatingPermission.html");
    }

    @ActionKey("/jx/updateRatingPermissionList")
    public void updateRatingPermissionList(){
        String cycleId = this.getPara("cycleId");
        int pageNumber = this.getParaToInt("page", 1);
        int pageSize = this.getParaToInt("limit", ConstUtil.PAGE_SIZE);

        // PageBean pageBean = performanceService.getJxEntryQueryList(SessionUtil.getCurrentPerson(), pageNumber, pageSize,cycleId,khOrg,personName);
//        PageBean pageBean = new PageBean();
//        List<OrgPerson> list = orgService.getOrgPersonList(khOrg,personName);
//        pageBean.setData(list);
//        pageBean.setCount(list.size());
//        this.renderJson(pageBean);
        PageBean orgPersonList = orgService.getUpdateRatingPermissionList(cycleId, pageNumber, pageSize);
        this.renderJson(orgPersonList);
    }



    @ActionKey("/jx/orgList")
    public void orgList(){
        PageBean pageBean = new PageBean();
        List<Org> list = orgService.getOrgList();
        pageBean.setData(list);
        pageBean.setCount(list.size());
        this.renderJson(pageBean);
    }
    @ActionKey("/jx/orgPersonList")
    public void orgPersonList(){
        String khOrg = this.getPara("khOrg");
        String personName = this.getPara("personName");
        int pageNumber = this.getParaToInt("page", 1);
        int pageSize = this.getParaToInt("limit", ConstUtil.PAGE_SIZE);

        // PageBean pageBean = performanceService.getJxEntryQueryList(SessionUtil.getCurrentPerson(), pageNumber, pageSize,cycleId,khOrg,personName);
//        PageBean pageBean = new PageBean();
//        List<OrgPerson> list = orgService.getOrgPersonList(khOrg,personName);
//        pageBean.setData(list);
//        pageBean.setCount(list.size());
//        this.renderJson(pageBean);
        this.renderJson(orgService.getOrgPersonList(khOrg,personName,pageNumber,pageSize));
    }
    @ActionKey("/jx/toAddOrg")
    public void toAddOrg(){

        //this.setAttr("orgList",orgService.getOrgList());
        this.setAttr("orgTypeList",orgService.getAllOrgType());
        this.render("orgAdd.html");
    }
    @ActionKey("/jx/toUpdateOrg")
    public void toUpdateOrg(){
        String orgId = this.getPara("orgId");
        String sql = " select  o.*,a.fname_l2,a.fdisplayname_l2 from ct_jx_org o " +
                " left join t_org_admin a on o.org_id  = a.fid  " +
                "  where o.org_id =? ";
        List<Org> list = Org.dao.find(sql,orgId);
        this.setAttr("org",list.get(0));
        this.setAttr("orgTypeList",orgService.getAllOrgType());
        this.render("orgAdd.html");
    }

    @ActionKey("/jx/toAddOrgPerson")
    public void toAddOrgPerson(){

        this.setAttr("orgList",orgService.getOrgList());
        this.render("orgPersonAdd.html");
    }

    @ActionKey("/jx/toAddPermission")
    public void toAddPermission(){
        List<Cycle> jxCycleList = jxCycleService.getJxCycleList();
        this.setAttr("jxCycleList", jxCycleList);
        this.render("addPermission.html");
    }

    @ActionKey("/jx/toChangeOrgPerson")
    public void toChangeOrgPerson(){
        this.render("orgPersonChange.html");
    }
    @ActionKey("/jx/saveOrg")
    public void saveOrg(){
        Person person = (Person) this.getSession().getAttribute("person");
        String orgId = this.getPara("orgId");
        String orgType = this.getPara("orgType");
        String orgName = this.getPara("orgName");
        boolean update = this.getParaToBoolean("update");
        int r = orgService.saveOrg(orgId,orgType,orgName,update,person.getId());
        JSONObject result = new JSONObject();
        result.put("code",r);
        this.renderJson(result);
    }
    @ActionKey("/jx/saveOrgPerson")
    public void saveOrgPerson(){
        Person person = (Person) this.getSession().getAttribute("person");
        String orgId = this.getPara("orgId");
        String personId = this.getPara("personId");
        int r = orgService.saveOrgPerson(orgId,personId,person.getId());
        JSONObject result = new JSONObject();
        result.put("code",r);
        this.renderJson(result);
    }
    @ActionKey("/jx/savePermission")
    public void savePermission(){
//        Person person = (Person) this.getSession().getAttribute("person");
        String jxCycleId = this.getPara("jxCycleId");
        String orgId = this.getPara("orgId");
        String isSubOrg = this.getPara("isSubOrg");
        int r = updateRatingPermissionService.addPermission(jxCycleId, orgId, isSubOrg);
        //int r = orgService.saveOrgPerson(orgId,personId,person.getId());
        JSONObject result = new JSONObject();
        result.put("code",r);
        this.renderJson(result);
    }
    @ActionKey("/jx/changePermission")
    public void changePermission(){
        Person person = (Person) this.getSession().getAttribute("person");
        String newPersonId = this.getPara("newPersonId");
        String personId = this.getPara("personId");
        JSONObject result = new JSONObject();
        result.put("code",0);
        try{
            orgService.changePermission(newPersonId,personId,person.getId());
        }catch (Exception e){
            result.put("code",-1);
            result.put("msg",e.getMessage());
        }
        this.renderJson(result);
    }
    @ActionKey("/jx/copyPermission")
    public void copyPermission(){
        Person person = (Person) this.getSession().getAttribute("person");
        String newPersonId = this.getPara("newPersonId");
        String personId = this.getPara("personId");
        JSONObject result = new JSONObject();
        result.put("code",0);
        try{
            orgService.copyPermission(newPersonId,personId,person.getId());
        }catch (Exception e){
            result.put("code",-1);
            result.put("msg",e.getMessage());
        }
        this.renderJson(result);
    }
    @ActionKey("/jx/clearPermission")
    public void clearPermission(){
        Person person = (Person) this.getSession().getAttribute("person");
        String personId = this.getPara("personId");
        JSONObject result = new JSONObject();
        result.put("code",0);
        try{
            orgService.clearPermission(personId,person.getId());
        }catch (Exception e){
            result.put("code",-1);
            result.put("msg",e.getMessage());
        }
        this.renderJson(result);
    }
//
    @ActionKey("/jx/disableOrg")
    public void disableOrg(){
        //Person person = (Person) this.getSession().getAttribute("person");
        String orgId = this.getPara("orgId");
        int r = orgService.disableOrg(orgId);
        JSONObject result = new JSONObject();
        result.put("code",r);
        this.renderJson(result);
    }
    @ActionKey("/jx/enableOrg")
    public void enableOrg(){
        //Person person = (Person) this.getSession().getAttribute("person");
        String orgId = this.getPara("orgId");
        int r = orgService.enableOrg(orgId);
        JSONObject result = new JSONObject();
        result.put("code",r);
        this.renderJson(result);
    }
    @ActionKey("/jx/deleteOrg")
    public void deleteOrg(){
        Person person = (Person) this.getSession().getAttribute("person");
        String orgId = this.getPara("orgId");
        int r = orgService.deleteOrg(orgId);
        JSONObject result = new JSONObject();
        result.put("code",r);
        this.renderJson(result);
    }
    @ActionKey("/jx/deleteOrgPerson")
    public void deleteOrgPerson(){
        Person person = (Person) this.getSession().getAttribute("person");
        String orgPersonId = this.getPara("orgPersonId");
        int r = orgService.deleteOrgPerson(orgPersonId);
        JSONObject result = new JSONObject();
        result.put("code",r);
        this.renderJson(result);
    }

    @ActionKey("/jx/deletePermission")
    public void deletePermission(){
        String permissionid = this.getPara("permissionid");
        boolean b = UpdateRatingPermission.dao.deleteById(permissionid);
        JSONObject result = new JSONObject();
        result.put("code",b?0:1);
        this.renderJson(result);
    }




}
