package com.sys.bus.hr.service;

import com.jfinal.plugin.activerecord.SqlPara;
import com.sys.bus.hr.model.Dept;
import com.sys.bus.hr.model.Person;
import com.sys.bus.model.SysCode;
import com.sys.bus.sys.model.DtBean;
import com.sys.bus.syscode.service.SysCodeService;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 获取人员组织相关信息
 * @author liw109370
 *
 */
public class HrService{
	static SysCodeService sysCodeService = new SysCodeService();


	 /**
	  * 获取Dept map集合  方便找数据
	  * @return  deptId>dept
	  */
	 public Map<String,Dept> getAllEnableDeptMap(){
		List<Dept> list =  Dept.dao.find("SELECT fid id,fname_l2 name,fdisplayname_l2 longname,fparentid parentId,flevel FROM  t_org_admin a WHERE a.FISSEALUP = 0 order by flevel ");
		 Map<String,Dept> map = new HashMap<String,Dept>();
		 for(Dept dept:list){
			map.put(dept.getId(), dept);
		 }
		 return map;
	 }
	 public List<Dept> getAllEnableDept(){
		 return Dept.dao.find("SELECT fid id,fname_l2 name,fdisplayname_l2 longname,fparentid parentId,flevel FROM  t_org_admin a WHERE a.FISSEALUP = 0 order by flevel ");
		 
	 }

	 public Dept getDeptById(String deptId){
		 return Dept.dao.findFirst("SELECT fid id,fname_l2 name,fdisplayname_l2 longname,fparentid parentId,flevel,fsortcode sortcode FROM  t_org_admin a WHERE fid = ? ",new Object[]{deptId});
		 
	 }

	 /**
	  * 根据工号获取person
	  */
	 public Person getPersonByNumber(String number){
		 String sql = "SELECT p.fid id,p.fname_l2||'_'||p.fnumber name,a.Fid deptid,a.FNAME_L2 deptname,p.FEMAIL email,p.fcell mobile, "
				 +" po.FID positionid,po.FNAME_L2 positionname,pp.FISPRIMARY isprimary FROM t_bd_person p  "
				 +" LEFT JOIN T_ORG_PositionMember pp ON p.fid = pp.FPERSONID "
				 +" LEFT JOIN T_ORG_Position po ON po.fid = pp.FPOSITIONID "
				 +" LEFT JOIN t_org_admin a ON a.FID = po.FADMINORGUNITID "
				 +" left join  T_HR_BDEmployeeType et on  et.fid = p.femployeetypeid  "
				 +" WHERE  p.fnumber='"+number+"' and pp.FISPRIMARY=1";
		 return Person.dao.findFirst(sql);
	 }
	 
	 /**
	  * 根据工号和姓名获取person
	  */
	 public Person getPersonByNumberAndName(String number,String username){

		 String sql = "SELECT p.fid id,p.fname_l2||'_'||p.fnumber name,a.Fid deptid,a.FNAME_L2 deptname,a.fdisplayname_l2 cname,p.FEMAIL email,p.fcell mobile, "
				 +" po.FID positionid,po.FNAME_L2 positionname,pp.FISPRIMARY isprimary FROM t_bd_person p  "
				 +" LEFT JOIN T_ORG_PositionMember pp ON p.fid = pp.FPERSONID "
				 +" LEFT JOIN T_ORG_Position po ON po.fid = pp.FPOSITIONID "
				 +" LEFT JOIN t_org_admin a ON a.FID = po.FADMINORGUNITID "
				 +" left join  T_HR_BDEmployeeType et on  et.fid = p.femployeetypeid  "
				 +" WHERE  p.fnumber='"+number+"' and p.fname_l2='"+username+"' and pp.FISPRIMARY=1";
		 return Person.dao.findFirst(sql);

	 }
	 
