package nc.pubimpl.ic.m4d.api;

import nc.pubimpl.ic.api.maintain.BillMaintainTool;
import nc.pubitf.ic.m4d.api.IMaterialOutMaintainAPI;
import nc.uap.oba.word.merger.Logger;
import nc.vo.ic.m4d.entity.MaterialOutVO;
import nc.vo.pub.BusinessException;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nc.vo.scmpub.res.billtype.ICBillType;
import nc.vo.scmpub.util.ArrayUtil;

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

public class MaterialOutMaintainAPIImpl implements IMaterialOutMaintainAPI {

    @Override
    public MaterialOutVO[] insertBills(MaterialOutVO[] vos)
            throws BusinessException {
        try {
            if (ArrayUtil.isEmpty(vos)) {
                return null;
            }
            BillMaintainTool<MaterialOutVO> tool = getBillMaintainTool();
            return tool.insert(vos);
        } catch (Exception e) {
            ExceptionUtils.marsh(e);
            return null;
        }
    }


    @Override
    public MaterialOutVO[] batchInsertBills_RequiresNew(MaterialOutVO[] vos) throws BusinessException {
        try {
            if (ArrayUtil.isEmpty(vos)) {
                return null;
            }
            BillMaintainTool<MaterialOutVO> tool = getBillMaintainTool();
            return tool.insert(vos);
        } catch (Exception e) {
            ExceptionUtils.marsh(e);
            return null;
        }
    }

    @Override
    public MaterialOutVO[] insertBillsAutoPick(MaterialOutVO[] vos)
            throws BusinessException {
        try {
            if (ArrayUtil.isEmpty(vos)) {
                return null;
            }
            BillMaintainTool<MaterialOutVO> tool = getBillMaintainTool();
            return tool.insertAutoPick(vos);
        } catch (Exception e) {
            ExceptionUtils.marsh(e);
            return null;
        }
    }

    @Override
    public void deleteBillsByID(String[] ids) throws BusinessException {
        try {
            if (ArrayUtil.isEmpty(ids)) {
                return;
            }

            BillMaintainTool<MaterialOutVO> tool = getBillMaintainTool();
            tool.deleteBillsByID(ids);
        } catch (Exception e) {
            ExceptionUtils.marsh(e);
        }
    }

    @Override
    public void deleteBillsBySourceID(String[] sourceIDs)
            throws BusinessException {
        try {
            if (ArrayUtil.isEmpty(sourceIDs)) {
                return;
            }
            BillMaintainTool<MaterialOutVO> tool = getBillMaintainTool();
            String[] ids = tool.queryIDsBySourceHID(sourceIDs);

            deleteBillsByID(ids);
        } catch (Exception e) {
            ExceptionUtils.marsh(e);
        }
    }

    @Override
    public MaterialOutVO[] signBills(MaterialOutVO[] vos)
            throws BusinessException {
        try {
            if (ArrayUtil.isEmpty(vos)) {
                return null;
            }

            BillMaintainTool<MaterialOutVO> tool = getBillMaintainTool();
            return tool.sign(vos);
        } catch (Exception e) {
            ExceptionUtils.marsh(e);
            return null;
        }
    }

    @Override
    public MaterialOutVO[] cancelSignBills(String[] ids) throws BusinessException {
        try {
            if (ArrayUtil.isEmpty(ids)) {
                return null;
            }

            BillMaintainTool<MaterialOutVO> tool = getBillMaintainTool();
            return tool.cancelSign(ids);
        } catch (Exception e) {
            ExceptionUtils.marsh(e);
            return null;
        }
    }

    private BillMaintainTool<MaterialOutVO> getBillMaintainTool() {
        BillMaintainTool<MaterialOutVO> tool =
                new BillMaintainTool<MaterialOutVO>(MaterialOutVO.class,
                        ICBillType.MaterialOut.getCode());
        return tool;
    }

}
