package nc.net.uapbd.activity;

import java.util.ArrayList;
import java.util.List;

import nc.bs.bd.bankacc.validator.BankaccIbanValidator;
import nc.bs.bd.bankacc.validator.PsnAndCustSubaccCurrtypeUniqueValidation;
import nc.bs.bd.baseservice.validator.RefPkExistsValidator;
import nc.bs.bd.psnbankacc.validator.PsnbankaccNullValidation;
import nc.bs.bd.psnbankacc.validator.PsnbankaccUniqueValidation;
import nc.bs.bd.psnbankacc.validator.PsnbankaccsubValidation;
import nc.bs.dao.BaseDAO;
import nc.bs.dao.DAOException;
import nc.bs.uif2.validation.IValidationService;
import nc.bs.uif2.validation.ValidationException;
import nc.bs.uif2.validation.ValidationFrameworkUtil;
import nc.bs.uif2.validation.Validator;
import nc.impl.bd.psnbankacc.PsnBankaccServiceImpl;
import nc.md.model.MetaDataException;
import nc.md.persist.framework.MDPersistenceService;
import nc.vo.bd.bankaccount.BankAccSubVO;
import nc.vo.bd.bankaccount.BankAccbasVO;
import nc.vo.bd.psnbankacc.PsnBankaccUnionVO;
import nc.vo.bd.psnbankacc.PsnBankaccVO;
import nc.vo.bd.pub.SingleDistributedUpdateValidator;
import nc.vo.pub.BusinessException;
import nc.vo.pub.SuperVO;
import nc.vo.pub.VOStatus;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.trade.voutils.VOUtil;
import nc.vo.util.BDAuditInfoUtil;
import nc.vo.util.BDPKLockUtil;
import nc.vo.util.BDVersionValidationUtil;
import nc.vo.bd.bankaccount.BankAccSubVO;

public class PsnbankUpdateImpl extends PsnBankaccServiceImpl {
	
	private BaseDAO baseDao = new BaseDAO();
	
	public void update(PsnBankaccUnionVO updateVO)
			/*     */     throws BusinessException
			/*     */   {
		
	    // 获取旧VO
	    PsnBankaccUnionVO oldvo = reQueryVO(updateVO);

	    PsnBankaccVO psnBankaccVO = updateVO.getPsnbankaccVO();
	    BankAccbasVO bankaccbasVO = updateVO.getBankaccbasVO();
	    // 主键锁
	    bdlockOperate(new SuperVO[] {psnBankaccVO, bankaccbasVO});
	    // 版本校验
	    versionValidate(new SuperVO[] {psnBankaccVO, bankaccbasVO});
	    // 后台逻辑校验
	    updateValidate(updateVO);
	    // 审计信息
	    BDAuditInfoUtil.updateData(bankaccbasVO);
	    // 库操作(先更新银行账户,再更新关联表信息)
	    bankaccbasVO.setStatus(VOStatus.UPDATED);
	    // 银行账户的账号和户名和子户上面的账号和户名同步的处理
	    String accnum_old = oldvo.getBankaccbasVO().getAccnum();
	    String accname_old = oldvo.getBankaccbasVO().getAccname();
	    String accnum_new = bankaccbasVO.getAccnum();
	    String accname_new = bankaccbasVO.getAccname();
	    if (!accnum_old.equals(accnum_new) || !accname_old.equals(accname_new)) {
	      BankAccSubVO[] accsubvos_new = bankaccbasVO.getBankaccsub();
	      BankAccSubVO[] accsubvos_old = oldvo.getBankaccbasVO().getBankaccsub();
	      List<BankAccSubVO> accsubvos_final = new ArrayList<BankAccSubVO>();
	      if (accsubvos_new != null && accname_new.length() > 0) {
	        for (int i = 0; i < accsubvos_new.length; i++) {
	          if (accsubvos_new[i].getStatus() != VOStatus.DELETED) {
	            accsubvos_new[i].setAccnum(accnum_new);
	            accsubvos_new[i].setAccname(accname_new);
	            accsubvos_final.add(accsubvos_new[i]);

	          }

	        }
	      }
	      List<String> pk_bankaccsubs_new =
	          VOUtil.extractFieldValues(accsubvos_new, BankAccSubVO.PK_BANKACCSUB, null);
	      for (int i = 0; i < accsubvos_old.length; i++) {
	        if (!pk_bankaccsubs_new.contains(accsubvos_old[i].getPk_bankaccsub())) {
	          accsubvos_old[i].setAccnum(accnum_new);
	          accsubvos_old[i].setAccname(accname_new);
	          accsubvos_old[i].setStatus(VOStatus.UPDATED);
	          accsubvos_final.add(accsubvos_old[i]);

	        }
	      }
	      bankaccbasVO.setBankaccsub(accsubvos_final.toArray(new BankAccSubVO[0]));
	    } else {
	      BankAccSubVO[] accsubvos_new = bankaccbasVO.getBankaccsub();
	      if (accsubvos_new != null && accsubvos_new.length > 0) {
	        for (BankAccSubVO vo : accsubvos_new) {
	          if (vo.getStatus() != VOStatus.DELETED) {
	            vo.setAccnum(accnum_old);
	            vo.setAccname(accname_old);
	          }
	        }
	      }

	    }
	    //add by daim 20220421 最近修改的卡即为工资卡1 和默认卡  start
	    psnBankaccVO.setPayacc(1);//工资卡1
	    psnBankaccVO.setIsexpenseacc(new UFBoolean("Y"));// 默认报销卡
	  //add by daim 20220421 最近修改的卡即为工资卡1 和默认卡  end
	    // 银行账户的库操作
	    MDPersistenceService.lookupPersistenceService().saveBillWithRealDelete(bankaccbasVO);
	    baseDao.updateVO(psnBankaccVO);
	}
	