	 /**
	  * 根据id获取员工主职位关联的各信息
	  * @param personId
	  * @return
	  */
	 public Person getPersonById(String personId){

		 String sql = "SELECT p.fid id,p.fname_l2||'_'||p.fnumber name,a.Fid deptid,a.FNAME_L2 deptname,a.fdisplayname_l2 cname,p.FEMAIL email,p.fcell mobile, "
				 +" po.FID positionid,po.FNAME_L2 positionname,pp.FISPRIMARY isprimary FROM t_bd_person p  "
				 +" LEFT JOIN T_ORG_PositionMember pp ON p.fid = pp.FPERSONID "
				 +" LEFT JOIN T_ORG_Position po ON po.fid = pp.FPOSITIONID "
				 +" LEFT JOIN t_org_admin a ON a.FID = po.FADMINORGUNITID "
				 +" left join  T_HR_BDEmployeeType et on  et.fid = p.femployeetypeid  "
				 +" WHERE  p.fid='"+personId+"' and pp.FISPRIMARY=1";
		 Person p = Person.dao.findFirst(sql);
		 if(p==null){
			 sql = "SELECT p.fid id,p.fname_l2||'_'||p.fnumber name,a.Fid deptid,a.FNAME_L2 deptname,a.fdisplayname_l2 cname,p.FEMAIL email,p.fcell mobile, "
					 +" po.FID positionid,po.FNAME_L2 positionname,pp.FISPRIMARY isprimary FROM t_bd_person p  "
					 +" LEFT JOIN T_ORG_PositionMember pp ON p.fid = pp.FPERSONID "
					 +" LEFT JOIN T_ORG_Position po ON po.fid = pp.FPOSITIONID "
					 +" LEFT JOIN t_org_admin a ON a.FID = po.FADMINORGUNITID "
					 +" left join  T_HR_BDEmployeeType et on  et.fid = p.femployeetypeid  "
					 +" WHERE  p.fid='"+personId+"'";
			 p = Person.dao.findFirst(sql);
		 }
		 return p; 
	 }
	public Person getEnablePersonById(String personId){

		String sql = "SELECT p.fid id,p.fname_l2||'_'||p.fnumber name,a.Fid deptid,a.FNAME_L2 deptname,a.fdisplayname_l2 cname,p.FEMAIL email,p.fcell mobile, "
				+" po.FID positionid,po.FNAME_L2 positionname,pp.FISPRIMARY isprimary FROM t_bd_person p  "
				+" LEFT JOIN T_ORG_PositionMember pp ON p.fid = pp.FPERSONID "
				+" LEFT JOIN T_ORG_Position po ON po.fid = pp.FPOSITIONID "
				+" LEFT JOIN t_org_admin a ON a.FID = po.FADMINORGUNITID "
				+" left join  T_HR_BDEmployeeType et on  et.fid = p.femployeetypeid  "
				+" WHERE  p.fid='"+personId+"' and pp.FISPRIMARY=1";
		return  Person.dao.findFirst(sql);
	}
	public Person getEnableDfSuperPersonById(String personId){

		String sql = "SELECT p.fid id,p.fname_l2||'_'||p.fnumber name,a.Fid deptid,a.FNAME_L2 deptname,a.fdisplayname_l2 cname,p.FEMAIL email,p.fcell mobile, "
				+" po.FID positionid,po.FNAME_L2 positionname,pp.FISPRIMARY isprimary FROM t_bd_person p  "
				+" LEFT JOIN T_ORG_PositionMember pp ON p.fid = pp.FPERSONID "
				+" LEFT JOIN T_ORG_Position po ON po.fid = pp.FPOSITIONID "
				+" LEFT JOIN t_org_admin a ON a.FID = po.FADMINORGUNITID "
				+" left join  T_HR_BDEmployeeType et on  et.fid = p.femployeetypeid "
				+" left join  t_bd_person df on  df.cfleaderid = p.fid "
				+" WHERE  df.fid='"+personId+"' and pp.FISPRIMARY=1";
		return  Person.dao.findFirst(sql);
	}
	public boolean isDfSuper(String personId,String superId){
	 	String sql = "select cfleaderid from t_bd_person where fid=? ";
		Person person = Person.dao.findFirst(sql, personId);
		String cfleaderid = person.getStr("cfleaderid");
		if(superId!= null && superId.equals(cfleaderid)){
			return true;
		}
		return false;
	}

