package nc.impl.scmf.ic.mbatchcode.rule;

import nc.impl.pubapp.pattern.rule.IRule;
import nc.vo.bd.meta.BatchOperateVO;
import nc.vo.pub.BusinessException;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nc.vo.scmf.ic.mbatchcode.BatchcodeVO;

/**
 * @description
 *              批次档案保存时检查编码字段长度
 * @scene
 *        批次档案保存时
 * @param
 * 
 * @author zhangsla
 * 
 *         2013-05-08 上午10:30:54
 * @since 6.3
 */
public class BatchcodeLengthRule implements IRule<BatchOperateVO> {

  @Override
  public void process(BatchOperateVO[] vos) {
    if (vos == null || vos.length == 0)
      return;
    if (vos[0].getAddObjs() != null && vos[0].getAddObjs().length != 0) {
      checkLength(vos[0].getAddObjs());
    }
    if (vos[0].getUpdObjs() != null && vos[0].getUpdObjs().length != 0) {
      checkLength(vos[0].getUpdObjs());
    }
  }

  private void checkLength(Object[] vos) {
    int length;
    for (int i = 0; i < vos.length; i++) {
      if (vos[i] instanceof BatchcodeVO) {
        length = ((BatchcodeVO) vos[i]).getVbatchcode().getBytes().length;
        if (length > 1000) {
          ExceptionUtils
              .wrappException(new BusinessException(nc.vo.ml.NCLangRes4VoTransl
                  .getNCLangRes().getStrByID("4008028_0", "04008028-0065")/*@res "生成批次号超出最大长度，请修改批次号规则或手工录入批次号!"*/));
        }
      }
    }
  }

}
