package com.sys.bus.syscode.controller;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.sys.bus.hr.model.Person;
import com.sys.bus.hr.service.HrService;
import com.sys.bus.model.SysCode;
import com.sys.bus.syscode.service.SysCodeService;
import com.sys.core.model.DataBean;
import com.sys.core.model.PageBean;
import com.sys.core.utils.ConstUtil;

import java.util.List;
public class SysCodeController extends Controller{
	public static SysCodeService service = new SysCodeService();
	public static HrService hrService = new HrService();
	/**
	 * 参数设置页面
	 */
	public void toParamSetting(){
		this.render("paramSetting.html");
	}
	
	/**
	 * 获取所有参数的列表
	 */
	public void parmList(){
	    int pageNumber = this.getParaToInt("page", 1);
        int pageSize = this.getParaToInt("limit", ConstUtil.PAGE_SIZE);
        PageBean pageBean = new PageBean();
        Page<SysCode> paramList = service.getParamList(pageNumber, pageSize);
        
        List<SysCode> list = paramList.getList();
        for(SysCode jsc:list){
        	if(jsc.getSyscodeIsPerson().equals("1")){
        		//根据id查询用户名
        		Person person = hrService.getPersonById(jsc.getSyscodeValue());
        		if(person != null){
        			jsc.setSyscodeValue(person.getName());
        		}
        	}else if(jsc.getSyscodeIsPerson().equals("9")){
        		String val = jsc.getSyscodeValue();
        		if(val != null){
        			String[] split = val.split(",");
            		String names = "";
            		for(String v:split){
            			Person person = hrService.getPersonById(v);
                		if(person != null){
                			names = names +","+ person.getName();
                		}
            		}
            		if(!names.equals("")){
            			names = names.substring(1, names.length());
            		}
            		
            		jsc.setSyscodeValue(names);
            		jsc.put("personIds", val);
        		}else{
        			jsc.setSyscodeValue("");
        			jsc.put("personIds", "");
        		}
        	
        	}else if(jsc.getSyscodeIsPerson().equals("2")){

			}
        }
        pageBean.setCount(paramList.getTotalRow());
        pageBean.setData(paramList.getList());
        this.renderJson(pageBean); 
        
	}
	/**
	 * 根据id更新参数的值
	 */
	public void updateParam(){
		String id = this.getPara("id");
		String value = this.getPara("value");
		service.updateParamValueById(id, value);
		DataBean dBean = new DataBean();
		dBean.setCode(0);
		dBean.setMsg("操作成功");
		this.renderJson("data",dBean);
	}
	
	public void selectUserUi(){
		String id = this.getPara("id");
		this.setSessionAttr("id", id);
		this.renderJsp("/pages/common/selectuser.jsp");
	}
//
//	public void selectOrgUi(){
//		this.renderJsp("/pages/user/selectorg.jsp");
//	}
	
	
}

	