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.sys.bus.hr.model.Person;
import com.sys.bus.jx.service.JxCycleService;
import com.sys.bus.model.Cycle;
import com.sys.core.model.PageBean;
import com.sys.core.model.Response;
import com.sys.core.utils.ConstUtil;
import com.sys.core.utils.Tool;


/**
 * 绩效周期管理控制器
 */
public class JxCycleController extends Controller {
    static JxCycleService jxCycleService = Enhancer.enhance(JxCycleService.class);

    @ActionKey("/jx/getJxCycleList")
    public void getJxCycleList() throws Exception{
        Cycle cycle = new Cycle();
        cycle.setCycleName(this.getPara("cycle_name"));
        cycle.setCycleType(this.getPara("cycle_type"));
        cycle.setCycleYear(this.getPara("cycle_year"));
        cycle.setCycleYeartype(this.getPara("cycle_yeartype"));
        cycle.setCycleQuarter(this.getPara("cycle_quarter"));

        int pageNumber = this.getParaToInt("page", 1);
        int pageSize = this.getParaToInt("limit", ConstUtil.PAGE_SIZE);
        PageBean pageBean = jxCycleService.getJxCyclePage(cycle,pageNumber,pageSize);
        this.renderJson(pageBean);
    }



    /**
     * 绩效周期列表
     * @throws Exception
     */
    @ActionKey("/jx/toJxCycleList")
    public void toJxCycleList() throws Exception{
        this.render("jxCycleList.html");
    }



    /**
     * 跳转到绩效周期创建页面
     */
    @ActionKey("/jx/toAddJxCycle")
    public void toAddJxCycle() {
        this.render("jxCycleAdd.html");
    }


    /**
     * 添加考核周期
     */
    @ActionKey("/jx/addJxCycle")
    public void addJxCycle() throws Exception {
        Person person = (Person)this.getSession().getAttribute("person");
        Response response = new Response();
        JSONObject jsonObj = Tool.getRequestObject(this.getRequest());
        Cycle cycle = JSON.toJavaObject(jsonObj, Cycle.class);
        String creatorid = person.getId();
        cycle.setCycleCreatorid(creatorid);
        jxCycleService.addJxCycle(cycle,response);
        renderJson(response);
    }
    /**
     * 删除考核周期
     */
    @ActionKey("/jx/deleteJxCycle")
    public void deleteJxCycle() throws Exception {
        Person person = (Person)this.getSession().getAttribute("person");
        String cycleId = this.getPara("cycleId");

        int r =  jxCycleService.deleteJxCycle(cycleId);
        JSONObject result = new JSONObject();
        result.put("code",r);
        renderJson(result);
    }



}
