package com.sys.bus.common.service;

import com.alibaba.druid.util.StringUtils;
import com.jfinal.plugin.activerecord.Page;
import com.sys.bus.model.SysCode;
import com.sys.bus.sys.model.DtBean;
import com.sys.bus.syscode.service.SysCodeService;
import org.jfree.util.Log;

import java.util.List;

public class CommonService {
	private static final DtBean comDao = new DtBean().dao();
	static SysCodeService sysCodeService = new SysCodeService();

	public List<DtBean> department(){
		String sql = "select orgtab.fid id,fparentid pId,fname_l2 name,fdisplayname_l2 t,fdisplayname_l2 fullname  from t_org_admin orgtab where FISSEALUP = 0"; // 过滤封存 master

		//"select orgtab.fid id,fparentid pId,fname_l2 name,fdisplayname_l2 t,fdisplayname_l2 fullname,rownum as rownnumber from t_org_admin orgtab  start with  fparentid is null connect by prior fid = fparentid ";
		return comDao.find(sql);
	}
	public List<DtBean>  department2(String orgId){
		DtBean department = department(orgId);
		String fullname = department.get("fullname");

		String sql = "select fid id,fparentid pId,fname_l2 name,fdisplayname_l2 fullname  from t_org_admin where fdisplayname_l2 like '"+fullname+"%' and FISSEALUP = 0";// 过滤封存 master

		//"select orgtab.fid id,fparentid pId,fname_l2 name,fdisplayname_l2 t,fdisplayname_l2 fullname,rownum as rownnumber from t_org_admin orgtab  start with  fparentid is null connect by prior fid = fparentid ";
		return  comDao.find(sql);

	}
	public DtBean department(String orgId){
		String sql = "select fid id,fparentid pId,fname_l2 name,fdisplayname_l2 fullname  from t_org_admin where fid=?";

		//"select orgtab.fid id,fparentid pId,fname_l2 name,fdisplayname_l2 t,fdisplayname_l2 fullname,rownum as rownnumber from t_org_admin orgtab  start with  fparentid is null connect by prior fid = fparentid ";
		List<DtBean> list = comDao.find(sql,orgId);
		if(list.size()>0){
			return list.get(0);
		}
		return null;
	}


