package com.sys.bus.hr.controller;

import com.jfinal.aop.Enhancer;
import com.jfinal.core.ActionKey;
import com.jfinal.core.Controller;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.PropKit;
import com.sys.bus.hr.model.Person;
import com.sys.bus.hr.service.HrService;
import com.sys.bus.jx.service.OrgService;
import com.sys.bus.model.Org;
import com.sys.bus.sys.model.DtBean;
import com.sys.bus.syscode.service.SysCodeService;
import com.sys.core.SecurityEncode;
import com.sys.core.utils.ConstUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

//import com.sys.bus.jx.service.BasePerformanceService;

public class HrController extends Controller {

    Log log = LogFactory.getLog(this.getClass());
//    static BasePerformanceService service = Enhancer.enhance(BasePerformanceService.class);
//    static PersonMonthPerformanceService pservice = Enhancer.enhance(PersonMhPerformanceService.class);
    static OrgService orgService = Enhancer.enhance(OrgService.class);
    static HrService hrService = Enhancer.enhance(HrService.class);//需要事务处理的service，必须使用增强方法
    static SysCodeService codeService = Enhancer.enhance(SysCodeService.class);
    static SecurityEncode s = new SecurityEncode();
    //static SysCodeService sysCodeService = new SysCodeService();
    public void index() throws Exception {
    	this.getSession().removeAttribute("jxjl");
    	this.getSession().removeAttribute("person");
    	this.getSession().removeAttribute("hrmanager");
    	this.getSession().removeAttribute("hrbz");
    	this.getSession().removeAttribute("isOrgKh");
    	this.getSession().removeAttribute("viewAuth");

        this.getSession().removeAttribute("jxManager_query");
        this.getSession().removeAttribute("jxAllEntry_query");
    	        
		this.setSessionAttr(ConstUtil.SESSION_USER, null);
		
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    	String pid = this.getPara("param1");//ID
    	//String pname = this.getPara("param2");//name
    	//String pnumber = this.getPara("param3");//number
    	String date = this.getPara("param4");//date
    	String param =  this.getPara("param");
    	Date d = null;
    	Person p = null;
    	String personId = "";
    	boolean devDebug = false;
		if(PropKit.get("devDebug")!=null &&PropKit.get("devDebug").equals("true")){
			devDebug = true;
		}

		if(devDebug){
			personId = this.getPara("pid");
			if(StringUtils.isNotBlank(personId) && personId.equals("jxAdmin")){
				personId = codeService.getSysCodeName("绩效管理员").getSyscodeValue();
			}
			if(StringUtils.isNotBlank(this.getPara("pname"))){
				Person pe = hrService.getPersonByName(this.getPara("pname"));
				if(pe!=null){
					personId = pe.getId();
				}
			}
			if(StringUtils.isNotBlank(this.getPara("pnumber"))){
				Person pe = hrService.getPersonByNumber(this.getPara("pnumber"));
				if(pe!=null){
					personId = pe.getId();
				}
			}
		}
		if(StringUtils.isNotBlank(personId)){
			pid = personId;
			p = hrService.getPersonById(pid);
		}else{
			if(StringUtils.isBlank(pid)||StringUtils.isBlank(date)){
				throw new RuntimeException("页面跳转参数错误！");
			}
			try {
				String keyStr = codeService.getSysCodeName("shr单点秘钥").getSyscodeValue();
				pid = s.decrypt(pid,keyStr);
				date=s.decrypt(date,keyStr);
				d = sdf.parse(date);
			} catch (Exception e) {
				throw new RuntimeException("页面地址解析错误！");
			}

			Calendar c = Calendar.getInstance();
			c.add(Calendar.MINUTE, -4);
			if(c.getTime().after(d)){
				throw new RuntimeException("页面地址已过期！请重新再试！");
			}
			c.add(Calendar.MINUTE, 8);
			if(c.getTime().before(d)){
				throw new RuntimeException("页面地址已过期！请重新再试！");
			}
			p = hrService.getPersonByUserId(pid);
		}

    	if(p==null){
    		throw new RuntimeException("页面打开异常！用户信息丢失！");
    	}
		this.getSession().setAttribute("person", p);
		this.setSessionAttr(ConstUtil.SESSION_USER, p.getId());

		//绩效经理需要查询授权  不是读配置

		List<Org> orgList = orgService.getOrgList(p.getId());
		if(orgList!=null&&orgList.size()>0){
			this.getSession().setAttribute("jxjl", true);
			this.getSession().setAttribute("orgList", orgList);
		}else{
			this.getSession().setAttribute("jxjl", false);
			this.getSession().setAttribute("orgList", null);
		}
		this.getSession().setAttribute("shrUrl",  codeService.getSysCodeName("金蝶shr登录路径").getSyscodeValue());
		this.getSession().setAttribute("jxUrl",  codeService.getSysCodeName("绩效登录url").getSyscodeValue());

		String admin = codeService.getSysCodeName("绩效管理员").getSyscodeValue();
		if(admin.equals(p.getId())){
			this.getSession().setAttribute("admin", true);
		}else{
			this.getSession().setAttribute("admin", false);
		}
		boolean menuShow = PropKit.getBoolean("menu", true);
		this.setSessionAttr("menuShow", menuShow);

    	this.redirect("/jxp/toMyToDoListPage");
    }
    @ActionKey("/getPerson")
    public void getPerson(){
    	String personId = this.getPara("personId");
    	Person p = hrService.getPersonById(personId);

	    DtBean dt = new DtBean();
	    dt.put("person", p);
	    dt.put("code", "success");
	    this.renderJson(JsonKit.toJson(dt));
    	  
    }

}
