package nccloud.api.impl.ic.m47;

import nc.bs.framework.common.NCLocator;
import nc.itf.ic.m47.ISubcontInMaintain;
import nc.vo.ic.m47.entity.SubcontInVO;
import nc.vo.scmpub.check.billvalidate.BillVOsCheckRule;
import nc.vo.scmpub.res.billtype.ICBillType;
import nccloud.api.ic.m47.IAPISubcontInMaintain;
import nccloud.api.impl.ic.m47.check.CheckSubcontInSaveValidator;
import nccloud.api.impl.ic.m47.fill.SubcontInSaveFillValue;
import nccloud.openapi.ic.util.ICAPILocationVOUtils;
import nccloud.openapi.scmpub.pub.TransferCodeToPKTool;
import nccloud.openapi.scmpub.pub.TransferMapToVOTool;

import java.util.List;
import java.util.Map;

/**
 * @Classname APISubcontInMaintainImpl
 * @Description TODO
 * @Version 1.0.0
 * @Date 2025/5/15 9:46
 * @Created by ame
 */
public class APISubcontInMaintainImpl implements IAPISubcontInMaintain {
    @Override
    public SubcontInVO[] save(List<Map<String, Object>> subcontInVOList) throws Exception {
        List<SubcontInVO> aggVOList = TransferMapToVOTool.transferMapToAggVO(subcontInVOList, SubcontInVO.class);
        SubcontInVO[] vos = aggVOList.toArray(new SubcontInVO[0]);
        // 设置货位序列号VO
        ICAPILocationVOUtils.setLocationVO(ICBillType.PurchaseIn.getCode(), vos);
        // 1、传入数据基本非空校验
        BillVOsCheckRule checker =
                new BillVOsCheckRule(new CheckSubcontInSaveValidator());
        checker.check(vos);
        // 2、编码翻译成pk
        aggVOList = TransferCodeToPKTool.transferAggVO(aggVOList);
        // 3、翻译货位
        ICAPILocationVOUtils.translate(vos);

        new SubcontInSaveFillValue().setDefaultValue(vos);
        SubcontInVO[] insert = NCLocator.getInstance().lookup(ISubcontInMaintain.class).insert(vos);
        return insert;
    }
}