	public Page<DtBean> searchUser(int pageNumber ,int pageSize,String orgid,String qName,boolean orgFlag){

		String sqlCount = "";
		String sqlPara  = "";
		String personType = getPersonTypeSqlStr();
		//调节查询
		if(!StringUtils.isEmpty(qName) ) {
			String[] names = null;
//			if(qName.indexOf("，")>0||qName.indexOf(",")>0){
//				qName =qName.replace(" ", "");//清除空格
//				 if(qName.indexOf("，")>0)qName =qName.replace("，", ",");
//				 if(qName.indexOf(",")>0){
//					 names = qName.split(",");
//				 }
//			}else if(qName.indexOf(" ")>0){
//				names = qName.split(" ");
//			}
			qName = qName.replaceAll("\\s{1,}", " ");//把多个连续空格替换成单空格
			names = qName.split("[ ,：，；;]");//任意分隔符分割
			String namesql = "";
			if(names!=null&&names.length>1){
				for (String n:names) {
					namesql+="'"+n+"',";
				}
				namesql=namesql.substring(0, namesql.length()-1);
			}

			sqlCount = "select count(1) from \r\n" + 
					"(\r\n" + 
					"SELECT person.fid ,person.fnumber ,person.fname_l2 uname,position.fname_l2 poname,position.fid poid,org.fdisplayname_l2 fullname\r\n" +
					"  FROM t_bd_person person\r\n" + 
					"  left join T_ORG_PositionMember opmap\r\n" + 
					"    on person.fid = opmap.fpersonid\r\n" + 
					"  left join t_org_position position\r\n" + 
					"    on position.fid = opmap.fpositionid\r\n" + 
					"  left join t_org_admin org\r\n" + 
					"    on org.fid = position.fadminorgunitid\r\n" + 
					"  left join T_HR_BDEmployeeType t\r\n" + 
					"    on t.fid = person.femployeetypeid\r\n" + 
					" where opmap.FISPRIMARY=1 and t.fname_l2 in ("+personType+") and (person.fnumber like '%"+qName;
			      if(org.apache.commons.lang3.StringUtils.isNotBlank(namesql)){
			    	  sqlCount += "%' or person.fname_l2 in( "+namesql+"))) temp";
			      }else{
			    	  sqlCount += "%' or person.fname_l2 like '%"+qName+"%')) temp";
			      }
					
			sqlPara = "SELECT person.fid id,person.fnumber ,person.fname_l2||'_'||person.fnumber uname,position.fname_l2 poname,position.fid poid,org.fdisplayname_l2 fullname\r\n" +
					"  FROM t_bd_person person\r\n" + 
					"  left join T_ORG_PositionMember opmap\r\n" + 
					"    on person.fid = opmap.fpersonid\r\n" + 
					"  left join t_org_position position\r\n" + 
					"    on position.fid = opmap.fpositionid\r\n" + 
					"  left join t_org_admin org\r\n" + 
					"    on org.fid = position.fadminorgunitid\r\n" + 
					"  left join T_HR_BDEmployeeType t\r\n" + 
					"    on t.fid = person.femployeetypeid\r\n" +
					" where opmap.FISPRIMARY=1 and t.fname_l2 in ("+personType+") and (person.fnumber like '%"+qName;
				 if(org.apache.commons.lang3.StringUtils.isNotBlank(namesql)){
					 sqlPara += "%' or person.fname_l2 in( "+namesql+"))";
			      }else{
			    	  sqlPara += "%' or person.fname_l2 like '%"+qName+"%')";
			      }
			     

			Log.info(sqlPara);

		}else {//按部门查询
			if(!orgFlag || StringUtils.isEmpty(orgid)) {
				sqlCount = "select count(1) from \r\n" +
						"(\r\n" +
						"SELECT person.fid ,person.fnumber ,person.fname_l2,org.fdisplayname_l2\r\n" +
						"  FROM t_bd_person person\r\n" +
						"  left join T_ORG_PositionMember opmap\r\n" +
						"    on person.fid = opmap.fpersonid\r\n" +
						"  left join t_org_position position\r\n" +
						"    on position.fid = opmap.fpositionid\r\n" +
						"  left join t_org_admin org\r\n" +
						"    on org.fid = position.fadminorgunitid\r\n" +
						"  left join T_HR_BDEmployeeType t\r\n" +
						"    on t.fid = person.femployeetypeid\r\n" +
						" where opmap.FISPRIMARY=1 and  t.fname_l2 in ("+personType+") and org.fid = '" + orgid + "'\r\n" +
						") temp \r\n" +
						"";
				sqlPara = "SELECT person.fid id,person.fnumber ,person.fname_l2||'_'||person.fnumber uname,position.fname_l2 poname,position.fid poid,org.fdisplayname_l2 fullname\r\n" +
						"  FROM t_bd_person person\r\n" +
						"  left join T_ORG_PositionMember opmap\r\n" +
						"    on person.fid = opmap.fpersonid\r\n" +
						"  left join t_org_position position\r\n" +
						"    on position.fid = opmap.fpositionid\r\n" +
						"  left join t_org_admin org\r\n" +
						"    on org.fid = position.fadminorgunitid\r\n" +
						"  left join T_HR_BDEmployeeType t\r\n" +
						"    on t.fid = person.femployeetypeid\r\n" +
						" where opmap.FISPRIMARY=1 and t.fname_l2 in ("+personType+") and org.fid = '" + orgid + "'";
			}else{//包含下级
				DtBean department = department(orgid);
				String fullname = department.get("fullname");
				sqlCount = "select count(1) from \r\n" +
						"(\r\n" +
						"SELECT person.fid ,person.fnumber ,person.fname_l2,org.fdisplayname_l2\r\n" +
						"  FROM t_bd_person person\r\n" +
						"  left join T_ORG_PositionMember opmap\r\n" +
						"    on person.fid = opmap.fpersonid\r\n" +
						"  left join t_org_position position\r\n" +
						"    on position.fid = opmap.fpositionid\r\n" +
						"  left join t_org_admin org\r\n" +
						"    on org.fid = position.fadminorgunitid\r\n" +
						"  left join T_HR_BDEmployeeType t\r\n" +
						"    on t.fid = person.femployeetypeid\r\n" +
						" where opmap.FISPRIMARY=1 and  t.fname_l2 in ("+personType+") and org.fdisplayname_l2 like '"+fullname+"%'\r\n" +
						") temp \r\n" +
						"";
				sqlPara = "SELECT person.fid id,person.fnumber ,person.fname_l2||'_'||person.fnumber uname,position.fname_l2 poname,position.fid poid,org.fdisplayname_l2 fullname\r\n" +
						"  FROM t_bd_person person\r\n" +
						"  left join T_ORG_PositionMember opmap\r\n" +
						"    on person.fid = opmap.fpersonid\r\n" +
						"  left join t_org_position position\r\n" +
						"    on position.fid = opmap.fpositionid\r\n" +
						"  left join t_org_admin org\r\n" +
						"    on org.fid = position.fadminorgunitid\r\n" +
						"  left join T_HR_BDEmployeeType t\r\n" +
						"    on t.fid = person.femployeetypeid\r\n" +
						" where opmap.FISPRIMARY=1 and  t.fname_l2 in ("+personType+") and org.fdisplayname_l2 like '"+fullname+"%'";
			}

		}

		return comDao.paginateByFullSql(pageNumber, pageSize, sqlCount, sqlPara);
	}
	public String getPara(String value) {
		if(!StringUtils.isEmpty(value)) {
			return "and person.fname_l2 like '%"+value+"%'";
		}
		return "";
	}
	public Page<DtBean> page(int pageNumber, int pageSize) {
		return comDao.paginate(pageNumber, pageSize, "select *", "from ZX_TEST_USER order by id asc");
		//comDao.paginateByFullSql(pageNumber, pageSize, totalRowSql, findSql, paras);
	}



    private String getPersonTypeSqlStr(){
		SysCode sysCode = sysCodeService.getSysCodeName("员工类型");
		String codeValue = "正式员工,试用员工";
		if(sysCode!=null && !StringUtils.isEmpty(sysCode.getSyscodeValue())){
			codeValue = sysCode.getSyscodeValue();
		}
		codeValue = codeValue.replace(" ","").replace("，",",");
		String[] codeValues = codeValue.split(",");
		StringBuilder sqlin = new StringBuilder("");
		for (String value : codeValues) {
			if(StringUtils.isEmpty(value)){
				continue;
			}
			sqlin.append("'"+value+"',");
		}
		String  sqlin_= sqlin.toString();
		sqlin_ = sqlin_.substring(0,sqlin_.length()-1);
		return sqlin_;
	}




	/**
	 * Id查询员工对象
	 * @param personId
	 * @return
	 */
	public DtBean getPersonById(String personId) {
		return comDao.findFirst("select * from t_bd_person where fid=?", personId);
	}



}