	 /**
	  * 多职位 多条
	  * @param personId
	  * @return
	  */
	 public List<Person> getPersonById2(String personId){

		 String sql = "SELECT p.fid id,p.fname_l2 name,a.Fid deptid,a.FNAME_L2 deptname,a.fdisplayname_l2 cname,p.FEMAIL email,p.fcell mobile, "
				 +" po.FID positionid,po.FNAME_L2 positionname,pp.FISPRIMARY isprimary FROM t_bd_person p  "
				 +" LEFT JOIN T_ORG_PositionMember pp ON p.fid = pp.FPERSONID "
				 +" LEFT JOIN T_ORG_Position po ON po.fid = pp.FPOSITIONID "
				 +" LEFT JOIN t_org_admin a ON a.FID = po.FADMINORGUNITID "
				 +" left join  T_HR_BDEmployeeType et on  et.fid = p.femployeetypeid  "
				 +" WHERE  p.fid='"+personId+"' ";
		 return Person.dao.find(sql);

	 }
	 /**
	  * 根据userid获取员工主职位关联的各信息
	  * @param userId
	  * @return
	  */
	 public Person getPersonByUserId(String userId){

		 String sql = "SELECT p.fid id,p.fname_l2 name,a.Fid deptid,a.FNAME_L2 deptname,p.FEMAIL email,p.fcell mobile, "
				 +" po.FID positionid,po.FNAME_L2 positionname,pp.FISPRIMARY isprimary FROM t_pm_user u"
				 +" LEFT JOIN t_bd_person p on p.fid = u.fpersonid "
				 +" LEFT JOIN T_ORG_PositionMember pp ON p.fid = pp.FPERSONID "
				 +" LEFT JOIN T_ORG_Position po ON po.fid = pp.FPOSITIONID "
				 +" LEFT JOIN t_org_admin a ON a.FID = po.FADMINORGUNITID "
				 +" left join  T_HR_BDEmployeeType et on  et.fid = p.femployeetypeid  "
				 +" WHERE  u.fid='"+userId+"' and pp.FISPRIMARY=1";

		 return Person.dao.findFirst(sql); 
	 }
	 /**
	  * 获取所有员工   包括离职员工  多职位的会返回多条信息
	  * @return
	  */
	 public List<Person> getAllPerson(){
		 String sql = "SELECT p.fid id,p.fname_l2 name,a.Fid deptid,a.FNAME_L2 deptname,p.FEMAIL email,p.fcell mobile, "
		+" po.FID positionid,po.FNAME_L2 positionname,pp.FISPRIMARY isprimary FROM t_bd_person p  "
		+" LEFT JOIN T_ORG_PositionMember pp ON p.fid = pp.FPERSONID "
		+" LEFT JOIN T_ORG_Position po ON po.fid = pp.FPOSITIONID "
		+" LEFT JOIN t_org_admin a ON a.FID = po.FADMINORGUNITID "
		+" left join  T_HR_BDEmployeeType et on  et.fid = p.femployeetypeid  order by a.fdisplayname_l2";
		 return Person.dao.find(sql); 
	 }
	 /**
     * 获取有职位员工  短名称
     */
    public List<Person> getAllEnablePerson2(){
        String sql = "SELECT p.fid id,p.fname_l2 name,p.fnumber fnumber,a.Fid deptid,a.FNAME_L2 deptname,p.FEMAIL email,p.fcell mobile ," +
                " po.FID positionid,po.FNAME_L2 positionname  from t_bd_person p " +
                " LEFT JOIN T_ORG_PositionMember pp ON p.fid = pp.FPERSONID " +
                " LEFT JOIN T_ORG_Position po ON po.fid = pp.FPOSITIONID " +
                " LEFT JOIN t_org_admin a ON a.FID = po.FADMINORGUNITID " +
                " left join  T_HR_BDEmployeeType et on  et.fid = p.femployeetypeid  " +
                " WHERE  pp.FISPRIMARY=1  ";
        SqlPara sqlpara = new SqlPara();

        sqlpara.setSql(sql+"  order by a.fdisplayname_l2");
        return Person.dao.find(sqlpara);
    }
    /**
     * 获取有职位员工
     */
    public List<Person> getAllEnablePerson(){
        String sql = "SELECT p.fid id,p.fname_l2||'_'||p.fnumber name,p.fnumber fnumber,a.Fid deptid,a.FNAME_L2 deptname,p.FEMAIL email,p.fcell mobile ," +
                " po.FID positionid,po.FNAME_L2 positionname  from t_bd_person p " +
                " LEFT JOIN T_ORG_PositionMember pp ON p.fid = pp.FPERSONID " +
                " LEFT JOIN T_ORG_Position po ON po.fid = pp.FPOSITIONID " +
                " LEFT JOIN t_org_admin a ON a.FID = po.FADMINORGUNITID " +
                " left join  T_HR_BDEmployeeType et on  et.fid = p.femployeetypeid  " +
                " WHERE  pp.FISPRIMARY=1  ";
        SqlPara sqlpara = new SqlPara();

        sqlpara.setSql(sql+"  order by a.fdisplayname_l2");
        return Person.dao.find(sqlpara);
    }

