package nc.impl.bd.psn.psndoc;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import nc.bs.bd.baseservice.busilog.IBDBusiLogUtil;
import nc.bs.bd.baseservice.md.SingleBaseService;
import nc.bs.bd.baseservice.validator.RefPkExistsValidator;
import nc.bs.bd.psn.psndoc.validator.PsndocEmailValidator;
import nc.bs.bd.psn.psndoc.validator.PsndocEnableValidator;
import nc.bs.bd.psn.psndoc.validator.PsndocHREnableValidator;
import nc.bs.bd.psn.psndoc.validator.PsndocNotNullValidator;
import nc.bs.bd.psn.psndoc.validator.PsndocPartTimeJobDeleteValidator;
import nc.bs.bd.psn.psndoc.validator.PsndocUniqueValidator;
import nc.bs.bd.psn.psndoc.validator.PsnjobUniqueValidator;
import nc.bs.bd.psn.psndoc.validator.PsnjobValidator;
import nc.bs.businessevent.EventDispatcher;
import nc.bs.businessevent.bd.BDCommonEvent;
import nc.bs.dao.BaseDAO;
import nc.bs.framework.common.NCLocator;
import nc.bs.uif2.validation.ValidationException;
import nc.bs.uif2.validation.Validator;
import nc.itf.bd.config.uniquerule.UniqueRuleConst;
import nc.itf.bd.psn.psndoc.IPsndocService;
import nc.itf.uap.rbac.IRoleManage;
import nc.itf.uap.rbac.IRoleManageQuery;
import nc.itf.uap.rbac.IUserManage;
import nc.itf.uap.rbac.IUserManageQuery;
import nc.jdbc.framework.SQLParameter;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.md.persist.framework.IMDPersistenceQueryService;
import nc.md.persist.framework.MDPersistenceService;
import nc.pub.billcode.itf.IBillcodeManage;
import nc.pub.billcode.vo.BillCodeContext;
import nc.vo.bd.meta.BatchOperateVO;
import nc.vo.bd.psn.IPsnConst;
import nc.vo.bd.psn.PsndocExtend;
import nc.vo.bd.psn.PsndocVO;
import nc.vo.bd.psn.PsnjobVO;
import nc.vo.ml.AbstractNCLangRes;
import nc.vo.ml.LanguageVO;
import nc.vo.ml.MultiLangContext;
import nc.vo.ml.NCLangRes4VoTransl;
import nc.vo.pub.BusinessException;
import nc.vo.pub.SuperVO;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.pub.lang.UFDate;
import nc.vo.sm.UserVO;
import nc.vo.uap.rbac.UserGroupVO;
import nc.vo.uap.rbac.role.RoleVO;
import nc.vo.util.BDVersionValidationUtil;
import nccloud.commons.lang.ArrayUtils;
import nccloud.commons.lang.StringUtils;

