package nccloud.api.impl.ic.onhand;

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

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import nc.bs.framework.common.NCLocator;
import nc.itf.ic.onhand.OnhandResService;
import nc.vo.ic.onhand.entity.OnhandDimVO;
import nc.vo.ic.onhand.entity.OnhandVO;
import nc.vo.pub.BusinessException;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nccloud.api.ic.onhand.IAPIOnhandQuery;
import nccloud.api.ic.onhand.OnhandDataSupplement;
import nccloud.openapi.ic.onhand.mapping.OnhandMapping;
import nccloud.openapi.scmpub.pub.TransferCodeToPKTool;
import nccloud.openapi.scmpub.pubitf.IJsonParamMapping;

/**
 * 现存量查询操作实现类
 *
 * @version NCC1909
 * @Description:
 * @author: shijkb
 * @date: 2019年11月14日 上午9:55:49
 */
public class APIOnhandQueryIpml implements IAPIOnhandQuery {

    String[] requiredField = new String[]{
            "pk_org", "cmaterialoid"
    };

    private IJsonParamMapping jsonParamMapping = new OnhandMapping();

    @Override
    public OnhandVO[] queryOnhandVOByDims(Map<String, Object> paramMap) throws BusinessException {


        // 必输项检查
        this.requiredFieldCheck(requiredField, paramMap);
        // 翻译
        paramMap = TransferCodeToPKTool.doTranslateFields(jsonParamMapping, paramMap);

        OnhandDataSupplement dataSupplement = new OnhandDataSupplement();
        OnhandDimVO onhandDimVO = new OnhandDimVO();
        try {
            onhandDimVO = dataSupplement.process(paramMap);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            ExceptionUtils.marsh(e);
        }

        OnhandVO[] onhandVOs = NCLocator.getInstance().lookup(OnhandResService.class)
                .queryOnhandVOByDims(new OnhandDimVO[]{onhandDimVO});
        return onhandVOs;
    }

    @Override
    public OnhandVO[] queryOnhandVOByDims(List<Map<String, Object>> paramMapList) throws Exception {
        // 必输项检查
        try {
            List<OnhandDimVO> onhandDimVOS = new ArrayList<>();
            for (Map<String, Object> paramMap : paramMapList) {
                this.requiredFieldCheck(requiredField, paramMap);
                // 翻译
                paramMap = TransferCodeToPKTool.doTranslateFields(jsonParamMapping, paramMap);
                OnhandDataSupplement dataSupplement = new OnhandDataSupplement();
                OnhandDimVO onhandDimVO = new OnhandDimVO();
                onhandDimVO = dataSupplement.process(paramMap);
                onhandDimVOS.add(onhandDimVO);
            }
            OnhandVO[] onhandVOs = NCLocator.getInstance().lookup(OnhandResService.class)
                    .queryOnhandVOByDims(onhandDimVOS.toArray(new OnhandDimVO[0]));
            return onhandVOs;
        } catch (Exception e) {
            ExceptionUtils.marsh(e);
            return null;
        }
    }


    @Override
    public OnhandVO[] queryOnhandVOByCondition(Map<String, Object> paramMap) throws Exception {
        this.requiredFieldCheck1(paramMap);
        // 翻译
        paramMap = TransferCodeToPKTool.doTranslateFields(jsonParamMapping, paramMap);
        OnhandDataSupplement dataSupplement = new OnhandDataSupplement();
        OnhandDimVO onhandDimVO = new OnhandDimVO();
        try {
            onhandDimVO = dataSupplement.process(paramMap);
        } catch (Exception e) {
            ExceptionUtils.marsh(e);
        }
        OnhandVO[] onhandVOs = NCLocator.getInstance().lookup(OnhandResService.class)
                .queryOnhandVOByDims(new OnhandDimVO[]{onhandDimVO});
        return onhandVOs;
    }


    // 必输项校验
    private void requiredFieldCheck(String[] requiredField, Map<String, Object> paramMap) {
        for (String field : requiredField) {
            Object keyValue = paramMap.get(field);
            if (null == keyValue) {
                ExceptionUtils.wrappBusinessException("字段:" + field + "为必输项!");
            }
        }
    }

    private void requiredFieldCheck1(Map<String, Object> paramMap) {
        Object orgValue = paramMap.get("pk_org");
        if (ObjectUtil.isEmpty(orgValue)) {
            ExceptionUtils.wrappBusinessException("组织字段为必输项!");
        }
        Object cmaterialoid = paramMap.get("cmaterialoid");
        Object cwarehouseid = paramMap.get("cwarehouseid");
        if (ObjectUtil.isEmpty(cmaterialoid) && ObjectUtil.isEmpty(cwarehouseid)) {
            ExceptionUtils.wrappBusinessException("仓库和物料字段不能都为空!");
        }
    }

}
