package com.cim.idm.service.Impl;

import com.cim.idm.exception.GlobalException;
import com.cim.idm.framework.IDMFrameServiceProxy;
import com.cim.idm.model.ReturnInstockDto;
import com.cim.idm.service.IReturnMaterialService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Service
public class ReturnMaterialServiceImpl implements IReturnMaterialService {

	/**
	 * 调用存储过程 RECEIVE_INSTOCK_DIRECT
	 */
	@Override
	public Map<String, Object> returnInstock(ReturnInstockDto returnInstockDto) {
		// 参数非空校验
		String site = returnInstockDto.getSite();
		String barCode = returnInstockDto.getBarCode();
		String billNo = returnInstockDto.getBillNo();
		String detailRowNo = returnInstockDto.getDetailRowNo();
		String userId = returnInstockDto.getUserId();
		String storageName = returnInstockDto.getStorageName();
		
		if(Objects.isNull(site)) {
			throw new GlobalException("site不能为空！");
		}
		if(Objects.isNull(barCode)) {
			throw new GlobalException("barCode不能为空！");
		}
		if(Objects.isNull(billNo)) {
			throw new GlobalException("billNo不能为空！");
		}
		if(Objects.isNull(detailRowNo)) {
			throw new GlobalException("detailRowNo不能为空！");
		}
		if(Objects.isNull(userId)) {
			throw new GlobalException("userId不能为空！");
		}
		Map<String, Object> map = new HashMap<String, Object>();
		// 将前端传入的参数和存储过程中的参数对应起来
		map.put("V_SITE",site);
		map.put("V_BILLNO",billNo);
		map.put("V_DETAILROWNO",detailRowNo);
		map.put("V_BARCODE",barCode);
		map.put("V_USERID",userId);
		map.put("V_KW",storageName);
		map.put("V_RESULT", "");
		try {
			// 调用存储过程返回参数
			String procedureName = "CALL RECEIVE_INSTOCK_DIRECT(:V_SITE, :V_BILLNO, :V_DETAILROWNO, :V_BARCODE,:V_KW, :V_USERID, :V_RESULT)";
			Map<String, Object> queryResult = IDMFrameServiceProxy.getSqlTemplate().queryForMap(procedureName,map);
			
			if(Objects.nonNull(queryResult)) {
				return queryResult;
			}
			throw new GlobalException("未查询到对应参数");
			
		}catch(Throwable e) {
			throw e;
		}
	
	}



}
