package nc.bs.hrhi.common;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONObject;

import nc.bs.framework.common.NCLocator;
import nc.bs.logging.Logger;
import nc.itf.hi.IPsndocQryService;
import nc.itf.uap.IUAPQueryBS;
import nc.jdbc.framework.processor.BeanListProcessor;
import nc.vo.er.exception.ExceptionHandler;
import nc.vo.hi.psndoc.PsnJobVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.compiler.PfParameterVO;
import nc.vo.pub.pf.plugin.IPfMsgCustomReceiver;
import nc.vo.pub.pf.plugin.ReceiverVO;
import nc.vo.sm.UserVO;

public class HrkqMgrReceiver implements IPfMsgCustomReceiver {

	@Override
	public ReceiverVO[] createReceivers() {
		ReceiverVO lrr = new ReceiverVO();
		lrr.setName("假勤管理员");
		lrr.setCode("hrkqMgr");
		return new ReceiverVO[] {lrr};
	}

	@Override
	public UserVO[] queryUsers(ReceiverVO arg0, PfParameterVO paravo) {
		return queryOperatorByPsn(paravo);
	}

	private UserVO[] queryOperatorByPsn(PfParameterVO paravo) {
		try {
			String pk_psndoc = null;
			String pk_psnjob = null;
			String pk_dept = null;
			if(paravo.m_preValueVo.getParentVO() != null) {
//				pk_psndoc = (String) paravo.m_preValueVo.getParentVO().getAttributeValue("pk_psndoc");
				pk_psnjob = (String) paravo.m_preValueVo.getParentVO().getAttributeValue("pk_psnjob");
			}
			List<UserVO> list = new ArrayList();
			Logger.debug("测试日志1："+pk_psndoc+"有人员");
			//单据人员的组织和这个职责所关联的人员的组织相同噢
			if (pk_psnjob != null) {
				if(pk_dept == null) {
					PsnJobVO psnJobVO = this.queryPsnJob(pk_psnjob);
					pk_dept = psnJobVO.getPk_dept();
				}
				UserVO[] users = this.queryUserByPkDept(pk_dept);
				Logger.debug("测试日志2："+JSONObject.toJSONString(users));
				if(users != null && users.length > 0) {
					for(UserVO vo : users) {
						list.add(vo);
					}
				}
			}
			
			return list.toArray(new UserVO[0]);
			
		} catch (Exception e) {
			Logger.debug("测试日志3："+e.getMessage());
			ExceptionHandler.handleExceptionRuntime(e);
		}
		return new UserVO[0];
	}
	
	public PsnJobVO queryPsnJob(String pk_psnjob) throws BusinessException{
//	    StringBuffer where = new StringBuffer();
//	    where.append(" bd_psndoc.dr = 0 and hi_psnorg.dr = 0 and hi_psnjob.dr = 0 and hi_psnorg.lastflag = 'Y' and hi_psnjob.lastflag = 'Y' and hi_psnjob.psntype = 0 and hi_psnorg.indocflag ='Y' and hi_psnjob.ismainjob = 'Y' and hi_psnjob.endflag = 'N' and bd_psndoc.enablestate = 2");
//
//	    where.append(" and bd_psndoc.pk_psndoc = ");
//	    if (StringUtils.isNotBlank(pk_psndoc)) {
//	      where.append("'" + pk_psndoc + "'");
//	    }  
		
//	    List<PsnJobVO> psnJobList = null;
//	    try {
//			psnJobList = ((IPsndocQryService)NCLocator.getInstance().lookup(IPsndocQryService.class)).queryPsninfoByCondition(where.toString());
//	    } catch (BusinessException e) {
//	      Logger.error(e.getMessage(), e);
//	    } 
//	    if (psnJobList == null ) {
//	    	throw new BusinessException("找不到对应的工作记录");
//	    }
		PsnJobVO psnjobVO = null;
		try {
			 PsnJobVO[] psnjobs = ((IPsndocQryService)NCLocator.getInstance().lookup(IPsndocQryService.class)).queryPsnjobByPKs(new String[] {pk_psnjob});
			 if(psnjobs != null) {
				 psnjobVO = psnjobs[0];
			 }
		} catch (BusinessException e) {
	      Logger.error(e.getMessage(), e);
	    } 
		if (psnjobVO == null ) {
	    	throw new BusinessException("找不到对应的工作记录");
	    }
	    return null;
	}
	
	public UserVO[] queryUserByPkDept(String pk_dept) throws BusinessException{
		StringBuffer sqlstr=new StringBuffer("");
		sqlstr.append("select sm_user.*").append("\r\n");
		sqlstr.append("  from sm_user").append("\r\n");
		sqlstr.append("  left join bd_psndoc").append("\r\n");
		sqlstr.append("    on bd_psndoc.pk_psndoc = sm_user.pk_psndoc").append("\r\n");
		sqlstr.append(" inner join hi_psnorg").append("\r\n");
		sqlstr.append("    on bd_psndoc.pk_psndoc = hi_psnorg.pk_psndoc").append("\r\n");
		sqlstr.append(" inner join hi_psnjob").append("\r\n");
		sqlstr.append("    on hi_psnorg.pk_psnorg = hi_psnjob.pk_psnorg").append("\r\n");
		sqlstr.append(" left join sm_user_role").append("\r\n");
		sqlstr.append("    on sm_user_role.cuserid = sm_user.cuserid").append("\r\n");
		sqlstr.append(" left join sm_role").append("\r\n");
		sqlstr.append("    on sm_role.pk_role = sm_user_role.pk_role").append("\r\n");
		sqlstr.append(" left join sm_perm_func").append("\r\n");
		sqlstr.append("    on sm_perm_func.subjectid = sm_role.pk_role").append("\r\n");
		sqlstr.append(" left join sm_responsibility").append("\r\n");
		sqlstr.append("    on sm_responsibility.pk_responsibility = sm_perm_func.ruleid").append("\r\n");
		sqlstr.append(" where bd_psndoc.dr = 0").append("\r\n");
		sqlstr.append("   and hi_psnorg.dr = 0").append("\r\n");
		sqlstr.append("   and hi_psnjob.dr = 0").append("\r\n");
		sqlstr.append("   and hi_psnorg.lastflag = 'Y'").append("\r\n");
		sqlstr.append("   and hi_psnjob.lastflag = 'Y'").append("\r\n");
		sqlstr.append("   and hi_psnjob.psntype = 0").append("\r\n");
		sqlstr.append("   and hi_psnorg.indocflag = 'Y'").append("\r\n");
		sqlstr.append("   and hi_psnjob.ismainjob = 'Y'").append("\r\n");
		sqlstr.append("   and hi_psnjob.endflag = 'N'").append("\r\n");
		sqlstr.append("   and bd_psndoc.enablestate = 2").append("\r\n");
		sqlstr.append("   and sm_responsibility.name = '假勤管理人'").append("\r\n");
		sqlstr.append("   and hi_psnjob.pk_dept = '"+pk_dept+"'"); 
		List<UserVO> userList = (List<UserVO>) NCLocator.getInstance().lookup(IUAPQueryBS.class).executeQuery(sqlstr.toString(), new BeanListProcessor(UserVO.class));
		if(userList != null && userList.size() > 0) {
			return userList.toArray(new UserVO[0]);
		}
		return null;
	}
}