	 private PsnBankaccUnionVO reQueryVO(PsnBankaccUnionVO vo)
			 /*     */     throws MetaDataException, DAOException
			 /*     */   {
			 /* 414 */     BankAccbasVO bankaccbasvo = (BankAccbasVO)MDPersistenceService.lookupPersistenceQueryService().queryBillOfVOByPK(BankAccbasVO.class, vo
			 /* 415 */       .getBankaccbasVO().getPk_bankaccbas(), false);
			 /*     */     
			 /* 417 */     PsnBankaccVO psnbankaccvo = (PsnBankaccVO)baseDao.retrieveByPK(PsnBankaccVO.class, vo
			 /* 418 */       .getPsnbankaccVO().getPk_psnbankacc());
			 /* 419 */     PsnBankaccUnionVO newvo = new PsnBankaccUnionVO();
			 /* 420 */     newvo.setPsnbankaccVO(psnbankaccvo);
			 /* 421 */     newvo.setBankaccbasVO(bankaccbasvo);
			 /* 422 */     return newvo;
			 /*     */   }
	 
	 private void bdlockOperate(SuperVO[] vos)    throws BusinessException
	 /*     */   {
	 /* 107 */     for (SuperVO vo : vos) {
	 /* 108 */       BDPKLockUtil.lockSuperVO(new SuperVO[] { vo });
	 /*     */     }
	 /*     */   }
	 
	 private void versionValidate(SuperVO[] vos)    throws BusinessException
	 /*     */   {
	 /* 113 */     for (SuperVO vo : vos) {
	 /* 114 */       BDVersionValidationUtil.validateSuperVO(new SuperVO[] { vo });
	 /*     */     }
	 /*     */   }
	 
	 private void updateValidate(PsnBankaccUnionVO updateVO)
			 /*     */     throws ValidationException
			 /*     */   {
			 /* 341 */     RefPkExistsValidator refValidator = new RefPkExistsValidator(new String[] { "pk_psndoc" });
			 /* 342 */     ValidationFrameworkUtil.createValidationService(new Validator[] { refValidator }).validate(updateVO
			 /* 343 */       .getPsnbankaccVO());
			 /* 344 */     PsnbankaccNullValidation nullValidation = new PsnbankaccNullValidation();
			 /* 345 */     PsnbankaccsubValidation accsubValidation = new PsnbankaccsubValidation();
			 /* 346 */     PsnbankaccUniqueValidation uniqueValidation = new PsnbankaccUniqueValidation();
			 /* 347 */     PsnAndCustSubaccCurrtypeUniqueValidation accsubUniqueValidation = new PsnAndCustSubaccCurrtypeUniqueValidation();
			 /*     */     
			 /*     */ 
			 /* 350 */     IValidationService vService = ValidationFrameworkUtil.createValidationService(new Validator[] { nullValidation, accsubValidation, uniqueValidation, accsubUniqueValidation });
			 /*     */     
			 /* 352 */     vService.validate(updateVO);
			 /*     */     
			 /* 354 */     vService = ValidationFrameworkUtil.createValidationService(new Validator[] { new SingleDistributedUpdateValidator() });
			 /* 355 */     vService.validate(updateVO.getPsnbankaccVO());
			 /* 356 */     ValidationFrameworkUtil.createValidationService(new Validator[] { new BankaccIbanValidator() }).validate(updateVO
			 /* 357 */       .getBankaccbasVO());
			 /*     */   }
			 /*     */ 

}