public class PsndocServiceImpl extends SingleBaseService<PsndocVO>
  implements IPsndocService
{
  private IBillcodeManage billcodeManage = null;
  private IUserManage userManageSerivce = null;
  private IRoleManage roleManageSercvice = null;
  private IRoleManageQuery roleManageQuery = null;
  private IUserManageQuery userManageQuery = null;
  private BaseDAO dao = null;
  private IPsndocService psndocService = null;

  public PsndocServiceImpl()
  {
    super("40d39c26-a2b6-4f16-a018-45664cac1a1f", new String[] { "psnjobs" });
  }

  public void deletePsndoc(PsndocVO vo)
    throws BusinessException
  {
    hrEnabledValidate(vo.getPk_org());
    deleteVO(vo);
    returnPsnCode(vo);
    returnPsnjobCode(vo);
  }

  private void returnPsnCode(PsndocVO vo)
    throws BusinessException
  {
    BillCodeContext billCodeContext = getBillcodeManage()
      .getBillCodeContext("psndoc", vo
      .getPk_group(), vo.getPk_org());

    if (billCodeContext != null)
      getBillcodeManage().returnBillCodeOnDelete("psndoc", vo
        .getPk_group(), vo
        .getPk_org(), vo.getCode(), vo);
  }

  private void returnPsnjobCode(PsndocVO vo)
    throws BusinessException
  {
    BillCodeContext billCodeContext2 = getBillcodeManage()
      .getBillCodeContext("psnjobs", vo
      .getPk_group(), vo
      .getPk_org());
    PsnjobVO[] jobs = vo.getPsnjobs();

    if ((billCodeContext2 != null) && (!(ArrayUtils.isEmpty(jobs))))
      getBillcodeManage().returnBillCodeOnDelete("psnjobs", vo
        .getPk_group(), vo.getPk_org(), jobs[0].getPsncode(), vo);
  }

  public PsndocVO insertPsndoc(PsndocVO vo, boolean isCheckRepeat)
    throws BusinessException
  {
    if (vo == null)
      return vo;
    vo.setAttributeValue("isCheckRepeat", Boolean.valueOf(isCheckRepeat));
    hrEnabledValidate(vo.getPk_org());

    if (StringUtils.isNotBlank(vo.getId())) {
      vo.setId(vo.getId().replaceAll(" ", ""));
    }
    syncPsnjobCodeWithFirst(vo);
    vo = excuteWithAutoCode(vo, new IPsnAutoCodeExcute() {
		@Override
		public PsndocVO excute(PsndocVO vo) throws BusinessException {
			return insertVO(vo);
		}
	});

    return vo;
  }

  private void syncPsnjobCodeWithFirst(PsndocVO vo) {
    PsnjobVO[] jobs = vo.getPsnjobs();
    if (ArrayUtils.isEmpty(jobs)) {
      return;
    }
    String psncode = jobs[0].getPsncode();
    for (PsnjobVO psnjobVO : jobs)
      psnjobVO.setPsncode(psncode);
  }

  private PsndocVO excuteWithAutoCode(PsndocVO vo, IPsnAutoCodeExcute excute)
    throws BusinessException
  {
    BillCodeContext billCodeContext = getBillcodeManage()
      .getBillCodeContext("psndoc", vo
      .getPk_group(), vo.getPk_org());

    BillCodeContext billCodeContext2 = getBillcodeManage()
      .getBillCodeContext("psnjobs", vo
      .getPk_group(), vo
      .getPk_org());
    PsnjobVO mainjob = getMainJob(vo);
    boolean sucessed = false;
	while (!sucessed) {
		try {
			if (billCodeContext != null && !billCodeContext.isPrecode()) {
				String billcode = getBillcodeManage()
						.getBillCode_RequiresNew(IPsnConst.PSNDOC_BILLCODE,
								vo.getPk_group(), vo.getPk_org(), vo);
				vo.setCode(billcode);
			}
			if (billCodeContext2 != null && !billCodeContext2.isPrecode()) {
				String billcode2 = getBillcodeManage()
						.getBillCode_RequiresNew(IPsnConst.PSNDOC_PSNJOB,
								mainjob.getPk_group(), mainjob.getPk_org(),
								mainjob);
				for (PsnjobVO jobvo : vo.getPsnjobs()) {
					jobvo.setPsncode(billcode2);
				}
			}
			vo = excute.excute(vo);
			sucessed = true;
			break;
		} catch (BusinessException e) {
			if (billCodeContext != null
					&& UniqueRuleConst.CODEBREAKUNIQUE.equals(e
							.getErrorCodeString())) {
				getBillcodeManage().AbandonBillCode_RequiresNew(
						IPsnConst.PSNDOC_BILLCODE, vo.getPk_group(),
						vo.getPk_org(), vo.getCode());
				if (!billCodeContext.isPrecode()) {
					continue;
				}
			} else if (PsnjobUniqueValidator.JOBCODE_UNIQUE_ERRCODE
					.equals(e.getErrorCodeString())) {
				getBillcodeManage().AbandonBillCode_RequiresNew(
						IPsnConst.PSNDOC_PSNJOB, mainjob.getPk_group(),
						mainjob.getPk_org(), mainjob.getPsncode());

				if (!billCodeContext2.isPrecode())
					continue;
			}
			throw e;
		}
	}
    if ((billCodeContext != null) && (billCodeContext.isPrecode())) {
      getBillcodeManage().commitPreBillCode("psndoc", vo
        .getPk_group(), vo.getPk_org(), vo.getCode());
    }
    if ((billCodeContext2 != null) && (billCodeContext2.isPrecode())) {
      mainjob = getMainJob(vo);
      getBillcodeManage().commitPreBillCode("psnjobs", mainjob
        .getPk_group(), mainjob.getPk_org(), mainjob
        .getPsncode());
    }
    return vo;
  }

  private PsnjobVO getMainJob(PsndocVO vo) {
    PsnjobVO mainjob = null;
    PsnjobVO[] psnjobVOs = vo.getPsnjobs();
    for (PsnjobVO jobvo : psnjobVOs) {
      if (jobvo.getIsmainjob() == UFBoolean.TRUE) {
        mainjob = jobvo;
        break;
      }
    }
    return mainjob;
  }

  protected String dbInsertVO(PsndocVO vo)
    throws BusinessException
  {
    if ((vo.getPsnjobs() != null) && (vo.getPsnjobs().length > 0)) {
      if (vo.getEnablestate().intValue() != 3)
        vo.setEnablestate(Integer.valueOf(2));
    }
    else {
      vo.setEnablestate(Integer.valueOf(3));
    }
    return super.dbInsertVO(vo);
  }

  protected Validator[] getInsertValidator()
  {
    PsndocNotNullValidator notNullValidator = new PsndocNotNullValidator();

    PsndocEmailValidator emailValidator = new PsndocEmailValidator();

    PsnjobValidator psnjobValidator = new PsnjobValidator(null);

    PsndocUniqueValidator uniqueValidator = new PsndocUniqueValidator();

    PsnjobUniqueValidator psnjobUniqueValidator = new PsnjobUniqueValidator();
    RefPkExistsValidator refPkExistsValidator = new RefPkExistsValidator(new String[0]);
    refPkExistsValidator.addChildAttrs("psnjobs", new String[] { "pk_psncl" });

    PsndocHREnableValidator hrEnableValidator = new PsndocHREnableValidator();
    return new Validator[] { notNullValidator, emailValidator, psnjobValidator, uniqueValidator, psnjobUniqueValidator, refPkExistsValidator, hrEnableValidator };
  }

  protected PsndocVO retrieveVO(String pk)
    throws BusinessException
  {
    if (StringUtils.isBlank(pk)) {
      return null;
    }
    PsndocVO newVO = (PsndocVO)getMDQueryService().queryBillOfVOByPK(PsndocVO.class, pk, false);

    return newVO;
  }

  public PsndocVO updatePsndoc(PsndocVO vo, boolean isCheckRepeat)
    throws BusinessException
  {
    if (vo == null)
      return vo;
    vo.setAttributeValue("isCheckRepeat", Boolean.valueOf(isCheckRepeat));

    hrEnabledValidate(vo.getPk_org());

    if (StringUtils.isNotBlank(vo.getId())) {
      vo.setId(vo.getId().replaceAll(" ", ""));
    }
    syncPsnjobCodeWithFirst(vo);
    return ((PsndocVO)updateVO(vo));
  }

  protected Validator[] getUpdateValidator(PsndocVO oldVO)
  {
    PsndocNotNullValidator notNullValidator = new PsndocNotNullValidator();

    PsndocEmailValidator emailValidator = new PsndocEmailValidator();

    PsnjobValidator psnjobValidator = new PsnjobValidator(oldVO);

    PsndocPartTimeJobDeleteValidator parttimejobValidator = new PsndocPartTimeJobDeleteValidator(oldVO);

    PsndocUniqueValidator uniqueValidator = new PsndocUniqueValidator();

    PsnjobUniqueValidator psnjobUniqueValidator = new PsnjobUniqueValidator();
    RefPkExistsValidator refPkExistsValidator = new RefPkExistsValidator(new String[0]);
    refPkExistsValidator.addChildAttrs("psnjob", new String[] { "pk_psncl" });

    PsndocHREnableValidator hrEnableValidator = new PsndocHREnableValidator();
    return new Validator[] { notNullValidator, emailValidator, psnjobValidator, parttimejobValidator, uniqueValidator, psnjobUniqueValidator, refPkExistsValidator, hrEnableValidator };
  }

  public PsndocVO disEnablePsndoc(PsndocVO vo)
    throws BusinessException
  {
    PsnjobVO[] vos = vo.getPsnjobs();
    if ((vos != null) && (vos.length > 0)) {
      getDao().updateVOList(Arrays.asList(vos));
    }
    return ((PsndocVO)super.disableSingleVO(vo));
  }

  protected Validator[] getDisableValidator()
  {
    PsnjobValidator psnjobValidator = new PsnjobValidator(null);
    return new Validator[] { psnjobValidator };
  }

  public PsndocVO enablePsndoc(PsndocVO vo) throws BusinessException
  {
    return ((PsndocVO)super.enableSingleVO(vo));
  }

  protected Validator[] getEnableValidator()
  {
    PsndocEnableValidator enableValidator = new PsndocEnableValidator();
    return new Validator[] { enableValidator };
  }

  protected void writeTransferBusiLog(PsndocVO vo)
    throws BusinessException
  {
    getBusiLogUtil().writeBusiLog("Transfer", null, new SuperVO[] { vo });
  }

  public PsndocExtend[] createUser(PsndocExtend[] vos, String pk_userGroup, String passwordStrategy)
    throws BusinessException
  {
    if ((vos == null) || (vos.length == 0)) {
      return null;
    }
    List<PsndocExtend> errorList = new ArrayList<PsndocExtend>();
	List<PsndocExtend> toCreateUserVOs = checkExistsUserData(vos, errorList);

    UFDate enableDate = new UFDate(true);
    UserGroupVO userGroupVo = (UserGroupVO)getDao().retrieveByPK(UserGroupVO.class, pk_userGroup, new String[] { "pk_org" });

    Map groupid_rolevo_map = new HashMap();
    BatchOperateVO batchvo = new BatchOperateVO();
    List uservos = new ArrayList();
    for (PsndocExtend psndocExtend : toCreateUserVOs) {
      if (isWithSameCode(psndocExtend.getUser_code())) {
        psndocExtend.setMessage(
          NCLangRes4VoTransl.getNCLangRes().getStrByID("10140psn", "010140psn0097"), true);

        errorList.add(psndocExtend);
      }

      UserVO uservo = createNewUservo(pk_userGroup, enableDate, userGroupVo, psndocExtend, passwordStrategy);

      uservos.add(uservo);
      getBusiLogUtil().writeBusiLog("CreateUser", null, new SuperVO[] { psndocExtend
        .getPsnDocVo() });
    }
    batchvo.setAddObjs(uservos.toArray(new UserVO[0]));
    batchvo = getUserManageSerivce().userbatchSave(batchvo);
    for (int i = 0; i < batchvo.getAddObjs().length; ++i) {
      UserVO uservo = (UserVO)batchvo.getAddObjs()[i];
      RoleVO rolevo = (RoleVO)groupid_rolevo_map.get(uservo.getPk_group());
      if (rolevo == null) {
        rolevo = getIRoleManageQuery().queryRoleByCode("SF_Role", uservo
          .getPk_group());
        groupid_rolevo_map.put(uservo.getPk_group(), rolevo);
      }
      if (rolevo != null) {
        getIRoleManage().assignRole2User(uservo.getCuserid(), new RoleVO[] { rolevo }, uservo
          .getPk_org(), enableDate, null);
      }

    }

    return ((errorList.size() == 0) ? null : 
      (PsndocExtend[])errorList
      .toArray(new PsndocExtend[0]));
  }

  private List<PsndocExtend> checkExistsUserData(PsndocExtend[] vos, List<PsndocExtend> errorList)
    throws BusinessException
  {
    List toCreateUserVOs = filterExistsUserData(vos, errorList);

    toCreateUserVOs = filterHRControlData(toCreateUserVOs, errorList);
    return filterUserNameNull(toCreateUserVOs, errorList);
  }

  private List<PsndocExtend> filterExistsUserData(PsndocExtend[] vos, List<PsndocExtend> errorList) throws BusinessException
  {
    List notRelUserPsnVOs = new ArrayList();
    for (PsndocExtend vo : vos) {
      if (ArrayUtils.isEmpty(getIUserManageQuery()
        .queryUserVOsByPsnDocID(vo
        .getPsnDocVo().getPrimaryKey()))) {
        notRelUserPsnVOs.add(vo);
      } else {
        vo.setMessage(NCLangRes4VoTransl.getNCLangRes()
          .getStrByID("10140psn", "010140psn0027"), 
          false);

        errorList.add(vo);
      }
    }
    return notRelUserPsnVOs;
  }

  private List<PsndocExtend> filterHRControlData(List<PsndocExtend> vos, List<PsndocExtend> errorList) throws BusinessException
  {
    Set pk_orgs = getPsndocOrgs(vos);

    Map orgid_ishrenable = getPsndocService()
      .isHREnabled((String[])pk_orgs
      .toArray(new String[0]));

    List toCreateUserVOs = new ArrayList();
    for (PsndocExtend vo : vos) {
      if ((orgid_ishrenable == null) || (orgid_ishrenable.size() == 0)) {
        toCreateUserVOs.add(vo);
      } else {
        UFBoolean isHREnable = (UFBoolean)orgid_ishrenable.get(vo.getPsnDocVo()
          .getPk_org());
        if (isHREnable.booleanValue()) {
          vo.setMessage(NCLangRes4VoTransl.getNCLangRes()
            .getStrByID("10140psn", "010140psn0115"), 
            false);
        }
        else
        {
          toCreateUserVOs.add(vo);
        }
      }
    }
    return toCreateUserVOs;
  }

  private List<PsndocExtend> filterUserNameNull(List<PsndocExtend> vos, List<PsndocExtend> errorList) throws BusinessException
  {
    List toCreateUserVOs = new ArrayList();
    for (PsndocExtend vo : vos) {
      if (StringUtils.isBlank(vo.getUser_code())) {
        vo.setMessage(NCLangRes4VoTransl.getNCLangRes()
          .getStrByID("10140psn", "010140psn0137"), 
          true);

        errorList.add(vo);
      } else {
        toCreateUserVOs.add(vo);
      }
    }
    return toCreateUserVOs;
  }

  private Set<String> getPsndocOrgs(List<PsndocExtend> vos) {
    Set orgIDs = new HashSet();
    for (PsndocExtend vo : vos) {
      orgIDs.add(vo.getPsnDocVo().getPk_org());
    }
    return orgIDs;
  }

  private UserVO createNewUservo(String pk_userGroup, UFDate enableDate, UserGroupVO userGroupVo, PsndocExtend psndocExtend, String passwordStrategy)
  {
    PsndocVO psndocVO = psndocExtend.getPsnDocVo();

    LanguageVO currentLang = MultiLangContext.getInstance()
      .getCurrentLangVO();
    UserVO uservo = new UserVO();
    uservo.setBase_doc_type(Integer.valueOf(0));
    uservo.setPk_base_doc(psndocVO.getPk_psndoc());
    uservo.setPk_group(psndocVO.getPk_group());
    uservo.setPk_org((userGroupVo == null) ? null : userGroupVo.getPk_org());
    //modify by@chenzf 修改用户编码为手机号 2021-1-5 11:39:12 begin
//    uservo.setUser_code(psndocExtend.getUser_code());
    uservo.setUser_code(psndocVO.getMobile());
    //modify by@chenzf 修改用户编码为手机号 2021-1-5 11:39:12 end
    uservo.setUser_name(psndocVO.getName());
    uservo.setUser_name2(psndocVO.getName2());
    uservo.setUser_name3(psndocVO.getName3());
    uservo.setUser_name4(psndocVO.getName4());
    uservo.setUser_name5(psndocVO.getName5());
    uservo.setUser_name6(psndocVO.getName6());
    uservo.setUser_type(Integer.valueOf(1));

    uservo.setIdentityverifycode("staticpwd");
    uservo.setContentlang(currentLang.getPk_multilang());
    uservo.setPwdlevelcode("update");
    uservo.setEnablestate(Integer.valueOf(2));
    uservo.setPk_usergroupforcreate(pk_userGroup);
    uservo.setFormat("FMT0Z000000000000000");
    uservo.setAbledate(enableDate);

    if (passwordStrategy.equals("password_default"))
      uservo.setUser_password(null);
    else if (passwordStrategy.equals("password_psnid")) {
      uservo.setUser_password(getPasswordByPsnid(psndocVO.getId()));
    }
    return uservo;
  }

  private String getPasswordByPsnid(String id) {
    if (StringUtils.isBlank(id)) {
      return null;
    }
    if (id.length() <= 8) {
      return StringUtils.rightPad(id, 8, "0");
    }
    return StringUtils.substring(id, id.length() - 8, id.length());
  }

  public PsndocVO deletePsnJob(PsndocVO vo, String pk_org)
    throws BusinessException
  {
    if (vo == null)
      return vo;
    try
    {
      updatelockOperate(vo);

      BDVersionValidationUtil.validateSuperVO(new SuperVO[] { vo });

      PsndocVO oldVO = retrieveVO(vo.getPrimaryKey());

      getDao().deleteByClause(PsnjobVO.class, "pk_org='" + pk_org + "' and pk_psndoc='" + vo
        .getPk_psndoc() + "'");

      setUpdateAuditInfo(vo);

      fireBeforeDeleteJobEvent(oldVO, vo);

      fireBeforeUpdateEvent(oldVO, vo);

      notifyVersionChangeWhenDataUpdated(vo);

      vo = retrieveVO(vo.getPrimaryKey());

      fireAfterUpdateEvent(oldVO, vo);

      writeUpdatedBusiLog(oldVO, vo);
    } catch (ValidationException e) {
      throw new BusinessException(e.getMessage());
    }

    return vo;
  }

  protected void fireBeforeDeleteJobEvent(PsndocVO oldVO, PsndocVO vo) throws BusinessException
  {
    EventDispatcher.fireEvent(new BDCommonEvent(getMDId(), "1097", new Object[] { oldVO }, vo ));
  }

  public void transferUser(PsndocVO vo, boolean innerGroup)
    throws BusinessException
  {
    if (vo == null) {
      return;
    }
    UserVO[] users = getIUserManageQuery().queryUserVOsByPsnDocID(vo
      .getPk_psndoc());
    if (ArrayUtils.isEmpty(users)) {
      return;
    }
    if (innerGroup) {
      for (UserVO userVO : users)
      {
        if (userVO.getEnablestate().intValue() != 3)
          getUserManageSerivce().disableUser(userVO);
      }
    }
    else
      getUserManageSerivce().migrateUserToGroupWithDocRelation(users, vo
        .getPk_group());
  }

  public void transferUserCrossGroup(PsndocVO vo, boolean ParamValue)
    throws BusinessException
  {
    if (vo == null) {
      return;
    }
    UserVO[] users = getIUserManageQuery().queryUserVOsByPsnDocID(vo
      .getPk_psndoc());
    if (ArrayUtils.isEmpty(users)) {
      return;
    }
    if (ParamValue)
    {
      getUserManageSerivce().migrateUserToGroupWithDocRelation(users, vo
        .getPk_group());
    }
    else
      for (UserVO userVO : users)
      {
        if (userVO.getEnablestate().intValue() != 3)
          getUserManageSerivce().disableUser(userVO);
      }
  }

  public PsndocVO transferPsndoc(PsndocVO vo)
    throws BusinessException
  {
    PsndocVO oldVO = retrieveVO(vo.getPrimaryKey());

    if (!(oldVO.getPk_org().equals(vo.getPk_org()))) {
      returnPsnCode(oldVO);
      returnPsnjobCode(oldVO);
    }
    vo = excuteWithAutoCode(vo, new IPsnAutoCodeExcute() {
		@Override
		public PsndocVO excute(PsndocVO vo) throws BusinessException {
			return updateVO(vo);
		}
	});

    return vo;
  }

  private PsnjobVO getMainJobVO(PsndocVO vo)
  {
    PsnjobVO[] jobs = vo.getPsnjobs();
    if (!(ArrayUtils.isEmpty(jobs))) {
      for (PsnjobVO psnjobVO : jobs) {
        if (psnjobVO.getIsmainjob().booleanValue()) {
          return psnjobVO;
        }
      }
    }
    return null;
  }

  private PsnjobVO getJobVOByID(PsndocVO vo, String pk) {
    if (StringUtils.isBlank(pk)) {
      return null;
    }
    PsnjobVO[] jobs = vo.getPsnjobs();
    if (!(ArrayUtils.isEmpty(jobs))) {
      for (PsnjobVO psnjobVO : jobs) {
        if (pk.equals(psnjobVO.getPrimaryKey())) {
          return psnjobVO;
        }
      }
    }
    return null;
  }

  private IMDPersistenceQueryService getMDQueryService() {
    return MDPersistenceService.lookupPersistenceQueryService();
  }

  private boolean isWithSameCode(String code) throws BusinessException
  {
    if (code == null)
      return false;
    String sql = "select 1 from sm_user where user_code=?";
    SQLParameter param = new SQLParameter();
    param.addParam(code);

    Object uservo = getDao()
      .executeQuery(sql, param, new ColumnProcessor());

    return (uservo != null);
  }

  private IBillcodeManage getBillcodeManage() {
    if (this.billcodeManage == null) {
      this.billcodeManage = ((IBillcodeManage)NCLocator.getInstance().lookup(IBillcodeManage.class));
    }

    return this.billcodeManage;
  }

  private IUserManage getUserManageSerivce() {
    if (this.userManageSerivce == null) {
      this.userManageSerivce = ((IUserManage)NCLocator.getInstance().lookup(IUserManage.class));
    }
    return this.userManageSerivce;
  }

  private IRoleManage getIRoleManage()
  {
    if (this.roleManageSercvice == null) {
      this.roleManageSercvice = ((IRoleManage)NCLocator.getInstance().lookup(IRoleManage.class));
    }
    return this.roleManageSercvice;
  }

  private IRoleManageQuery getIRoleManageQuery() {
    if (this.roleManageQuery == null) {
      this.roleManageQuery = ((IRoleManageQuery)NCLocator.getInstance().lookup(IRoleManageQuery.class));
    }
    return this.roleManageQuery;
  }

  private IUserManageQuery getIUserManageQuery()
  {
    if (this.userManageQuery == null) {
      this.userManageQuery = ((IUserManageQuery)NCLocator.getInstance().lookup(IUserManageQuery.class));
    }
    return this.userManageQuery;
  }

  private BaseDAO getDao() {
    if (this.dao == null)
      this.dao = new BaseDAO();
    return this.dao;
  }

  public boolean isHREnabled(String pk_org)
    throws BusinessException
  {
    return false;
  }

  public Map<String, UFBoolean> isHREnabled(String[] pk_orgs)
    throws BusinessException
  {
    Map map = new HashMap();
    for (String pk_org : pk_orgs) {
      map.put(pk_org, 
        UFBoolean.valueOf(getPsndocService().isHREnabled(pk_org)));
    }
    return map;
  }

  private void hrEnabledValidate(String pk_org) throws BusinessException
  {
    if (!(getPsndocService().isHREnabled(pk_org)))
      return;
    throw new BusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("10140psn", "010140psn0103"));
  }

  private IPsndocService getPsndocService()
  {
    if (this.psndocService == null) {
      this.psndocService = ((IPsndocService)NCLocator.getInstance().lookup(IPsndocService.class));
    }

    return this.psndocService;
  }
}