	 /**
	  * 根据职位获取员工的上级 
	  * @return
	  */
	 public Person getSuperPersonByPositionId(String positionId){
		 String sql ="SELECT fparentid FROM T_ORG_Position where fid =?";
		 DtBean bean = new DtBean().dao().findFirst(sql,new Object[]{positionId});
		 String supPosiontId = bean.getStr("fparentid");
		 for(int i=0;i<20;i++){
			 sql = "SELECT p.fid id, p.fname_l2||'_'||p.fnumber name ,a.Fid deptid,a.FNAME_L2 deptname,p.FEMAIL email,p.fcell mobile, "
					 +" po.FID positionid,po.fparentid,po.FNAME_L2 positionname,pp.FISPRIMARY isprimary FROM t_bd_person p  "
					 +" LEFT JOIN T_ORG_PositionMember pp ON p.fid = pp.FPERSONID "
					 +" LEFT JOIN T_ORG_Position po ON po.fid = pp.FPOSITIONID "
					 +" LEFT JOIN t_org_admin a ON a.FID = po.FADMINORGUNITID "
					 +" left join  T_HR_BDEmployeeType et on  et.fid = p.femployeetypeid  "
					 +" WHERE  po.fid=? ";
			Person person =  Person.dao.findFirst(sql,new Object[]{supPosiontId}); 

			if(person!=null && !StringUtils.isEmpty(person.getName())){
				return person;
			}
			 sql ="SELECT fparentid FROM T_ORG_Position where fid =?";
			 bean = new DtBean().dao().findFirst(sql,new Object[]{supPosiontId});
			 supPosiontId = bean.getStr("fparentid");
		 }
		return null;
	 }

		/**
		  * 根据职位获取所有下级职位
		  *  返回到plist中
		  * @return
		  */
		 public void getLowerPositionByPositionId(List<String> plist,String positionId){
			
			 String sql ="SELECT fid FROM T_ORG_Position where fparentid =?";
			 List<DtBean> list = new DtBean().dao().find(sql,new Object[]{positionId});
			 if(list==null||list.size()==0)return;
			 StringBuilder sb = new StringBuilder("'15628'");
			 SqlPara sp = new SqlPara();
			 for(DtBean b:list){
				 sb.append(",?");
				 sp.addPara(b.getStr("fid"));
				 plist.add(b.getStr("fid"));
			 }
//			 sql = "SELECT p.fid id FROM t_bd_person p  "
//					 +" LEFT JOIN T_ORG_PositionMember pp ON p.fid = pp.FPERSONID "
//					 +" LEFT JOIN T_ORG_Position po ON po.fid = pp.FPOSITIONID "
//					 +" WHERE  po.fid in( "+sb.toString()+")";
//			 sp.setSql(sql);
//			 List<DtBean> personlist = new DtBean().dao().find(sp);
//			 for(DtBean b:personlist){//获取当前所有下级职位工
//				 plist.add(b.getStr("id"));
//			 }
			 for(DtBean b:list){
				 getLowerPositionByPositionId(plist,b.getStr("fid"));//继续获取下一层
			 }
		 }
		 /**
		  * 根据id获取员工多个职位
		  * @param personId
		  * @return
		  */
		 public List<String> getPositionByPId(String personId){

			 String sql = "SELECT  po.FID positionid FROM t_bd_person p  "
					 +" LEFT JOIN T_ORG_PositionMember pp ON p.fid = pp.FPERSONID "
					 +" LEFT JOIN T_ORG_Position po ON po.fid = pp.FPOSITIONID "
					 +" LEFT JOIN t_org_admin a ON a.FID = po.FADMINORGUNITID "
					 +" left join  T_HR_BDEmployeeType et on  et.fid = p.femployeetypeid  "
					 +" WHERE  p.fid=? ";
			 List<Person> plist =  Person.dao.find(sql,personId);
			 List<String> list = new ArrayList<String>();
			 for(Person p:plist){
				 list.add(p.getPositionId());
			 }
			 return list; 
		 }
		 /**
		  * 调试用。。。。
		  * @param pname
		  * @return
		  */
		 public Person getPersonByName(String pname){

			 String sql = "SELECT p.fid id,p.fname_l2 name,a.Fid deptid,a.FNAME_L2 deptname,p.FEMAIL email,p.fcell mobile, "
					 +" po.FID positionid,po.FNAME_L2 positionname,pp.FISPRIMARY isprimary FROM t_bd_person p  "
					 +" LEFT JOIN T_ORG_PositionMember pp ON p.fid = pp.FPERSONID "
					 +" LEFT JOIN T_ORG_Position po ON po.fid = pp.FPOSITIONID "
					 +" LEFT JOIN t_org_admin a ON a.FID = po.FADMINORGUNITID "
					 +" left join  T_HR_BDEmployeeType et on  et.fid = p.femployeetypeid  "
					 +" WHERE  p.fname_l2='"+pname+"' and pp.FISPRIMARY=1";

			 return Person.dao.findFirst(sql); 
		 }


		 /**
		  * 根据父id查询 下一级的所有子节点
		  */
		 public List<Dept> getDeptsByParentId(String parentId){
			 List<Dept> depts = Dept.dao.find("select * from t_org_admin where fparentid = '"+parentId+"'");
			 return depts;
		 }
		 /**
		  * 根据父id查询 所有子节点
		  */
		 public void getAllDeptsByParentId(String parentId,List<Dept> deptList){
			 if(deptList==null){
				 deptList = new ArrayList<Dept>();
			 }
			 List<Dept> depts = Dept.dao.find("select * from t_org_admin where fparentid = '"+parentId+"'");
			 deptList.addAll(depts);
			 for(Dept d:depts){
				 this.getAllDeptsByParentId(d.getId(), deptList);
			 }
		 }

	/**
	 * 根据personid获取所有直属下级人员id
	 * 在通过职位找下级是包含了主次职位
 	 * @param personid
	 * @return
	 */
	public List<DtBean> getFirstLowerPersonByPersonId(String personid) {
		String sql =
				"SELECT p.fid id " +
				"FROM t_bd_person p " +
				"LEFT JOIN T_ORG_PositionMember pp ON p.fid = pp.FPERSONID " +
				"LEFT JOIN T_ORG_Position po ON po.fid = pp.FPOSITIONID " +
				"where po.fid in " +
				"(SELECT fid FROM T_ORG_Position where fparentid in " +
				"	(SELECT po.FID positionid FROM t_bd_person p " +
				"	LEFT JOIN T_ORG_PositionMember pp ON p.fid = pp.FPERSONID " +
				"	LEFT JOIN T_ORG_Position po ON po.fid = pp.FPOSITIONID " +
				"    where p.fid = ?) " +
				") ";
		List<DtBean> dtBeans = new DtBean().dao().find(sql, personid);
		return dtBeans;

	}

	/**
	 * 根据人员ID判断是否在职
	 * @param personId
	 * @return
	 */
	public boolean  isPersonInService(String personId){
		SysCode sysCode = sysCodeService.getSysCodeName("员工类型");
		String codeValue = "正式员工,试用员工";
		if(sysCode!=null && StringUtils.isNotBlank(sysCode.getSyscodeValue())){
			codeValue = sysCode.getSyscodeValue();
		}
		codeValue = codeValue.replace(" ","").replace("，",",");
		String[] codeValues = codeValue.split(",");
		StringBuilder sqlin = new StringBuilder("");
		for (String value : codeValues) {
			if(StringUtils.isBlank(value)){
				continue;
			}
			sqlin.append("'"+value+"',");
		}
		String  sqlin_= sqlin.toString();
		sqlin_ = sqlin_.substring(0,sqlin_.length()-1);

		String sql = "SELECT person.fid id FROM t_bd_person person left join T_HR_BDEmployeeType t on t.fid = person.femployeetypeid " +
				" where t.fname_l2 in ("+sqlin_+") and person.fid = ?";
		List<DtBean> dtBeans = new DtBean().dao().find(sql, personId);
		if(dtBeans.size() > 0){
			return true;
		}else{
			return false;
		}
	}


	/**
	 * 判断是否为直接上下级关系
	 * @param lowPersonid 下级id
	 * @param superPersonId 上级id
	 * @return
	 */
	public boolean isFirstSuper(String lowPersonid,String superPersonId){
		boolean flag = false;
		List<DtBean> dt = getFirstLowerPersonByPersonId(superPersonId);
		for(DtBean d:dt){
			if(d.getStr("id").equals(lowPersonid)){
				flag = true;
				break;
			}
		}
		return flag;
	}
	/**
	 * 判断是否为上下级关系
	 * @param lowPersonid 下级id
	 * @param superPersonId 上级id
	 * @return
	 */
	public boolean isSuper(String lowPersonid,String superPersonId){
		boolean flag = false;
		Person person = this.getPersonById(lowPersonid);
		Person superPerson = this.getSuperPersonByPositionId(person.getPositionId());
		if(superPerson == null )return false;
		return superPerson.getId().equals(superPersonId);
	}
	//hrService.getSuperPersonByPositionId(khperson.getPositionId())
}
