package com.cim.idm.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cim.idm.exception.GlobalException;
import com.cim.idm.framework.IDMFrameServiceProxy;
import com.cim.idm.framework.data.EventInfo;
import com.cim.idm.model.*;
import com.cim.idm.model.po.MaterialReceiveRequest;
import com.cim.idm.service.QueryService;
import com.cim.idm.utils.AjaxResult;
import com.cim.idm.utils.EventInfoUtil;
import com.cim.idm.utils.PageResult;
import com.cim.idm.utils.ToUpperUtils;
import com.cim.idm.wmsextend.generic.errorHandler.CustomException;
import com.cim.idm.wmspackage.materialpacking.MaterialPackingServiceProxy;
import com.cim.idm.wmspackage.materialpacking.management.data.MaterialPackingKey;
import com.cim.idm.wmspackage.materialpacking.management.info.SetEventInfo;
import com.cim.idm.wmspackage.materialshiprequest.MaterialShipRequestServiceProxy;
import com.cim.idm.wmspackage.materialshiprequest.management.info.CreateMaterialShipRequestInfo;
import com.cim.idm.wmspackage.materialshiprequestdetail.MaterialShipRequestDetailServiceProxy;
import com.cim.idm.wmspackage.materialshiprequestdetail.management.info.CreateMaterialShipRequestDetailInfo;
import com.cim.idm.wmspackage.name.NameServiceProxy;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import com.cim.idm.service.Impl.InvoiceServiceImpl;


@Api("单据接口")
@RestController
@RequestMapping("/invoice")
@EnableAutoConfiguration
public class InvoiceController {
	
	@Autowired
	private InvoiceServiceImpl InvoiceServiceImpl;

	@Autowired
	private QueryService queryService;


	
	/******************************************************************************************************************************/
	/*****************************************调拨入库单********************************************************************************/
	/******************************************************************************************************************************/
	
	/**
	 * 获取调拨退待操作单号集合
	 * @param in
	 * @return
	 */
	@RequestMapping(value = "/getTransferInList", method = RequestMethod.POST)
	public AjaxResult getTransferInList(@RequestBody JSONObject in ){

		MaterialshipRequest materialShipRequest = JSON.toJavaObject(in, MaterialshipRequest.class);
		String shipRequestName = materialShipRequest.getShipRequestName() == null ? "" : materialShipRequest.getShipRequestName();
		String shipRequestType = materialShipRequest.getShipRequestType();
		String sql = "SELECT T.SHIPREQUESTNAME,T.SHIPREQUESTDETAILNAME,T.MATERIALSPECNAME,T.REQUESTQUANTITY ,T.ASSIGNEDQUANTITY FROM MATERIALSHIPREQUESTDETAIL T  LEFT JOIN MATERIALSHIPREQUEST M ON T.SHIPREQUESTNAME=M.SHIPREQUESTNAME  WHERE M.SHIPREQUESTTYPE = :SHIPREQUESTTYPE AND T.SHIPREQUESTNAME LIKE :SHIPREQUESTNAME AND M.SHIPREQUESTSTATE = 'Created'";

		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("SHIPREQUESTTYPE",shipRequestType); 
		bindMap.put("SHIPREQUESTNAME","%" + shipRequestName + "%"); 
		List<Map<String,Object>> list = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, bindMap);

		if(Objects.isNull(list) || list.isEmpty()) {
			throw new GlobalException("未找到对应的数据！");
		}
		return AjaxResult.me().setResultObj(list);

	}
	
	/**
	 * 根据调拨退库单号获取明细
	 * @param in
	 * @return
	 */
	@RequestMapping(value = "/getTransferInLine", method = RequestMethod.POST)
	public AjaxResult getTransferInLine(@RequestBody JSONObject in) {

		MaterialshipRequest sl = JSON.toJavaObject(in, MaterialshipRequest.class);
		String shipRequestName = sl.getShipRequestName();
		String sql = "SELECT M.REQUESTQUANTITY,M.ASSIGNEDQUANTITY,SHIPREQUESTNAME || ' ' || M.SHIPREQUESTDETAILNAME  WL FROM MATERIALSHIPREQUESTDETAIL M WHERE M.SHIPREQUESTNAME  = :SHIPREQUESTNAME";

		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("SHIPREQUESTNAME", shipRequestName);
		List<Map<String, Object>> list = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, bindMap);

		
		if (Objects.isNull(list) || list.isEmpty()) {
			throw new GlobalException("未找到对应的数据！");
		}
		List<Map<String, Object>> upper = new ToUpperUtils().toUpper(list);
	      return AjaxResult.me().setResultObj(upper);
	}

	/**
	 * 根据调拨入库单行信息获取行详细
	 * @param in
	 * @return
	 */
	@RequestMapping(value = "/getTransferInLineInfo", method = RequestMethod.POST)
	public AjaxResult getTransferInLineInfo(@RequestBody JSONObject in) {

		MaterialshipRequest ms = JSON.toJavaObject(in, MaterialshipRequest.class);
		String shipRequestName = ms.getShipRequestName();
		if(Objects.isNull(shipRequestName)) {
			throw new GlobalException("下拉框选项不能为空！");
		}
		String[] split = shipRequestName.split(" ");
		String sql ="SELECT\r\n" + 
				"	M.MATERIALSPECNAME ,\r\n" + 
				"	M2.DESC_CN ,\r\n" + 
				"	M.REQUESTQUANTITY ,\r\n" + 
				"	M.ASSIGNEDQUANTITY ,\r\n" + 
				"	SS.SPECNAME,M.SHIPREQUESTNAME,M.SHIPREQUESTDETAILNAME,M.PHASE \r\n" + 
				"FROM\r\n" + 
				"	MATERIALSHIPREQUESTDETAIL M\r\n" + 
				"LEFT JOIN MATERIALSPEC M2 ON\r\n" + 
				"	M.MATERIALSPECNAME = M2.MATERIALSPECNAME\r\n" + 
				"LEFT JOIN SDK_SPEC SS ON\r\n" + 
				"	M.SDK_ID = SS.SDK_ID\r\n" + 
				"WHERE M.SHIPREQUESTNAME = :SHIPREQUESTNAME\r\n" + 
				"AND M.SHIPREQUESTDETAILNAME = :SHIPREQUESTDETAILNAME";
		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("SHIPREQUESTNAME", split[0]);
		bindMap.put("SHIPREQUESTDETAILNAME", split[1]);
		List<Map<String, Object>> list = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, bindMap);

		
		if (Objects.isNull(list) || list.isEmpty()) {
			throw new GlobalException("未找到对应的数据！");
		}
	      return AjaxResult.me().setResultObj(list);
	}
	
	@RequestMapping(value = "/getTransferInBindBarCode", method = RequestMethod.POST)
	public AjaxResult getTransferInBindBarCode(@RequestBody JSONObject in) {

		MaterialshipRequest ms = JSON.toJavaObject(in, MaterialshipRequest.class);
		String shipRequestName = ms.getShipRequestName();
		if(Objects.isNull(shipRequestName)) {
			throw new GlobalException("单号信息不能为空！");
		}
		String sql ="SELECT\r\n" + 
				"	M.MATERIALPACKINGNAME ,\r\n" + 
				"	M.CHARGE ,\r\n" + 
				"	M2.DESC_CN ,\r\n" + 
				"	M.ERPFACTORY ,\r\n" + 
				"	M.ERPLOCATION ,\r\n" + 
				"	M.MATERIALQUANTITY ,\r\n" + 
				"	M.LOCATIONNAME ,\r\n" + 
				"	M.MATERIALSPECNAME \r\n" + 
				"FROM\r\n" + 
				"	MATERIALPACKING M\r\n" + 
				"LEFT JOIN MATERIALSPEC M2 ON\r\n" + 
				"	M.MATERIALSPECNAME = M2.MATERIALSPECNAME\r\n" + 
				"LEFT JOIN SDK_SPEC SS ON\r\n" + 
				"	M.SDK_ID = SS.SDK_ID\r\n" + 
				"WHERE\r\n" + 
				"	M.SHIPREQUESTNAME = :SHIPREQUESTNAME";
		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("SHIPREQUESTNAME", shipRequestName);
		List<Map<String, Object>> list = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, bindMap);

		
		if (Objects.isNull(list) || list.isEmpty()) {
			throw new GlobalException("未找到对应的数据！");
		}
	      return AjaxResult.me().setResultObj(list);
	}
	
	
	
	/******************************************************************************************************************************/
	/*****************************************移库单********************************************************************************/
	/******************************************************************************************************************************/
	
	
	
	/**
	 * 获取移库待操作单号集合
	 * @param in
	 * @return
	 */
	@RequestMapping(value = "/getMoveInList", method = RequestMethod.POST)
	public AjaxResult getMoveInList(@RequestBody JSONObject in ){

		MaterialshipRequest materialShipRequest = JSON.toJavaObject(in, MaterialshipRequest.class);
		String shipRequestName = materialShipRequest.getShipRequestName() == null ? "" : materialShipRequest.getShipRequestName();
		String shipRequestType = materialShipRequest.getShipRequestType();
		String sql = "SELECT T.SHIPREQUESTNAME FROM MATERIALSHIPREQUEST T WHERE T.SHIPREQUESTTYPE = :SHIPREQUESTTYPE AND T.SHIPREQUESTNAME LIKE :SHIPREQUESTNAME AND T.SHIPREQUESTSTATE = 'Created'";

		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("SHIPREQUESTTYPE",shipRequestType); 
		bindMap.put("SHIPREQUESTNAME","%" + shipRequestName + "%"); 
		List<Map<String,Object>> list = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, bindMap);

		if(Objects.isNull(list) || list.isEmpty()) {
			throw new GlobalException("未找到对应的数据！");
		}
		return AjaxResult.me().setResultObj(list);

	}
	
	/**
	 * 根据移库单号获取明细
	 * @param in
	 * @return
	 */
	@RequestMapping(value = "/getMoveInLine", method = RequestMethod.POST)
	public AjaxResult getMoveInLine(@RequestBody JSONObject in) {

		MaterialshipRequest sl = JSON.toJavaObject(in, MaterialshipRequest.class);
		String shipRequestName = sl.getShipRequestName();
		String sql = "SELECT M.SHIPREQUESTNAME  || ' ' || M.SHIPREQUESTDETAILNAME  WL FROM MATERIALSHIPREQUESTDETAIL M WHERE M.SHIPREQUESTNAME  = :SHIPREQUESTNAME";

		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("SHIPREQUESTNAME", shipRequestName);
		List<Map<String, Object>> list = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, bindMap);

		
		if (Objects.isNull(list) || list.isEmpty()) {
			throw new GlobalException("未找到对应的数据！");
		}
		List<Map<String, Object>> upper = new ToUpperUtils().toUpper(list);
	      return AjaxResult.me().setResultObj(upper);
	}

	/**
	 * 根据移库单行信息获取行详细
	 * @param in
	 * @return
	 */
	@RequestMapping(value = "/getMoveInLineInfo", method = RequestMethod.POST)
	public AjaxResult getMoveInLineInfo(@RequestBody JSONObject in) {

		MaterialshipRequest ms = JSON.toJavaObject(in, MaterialshipRequest.class);
		String shipRequestName = ms.getShipRequestName();
		if(Objects.isNull(shipRequestName)) {
			throw new GlobalException("下拉框选项不能为空！");
		}
		String[] split = shipRequestName.split(" ");
		String sql ="SELECT\r\n" + 
				"	M.MATERIALSPECNAME ,\r\n" + 
				"	M2.DESC_CN ,\r\n" + 
				"	M.REQUESTQUANTITY ,\r\n" + 
				"	M.ASSIGNEDQUANTITY ,\r\n" + 
				"	SS.SPECNAME,M.SHIPREQUESTNAME,M.SHIPREQUESTDETAILNAME,M.PHASE,MM.ERPFACTORY,MM.ERPLOCATION,MM.ERPRECEIVEFACTORY,MM.ERPRECEIVELOCATION  \r\n" + 
				"FROM\r\n" + 
				"	MATERIALSHIPREQUESTDETAIL M\r\n" + 
				"LEFT JOIN MATERIALSPEC M2 ON\r\n" + 
				"	M.MATERIALSPECNAME = M2.MATERIALSPECNAME\r\n" + 
				"LEFT JOIN SDK_SPEC SS ON\r\n" + 
				"	M.SDK_ID = SS.SDK_ID\r\n" + 
				"LEFT JOIN MATERIALSHIPREQUEST MM ON\r\n" + 
				"	MM.SHIPREQUESTNAME = M.SHIPREQUESTNAME\r\n" + 
				"WHERE M.SHIPREQUESTNAME = :SHIPREQUESTNAME\r\n" + 
				"AND M.SHIPREQUESTDETAILNAME = :SHIPREQUESTDETAILNAME";
		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("SHIPREQUESTNAME", split[0]);
		bindMap.put("SHIPREQUESTDETAILNAME", split[1]);
		List<Map<String, Object>> list = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, bindMap);

		
		if (Objects.isNull(list) || list.isEmpty()) {
			throw new GlobalException("未找到对应的数据！");
		}
	      return AjaxResult.me().setResultObj(list);
	}
	
	@RequestMapping(value = "/getMoveInBindBarCode", method = RequestMethod.POST)
	public AjaxResult getMoveInBindBarCode(@RequestBody JSONObject in) {

		MaterialshipRequest ms = JSON.toJavaObject(in, MaterialshipRequest.class);
		String shipRequestName = ms.getShipRequestName();
		if(Objects.isNull(shipRequestName)) {
			throw new GlobalException("单号信息不能为空！");
		}
		String sql ="SELECT\r\n" + 
				"	M.MATERIALPACKINGNAME ,\r\n" + 
				"	M.CHARGE ,\r\n" + 
				"	M2.DESC_CN ,\r\n" + 
				"	M.ERPFACTORY ,\r\n" + 
				"	M.ERPLOCATION ,\r\n" + 
				"	M.MATERIALQUANTITY ,\r\n" + 
				"	M.LOCATIONNAME ,\r\n" + 
				"	M.MATERIALSPECNAME \r\n" + 
				"FROM\r\n" + 
				"	MATERIALPACKING M\r\n" + 
				"LEFT JOIN MATERIALSPEC M2 ON\r\n" + 
				"	M.MATERIALSPECNAME = M2.MATERIALSPECNAME\r\n" + 
				"LEFT JOIN SDK_SPEC SS ON\r\n" + 
				"	M.SDK_ID = SS.SDK_ID\r\n" + 
				"WHERE\r\n" + 
				"	M.SHIPREQUESTNAME = :SHIPREQUESTNAME";
		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("SHIPREQUESTNAME", shipRequestName);
		List<Map<String, Object>> list = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, bindMap);

		
		if (Objects.isNull(list) || list.isEmpty()) {
			throw new GlobalException("未找到对应的数据！");
		}
	      return AjaxResult.me().setResultObj(list);
	}
	
	
	/******************************************************************************************************************************/
	/*****************************************出库单********************************************************************************/
	/******************************************************************************************************************************/
	/**
	 * 获取出库单待审核
	 * @param in
	 * @return
	 */
	@RequestMapping(value = "/getShipList", method = RequestMethod.POST)
	public AjaxResult getShipList(@RequestBody JSONObject in ){

		MaterialshipRequest materialShipRequest = JSON.toJavaObject(in, MaterialshipRequest.class);
		String shipRequestName = materialShipRequest.getShipRequestName() == null ? "" : materialShipRequest.getShipRequestName();
		String shipRequestType = materialShipRequest.getShipRequestType();
		String sql = "SELECT DISTINCT T.SHIPREQUESTNAME FROM MATERIALSHIPREQUEST T "
				+ "WHERE T.SHIPREQUESTTYPE = :SHIPREQUESTTYPE "
				+ "AND T.SHIPREQUESTNAME LIKE :SHIPREQUESTNAME AND ( RETURNFLAG <> 'Y' OR RETURNFLAG IS NULL )"
				+ "AND SHIPREQUESTSTATE <> 'Completed' ";

		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("SHIPREQUESTTYPE",shipRequestType); 
		bindMap.put("SHIPREQUESTNAME","%" + shipRequestName + "%"); 
		List<Map<String,Object>> list = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, bindMap);

		if(Objects.isNull(list) || list.isEmpty()) {
			throw new GlobalException("未找到出库单据！");
		}
		return AjaxResult.me().setResultObj(list);

	}
	
	
	@RequestMapping(value = "/getShipReservedBoxInfo", method = RequestMethod.POST)
	public AjaxResult getShipReservedBoxInfo(@RequestBody JSONObject in ) throws Exception{

		MaterialshipRequest ms = JSON.toJavaObject(in, MaterialshipRequest.class);
		String shipRequestName = ms.getShipRequestName();
		int pageNum = ms.getPageNum();
		int pageSize = ms.getPageSize();
		if(Objects.isNull(shipRequestName)) {
			throw new GlobalException("单号信息不能为空！");
		}
		String sql ="SELECT\r\n" + 
				"	M.MATERIALPACKINGNAME ,\r\n" + 
				"	M.CHARGE ,\r\n" + 
				"	M2.DESC_CN ,\r\n" + 
				"	M.ERPFACTORY ,\r\n" + 
				"	M.ERPLOCATION ,\r\n" + 
				"	M.MATERIALQUANTITY ,\r\n" + 
				"	M.LOCATIONNAME ,\r\n" + 
				"	M.MATERIALSPECNAME, '' ISCHECK \r\n" + 
				"FROM\r\n" + 
				"	MATERIALPACKING M\r\n" + 
				"LEFT JOIN MATERIALSPEC M2 ON\r\n" + 
				"	M.MATERIALSPECNAME = M2.MATERIALSPECNAME\r\n" + 
				"LEFT JOIN SDK_SPEC SS ON\r\n" + 
				"	M.SDK_ID = SS.SDK_ID\r\n" + 
				"WHERE\r\n" + 
				"	M.SHIPREQUESTNAME = :SHIPREQUESTNAME AND M.STOCKSTATE = 'Stocked'";
		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("SHIPREQUESTNAME", shipRequestName);
//		List<Map<String, Object>> list = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, bindMap);
		PageResult<Map<String, Object>> queryResultPage = queryService.getQueryResultPage(sql, bindMap, pageNum, pageSize);
		
		if (Objects.isNull(queryResultPage)) {
			throw new GlobalException("未找到对应的数据！");
		}
		return AjaxResult.me().setResultObj(queryResultPage);

	}
	
	
	/******************************************************************************************************************************/
	/*****************************************出库过账********************************************************************************/

	@RequestMapping(value = "/commitShipRequestNameNew", method = RequestMethod.POST)
	public AjaxResult commitShipRequestNameNew(@RequestBody JSONObject in ) {
		StockOutDto ms = JSON.toJavaObject(in, StockOutDto.class);
		String shipRequestName = ms.getShipRequestName();
		String siteName = ms.getSiteName();
		String user = ms.getUser();
		String erpfactory = ms.getErpfactory();
		String erplocation = ms.getErplocation();
		String locationName = ms.getLocationName();
		String shipRequestType = ms.getShipRequestType();
		String commitDate = ms.getCommitDate();
		String opCode = ms.getOpCode();
		List<com.cim.idm.wmspackage.materialpacking.management.data.MaterialPacking> boxList = ms.getBoxList();
		String sendERPStockOut = null;
		String sql = "SELECT m.SHIPREQUESTTYPE FROM MATERIALSHIPREQUEST m WHERE m.SHIPREQUESTNAME = :SHIPREQUESTNAME";
		Map<String, Object> hashMap = new HashMap<String,Object> ();
		hashMap.put("SHIPREQUESTNAME", shipRequestName);
		List<Map<String, Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, hashMap);
		if (queryForList != null && queryForList.size() > 0) {
			String type = queryForList.get(0).get("SHIPREQUESTTYPE").toString();
			shipRequestType = type;
		} else {
			return AjaxResult.me().setErrorCode(500).setMessage("找不到出库单据" + shipRequestName);
		}

        try {
            InvoiceServiceImpl.SendERPStockOut20240802(shipRequestName, siteName, user, boxList,commitDate,opCode);
        } catch (Exception e) {
			return AjaxResult.me().setErrorCode(500).setMessage(e.toString());
        }


        return AjaxResult.me().setResultObj(null);
	}
	

	
	/******************************************************************************************************************************/
	/*****************************************其他入库单********************************************************************************/
	/******************************************************************************************************************************/
	/**
	 * 获取其他入库单单号集合
	 * @param in
	 * @return
	 */
	@RequestMapping(value = "/getOtherInList", method = RequestMethod.POST)
	public AjaxResult getOtherInList(@RequestBody JSONObject in ){

		MaterialReceiveRequest materialReceiveRequest = JSON.toJavaObject(in, MaterialReceiveRequest.class);
		String receiveRequestName = materialReceiveRequest.getReceiveRequestName();
		String receiveRequestType = materialReceiveRequest.getReceiveRequestType();

		String sql = ""
				+ "SELECT\r\n" + 
				"	*\r\n" + 
				"FROM\r\n" + 
				"	MATERIALRECEIVEREQUESTDETAIL M\r\n" + 
				"LEFT JOIN MATERIALRECEIVEREQUEST M2 ON\r\n" + 
				"	M.RECEIVEREQUESTNAME = M2.RECEIVEREQUESTNAME\r\n" + 
				"WHERE\r\n" + 
				"	M2.RECEIVEREQUESTNAME LIKE :RECEIVEREQUESTNAME\r\n" + 
				"	AND M2.RECEIVEREQUESTTYPE = :RECEIVEREQUESTTYPE\r\n" + 
				"	AND M.RECEIVEDQUANTITY < M.REQUESTQUANTITY ";

		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("RECEIVEREQUESTTYPE",receiveRequestType); 
		bindMap.put("RECEIVEREQUESTNAME","%" + receiveRequestName + "%"); 
		List<Map<String,Object>> list = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, bindMap);

		if(Objects.isNull(list) || list.isEmpty()) {
			throw new GlobalException("未找到对应的其他入库单数据！");
		}
		return AjaxResult.me().setResultObj(list);

	}
	
	/**
	 * 根据其他入库单单号获取明细
	 * @param in
	 * @return
	 */
	@RequestMapping(value = "/getOtherInLine", method = RequestMethod.POST)
	public AjaxResult getOtherInLine(@RequestBody JSONObject in) {

		MaterialReceiveRequest materialReceiveRequest = JSON.toJavaObject(in, MaterialReceiveRequest.class);
		String receiveRequestName = materialReceiveRequest.getReceiveRequestName();
		String receiveRequestType = materialReceiveRequest.getReceiveRequestType();
		String sql = "SELECT\r\n" + 
				"	M.RECEIVEDQUANTITY,M.REQUESTQUANTITY,M.RECEIVEREQUESTNAME || ' ' || M.RECEIVEREQUESTDETAILNAME WL\r\n" + 
				"FROM\r\n" + 
				"	MATERIALRECEIVEREQUESTDETAIL M\r\n" + 
				"LEFT JOIN MATERIALRECEIVEREQUEST M2 ON\r\n" + 
				"	M.RECEIVEREQUESTNAME = M2.RECEIVEREQUESTNAME\r\n" + 
				"WHERE\r\n" + 
				"	M.RECEIVEREQUESTNAME = :RECEIVEREQUESTNAME\r\n" + 
				"	AND M2.RECEIVEREQUESTTYPE = :RECEIVEREQUESTTYPE";

		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("RECEIVEREQUESTNAME", receiveRequestName);
		bindMap.put("RECEIVEREQUESTTYPE", receiveRequestType);
		List<Map<String, Object>> list = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, bindMap);

		
		if (Objects.isNull(list) || list.isEmpty()) {
			throw new GlobalException("未找到对应的数据！");
		}
		List<Map<String, Object>> upper = new ToUpperUtils().toUpper(list);
	      return AjaxResult.me().setResultObj(upper);
	}

	

	
	
	/******************************************************************************************************************************/
	/*****************************************其他入库单********************************************************************************/
	/******************************************************************************************************************************/
	/**
	 * 获取其他入库单单号集合
	 * @param in
	 * @return
	 */
	@RequestMapping(value = "/getCheckPlanList", method = RequestMethod.POST)
	public AjaxResult getCheckPlanList(@RequestBody JSONObject in ){

		CheckPlan CheckPlan = JSON.toJavaObject(in, CheckPlan.class);
		String checkPlanNo = CheckPlan.getCheckPlanNo();

		String sql = "SELECT DISTINCT C.CHECKPLANNAME FROM CHECKPLAN C WHERE C.CHECKPLANNAME LIKE :CHECKPLANNAME  AND C.CHECKPLANSTATE = 'Created'";

		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("CHECKPLANNAME","%" + checkPlanNo + "%"); 
		List<Map<String,Object>> list = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, bindMap);

		if(Objects.isNull(list) || list.isEmpty()) {
			throw new GlobalException("未找到对应的待盘点单数据！");
		}
		return AjaxResult.me().setResultObj(list);

	}
	
	/**
	 * 根据其他入库单单号获取明细
	 * @param in
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "/getCheckPlanInfo", method = RequestMethod.POST)
	public AjaxResult getCheckPlanInfo(@RequestBody JSONObject in) throws Exception {

		CheckPlan CheckPlan = JSON.toJavaObject(in, CheckPlan.class);
		String checkPlanNo = CheckPlan.getCheckPlanNo();
		String siteName = CheckPlan.getSiteName();
		int pageNum = CheckPlan.getPageNum();
		int pageSize = CheckPlan.getPageSize();
		String sql = "SELECT\r\n" + 
				"	DISTINCT \r\n" + 
				"	A.MATERIALPACKINGNAME,\r\n" + 
				"	m.CHARGE,\r\n" + 
				"	A.SYSLOCATION,\r\n" + 
				"	'' AS ACTLOCATION ,\r\n" + 
				"	'未盘点物理库位' AS LOCATIONSTATE1,\r\n" + 
				"	A.ERPLOCATION,\r\n" + 
				"	'' AS ERPLOCATION1,\r\n" + 
				"	'未盘点ERP库位' AS LOCATIONSTATE2,\r\n" + 
				"	A.MATERIALQUANTITY AS MATERIALQUANTITY1 ,\r\n" + 
				"	NULL AS MATERIALQUANTITY,\r\n" + 
				"	'未盘点数量' AS QTYSTATE,\r\n" + 
				"	A.MATERIALSPECNAME,\r\n" + 
				"	C.DESC_CN ,\r\n" + 
				"	A.LASTEVENTUSER,\r\n" + 
				"	A.LASTACTION,\r\n" + 
				"	'未盘点' AS CHECKRESULT\r\n" + 
				"FROM\r\n" + 
				"	CHECKPLAN A,\r\n" + 
				"	MATERIALSPEC C,\r\n" +
				"	MATERIALPACKING m\r\n" +
				"WHERE\r\n" + 
				"	(A.SITENAME = :SITENAME\r\n" + 
				"		AND A.CHECKPLANNAME = :CHECKPLANNAME\r\n" + 
				"		--AND A.PLANDATE = :PLANDATE \r\n" + 
				"		AND A.MATERIALPACKINGNAME = m.MATERIALPACKINGNAME\r\n" + 
				"		AND C.MATERIALSPECNAME = m.MATERIALSPECNAME\r\n" + 
				"		AND A.MATERIALSPECNAME = C.MATERIALSPECNAME\r\n" + 
				"		AND A.SITENAME = C.SITENAME) \r\n" + 
				"MINUS \r\n" + 
				"SELECT\r\n" + 
				"	DISTINCT \r\n" + 
				"	A.MATERIALPACKINGNAME,\r\n" + 
				"	m.CHARGE,\r\n" + 
				"	A.SYSLOCATION,\r\n" + 
				"	'' AS ACTLOCATION ,\r\n" + 
				"	'未盘点物理库位' AS LOCATIONSTATE1,\r\n" + 
				"	A.ERPLOCATION,\r\n" + 
				"	'' AS ERPLOCATION1,\r\n" + 
				"	'未盘点ERP库位' AS LOCATIONSTATE2,\r\n" + 
				"	A.MATERIALQUANTITY AS MATERIALQUANTITY1 ,\r\n" + 
				"	NULL AS MATERIALQUANTITY,\r\n" + 
				"	'未盘点数量' AS QTYSTATE,\r\n" + 
				"	A.MATERIALSPECNAME,\r\n" + 
				"	C.DESC_CN ,\r\n" + 
				"	A.LASTEVENTUSER,\r\n" + 
				"	A.LASTACTION,\r\n" + 
				"	'未盘点' AS CHECKRESULT\r\n" + 
				"FROM\r\n" + 
				"	CHECKPLAN A,\r\n" + 
				"	CHECKRECORD B,\r\n" + 
				"	MATERIALSPEC C,\r\n" + 
				"	MATERIALPACKING m\r\n" +
				"WHERE\r\n" + 
				"	(A.SITENAME = B.SITENAME\r\n" + 
				"		AND A.SITENAME = :SITENAME\r\n" + 
				"		AND A.CHECKPLANNAME = :CHECKPLANNAME\r\n" + 
				"		AND A.CHECKPLANNAME = B.CHECKPLANNAME\r\n" + 
				"		--AND A.PLANDATE = :PLANDATE \r\n" + 
				"		AND A.MATERIALPACKINGNAME = m.MATERIALPACKINGNAME\r\n" + 
				"		AND C.MATERIALSPECNAME = m.MATERIALSPECNAME\r\n" + 
				"		AND A.PLANDATE = B.PLANDATE\r\n" + 
				"		AND A.MATERIALPACKINGNAME = B.MATERIALPACKINGNAME\r\n" + 
				"		AND A.MATERIALSPECNAME = C.MATERIALSPECNAME\r\n" + 
				"		AND A.SITENAME = C.SITENAME)\r\n" + 
				"UNION ALL \r\n" + 
				"(\r\n" + 
				"SELECT\r\n" + 
				"	DISTINCT \r\n" + 
				"	A.MATERIALPACKINGNAME,\r\n" + 
				"	m.CHARGE,\r\n" + 
				"	A.SYSLOCATION,\r\n" + 
				"	B.ACTLOCATION,\r\n" + 
				"	CASE\r\n" + 
				"		WHEN B.SYSLOCATION = B.ACTLOCATION THEN '物理库位一致'\r\n" + 
				"		ELSE '物理库位异动'\r\n" + 
				"	END AS LOCATIONSTATE1,\r\n" + 
				"	A.ERPLOCATION,\r\n" + 
				"	B.ERPLOCATION AS ERPLOCATION1 ,\r\n" + 
				"	CASE\r\n" + 
				"		WHEN A.ERPLOCATION = B.ERPLOCATION THEN 'ERP库位一致'\r\n" + 
				"		ELSE 'ERP库位异动'\r\n" + 
				"	END AS LOCATIONSTATE2,\r\n" + 
				"	A.MATERIALQUANTITY AS MATERIALQUANTITY1,\r\n" + 
				"	B.MATERIALQUANTITY ,\r\n" + 
				"	CASE\r\n" + 
				"		WHEN B.MATERIALQUANTITY = A.MATERIALQUANTITY THEN '数量一致'\r\n" + 
				"		ELSE '数量有异'\r\n" + 
				"	END AS QTYSTATE,\r\n" + 
				"	A.MATERIALSPECNAME,\r\n" + 
				"	C.DESC_CN,\r\n" + 
				"	B.LASTEVENTUSER,\r\n" + 
				"	B.LASTACTION,\r\n" + 
				"	'已盘点' AS CHECKRESULT\r\n" + 
				"FROM\r\n" + 
				"	CHECKPLAN A,\r\n" + 
				"	CHECKRECORD B,\r\n" + 
				"	MATERIALSPEC C,\r\n" + 
				"	MATERIALPACKING m\r\n" +
				"WHERE\r\n" + 
				"	(A.SITENAME = B.SITENAME\r\n" + 
				"		AND A.SITENAME = :SITENAME\r\n" + 
				"		AND A.CHECKPLANNAME = :CHECKPLANNAME\r\n" + 
				"		AND A.CHECKPLANNAME = B.CHECKPLANNAME\r\n" + 
				"		AND A.MATERIALPACKINGNAME = m.MATERIALPACKINGNAME\r\n" + 
				"		AND C.MATERIALSPECNAME = m.MATERIALSPECNAME\r\n" + 
				"		--AND A.PLANDATE = :PLANDATE\r\n" + 
				"		AND A.PLANDATE = B.PLANDATE\r\n" + 
				"		AND A.MATERIALPACKINGNAME = B.MATERIALPACKINGNAME\r\n" + 
				"		AND A.MATERIALSPECNAME = C.MATERIALSPECNAME\r\n" + 
				"		AND A.SITENAME = C.SITENAME))\r\n" + 
				"UNION ALL \r\n" + 
				"(\r\n" + 
				"SELECT\r\n" + 
				"DISTINCT \r\n" + 
				"B.MATERIALPACKINGNAME,\r\n" + 
				"m.CHARGE,\r\n" + 
				"B.SYSLOCATION,\r\n" + 
				"B.ACTLOCATION ,\r\n" + 
				"'多盘物理库位' AS LOCATIONSTATE1,\r\n" + 
				"'' AS ERPLOCATION,\r\n" + 
				"B.ERPLOCATION AS ERPLOCATION1,\r\n" + 
				"'多盘ERP库位' AS LOCATIONSTATE2,\r\n" + 
				"NULL AS MATERIALQUANTITY1 ,\r\n" + 
				"B.MATERIALQUANTITY,\r\n" + 
				"'多盘数量' AS QTYSTATE,\r\n" + 
				"B.MATERIALSPECNAME,\r\n" + 
				"C.DESC_CN ,\r\n" + 
				"B.LASTEVENTUSER,\r\n" + 
				"B.LASTACTION,\r\n" + 
				"'多盘' AS CHECKRESULT\r\n" + 
				"FROM\r\n" + 
				"CHECKRECORD B,\r\n" + 
				"MATERIALSPEC C,\r\n" + 
				"MATERIALPACKING m\r\n" +
				"WHERE \r\n" + 
				"(B.SITENAME = :SITENAME\r\n" + 
				"	AND B.CHECKPLANNAME = :CHECKPLANNAME\r\n" + 
				"	--AND B.PLANDATE = :PLANDATE \r\n" + 
				"	AND B.MATERIALPACKINGNAME = m.MATERIALPACKINGNAME\r\n" + 
				"	AND C.MATERIALSPECNAME = m.MATERIALSPECNAME\r\n" + 
				"	AND B.MATERIALSPECNAME = C.MATERIALSPECNAME\r\n" + 
				"	AND B.SITENAME = C.SITENAME) \r\n" + 
				"MINUS\r\n" + 
				"SELECT\r\n" + 
				"	DISTINCT \r\n" + 
				"	B.MATERIALPACKINGNAME,\r\n" + 
				"   m.CHARGE,\r\n" + 
				"	B.SYSLOCATION,\r\n" + 
				"	B.ACTLOCATION ,\r\n" + 
				"	'多盘物理库位' AS LOCATIONSTATE1,\r\n" + 
				"	'' AS ERPLOCATION,\r\n" + 
				"	B.ERPLOCATION AS ERPLOCATION1,\r\n" + 
				"	'多盘ERP库位' AS LOCATIONSTATE2,\r\n" + 
				"	NULL AS MATERIALQUANTITY1 ,\r\n" + 
				"	B.MATERIALQUANTITY,\r\n" + 
				"	'多盘数量' AS QTYSTATE,\r\n" + 
				"	B.MATERIALSPECNAME,\r\n" + 
				"	C.DESC_CN ,\r\n" + 
				"	B.LASTEVENTUSER,\r\n" + 
				"	B.LASTACTION,\r\n" + 
				"	'多盘' AS CHECKRESULT\r\n" + 
				"FROM\r\n" + 
				"	CHECKPLAN A,\r\n" + 
				"	CHECKRECORD B,\r\n" + 
				"	MATERIALSPEC C,\r\n" + 
				"   MATERIALPACKING m\r\n" +
				"WHERE\r\n" + 
				"	(A.SITENAME = B.SITENAME\r\n" + 
				"		AND A.SITENAME = :SITENAME\r\n" + 
				"		AND A.CHECKPLANNAME = :CHECKPLANNAME\r\n" + 
				"		--AND A.PLANDATE = :PLANDATE \r\n" + 
				"		AND A.PLANDATE = B.PLANDATE\r\n" + 
				"	    AND B.MATERIALPACKINGNAME = m.MATERIALPACKINGNAME\r\n" + 
				"	    AND C.MATERIALSPECNAME = m.MATERIALSPECNAME\r\n" + 
				"		AND A.CHECKPLANNAME = B.CHECKPLANNAME\r\n" + 
				"		AND B.MATERIALPACKINGNAME = A.MATERIALPACKINGNAME\r\n" + 
				"		AND A.MATERIALSPECNAME = C.MATERIALSPECNAME\r\n" + 
				"		AND A.SITENAME = C.SITENAME))\r\n" + 
				"ORDER BY\r\n" + 
				"CHECKRESULT DESC";

		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("CHECKPLANNAME", checkPlanNo);
		bindMap.put("SITENAME", siteName);
		PageResult<Map<String, Object>> queryResultPage = queryService.getQueryResultPage(sql, bindMap, pageNum, pageSize);
	//	List<Map<String, Object>> list = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, bindMap);
		if (Objects.isNull(queryResultPage) ) {
			throw new GlobalException("未找到需要盘点的数据！");
		}
	      return AjaxResult.me().setResultObj(queryResultPage);
	}

	/**
	 * 盘点修改
	 * @param in
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "/getCheckPlanInfo1", method = RequestMethod.POST)
	public AjaxResult getCheckPlanInfo1(@RequestBody JSONObject in) throws Exception {

		CheckPlan1 CheckPlan = JSON.toJavaObject(in, CheckPlan1.class);
		String checkPlanNo = CheckPlan.getCheckPlanNo();
		String siteName = CheckPlan.getSiteName();
		String erplocation = CheckPlan.getERPLOCATION();
		String locationname = CheckPlan.getLOCATIONNAME();
		int pageNum = CheckPlan.getPageNum();
		int pageSize = CheckPlan.getPageSize();
		String sql = "SELECT c.ERPLOCATION,be.DESCRIPTION ERPLOCATION1,c.SYSLOCATION,s.DESCRIPTION ACTLOCATION1,c.MATERIALSPECNAME,m.DESC_CN,\r\n"
				+ "	SUM(C.MATERIALQUANTITY - NVL(c2.MATERIALQUANTITY,0)) Square_quantity  ,COUNT(C.MATERIALQUANTITY)-COUNT(c2.MATERIALQUANTITY)  Remaining_Quantity\r\n"
				+ "FROM CHECKPLAN C \r\n"
				+ "LEFT JOIN CHECKRECORD c2 ON C.CHECKPLANNAME = c2.CHECKPLANNAME   AND C.PLANDATE = c2.PLANDATE AND C.MATERIALPACKINGNAME  = c2.MATERIALPACKINGNAME\r\n"
				+ "LEFT JOIN BS_ERPLOCATION be ON c.ERPLOCATION = be.ERPLOCATIONNAME\r\n"
				+ "LEFT JOIN STORAGESPEC s ON c.SYSLOCATION = s.STORAGENAME AND c.ERPLOCATION = s.ERPLOCATION\r\n"
				+ "LEFT JOIN MATERIALSPEC m ON C.MATERIALSPECNAME = m.MATERIALSPECNAME\r\n"
				+ "WHERE c.CHECKPLANNAME = :CHECKPLANNAME \r\n"
				+ "GROUP BY c.ERPLOCATION,be.DESCRIPTION ,c.SYSLOCATION,s.DESCRIPTION ,c.MATERIALSPECNAME,m.DESC_CN\r\n"
				+ "ORDER  BY C.ERPLOCATION,c.SYSLOCATION,c.MATERIALSPECNAME";
		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("CHECKPLANNAME", checkPlanNo);
		bindMap.put("SITENAME", siteName);
		bindMap.put("LOCATIONNAME", locationname);
		bindMap.put("ERPLOCATION", erplocation);
//		PageResult<Map<String, Object>> queryResultPage = PDAQuery.getQueryResultPage(sql, bindMap, pageNum, pageSize);
		List<Map<String, Object>> list = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, bindMap);
		if (list.size() <= 0 ) {
			throw new GlobalException("未找到需要盘点的数据！");
		}
	      return AjaxResult.me().setResultObj(list);
	}
	
	/**
	 * 获取库存查询信息
	 * @param in
	 * @return
	 */
	@RequestMapping(value = "/getMaterialQuery", method = RequestMethod.POST)
	public AjaxResult getBarCode(@RequestBody JSONObject in ){

		MaterialPacking materialPacking = JSON.toJavaObject(in, MaterialPacking.class);
		String materialPackingName = materialPacking.getMaterialPackingName();
		String erplocaatin=materialPacking.getErpLocation();
		String location =materialPacking.getLocationName();
		String materialspec=materialPacking.getMaterialSpecName();
		String desc_cn=materialPacking.getDesc_cn();
		String sdk_id=materialPacking.getSdk_id();
		
		String sql="";
		//判断条码是否存在且状态是在库
		if(!("".equals(materialPackingName)))
		{
			 sql= "SELECT M.STOCKSTATE,M.RECEIVEACTNO FROM MATERIALPACKING M WHERE STOCKSTATE ='Stocked' AND M.MATERIALPACKINGNAME = :MATERIALPACKINGNAME ";
			
			Map<String, Object> bindMap = new HashMap<String, Object>();
			bindMap.put("MATERIALPACKINGNAME",materialPackingName); 
			List<Map<String,Object>> list = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, bindMap);
			if(list.size()==0) {
				throw new GlobalException("条码不存在或不为在库状态");
			}
		}
		String sql2 = "SELECT\r\n" + 
				"	mp.CHARGE,MP.MATERIALPACKINGNAME,mp.MATERIALSPECNAME, MU.FNAME UNIT, mp.SHIPREQUESTNAME, \r\n" + 
				"	mp.PHASE,\r\n" + 
				"	mp.TRUEGG ,mp.MATERIALQUANTITY,DECODE(mp.OQARESULT,'OK','合格','NG','不良','','待检') OQARESULT ,mp.HOLDSTATE, TO_CHAR(mp.MAKEDATE, 'YYYY-MM-DD') MAKEDATE,TO_CHAR(mp.EXPIRINGDATE, 'YYYY-MM-DD') EXPIRINGDATE,  \r\n" + 
				"	SS.SPECNAME ,mp.MJPC,\r\n" + 
				"	m.DESC_CN,BS.ERPLOCATIONNAME || ' ' || BS.DESCRIPTION as ERPLOCATION, O.DESCRIPTION as ERPFACTORY, SP.STORAGENAME || ' ' || SP.DESCRIPTION as LOCATIONNAME,m.DESCRIPTION\r\n" + 
				"FROM\r\n" + 
				"	MATERIALPACKING mp LEFT JOIN MATERIALSPEC m ON m.MATERIALSPECNAME = mp.MATERIALSPECNAME \r\n" + 
				"	LEFT JOIN SDK_SPEC SS ON SS.SDK_ID = mp.SDK_ID   \r\n" + 
				"	LEFT JOIN BS_ERPLOCATION BS ON BS.ERPLOCATIONNAME = mp.ERPLOCATION   \r\n" + 
				"	LEFT JOIN ORG O ON O.ORGNO = mp.ERPFACTORY   \r\n" + 
				"	LEFT JOIN STORAGESPEC SP ON SP.STORAGENAME = mp.LOCATIONNAME AND mp.ERPFACTORY = SP.ERPFACTORY  \r\n" + 
				"	LEFT JOIN MATERIALUNIT MU ON MU.FNUMBER = mp.UNIT   \r\n" + 
				"WHERE\r\n" + 
				"	(:ERPLOCATION IS NULL OR MP.ERPLOCATION=:ERPLOCATION)  AND (:MATERIALSPECNAME IS NULL OR mp.MATERIALSPECNAME=:MATERIALSPECNAME) AND m.DESC_CN LIKE '%'||:DESC_CN ||'%' \r\n" + 
				"	AND (:SDK_ID IS NULL\r\n" + 
				"		OR nvl(mp.SDK_ID, '~') = nvl(:SDK_ID, '~'))"+
				"	AND (:LOCATIONNAME IS NULL\r\n" + 
				"		OR mp.LOCATIONNAME =:LOCATIONNAME)"+
				"	AND (:MATERIALPACKINGNAME IS NULL\r\n" + 
				"		OR mp.MATERIALPACKINGNAME =:MATERIALPACKINGNAME) AND mp.STOCKSTATE = 'Stocked' AND (BS.DEPARTNAME IN ( '泗洪物流部','胶水车间','五金仓库') OR BS.ERPFACTORYNAME  = '103' OR BS.ERPFACTORYNAME  = '105' OR BS.ERPFACTORYNAME  = '106') order by mp.erpfactory,mp.erplocation,mp.MATERIALSPECNAME,mp.MATERIALQUANTITY";
		Map<String, Object> bindMap2 = new HashMap<String, Object>();
		bindMap2.put("MATERIALPACKINGNAME",materialPackingName); 
		bindMap2.put("ERPLOCATION",erplocaatin); 
		bindMap2.put("LOCATIONNAME",location); 
		bindMap2.put("MATERIALSPECNAME",materialspec); 
		bindMap2.put("DESC_CN",desc_cn); 
		bindMap2.put("SDK_ID",sdk_id); 
		List<Map<String,Object>> list2 = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql2, bindMap2);
		if(list2.size() < 1) {
			throw new GlobalException("查询结果为空");
		}
		
		return AjaxResult.me().setResultObj(list2);
	}
	
	
	
	/******************************************************************************************************************************/
	/*****************************************20240712修改出库单********************************************************************************/
	/******************************************************************************************************************************/
	/**
	 * 获取待完成的出库单集合
	 * @param in
	 * @return
	 */
	@RequestMapping(value = "/getStockOutInvoiceList", method = RequestMethod.POST)
	public AjaxResult getStockOutInvoiceList(@RequestBody JSONObject in ){

		MaterialshipRequest materialShipRequest = JSON.toJavaObject(in, MaterialshipRequest.class);
		String shipRequestName = materialShipRequest.getShipRequestName() == null ? "" : materialShipRequest.getShipRequestName();
		String erpfactory = materialShipRequest.getErpfactory();
		String sql = "SELECT DISTINCT SHIPREQUESTNAME FROM (SELECT\r\n" + 
				"	 T.SHIPREQUESTNAME " + 
				"FROM\r\n" + 
				"	MATERIALSHIPREQUESTDETAIL T\r\n" + 
				"LEFT JOIN MATERIALSHIPREQUEST M ON\r\n" + 
				"	T.SHIPREQUESTNAME = M.SHIPREQUESTNAME\r\n" + 
				"WHERE\r\n" + 
				"	T.SHIPREQUESTNAME LIKE :SHIPREQUESTNAME  \r\n" + 
//				"	AND SHIPREQUESTTYPE NOT IN ( 'SaleOutPre', '4E') " +
				 "AND SHIPREQUESTSTATE <> 'Completed' AND M.ERPFACTORY = :ERPFACTORY ORDER BY M.ORDERDATE ASC,SHIPREQUESTNAME)";

		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("SHIPREQUESTNAME","%" + shipRequestName + "%"); 
		bindMap.put("ERPFACTORY",erpfactory); 
		List<Map<String,Object>> list = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, bindMap);

		if(Objects.isNull(list) || list.isEmpty()) {
			throw new GlobalException("未找到对应的数据！");
		}
		return AjaxResult.me().setResultObj(list);

	}

	/**
	 * 获取已绑定的批次信息
	 * @param in
	 * @return
	 */
	@RequestMapping(value = "/getBindStockOutInfo", method = RequestMethod.POST)
	public AjaxResult getBindStockOutInfo(@RequestBody JSONObject in ){

		MaterialshipRequest materialShipRequest = JSON.toJavaObject(in, MaterialshipRequest.class);
		String shipRequestName = materialShipRequest.getShipRequestName() == null ? "" : materialShipRequest.getShipRequestName();
		String shipRequestDetailName = materialShipRequest.getShipRequestDetailName();
		String sdk_id = materialShipRequest.getSdk_id();
		String materialSpecName = materialShipRequest.getMaterialSpecName();
		String phase = materialShipRequest.getPhase();
		String siteName = materialShipRequest.getSiteName();
		String sql = " SELECT   \r\n" + 
				"         to_char(A.MAKEDATE,'yyyymmdd') as MAKEDATE,to_char(A.EXPIRINGDATE,'yyyymmdd') as EXPIRINGDATE,to_char(A.RECEIVETIME,'yyyymmdd') as RECEIVETIME,\r\n" + 
				"         0 as groupno,\r\n" + 
				"         0 as groupdetailno,\r\n" + 
				"         A.MATERIALPACKINGNAME,\r\n" + 
				"         A.STOCKSTATE,\r\n" + 
				"         A.CHARGE,\r\n" + 
				"         SSS.DESCRIPTION LOCATIONNAME,\r\n" + 
				"         A.SHIPREQUESTNAME,\r\n" + 
				"         A.SHIPREQUESTDETAILNAME,\r\n" + 
				"         BE.DESCRIPTION ERPLOCATION,\r\n" + 
				"         A.ERPFACTORY,\r\n" + 
				"         A.MATERIALSPECNAME,\r\n" + 
				"         A.PACKINGGRADE,\r\n" + 
				"         A.MATERIALQUANTITY,\r\n" + 
				"         A.MATERIALQUANTITY2 MATERIALQUANTITY2,\r\n" + 
				"         A.PACKINGSTATE,\r\n" + 
				"         B.EXPIRATIONDAY,\r\n" + 
				"         A.SDK_ID ,SS.SPECNAME,A.PHASE,\r\n" + 
				"         B.DESC_CN,\r\n" + 
				"         B.MATERIALUNIT,'Y' ISCHECK\r\n" + 
				"                    FROM   MATERIALPACKING A LEFT JOIN SDK_SPEC SS ON A.SDK_ID = SS.SDK_ID LEFT JOIN MATERIALSPEC B ON A.MATERIALSPECNAME = B.MATERIALSPECNAME "
				+ " LEFT JOIN BS_ERPLOCATION BE ON A.ERPLOCATION = BE.ERPLOCATIONNAME "
				+ "LEFT JOIN STORAGESPEC SSS ON SSS.STORAGENAME = A.LOCATIONNAME AND SSS.ERPLOCATION = A.ERPLOCATION \r\n" + 
				" WHERE   1 = 1   \r\n" + 
				"         AND A.MATERIALSPECNAME=B.MATERIALSPECNAME\r\n" + 
				"         AND A.SITENAME=B.SITENAME\r\n" + 
				"         AND A.SITENAME=:SITENAME\r\n" + 
				"         AND A.MATERIALPACKINGTYPE='Box'\r\n" + 
//				"         AND (:SDK_ID IS NULL OR A.SDK_ID = :SDK_ID)\r\n" + 
//				"          AND (:PHASE IS NULL OR A.PHASE = :PHASE)\r\n" + 
				"         AND A.SHIPREQUESTNAME = :SHIPREQUESTNAME\r\n" + 
//				"         AND A.SHIPREQUESTDETAILNAME  = :SHIPREQUESTDETAILNAME\r\n" + 
				"         AND A.STOCKSTATE = 'Stocked'\r\n" + 
				"         and a.PACKINGSTATE='Released'\r\n" + 
				"         AND A.HOLDSTATE='N'\r\n" + 
				"         AND A.PACKINGGRADE IN ('OK','OKEY')\r\n" + 
//				"         AND A.MATERIALQUANTITY<>'0'\r\n" + 
				"         AND (A.NPFLAG<>'Y' or A.NPFLAG is null)\r\n" + 
				"         AND (A.ETCSTATE is null or A.ETCSTATE='EtcIn')\r\n" + 
				"         AND A.OQARESULTSTATE <> 'PREOQA'  ORDER BY A.MATERIALSPECNAME ASC";

		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("SHIPREQUESTNAME",shipRequestName); 
		bindMap.put("MATERIALSPECNAME",materialSpecName); 
		bindMap.put("SHIPREQUESTDETAILNAME",shipRequestDetailName); 
		bindMap.put("SDK_ID",sdk_id); 
		bindMap.put("PHASE",phase); 
		bindMap.put("SITENAME",siteName); 
		List<Map<String,Object>> list = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, bindMap);

		return AjaxResult.me().setResultObj(list);
	}
	
	/**
	 * 获取待出库的批次信息，先进先出排序
	 * @param in
	 * @return
	 */
	@RequestMapping(value = "/getPreStockOutInfo", method = RequestMethod.POST)
	public AjaxResult getPreStockOutInfo(@RequestBody JSONObject in ){

		MaterialshipRequest materialShipRequest = JSON.toJavaObject(in, MaterialshipRequest.class);
		String shipRequestName = materialShipRequest.getShipRequestName() == null ? "" : materialShipRequest.getShipRequestName();
		String shipRequestDetailName = materialShipRequest.getShipRequestDetailName();
		String sdk_id = materialShipRequest.getSdk_id();
		String materialSpecName = materialShipRequest.getMaterialSpecName();
		String phase = materialShipRequest.getPhase();
		String erpfactory = materialShipRequest.getErpfactory();
		String siteName = materialShipRequest.getSiteName();
		String CAREER_ASSISTANCE = materialShipRequest.getCAREER_ASSISTANCE();
		String sql = "  SELECT   \r\n" + 
				"         to_char(A.MAKEDATE,'yyyymmdd') as MAKEDATE,to_char(A.EXPIRINGDATE,'yyyymmdd') as EXPIRINGDATE,to_char(A.RECEIVETIME,'yyyymmdd') as RECEIVETIME,\r\n" + 
				"         dense_rank()over( ORDER BY  to_char(A.RECEIVETIME,'yyyymmdd')) as groupno,\r\n" + 
				"         row_number()over(partition by to_char(A.RECEIVETIME,'yyyymmdd') ORDER BY A.LOCATIONNAME) as groupdetailno,\r\n" + 
				"         A.MATERIALPACKINGNAME,\r\n" + 
				"         A.STOCKSTATE,\r\n" + 
				"         A.CHARGE,\r\n" + 
				"         SSS.DESCRIPTION LOCATIONNAME,\r\n" + 
				"         :SHIPREQUESTNAME SHIPREQUESTNAME,\r\n" + 
				"         :SHIPREQUESTDETAILNAME SHIPREQUESTDETAILNAME,\r\n" + 
				"         BE.DESCRIPTION ERPLOCATION,\r\n" + 
				"         A.ERPFACTORY,A.AREANAME,A.SHELFNAME,\r\n" + 
				"         A.MATERIALSPECNAME,\r\n" + 
				"         A.PACKINGGRADE,\r\n" + 
				"         A.MATERIALQUANTITY,\r\n" + 
				"         CASE WHEN MATERIALQUANTITY2 > 0 THEN A.MATERIALQUANTITY2"
				+ "       ELSE A.MATERIALQUANTITY END    MATERIALQUANTITY2,\r\n" + 
				"         A.PACKINGSTATE,\r\n" + 
				"         B.EXPIRATIONDAY,\r\n" + 
				"         A.SDK_ID ,SS.SPECNAME, A.PHASE, \r\n" + 
				"         B.DESC_CN,\r\n" + 
				"         B.MATERIALUNIT,'N' ISCHECK\r\n" + 
				"                    FROM   MATERIALPACKING A LEFT JOIN SDK_SPEC SS ON A.SDK_ID = SS.SDK_ID LEFT JOIN MATERIALSPEC B ON A.MATERIALSPECNAME = B.MATERIALSPECNAME  "
				+ " LEFT JOIN BS_ERPLOCATION BE ON A.ERPLOCATION = BE.ERPLOCATIONNAME "
				+ "LEFT JOIN STORAGESPEC SSS ON SSS.STORAGENAME = A.LOCATIONNAME AND SSS.ERPLOCATION = A.ERPLOCATION \r\n" + 
				" WHERE    A.MATERIALSPECNAME = :MATERIALSPECNAME\r\n" + 
				"         AND A.MATERIALSPECNAME=B.MATERIALSPECNAME\r\n" + 
				"         AND A.SITENAME=B.SITENAME\r\n" + 
				"         AND A.SITENAME=:SITENAME\r\n" + 
				"         AND A.MATERIALPACKINGTYPE='Box'\r\n" + 
				"         AND (:SDK_ID IS NULL OR A.SDK_ID = :SDK_ID)\r\n" + 
				"         AND (:CAREER_ASSISTANCE IS NULL OR A.CAREER_ASSISTANCE = :CAREER_ASSISTANCE)\r\n" + 
				"          AND (:PHASE IS NULL OR A.PHASE = :PHASE)\r\n" + 
				"          AND (:ERPFACTORY IS NULL OR A.ERPFACTORY = :ERPFACTORY)\r\n" + 
				"         AND A.SHIPREQUESTNAME IS NULL\r\n" + 
				"         AND A.SHIPREQUESTDETAILNAME IS NULL\r\n" + 
				"         AND A.STOCKSTATE = 'Stocked'\r\n" + 
				"         and a.PACKINGSTATE='Released'\r\n" + 
				"         AND A.HOLDSTATE='N'\r\n" + 
				"         AND A.PACKINGGRADE IN ('OK','OKEY')\r\n" + 
				"         AND A.MATERIALQUANTITY<>'0'\r\n" + 
				"         AND (A.NPFLAG<>'Y' or A.NPFLAG is null)\r\n" + 
				"         AND (A.ETCSTATE is null or A.ETCSTATE='EtcIn')\r\n" + 
				"         AND A.OQARESULTSTATE <> 'PREOQA'\r\n" + 
				"         AND to_char(A.EXPIRINGDATE,'yyyymmdd') >= to_char(SYSDATE,'yyyymmdd')";

		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("SHIPREQUESTNAME",shipRequestName); 
		bindMap.put("MATERIALSPECNAME",materialSpecName); 
		bindMap.put("SHIPREQUESTDETAILNAME",shipRequestDetailName); 
		bindMap.put("SDK_ID",sdk_id); 
		bindMap.put("PHASE",phase); 
		bindMap.put("ERPFACTORY",erpfactory); 
		bindMap.put("SITENAME",siteName); 
		bindMap.put("CAREER_ASSISTANCE",CAREER_ASSISTANCE); 
		List<Map<String,Object>> list = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, bindMap);

		return AjaxResult.me().setResultObj(list);
	}
	
	/**
	 * 获取出库汇总明细数据
	 * @param in
	 * @return
	 */
	@RequestMapping(value = "/getStockOutInvoiceSumInfo", method = RequestMethod.POST)
	public AjaxResult getStockOutInvoiceSumInfo(@RequestBody JSONObject in ){

		MaterialshipRequest materialShipRequest = JSON.toJavaObject(in, MaterialshipRequest.class);
		String shipRequestName = materialShipRequest.getShipRequestName() == null ? "" : materialShipRequest.getShipRequestName();
		String erpfactory = materialShipRequest.getErpfactory();
		//只查询未拣配完成的行项.zd.2024.9.26
		String sql=" SELECT *\n" +
				"  FROM (WITH AA AS (SELECT SHIPREQUESTNAME,\n" +
				"                           MATERIALSPECNAME,\n" +
				"                           SDK_ID,\n" +
				"                           PHASE,\n" +
				"                           COUNT(MATERIALPACKINGNAME) JS\n" +
				"                      FROM MATERIALPACKING A\n" +
				"                     WHERE A.SHIPREQUESTNAME = :SHIPREQUESTNAME\n" +
				"                       AND A.ERPFACTORY = :ERPFACTORY\n" +
				"                     GROUP BY SHIPREQUESTNAME,\n" +
				"                              MATERIALSPECNAME,\n" +
				"                              SDK_ID,\n" +
				"                              PHASE)\n" +
				"         SELECT m.MATERIALSPECNAME,\n" +
				"                SUM(m.REQUESTQUANTITY) REQUESTQUANTITY,\n" +
				"                SUM(m.ASSIGNEDQUANTITY) ASSIGNEDQUANTITY,\n" +
				"                SUM(m.SALEOUTQUANTITY) SALEOUTQUANTITY,\n" +
				"                SUM(m.REQUESTQUANTITY - m.SALEOUTQUANTITY) SMQTY,\n" +
				"                m.PHASE,\n" +
				"                m.SDK_ID,\n" +
				"                m2.DESCRIPTION,\n" +
				"                m2.DESC_CN,\n" +
				"                m.ERPFACTORY,\n" +
				"                ss.SPECNAME,\n" +
				"                m.SHIPREQUESTNAME,\n" +
				"                mm.JS,\n" +
				"                mmm.CUSTOMERNO,\n" +
				"                mmm.SJMS,\n" +
				"                mmm.PRODUCTORDERTYPE,\n" +
				"                m2.COMMONMATERIALFLAG,\n" +
				"                max(m.INDEPTNO) DEPARTMENT\n" +
				"           FROM MATERIALSHIPREQUESTDETAIL m\n" +
				"           LEFT JOIN MATERIALSPEC m2\n" +
				"             ON m.MATERIALSPECNAME = m2.MATERIALSPECNAME\n" +
				"           LEFT JOIN SDK_SPEC ss\n" +
				"             ON ss.SDK_ID = m.SDK_ID\n" +
				"           LEFT JOIN MATERIALSHIPREQUEST mmm\n" +
				"             ON mmm.SHIPREQUESTNAME = m.SHIPREQUESTNAME\n" +
				"           LEFT JOIN AA mm\n" +
				"             ON mm.SHIPREQUESTNAME = m.SHIPREQUESTNAME\n" +
				"            AND mm.MATERIALSPECNAME = m.MATERIALSPECNAME\n" +
				"            AND ((mm.SDK_ID IS NULL AND m.SDK_ID IS NULL) OR\n" +
				"                mm.SDK_ID = m.SDK_ID)\n" +
				"            AND ((mm.phase IS NULL AND m.phase IS NULL) OR\n" +
				"                mm.phase = m.phase)\n" +
				"          WHERE m.SHIPREQUESTNAME = :SHIPREQUESTNAME\n" +
				"            AND mmm.ERPFACTORY = :ERPFACTORY\n" +
				"          GROUP BY m.MATERIALSPECNAME,\n" +
				"                   m.PHASE,\n" +
				"                   m.SDK_ID,\n" +
				"                   m2.DESCRIPTION,\n" +
				"                   m2.DESC_CN,\n" +
				"                   ss.SPECNAME,\n" +
				"                   m.SHIPREQUESTNAME,\n" +
				"                   mm.JS,\n" +
				"                   m.ERPFACTORY,\n" +
				"                   mmm.CUSTOMERNO,\n" +
				"                   mmm.SJMS,\n" +
				"                   mmm.PRODUCTORDERTYPE,\n" +
				"                   m2.COMMONMATERIALFLAG)\n" +
				"          WHERE REQUESTQUANTITY - SALEOUTQUANTITY > 0";
		
		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("SHIPREQUESTNAME",shipRequestName); 
		bindMap.put("ERPFACTORY",erpfactory); 
		List<Map<String,Object>> list = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, bindMap);
		if(Objects.isNull(list) || list.isEmpty()) {
			return AjaxResult.me().setErrorCode(500).setMessage("未找到对应的数据！");
		}
		return AjaxResult.me().setResultObj(list);
	}
	
	/**
	 * 绑定解绑发货单
	 * @param in
	 * @return
	 */
	@Transactional
	@RequestMapping(value = "/AssiginOrDessiginBarCode", method = RequestMethod.POST)
	public AjaxResult AssiginOrDessiginBarCode(@RequestBody JSONObject in ){

		MaterialshipRequest materialShipRequest = JSON.toJavaObject(in, MaterialshipRequest.class);
		String shipRequestName = materialShipRequest.getShipRequestName() == null ? "" : materialShipRequest.getShipRequestName();
		String shipRequestDetailName = materialShipRequest.getShipRequestDetailName();
		String type = materialShipRequest.getType();//Deassign 条码解绑 Assign 条码绑定  
		String user = materialShipRequest.getUser();
		String siteName = materialShipRequest.getSiteName();
		String shipRequestType = materialShipRequest.getShipRequestType();//订单类型
		List<MaterialPacking> boxList = materialShipRequest.getBoxList();
		EventInfo makeEventInfo;
		for (MaterialPacking materialPacking : boxList) {
			String materialPackingName = materialPacking.getMaterialPackingName();//条码
			String materialQuantity = materialPacking.getMaterialQuantity();//原数量
			String materialQuantity2 = materialPacking.getMaterialQuantity2();//发货数量
			String charge = materialPacking.getCharge();//批次
			//1.绑定
			//校验Box有没有被其他订单绑过
			if ("Assign".equals(type)) {
				makeEventInfo = new EventInfoUtil().makeEventInfo("AssiginShipRequest", user, "AssiginShipRequest");
				String sql = "SELECT SHIPREQUESTNAME FROM MATERIALPACKING T WHERE T.MATERIALPACKINGNAME = :MATERIALPACKINGNAME";
				Map<String, Object> hashMap = new HashMap<String,Object> ();
				hashMap.put("MATERIALPACKINGNAME", materialPackingName);
				List<Map<String,Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, hashMap);
				if (queryForList == null || queryForList.size() < 1) {
					return AjaxResult.me().setSuccess(false).setMessage("条码在系统中不存在");
				} else {
					String invoice = queryForList.get(0).get("SHIPREQUESTNAME") == null ? "" : queryForList.get(0).get("SHIPREQUESTNAME").toString().toString();
					if (!"".equals(invoice) && !invoice.equals(shipRequestName)) {
						return AjaxResult.me().setSuccess(false).setMessage("条码已被" + invoice + "备货");
					}
					//更新Box绑定关系、Box发货数量、拆分到条码明细表
					SetEventInfo setEventInfo = new SetEventInfo();
					Map<String, Object> hashMap2 = new HashMap<String,Object> ();
					float fmaterialQuantity = Float.parseFloat(materialQuantity);//标签数量
					float fmaterialQuantity2 = Float.parseFloat(materialQuantity2);//实际发货数量
					//更新实际实际出库数量到库存表
					MaterialPackingKey materialPackingKey = new MaterialPackingKey(siteName, materialPackingName);
					if (fmaterialQuantity2 < fmaterialQuantity) {//拆托发货
//						hashMap2.put("materialQuantity", fmaterialQuantity - fmaterialQuantity2);//更新标签剩余数量
						hashMap2.put("materialQuantity2", fmaterialQuantity2);//更新发货数量
						hashMap2.put("shipRequestName", shipRequestName);
						hashMap2.put("shipRequestDetailName", shipRequestDetailName);
						setEventInfo.setUserColumns(hashMap2);
						MaterialPackingServiceProxy.getMaterialPackingService().setEvent(materialPackingKey, makeEventInfo, setEventInfo);
					} else if (fmaterialQuantity2 == fmaterialQuantity) {//完全发货
						hashMap2.put("materialQuantity2", fmaterialQuantity2);
						hashMap2.put("shipRequestName", shipRequestName);
						hashMap2.put("shipRequestDetailName", shipRequestDetailName);
						setEventInfo.setUserColumns(hashMap2);
						MaterialPackingServiceProxy.getMaterialPackingService().setEvent(materialPackingKey, makeEventInfo, setEventInfo);
					} else {//不满足发货条件
						return AjaxResult.me().setSuccess(false).setMessage("发货数量超出");
					}
					//更新备货数量
					BigDecimal QTY = new BigDecimal(materialQuantity2);
					if("Assign".equals(type)){
						String updateSql= " UPDATE MATERIALSHIPREQUESTDETAIL m SET m.ASSIGNEDQUANTITY = m.ASSIGNEDQUANTITY + :QTY WHERE m.SITENAME = :SITENAME"
								+ " AND m.SHIPREQUESTNAME = :SHIPREQUESTNAME AND m.SHIPREQUESTDETAILNAME = :SHIPREQUESTDETAILNAME";
						Map<String, Object> bindMap = new HashMap<String, Object>();
						bindMap.put("QTY", QTY);
						bindMap.put("SITENAME", siteName);
						bindMap.put("SHIPREQUESTNAME", shipRequestName);
						bindMap.put("SHIPREQUESTDETAILNAME", shipRequestDetailName);
						IDMFrameServiceProxy.getSqlTemplate().update(updateSql, bindMap);
					} else if("Deassign".equals(type)){
						String updateSql= " UPDATE MATERIALSHIPREQUESTDETAIL m SET m.ASSIGNEDQUANTITY = m.ASSIGNEDQUANTITY - :QTY WHERE m.SITENAME = :SITENAME"
								+ " AND m.SHIPREQUESTNAME = :SHIPREQUESTNAME AND m.SHIPREQUESTDETAILNAME = :SHIPREQUESTDETAILNAME";
						Map<String, Object> bindMap = new HashMap<String, Object>();
						bindMap.put("QTY", QTY);
						bindMap.put("SITENAME", siteName);
						bindMap.put("SHIPREQUESTNAME", shipRequestName);
						bindMap.put("SHIPREQUESTDETAILNAME", shipRequestDetailName);
						IDMFrameServiceProxy.getSqlTemplate().update(updateSql, bindMap);
					}
				}
				
			} else 	if ("Dessign".equals(type)) {
				makeEventInfo = new EventInfoUtil().makeEventInfo("DessiginShipRequest", user, "DessiginShipRequest");
				String sql = "SELECT SHIPREQUESTNAME FROM MATERIALPACKING T WHERE T.MATERIALPACKINGNAME = :MATERIALPACKINGNAME";
				Map<String, Object> hashMap = new HashMap<String,Object> ();
				hashMap.put("MATERIALPACKINGNAME", materialPackingName);
				List<Map<String,Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, hashMap);
				if (queryForList == null || queryForList.size() < 1) {
					return AjaxResult.me().setSuccess(false).setMessage("条码在系统中不存在");
				} else {
					String invoice = queryForList.get(0).get("SHIPREQUESTNAME") == null ? "" : queryForList.get(0).get("SHIPREQUESTNAME").toString().toString();
					if (!"".equals(shipRequestName) && !invoice.equals(shipRequestName)) {
						return AjaxResult.me().setSuccess(false).setMessage("条码已被" + invoice + "备货");
					}
					//更新Box绑定关系、Box发货数量、拆分到条码明细表
					SetEventInfo setEventInfo = new SetEventInfo();
					Map<String, Object> hashMap2 = new HashMap<String,Object> ();
					float fmaterialQuantity = Float.parseFloat(materialQuantity);//标签数量
					float fmaterialQuantity2 = Float.parseFloat(materialQuantity2);//实际发货数量
					//更新实际实际出库数量到库存表
					MaterialPackingKey materialPackingKey = new MaterialPackingKey(siteName, materialPackingName);
					hashMap2.put("materialQuantity2", 0);
					hashMap2.put("shipRequestName", "");
					hashMap2.put("shipRequestDetailName", "");
					setEventInfo.setUserColumns(hashMap2);
					MaterialPackingServiceProxy.getMaterialPackingService().setEvent(materialPackingKey, makeEventInfo, setEventInfo);
					//更新备货数量
					BigDecimal QTY = new BigDecimal(materialQuantity2);
					if("Assign".equals(type)){
						String updateSql= " UPDATE MATERIALSHIPREQUESTDETAIL m SET m.ASSIGNEDQUANTITY = m.ASSIGNEDQUANTITY + :QTY WHERE m.SITENAME = :SITENAME"
								+ " AND m.SHIPREQUESTNAME = :SHIPREQUESTNAME AND m.SHIPREQUESTDETAILNAME = :SHIPREQUESTDETAILNAME";
						Map<String, Object> bindMap = new HashMap<String, Object>();
						bindMap.put("QTY", QTY);
						bindMap.put("SITENAME", siteName);
						bindMap.put("SHIPREQUESTNAME", shipRequestName);
						bindMap.put("SHIPREQUESTDETAILNAME", shipRequestDetailName);
						IDMFrameServiceProxy.getSqlTemplate().update(updateSql, bindMap);
					} else if("Deassign".equals(type)){
						String updateSql= " UPDATE MATERIALSHIPREQUESTDETAIL m SET m.ASSIGNEDQUANTITY = m.ASSIGNEDQUANTITY - :QTY WHERE m.SITENAME = :SITENAME"
								+ " AND m.SHIPREQUESTNAME = :SHIPREQUESTNAME AND m.SHIPREQUESTDETAILNAME = :SHIPREQUESTDETAILNAME";
						Map<String, Object> bindMap = new HashMap<String, Object>();
						bindMap.put("QTY", QTY);
						bindMap.put("SITENAME", siteName);
						bindMap.put("SHIPREQUESTNAME", shipRequestName);
						bindMap.put("SHIPREQUESTDETAILNAME", shipRequestDetailName);
						IDMFrameServiceProxy.getSqlTemplate().update(updateSql, bindMap);
					}
//					String update = "UPDATE MATERIALPACKING T SET T.MATERIALQUANTITY = T.MATERIALQUANTITY + :QTY ,"
//							+ "T.MATERIALQUANTITY2 = T.MATERIALQUANTITY2 - :QTY WHERE T.MATERIALPACKINGNAME = :MATERIALPACKINGNAME";
//					Map<String, Object> hashMap3 = new HashMap<String,Object> ();
//					hashMap3.put("MATERIALPACKINGNAME", materialPackingName);
//					hashMap3.put("QTY", QTY);
//					IDMFrameServiceProxy.getSqlTemplate().update(update, hashMap3);
				}
			}
			//2.解绑
			//检验Box是否已经完成解绑
			//更新Box绑定关系、Box发货数量、删除条码明细表
		}
		return AjaxResult.me().setResultObj(null);
	}
	
	/*****************************************************************************************************************************/
	/***********************************************销售备货20240716***************************************************************/
	/***************************************************************************************************************************/
	/**
	 * 生成销售备货单号
	 * @param in
	 * @return
	 */
	@RequestMapping(value = "/generateSalePreOrder", method = RequestMethod.POST)
	public AjaxResult generateSalePreOrder(@RequestBody JSONObject in ){
		MaterialshipRequest materialShipRequest = JSON.toJavaObject(in, MaterialshipRequest.class);
		String user = materialShipRequest.getUser();
		String materialSpecName = materialShipRequest.getMaterialSpecName();//料号
		String sdk_id = materialShipRequest.getSdk_id();//规格
		String phase = materialShipRequest.getPhase();//阶段
		String shipRequestName = materialShipRequest.getShipRequestName() == null ? "" : materialShipRequest.getShipRequestName();
		String erpfactory = materialShipRequest.getErpfactory();
		if (shipRequestName != null && !"".equals(shipRequestName)) {
			throw new GlobalException("已存在预备货单");
		}
		if (user == null ||  "".equals(user)) {
			throw new GlobalException("业助信息不能为空");
		}
		List<String> args = new ArrayList<String>();
		// 获取系统当前时间

		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYYMMDD");
		String date = simpleDateFormat.format(new Date());
		args.add(date);
		List<String> invoice = NameServiceProxy.getNameRuleDefService().generateName(
				"ShipRequestName", args, 1);
		if(Objects.isNull(invoice) || invoice.isEmpty()) {
			throw new GlobalException("生成失败！");
		}
		
		shipRequestName = user + "-" + invoice.get(0);
		/**
		 * 预留单主表
		 */
		CreateMaterialShipRequestInfo createMaterialShipRequestInfo = new CreateMaterialShipRequestInfo();
		createMaterialShipRequestInfo.setSiteName("SDK");
		createMaterialShipRequestInfo.setShipRequestType("SaleOutPre");
		createMaterialShipRequestInfo.setShipRequestName( shipRequestName);
		createMaterialShipRequestInfo.setShipRequestState("Created");
		EventInfo makeEventInfo = new EventInfoUtil().makeEventInfo("SaleOutPre", user, "SaleOutPre");
		MaterialShipRequestServiceProxy.getMaterialShipRequestService().create(makeEventInfo, createMaterialShipRequestInfo);
		/**
		 * 预留单明细表
		 */
		CreateMaterialShipRequestDetailInfo createMaterialShipRequestDetailInfo = new CreateMaterialShipRequestDetailInfo();
		createMaterialShipRequestDetailInfo.setSiteName("SDK");
		createMaterialShipRequestDetailInfo.setMaterialSpecName(materialSpecName);
		createMaterialShipRequestDetailInfo.setShipRequestDetailName("10");
		createMaterialShipRequestDetailInfo.setShipRequestName(shipRequestName);
		Map<String, Object> hashMap = new HashMap<String,Object> ();
//		hashMap.put("sdk_id", sdk_id);
//		hashMap.put("phase", phase);
		hashMap.put("erpFactory", erpfactory);
		createMaterialShipRequestDetailInfo.setUserColumns(hashMap);
		MaterialShipRequestDetailServiceProxy.getMaterialShipRequestDetailService().create(createMaterialShipRequestDetailInfo);
		return AjaxResult.me().setResultObj(shipRequestName);
	}
	
	/**
	 * 生成销售备货单号
	 * @param in
	 * @return
	 */
	@RequestMapping(value = "/getSDKSPEC", method = RequestMethod.POST)
	public AjaxResult getSDKSPEC(@RequestBody JSONObject in ){
		MaterialshipRequest materialShipRequest = JSON.toJavaObject(in, MaterialshipRequest.class);
		String materialSpecName = materialShipRequest.getMaterialSpecName();//料号
		//根据料号获取sdk规格信息
		if (materialSpecName == null || "".equals(materialSpecName)) {
			return null;
		}
		if (materialSpecName.startsWith("4")) {
			materialSpecName = "4";
		}
		String sql = " SELECT SDK_ID  as \"value\" ,SPECNAME  as \"text\" FROM SDK_SPEC ss WHERE ss.PARTNUMBER = :PARTNUMBER";
		Map<String, Object> hashMap = new HashMap<String,Object> ();
		hashMap.put("PARTNUMBER", materialSpecName);
		List<Map<String, Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, hashMap);
		return AjaxResult.me().setResultObj(queryForList);
	}
	
	
	
	/******************************************************************************************************************************/
	/*****************************************20240719修改出库单自动备货********************************************************************************/
	/******************************************************************************************************************************/
	/**
	 * 获取待完成的出库单集合
	 * @param in
	 * @return
	 */
	@RequestMapping(value = "/getStockOutInvoiceList2", method = RequestMethod.POST)
	public AjaxResult getStockOutInvoiceList2(@RequestBody JSONObject in ){

		MaterialshipRequest materialShipRequest = JSON.toJavaObject(in, MaterialshipRequest.class);
		String shipRequestName = materialShipRequest.getShipRequestName() == null ? "" : materialShipRequest.getShipRequestName();
		String sql = "SELECT\r\n" + 
				"	DISTINCT T.SHIPREQUESTNAME " + 
				"FROM\r\n" + 
				"	MATERIALSHIPREQUESTDETAIL T\r\n" + 
				"LEFT JOIN MATERIALSHIPREQUEST M ON\r\n" + 
				"	T.SHIPREQUESTNAME = M.SHIPREQUESTNAME\r\n" + 
				"WHERE\r\n" + 
				"	T.SHIPREQUESTNAME LIKE :SHIPREQUESTNAME  \r\n" + 
//				"	AND SHIPREQUESTTYPE <> '4C' " +
				 "AND SHIPREQUESTSTATE <> 'Completed'";

		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("SHIPREQUESTNAME","%" + shipRequestName + "%"); 
		List<Map<String,Object>> list = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, bindMap);

		if(Objects.isNull(list) || list.isEmpty()) {
			throw new GlobalException("未找到对应的数据！");
		}
		return AjaxResult.me().setResultObj(list);
	}
	
	
	/**
	 * 获取出库明细信息
	 * @param in
	 * @return
	 */
	@RequestMapping(value = "/getStockOutInvoiceInfo", method = RequestMethod.POST)
	public AjaxResult getStockOutInvoiceInfo(@RequestBody JSONObject in ){
		MaterialshipRequest materialShipRequest = JSON.toJavaObject(in, MaterialshipRequest.class);
		String shipRequestName = materialShipRequest.getShipRequestName() == null ? "" : materialShipRequest.getShipRequestName();
		String sql = "SELECT\r\n" + 
				"	m.MATERIALSPECNAME ,\r\n" + 
				"	m.SDK_ID ,\r\n" + 
				"	ss.SPECNAME ,\r\n" + 
				"	m.PHASE ,\r\n" + 
				"	m.REQUESTQUANTITY ,\r\n" + 
				"	m.ASSIGNEDQUANTITY ,\r\n" + 
				"	m3.DESC_CN ,\r\n" + 
				"	m4.FNAME UNIT,\r\n" + 
				"	m.SHIPREQUESTNAME ,\r\n" + 
				"	m.SHIPREQUESTDETAILNAME ,\r\n" + 
				"	count(DISTINCT m5.MATERIALPACKINGNAME) JS\r\n" + 
				"FROM\r\n" + 
				"	MATERIALSHIPREQUESTDETAIL m\r\n" + 
				"LEFT JOIN MATERIALSHIPREQUEST m2 ON\r\n" + 
				"	m.SHIPREQUESTNAME = m2.SHIPREQUESTNAME\r\n" + 
				"LEFT JOIN MATERIALSPEC m3 ON\r\n" + 
				"	m.MATERIALSPECNAME = m3.MATERIALSPECNAME\r\n" + 
				"LEFT JOIN SDK_SPEC ss ON\r\n" + 
				"	ss.SDK_ID = m.SDK_ID\r\n" + 
				"LEFT JOIN MATERIALUNIT m4 ON\r\n" + 
				"	m.UNIT = m4.FNUMBER\r\n" + 
				"LEFT JOIN MATERIALPACKING m5 ON\r\n" + 
				"	m.SHIPREQUESTNAME = m5.SHIPREQUESTNAME\r\n" + 
				"	AND m.SHIPREQUESTDETAILNAME = m5.SHIPREQUESTDETAILNAME\r\n" + 
				"	AND m5.STOCKSTATE = 'Stocked'\r\n" + 
				"WHERE 1= 1 \r\n" + 
//				"	AND m2.SHIPREQUESTTYPE <> '4C'\r\n" + 
				"	AND m2.SHIPREQUESTNAME = :SHIPREQUESTNAME\r\n" + 
				"	AND m2.SHIPREQUESTSTATE <> 'Completed'\r\n" + 
				"GROUP BY\r\n" + 
				"	m.MATERIALSPECNAME ,\r\n" + 
				"	m.SDK_ID ,\r\n" + 
				"	m.PHASE ,\r\n" + 
				"	m.REQUESTQUANTITY ,\r\n" + 
				"	m.ASSIGNEDQUANTITY ,\r\n" + 
				"	m3.DESC_CN ,\r\n" + 
				"	m4.FNAME ,\r\n" + 
				"	ss.SPECNAME ,\r\n" + 
				"	m.SHIPREQUESTNAME ,\r\n" + 
				"	m.SHIPREQUESTDETAILNAME";
		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("SHIPREQUESTNAME",shipRequestName); 
		List<Map<String,Object>> list = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, bindMap);
		if(Objects.isNull(list) || list.isEmpty()) {
			throw new GlobalException("未找到对应的数据！");
		}
		return AjaxResult.me().setResultObj(list);
	}
	
	@RequestMapping(value = "/ReservePackingInfo2", method = RequestMethod.POST)
	public AjaxResult ReservePackingInfo2(@RequestBody JSONObject in ) {
		MaterialshipRequest materialShipRequest = JSON.toJavaObject(in, MaterialshipRequest.class);
		String shipRequestName = materialShipRequest.getShipRequestName() == null ? "" : materialShipRequest.getShipRequestName();
		String user = materialShipRequest.getUser();
		String erpfactory2 = materialShipRequest.getErpfactory();
		List<Map<String, Object>> zdbh = InvoiceServiceImpl.ZDBH(shipRequestName,user,erpfactory2);
//		if (zdbh == null || zdbh.size() < 1) {
//			return AjaxResult.me().setSuccess(false).setMessage("备货失败");
//		}
		return AjaxResult.me().setResultObj(zdbh);
	}
	
	/**
	 * 查询单据时自动备货
	 * @param in
	 * @return
	 */
	@Transactional
	@RequestMapping(value = "/ReservePackingInfo", method = RequestMethod.POST)
	public AjaxResult ReservePackingInfo(@RequestBody JSONObject in ){
		MaterialshipRequest materialShipRequest = JSON.toJavaObject(in, MaterialshipRequest.class);
		String shipRequestName = materialShipRequest.getShipRequestName() == null ? "" : materialShipRequest.getShipRequestName();
		String user = materialShipRequest.getUser();
		String erpfactory2 = materialShipRequest.getErpfactory();
		//查询所有单号行项目明细
		//只查询未拣配完成的单号行项目明细。zd.2024.9.26
		String sql=" SELECT\r\n" + 
				"	*\r\n" + 
				"FROM\r\n" + 
				"	(\r\n" + 
				"	SELECT\r\n" + 
				"		m.SITENAME,\r\n" + 
				"		m.ERPFACTORY,\r\n" + 
				"		m.SHIPREQUESTNAME ,\r\n" + 
				"		SUM(m.REQUESTQUANTITY - m.ASSIGNEDQUANTITY) MINUSQTY,\r\n" + 
				"		CASE WHEN U.USERNAME IS NULL THEN  m.CAREER_ASSISTANCE ELSE U.USERNAME END  CAREER_ASSISTANCE,\r\n" + 
				"		m.CUSTOMERNO,\r\n" + 
				"		SUM(m.REQUESTQUANTITY - m.SALEOUTQUANTITY) MQTY,\r\n" + 
				"		m.MATERIALSPECNAME ,\r\n" + 
				"		m.SDK_ID ,\r\n" + 
				"		m.PHASE,\r\n" + 
				"		T.SJMS ,\r\n" + 
				"		T.SHIPREQUESTTYPE ,\r\n" + 
				"		M2.COMMONMATERIALFLAG\r\n" + 
				"	FROM\r\n" + 
				"		MATERIALSHIPREQUESTDETAIL m\r\n" + 
				"	LEFT JOIN MATERIALSHIPREQUEST T ON\r\n" + 
				"		m.SHIPREQUESTNAME = T.SHIPREQUESTNAME\r\n" + 
				"	LEFT JOIN MATERIALSPEC M2 ON\r\n" + 
				"		m.MATERIALSPECNAME = M2.MATERIALSPECNAME\r\n" + 
				"		LEFT JOIN NC_USER U ON m.CAREER_ASSISTANCE = U.USERID \r\n" + 
				"	WHERE\r\n" + 
				"		m.SHIPREQUESTNAME = :SHIPREQUESTNAME\r\n" + 
				"		AND T.ERPFACTORY = :ERPFACTORY\r\n" + 
				"	GROUP BY\r\n" + 
				"		m.SITENAME,\r\n" + 
				"		m.ERPFACTORY,\r\n" + 
				"		m.SHIPREQUESTNAME ,\r\n" + 
				"		m.MATERIALSPECNAME ,\r\n" + 
				"		m.SDK_ID ,\r\n" + 
				"		m.PHASE ,\r\n" + 
				"		U.USERNAME,\r\n" + 
				"		m.CUSTOMERNO,\r\n" + 
				"		T.SJMS ,\r\n" + 
				"		T.SHIPREQUESTTYPE ,m.CAREER_ASSISTANCE,\r\n" + 
				"		M2.COMMONMATERIALFLAG\r\n" + 
				"	ORDER BY\r\n" + 
				"		m.MATERIALSPECNAME ,\r\n" + 
				"		m.SDK_ID ,\r\n" + 
				"		m.PHASE)\r\n" + 
				"WHERE\r\n" + 
				"	MQTY>0 ";
		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("SHIPREQUESTNAME",shipRequestName); 
		bindMap.put("ERPFACTORY",erpfactory2); 
		List<Map<String,Object>> list = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, bindMap);
		if(Objects.isNull(list) || list.isEmpty()) {
			throw new GlobalException("未找到对应的数据！");
		}
		List<Map<String, Object>> resList = new ArrayList<Map<String,Object>> ();
		List<Map<String, Object>> tempList = new ArrayList<Map<String,Object>> ();
		for (Map<String, Object> map : list) {
			String sdk_id = map.get("SDK_ID") == null ? "" : map.get("SDK_ID").toString();
			String materialSpecName = map.get("MATERIALSPECNAME") == null ? "" : map.get("MATERIALSPECNAME").toString();
			String phase = map.get("PHASE") == null ? "" : map.get("PHASE").toString();
			String erpfactory = map.get("ERPFACTORY") == null ? "" : map.get("ERPFACTORY").toString();
			String siteName = map.get("SITENAME") == null ? "" : map.get("SITENAME").toString();
			String MINUSQTY = map.get("MINUSQTY") == null ? "" : map.get("MINUSQTY").toString();
			String SJMS = map.get("SJMS") == null ? "" : map.get("SJMS").toString();
			String CAREER_ASSISTANCE = map.get("CAREER_ASSISTANCE") == null ? "" : map.get("CAREER_ASSISTANCE").toString();
			String CUSTOMERNO = map.get("CUSTOMERNO") == null ? "" : map.get("CUSTOMERNO").toString();
			String COMMONMATERIALFLAG = map.get("COMMONMATERIALFLAG") == null ? "" : map.get("COMMONMATERIALFLAG").toString();
			if ("Y".equals(COMMONMATERIALFLAG)) {
				CUSTOMERNO = "";
				CAREER_ASSISTANCE = "";
			}
			BigDecimal minQty = new BigDecimal(MINUSQTY);//剩余需求数量
			//汇总满足条件的box
			String boxList = "SELECT\r\n" + 
					"	A.MATERIALSPECNAME ,\r\n" + 
					"	A.PHASE ,\r\n" + 
					"	A.SDK_ID ,\r\n" + 
					"	to_char(A.MAKEDATE, 'yyyymmdd') JSSJ,\r\n" + 
					"	SUM(A.MATERIALQUANTITY) MATERIALQUANTITY\r\n" + 
					"FROM\r\n" + 
					"	MATERIALPACKING A\r\n" + 
					"LEFT JOIN SDK_SPEC SS ON\r\n" + 
					"	A.SDK_ID = SS.SDK_ID \r\n" + 
					"	LEFT JOIN BS_ERPLOCATION B ON A.ERPLOCATION  = B.ERPLOCATIONNAME \r\n" + 
					"WHERE\r\n" + 
					"	A.MATERIALSPECNAME = :MATERIALSPECNAME\r\n" + 
					"	AND A.SITENAME =:SITENAME\r\n" + 
					"	AND A.MATERIALPACKINGTYPE = 'Box'\r\n" + 
					"	AND (:SDK_ID IS NULL\r\n" + 
					"		OR A.SDK_ID = :SDK_ID)\r\n" + 
					"	AND (:SJMS IS NULL\r\n" + 
					"		OR A.MATERIALQUANTITY * 1000 / SUBSTR( SS.SPECNAME , 1, INSTR( SS.SPECNAME , 'mm') -1 ) >= :SJMS)\r\n" + 
					"	AND (:PHASE IS NULL\r\n" + 
					"		OR A.PHASE = :PHASE)\r\n" + 
//					"	AND (:CAREER_ASSISTANCE IS NULL AND A.CAREER_ASSISTANCE IS NULL)\r\n" + 
//					"	AND (:CAREER_ASSISTANCE IS NULL OR (:CAREER_ASSISTANCE IS NOT NULL AND A.CAREER_ASSISTANCE = :CAREER_ASSISTANCE))\r\n" + 
					"	\r\n" + 
//					"	AND (:CUSTOMERNO IS NULL AND A.CUSTOMNO IS NULL)\r\n" + 
//					"	AND (:CUSTOMERNO IS NULL OR (:CUSTOMERNO IS NOT NULL AND A.CUSTOMNO IN (	SELECT CUSTOMERNO FROM CUSTOMERGROUP T WHERE T.CUSTOMERGROUPNO IN  (   \r\n" + 
//					"	SELECT C.CUSTOMERGROUPNO FROM CUSTOMERGROUP C WHERE C.CUSTOMERNO = :CUSTOMERNO)  )   )    )\r\n" + 
					"		\r\n" + 
					"	AND A.ERPFACTORY = :ERPFACTORY\r\n" + 
					"	AND B.ZDBHFLAG = 'Y'\r\n" + 
					"	AND A.SHIPREQUESTNAME IS NULL\r\n" + 
					"	AND A.SHIPREQUESTDETAILNAME IS NULL\r\n" + 
					"	AND A.STOCKSTATE = 'Stocked'\r\n" + 
					"	AND A.PACKINGSTATE = 'Released'\r\n" + 
					"	AND A.HOLDSTATE = 'N'\r\n" + 
					"	AND A.PACKINGGRADE IN ('OK', 'OKEY')\r\n" + 
					"	AND A.MATERIALQUANTITY <> '0'\r\n" + 
					"	AND (A.NPFLAG <> 'Y'\r\n" + 
					"		OR A.NPFLAG IS NULL)\r\n" + 
					"	AND (A.ETCSTATE IS NULL\r\n" + 
					"		OR A.ETCSTATE = 'EtcIn')\r\n" + 
					"	AND A.OQARESULTSTATE <> 'PREOQA'\r\n" + 
					"	AND to_char(A.EXPIRINGDATE, 'yyyymmdd') >= to_char(SYSDATE, 'yyyymmdd')\r\n" + 
					"GROUP BY\r\n" + 
					"	A.MATERIALSPECNAME ,\r\n" + 
					"	A.PHASE ,\r\n" + 
					"	A.SDK_ID ,\r\n" + 
					"	to_char(A.MAKEDATE, 'yyyymmdd')\r\n" + 
					"ORDER BY\r\n" + 
					"	to_char(A.MAKEDATE, 'yyyymmdd')";
			Map<String, Object> bindMap1 = new HashMap<String, Object>();
			bindMap1.put("SHIPREQUESTNAME",shipRequestName); 
			bindMap1.put("MATERIALSPECNAME",materialSpecName); 
			bindMap1.put("SDK_ID",sdk_id); 
			bindMap1.put("PHASE",phase); 
			bindMap1.put("ERPFACTORY",erpfactory); 
			bindMap1.put("SITENAME",siteName); 
			bindMap1.put("CAREER_ASSISTANCE",CAREER_ASSISTANCE); 
			bindMap1.put("CUSTOMERNO",CUSTOMERNO); 
			bindMap1.put("SJMS",SJMS); 
			List<Map<String,Object>> list2 = IDMFrameServiceProxy.getSqlTemplate().queryForList(boxList, bindMap1);
			for (Map<String, Object> map2 : list2) {
				String string = map2.get("MATERIALQUANTITY").toString();
				BigDecimal pcQty = new BigDecimal(string);//批次数量
				String jssj = map2.get("JSSJ").toString();
				bindMap1.put("JSSJ", jssj);
				//如果需求数量大于批次数量，则要求全部备货。小于则表示只能备货这一批次内数量，并且将批次结果传递到前端可以扫描备货
				int compareTo = minQty.compareTo(pcQty); 
				if (compareTo >= 0) {//要求该批次全备货
					minQty = minQty.subtract(pcQty);
					//1.备货
					//2.更新单据数量与剩余数量比较
					String sqll = "SELECT\r\n" + 
							"	A.MATERIALPACKINGNAME ,\r\n" + 
							"	A.SITENAME\r\n" + 
							"FROM\r\n" + 
							"						MATERIALPACKING A\r\n" + 
							"LEFT JOIN SDK_SPEC SS ON\r\n" + 
							"						A.SDK_ID = SS.SDK_ID  \r\n" + 
							"	LEFT JOIN BS_ERPLOCATION B ON A.ERPLOCATION  = B.ERPLOCATIONNAME \r\n" + 
							"WHERE\r\n" + 
							"						A.MATERIALSPECNAME = :MATERIALSPECNAME\r\n" + 
//							"	AND A.MATERIALSPECNAME = B.MATERIALSPECNAME\r\n" + 
//							"	AND A.SITENAME = B.SITENAME\r\n" + 
							"	AND A.SITENAME =:SITENAME\r\n" + 
							"	AND A.MATERIALPACKINGTYPE = 'Box'\r\n" + 
							"	AND (:SDK_ID IS NULL\r\n" + 
							"		OR A.SDK_ID = :SDK_ID)\r\n" + 
							//收卷米数
							"	AND (:SJMS IS NULL\r\n" + 
							"		OR  A.MATERIALQUANTITY * 1000 / SUBSTR( SS.SPECNAME , 1, INSTR(  SS.SPECNAME , 'mm')  -1 ) >=  :SJMS)\r\n" + 
							"	AND (:PHASE IS NULL\r\n" + 
							"		OR A.PHASE = :PHASE)\r\n" + 
//							"	AND (:CAREER_ASSISTANCE IS NULL AND A.CAREER_ASSISTANCE IS NULL)\r\n" + 
//							"	AND (:CAREER_ASSISTANCE IS NULL OR (:CAREER_ASSISTANCE IS NOT NULL AND A.CAREER_ASSISTANCE = :CAREER_ASSISTANCE))\r\n" + 
							"	\r\n" + 
//							"	AND (:CUSTOMERNO IS NULL AND A.CUSTOMNO IS NULL)\r\n" + 
//							"	AND (:CUSTOMERNO IS NULL OR (:CUSTOMERNO IS NOT NULL AND A.CUSTOMNO IN (	SELECT CUSTOMERNO FROM CUSTOMERGROUP T WHERE T.CUSTOMERGROUPNO IN  (\r\n" + 
//							"	SELECT C.CUSTOMERGROUPNO FROM CUSTOMERGROUP C WHERE C.CUSTOMERNO = :CUSTOMERNO)  )))\r\n" + 
							"		\r\n" + 
							"	AND A.ERPFACTORY = :ERPFACTORY\r\n" + 
							"	AND B.ZDBHFLAG = 'Y'\r\n" + 
							"	AND A.SHIPREQUESTNAME IS NULL\r\n" + 
							"	AND A.SHIPREQUESTDETAILNAME IS NULL\r\n" + 
							"	AND A.STOCKSTATE = 'Stocked'\r\n" + 
							"	AND A.PACKINGSTATE = 'Released'\r\n" + 
							"	AND A.HOLDSTATE = 'N'\r\n" + 
							"	AND A.PACKINGGRADE IN ('OK', 'OKEY')\r\n" + 
							"	AND A.MATERIALQUANTITY <> '0'\r\n" + 
							"	AND (A.NPFLAG <> 'Y'\r\n" + 
							"		OR A.NPFLAG IS NULL)\r\n" + 
							"	AND (A.ETCSTATE IS NULL\r\n" + 
							"		OR A.ETCSTATE = 'EtcIn')\r\n" + 
							"	AND A.OQARESULTSTATE <> 'PREOQA'\r\n" + 
							"	AND to_char(A.EXPIRINGDATE, 'yyyymmdd') >= to_char(SYSDATE, 'yyyymmdd')\r\n" + 
							"	AND to_char(A.MAKEDATE, 'yyyymmdd') = :JSSJ";
					
					List<Map<String, Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sqll, bindMap1);
					for (Map<String, Object> map3 : queryForList) {
						MaterialPackingKey materialPackingKey = new MaterialPackingKey("SDK",map3.get("MATERIALPACKINGNAME").toString());
						com.cim.idm.wmspackage.materialpacking.management.data.MaterialPacking selectByKey = MaterialPackingServiceProxy.getMaterialPackingService().selectByKey(materialPackingKey);
						try {
							InvoiceServiceImpl.Reserve(selectByKey, shipRequestName,materialSpecName,sdk_id,phase, "Assign", siteName, user,"","Y");
						} catch (CustomException e) {
							e.printStackTrace();
							AjaxResult.me().setSuccess(false).setMessage(e.toString());
						}
					}
				} else { //记录批次并传递回前端查询
					String resSql = "SELECT\r\n" + 
							"	A.MATERIALPACKINGNAME ,A.MATERIALSPECNAME,A.MATERIALQUANTITY, CASE WHEN MATERIALQUANTITY2 > 0 THEN A.MATERIALQUANTITY2   " + 
							"			    ELSE A.MATERIALQUANTITY END    MATERIALQUANTITY2 ,A.AREANAME ,A.SHELFNAME ,\r\n" + 
							"	A.SDK_ID ,SS.SPECNAME,A.PHASE,\r\n" + 
							"	TO_CHAR(A.MAKEDATE, 'YYYY-MM-DD') RECEIVETIME,\r\n" + 
							"	be.DESCRIPTION ERPLOCATION ,\r\n" + 
							"	s.DESCRIPTION LOCATIONNAME ,\r\n" + 
							"	A.CHARGE ,\r\n" + 
							"								'N' BHFLG, \r\n" + 
							"								'N' IS_CHECK\r\n" + 
							"FROM\r\n" + 
							"	MATERIALPACKING A\r\n" + 
							"LEFT JOIN SDK_SPEC SS ON\r\n" + 
							"	A.SDK_ID = SS.SDK_ID\r\n" + 
							"LEFT JOIN MATERIALSPEC B ON\r\n" + 
							"	A.MATERIALSPECNAME = B.MATERIALSPECNAME\r\n" + 
							"LEFT JOIN BS_ERPLOCATION be ON\r\n" + 
							"	A.ERPLOCATION = be.ERPLOCATIONNAME\r\n" + 
							"LEFT JOIN STORAGESPEC s ON\r\n" + 
							"	A.LOCATIONNAME = s.STORAGENAME\r\n" + 
							"	AND s.ERPLOCATION = A.ERPLOCATION   AND A.ERPFACTORY = s.ERPFACTORY\r\n" + 
							"WHERE\r\n" + 
							"	A.MATERIALSPECNAME = :MATERIALSPECNAME\r\n" + 
							"	AND A.MATERIALSPECNAME = B.MATERIALSPECNAME\r\n" + 
							"	AND A.SITENAME = B.SITENAME\r\n" + 
							"	AND A.SITENAME =:SITENAME\r\n" + 
							"	AND A.MATERIALPACKINGTYPE = 'Box'\r\n" + 
							"	AND (:SDK_ID IS NULL\r\n" + 
							"		OR A.SDK_ID = :SDK_ID)\r\n" + 
							//收卷米数
							"	AND (:SJMS IS NULL\r\n" + 
							"		OR  A.MATERIALQUANTITY * 1000 / SUBSTR( SS.SPECNAME , 1, INSTR(  SS.SPECNAME , 'mm')  -1 ) >=  :SJMS)\r\n" + 
							"	AND (:PHASE IS NULL\r\n" + 
							"		OR A.PHASE = :PHASE)\r\n" + 
//							"	AND (:CAREER_ASSISTANCE IS NULL AND A.CAREER_ASSISTANCE IS NULL)\r\n" + 
//							"	AND (:CAREER_ASSISTANCE IS NULL OR (:CAREER_ASSISTANCE IS NOT NULL AND A.CAREER_ASSISTANCE = :CAREER_ASSISTANCE))\r\n" + 
							"	\r\n" + 
//							"	AND (:CUSTOMERNO IS NULL AND A.CUSTOMNO IS NULL)\r\n" + 
//							"	AND (:CUSTOMERNO IS NULL OR (:CUSTOMERNO IS NOT NULL AND A.CUSTOMNO IN (	SELECT CUSTOMERNO FROM CUSTOMERGROUP T WHERE T.CUSTOMERGROUPNO IN  (\r\n" + 
//							"	SELECT C.CUSTOMERGROUPNO FROM CUSTOMERGROUP C WHERE C.CUSTOMERNO = :CUSTOMERNO)  )))\r\n" + 
							"		\r\n" + 
							"	AND A.ERPFACTORY = :ERPFACTORY\r\n" + 
							"	AND be.ZDBHFLAG = 'Y'\r\n" + 
							"	AND A.SHIPREQUESTNAME IS NULL\r\n" + 
							"	AND A.SHIPREQUESTDETAILNAME IS NULL\r\n" + 
							"	AND A.STOCKSTATE = 'Stocked'\r\n" + 
							"	AND A.PACKINGSTATE = 'Released'\r\n" + 
							"	AND A.HOLDSTATE = 'N'\r\n" + 
							"	AND A.PACKINGGRADE IN ('OK', 'OKEY')\r\n" + 
							"	AND A.MATERIALQUANTITY <> '0'\r\n" + 
							"	AND (A.NPFLAG <> 'Y'\r\n" + 
							"		OR A.NPFLAG IS NULL)\r\n" + 
							"	AND (A.ETCSTATE IS NULL\r\n" + 
							"		OR A.ETCSTATE = 'EtcIn')\r\n" + 
							"	AND A.OQARESULTSTATE <> 'PREOQA'\r\n" + 
							"	AND to_char(A.EXPIRINGDATE, 'yyyymmdd') >= to_char(SYSDATE, 'yyyymmdd')\r\n" + 
							"	AND to_char(A.MAKEDATE, 'yyyymmdd') = :JSSJ";
					List<Map<String, Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(resSql, bindMap1);
//					tempList = new ArrayList<Map<String,Object>> ();
					tempList.addAll(queryForList);
					break;
				}
			}
		} 
		String ressSql = "SELECT\r\n" + 
				"	A.MATERIALPACKINGNAME ,A.MATERIALSPECNAME,A.MATERIALQUANTITY, CASE WHEN MATERIALQUANTITY2 > 0 THEN A.MATERIALQUANTITY2  " + 
				"				   ELSE A.MATERIALQUANTITY END    MATERIALQUANTITY2,A.AREANAME ,A.SHELFNAME ,\r\n" + 
				"	A.CHARGE ,\r\n" + 
				"	TO_CHAR(A.MAKEDATE, 'YYYY-MM-DD') RECEIVETIME,A.SDK_ID,A.PHASE,SS.SPECNAME,\r\n" + 
				"	be.DESCRIPTION ERPLOCATION,\r\n" + 
				"	s.DESCRIPTION LOCATIONNAME,\r\n" + 
				"	'Y' BHFLG, A.SHIPREQUESTNAME,A.SHIPREQUESTDETAILNAME, \r\n" + 
				"					'N' IS_CHECK\r\n" + 
				"FROM\r\n" + 
				"	MATERIALPACKING A\r\n" + 
				"LEFT JOIN SDK_SPEC SS ON\r\n" + 
				"	A.SDK_ID = SS.SDK_ID\r\n" + 
				"LEFT JOIN \r\n" + 
				"	MATERIALSPEC B ON\r\n" + 
				"	A.MATERIALSPECNAME = B.MATERIALSPECNAME\r\n" + 
				"LEFT JOIN BS_ERPLOCATION be ON\r\n" + 
				"	A.ERPLOCATION = be.ERPLOCATIONNAME\r\n" + 
				"LEFT JOIN STORAGESPEC s ON\r\n" + 
				"	A.LOCATIONNAME = s.STORAGENAME  AND A.ERPLOCATION = s.ERPLOCATION   AND A.ERPFACTORY = s.ERPFACTORY  \r\n" + 
				"WHERE\r\n" + 
				"	1 = 1\r\n" + 
				"	AND A.MATERIALSPECNAME = B.MATERIALSPECNAME\r\n" + 
				"	AND A.SITENAME = B.SITENAME\r\n" + 
				"	AND A.SITENAME =:SITENAME\r\n" + 
				"	AND A.SHIPREQUESTNAME = :SHIPREQUESTNAME\r\n" + 
				"	AND A.STOCKSTATE = 'Stocked'\r\n" + 
				"	AND A.PACKINGSTATE = 'Released'\r\n" + 
				"	AND A.HOLDSTATE = 'N'\r\n" + 
				"	AND A.PACKINGGRADE IN ('OK', 'OKEY')\r\n" + 
				"	AND A.MATERIALQUANTITY <> '0'\r\n" + 
				"	AND (A.NPFLAG <> 'Y'\r\n" + 
				"		OR A.NPFLAG IS NULL)\r\n" + 
				"	AND (A.ETCSTATE IS NULL\r\n" + 
				"		OR A.ETCSTATE = 'EtcIn')\r\n" + 
				"	AND A.OQARESULTSTATE <> 'PREOQA'\r\n" + 
				"	AND to_char(A.EXPIRINGDATE, 'yyyymmdd') >= to_char(SYSDATE, 'yyyymmdd')";
		Map<String, Object> hashMap = new HashMap<String,Object> ();
		hashMap.put("SITENAME", "SDK");
		hashMap.put("SHIPREQUESTNAME", shipRequestName);
		List<Map<String, Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(ressSql, hashMap);
		resList.addAll(queryForList);
		resList.addAll(tempList);
		//根据规则自动筛选当前满足条件的库存
		return AjaxResult.me().setResultObj(resList);
	}
	
	
	/**
	 * 非先进先出备货
	 * @param in
	 * @return
	 */
	@Transactional
	@RequestMapping(value = "/getReservePackingInfo", method = RequestMethod.POST)
	public AjaxResult getReservePackingInfo(@RequestBody JSONObject in ){
		MaterialshipRequest materialShipRequest = JSON.toJavaObject(in, MaterialshipRequest.class);
		String shipRequestName = materialShipRequest.getShipRequestName() == null ? "" : materialShipRequest.getShipRequestName();
		String user = materialShipRequest.getUser();
		String ressSql = "SELECT\r\n" + 
				"	A.MATERIALPACKINGNAME ,A.MATERIALSPECNAME,A.MATERIALQUANTITY, CASE WHEN MATERIALQUANTITY2 > 0 THEN A.MATERIALQUANTITY2  " + 
				"				   ELSE A.MATERIALQUANTITY END    MATERIALQUANTITY2,A.AREANAME ,A.SHELFNAME ,\r\n" + 
				"	A.CHARGE ,\r\n" + 
				"	TO_CHAR(A.RECEIVETIME, 'YYYY-MM-DD') RECEIVETIME,A.SDK_ID,A.PHASE,\r\n" + 
				"	be.DESCRIPTION ERPLOCATION,\r\n" + 
				"	s.DESCRIPTION LOCATIONNAME,\r\n" + 
				"	'Y' BHFLG, A.SHIPREQUESTNAME,A.SHIPREQUESTDETAILNAME, \r\n" + 
				"					'N' IS_CHECK\r\n" + 
				"FROM\r\n" + 
				"	MATERIALPACKING A\r\n" + 
				"LEFT JOIN SDK_SPEC SS ON\r\n" + 
				"	A.SDK_ID = SS.SDK_ID\r\n" + 
				"LEFT JOIN \r\n" + 
				"	MATERIALSPEC B ON\r\n" + 
				"	A.MATERIALSPECNAME = B.MATERIALSPECNAME\r\n" + 
				"LEFT JOIN BS_ERPLOCATION be ON\r\n" + 
				"	A.ERPLOCATION = be.ERPLOCATIONNAME\r\n" + 
				"LEFT JOIN STORAGESPEC s ON\r\n" + 
				"	A.LOCATIONNAME = s.STORAGENAME AND A.ERPLOCATION = s.ERPLOCATION  AND A.ERPFACTORY = s.ERPFACTORY  \r\n" + 
				"WHERE\r\n" + 
				"	1 = 1\r\n" + 
				"	AND A.MATERIALSPECNAME = B.MATERIALSPECNAME\r\n" + 
				"	AND A.SITENAME = B.SITENAME\r\n" + 
				"	AND A.SITENAME =:SITENAME\r\n" + 
				"	AND A.SHIPREQUESTNAME = :SHIPREQUESTNAME\r\n" + 
				"	AND A.STOCKSTATE = 'Stocked'\r\n" + 
				"	AND A.PACKINGSTATE = 'Released'\r\n" + 
				"	AND A.HOLDSTATE = 'N'\r\n" + 
//				"	AND A.PACKINGGRADE IN ('OK', 'OKEY')\r\n" + 
				"	AND A.MATERIALQUANTITY <> '0'\r\n" + 
				"	AND (A.NPFLAG <> 'Y'\r\n" + 
				"		OR A.NPFLAG IS NULL)\r\n" + 
				"	AND (A.ETCSTATE IS NULL\r\n" + 
				"		OR A.ETCSTATE = 'EtcIn')\r\n" + 
				"	AND A.OQARESULTSTATE <> 'PREOQA'\r\n" + 
				"	 ORDER BY A.MATERIALSPECNAME,A.SDK_ID,A.PHASE, A.RECEIVETIME";
		Map<String, Object> hashMap = new HashMap<String,Object> ();
		hashMap.put("SITENAME", "SDK");
		hashMap.put("SHIPREQUESTNAME", shipRequestName);
		List<Map<String, Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(ressSql, hashMap);
		return AjaxResult.me().setResultObj(queryForList);
	}
	
	/**
	 * 备货或者取消
	 * @param in
	 * @return
	 */
	@Transactional
	@RequestMapping(value = "/DessignOrAssign", method = RequestMethod.POST)
	public AjaxResult DessignOrAssign(@RequestBody JSONObject in ){
		MaterialshipRequest materialShipRequest = JSON.toJavaObject(in, MaterialshipRequest.class);
		String type = materialShipRequest.getType();//Assign 备货 Dessign 解绑
		String materialPackingName = materialShipRequest.getMaterialPackingName();//标签
		String materialSpecName = materialShipRequest.getMaterialSpecName();
		String shipRequestName = materialShipRequest.getShipRequestName();
		String materialQuantity = materialShipRequest.getMaterialQuantity();
		String materialQuantity2 = materialShipRequest.getMaterialQuantity2();
		String sdk_id = (materialShipRequest.getSdk_id() == null || "~".equals(materialShipRequest.getSdk_id().toString())) ? "" : materialShipRequest.getSdk_id();
		String phase = materialShipRequest.getPhase() == null ? "" :  materialShipRequest.getPhase();
		String user = materialShipRequest.getUser();
		//查询单据类型
		String sql = "SELECT\r\n" + 
				"	m.SHIPREQUESTTYPE ,\r\n" + 
				"	m.SHIPREQUESTDETAILTYPE,\r\n" + 
				"	t.MATERIALSPECNAME ,\r\n" + 
				"	SUM(t.REQUESTQUANTITY - t.ASSIGNEDQUANTITY) MINUSQTY,t.SDK_ID,t.PHASE\r\n" + 
				"FROM\r\n" + 
				"	MATERIALSHIPREQUESTDETAIL t\r\n" + 
				"LEFT JOIN MATERIALSHIPREQUEST m ON\r\n" + 
				"	t.SHIPREQUESTNAME = m.SHIPREQUESTNAME\r\n" + 
				"WHERE\r\n" + 
				"	m.SHIPREQUESTNAME = :SHIPREQUESTNAME\r\n" + 
				"	AND t.MATERIALSPECNAME = :MATERIALSPECNAME\r\n" + 
				"	AND (:SDK_ID IS NULL\r\n" + 
				"		OR t.SDK_ID = :SDK_ID)\r\n" + 
				"	AND (:PHASE IS NULL\r\n" + 
				"		OR t.PHASE = :PHASE)\r\n" + 
				"		GROUP BY m.SHIPREQUESTTYPE ,\r\n" + 
				"	m.SHIPREQUESTDETAILTYPE,\r\n" + 
				"	t.MATERIALSPECNAME,t.SDK_ID,t.PHASE\r\n" + 
				"ORDER BY\r\n" + 
				"	t.MATERIALSPECNAME ,\r\n" + 
				"	t.SDK_ID,\r\n" + 
				"	t.PHASE";
		Map<String, Object> hashMap = new HashMap<String,Object> ();
		hashMap.put("SHIPREQUESTNAME", shipRequestName);
		hashMap.put("MATERIALSPECNAME", materialSpecName);
		hashMap.put("SDK_ID", sdk_id);
		hashMap.put("PHASE", phase);
		List<Map<String, Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, hashMap);
		if(Objects.isNull(queryForList) || queryForList.isEmpty()) {
			return AjaxResult.me().setSuccess(false).setMessage("未找到对应的数据！");
		}
		String judge = "SELECT\r\n" + 
				"	m.SDK_ID,MATERIALSPECNAME,\r\n" + 
				"	m.PHASE,\r\n" + 
				"	CASE WHEN MATERIALQUANTITY2 > 0 THEN MATERIALQUANTITY2    " + 
				"	 ELSE MATERIALQUANTITY   END    MATERIALQUANTITY2 , " + 
				"	m.SHIPREQUESTNAME,\r\n" + 
				"	m.STOCKSTATE,\r\n" + 
				"	m.PACKINGGRADE ,\r\n" + 
				"	m.SHELFNAME ,\r\n" + 
				"	CASE WHEN to_char(EXPIRINGDATE, 'yyyymmdd') >= to_char(SYSDATE, 'yyyymmdd') THEN 1\r\n" + 
				"	ELSE 0 END EXP, OQARESULTSTATE \r\n" + 
				"FROM\r\n" + 
				"	MATERIALPACKING m\r\n" + 
				"WHERE\r\n" + 
				"	m.MATERIALPACKINGNAME = :MATERIALPACKINGNAME";
		Map<String, Object> hashMap2 = new HashMap<String,Object> ();
		hashMap2.put("MATERIALPACKINGNAME", materialPackingName);
		List<Map<String, Object>> queryForList2 = IDMFrameServiceProxy.getSqlTemplate().queryForList(judge, hashMap2);
		if (queryForList2 != null && queryForList2.size() > 0) {
			materialSpecName = queryForList2.get(0).get("MATERIALSPECNAME") == null ? "" : queryForList2.get(0).get("MATERIALSPECNAME").toString();
			sdk_id = (queryForList2.get(0).get("SDK_ID") == null || "~".equals(materialShipRequest.getSdk_id().toString()))  ? "" : queryForList2.get(0).get("SDK_ID").toString();
			phase = queryForList2.get(0).get("PHASE") == null ? "" : queryForList2.get(0).get("PHASE").toString();
			materialQuantity2 = queryForList2.get(0).get("MATERIALQUANTITY2") == null ? "" : queryForList2.get(0).get("MATERIALQUANTITY2").toString();
			String  invoice = queryForList2.get(0).get("SHIPREQUESTNAME") == null ? "" : queryForList2.get(0).get("SHIPREQUESTNAME").toString();
			String stockState = queryForList2.get(0).get("STOCKSTATE") == null ? "" : queryForList2.get(0).get("STOCKSTATE").toString();
			String packinggrade  = queryForList2.get(0).get("PACKINGGRADE") == null ? "" : queryForList2.get(0).get("PACKINGGRADE").toString();
			String exp  = queryForList2.get(0).get("EXP") == null ? "" : queryForList2.get(0).get("EXP").toString();
			String oqaresultstate  = queryForList2.get(0).get("OQARESULTSTATE") == null ? "" : queryForList2.get(0).get("OQARESULTSTATE").toString();
			String shelfName  = queryForList2.get(0).get("SHELFNAME") == null ? "" : queryForList2.get(0).get("SHELFNAME").toString();
			
			if (!"Stocked".equals(stockState)) {
				return AjaxResult.me().setSuccess(false).setMessage("条码不在库！");
			}
			if (!"OK".equals(packinggrade)) {
				return AjaxResult.me().setSuccess(false).setMessage("批次不合格！");
			}
			if (!"1".equals(exp)) {
				return AjaxResult.me().setSuccess(false).setMessage("批次已过期！");
			}
			if (!"END".equals(oqaresultstate)) {
				return AjaxResult.me().setSuccess(false).setMessage("未完成质检！");
			}
//			if (!"".equals(invoice)) {
//				return AjaxResult.me().setSuccess(false).setMessage("条码已被" + invoice + "备货！");
//			}
//			if(!"".equals(shelfName) && !"B".equals(shelfName.substring(0, 1))) {
//				return AjaxResult.me().setSuccess(false).setMessage("产品在立库上，请先下架再出库！");
//			}
		} else {
			return AjaxResult.me().setSuccess(false).setMessage("未找到对应的条码！");
		}
		String shipRequestType = queryForList.get(0).get("SHIPREQUESTTYPE").toString();
		String shipRequestDetailType = queryForList.get(0).get("SHIPREQUESTDETAILTYPE").toString();
		String minusQty = queryForList.get(0).get("MINUSQTY").toString();
		BigDecimal FmaterialQuantity = new BigDecimal(materialQuantity);
		BigDecimal FminusQty = new BigDecimal(minusQty);
		BigDecimal Fzero = new BigDecimal(0);
		//"SHIPREQUESTDETAILTYPE": "4Y-Cxx-03",//跨组织领料调拨单 
		//"BLSQ4K".equals(shipRequestType) || "4Y".equals(shipRequestType) || "CKSQ4I".equals(shipRequestType)
		int dy0 = FminusQty.compareTo(Fzero);
		MaterialPackingKey materialPackingKey = new MaterialPackingKey("SDK",materialPackingName);
		com.cim.idm.wmspackage.materialpacking.management.data.MaterialPacking selectByKey = MaterialPackingServiceProxy.getMaterialPackingService().selectByKey(materialPackingKey);
		if ("Assign".equals(type)) {
			System.out.println("4Y".equals(shipRequestType) + "*****************");
			if ("4Y".equals(shipRequestType)) {//调拨单
				if ( "4Y-Cxx-03".equals(shipRequestDetailType)) {
					if (dy0 < 0) {//只能超发一个
						return AjaxResult.me().setSuccess(false).setMessage("只能超发一个不能出库了！");
					}
				} 
			} else if ("BLSQ4K".equals(shipRequestType)) {//领料
				if (dy0 < 0) {//只能超发一个
					return AjaxResult.me().setSuccess(false).setMessage("只能超发一个不能出库了！");
				}
			} else if ("CKSQ4I".equals(shipRequestType)) { //其他出库
				if (dy0 < 0) {//只能超发一个
					return AjaxResult.me().setSuccess(false).setMessage("只能超发一个不能出库了！");
				}
			} else if ("4C".equals(shipRequestType)) {
				if (dy0 <= 0) {//只能超发一个
					return AjaxResult.me().setSuccess(false).setMessage("销售不允许超发！");
				}
			}else {
				return AjaxResult.me().setSuccess(false).setMessage("单据类型不正确！");
			}
		} else if ("Dessign".equals(type)) {
			
		}
		try {
			InvoiceServiceImpl.Reserve(selectByKey, shipRequestName, materialSpecName,sdk_id, phase, type, "SDK", user,materialQuantity2,"Y");
		} catch (CustomException e) {
			e.printStackTrace();
			return AjaxResult.me().setSuccess(false).setMessage(e.toString());
		}
		
		return  AjaxResult.me().setSuccess(true).setMessage("备货成功");
	}
	
	
	
	/**
	 * 备货或者取消
	 * @param in
	 * @return
	 */
	@Transactional
	@RequestMapping(value = "/UnFIFODessignOrAssign", method = RequestMethod.POST)
	public AjaxResult UnFIFODessignOrAssign(@RequestBody JSONObject in ){
		MaterialshipRequest materialShipRequest = JSON.toJavaObject(in, MaterialshipRequest.class);
		String shipRequestName2 = materialShipRequest.getShipRequestName();
		//根据单据类型判断生产订单类型，如果是领料单且单据类型是复卷和返工的不卡控批次质量状态
		String dTypeSql = "SELECT M.PRODUCTORDERTYPE,M.PRODUCTORDERNO FROM MATERIALSHIPREQUEST M WHERE M.SHIPREQUESTNAME = :SHIPREQUESTNAME";
		Map<String, Object> hashMap3 = new HashMap<String,Object> ();
		hashMap3.put("SHIPREQUESTNAME", shipRequestName2);
		String productOrderType = "";
		String productOrderNo = ""; // 生产订单
		List<Map<String, Object>> queryForListW = new ArrayList<>();
		List<Map<String, Object>> queryForList3 = IDMFrameServiceProxy.getSqlTemplate().queryForList(dTypeSql, hashMap3);
		if (queryForList3 != null && queryForList3.size() > 0) {
			productOrderType = queryForList3.get(0).get("PRODUCTORDERTYPE") == null ? "" :queryForList3.get(0).get("PRODUCTORDERTYPE").toString();
			productOrderNo = queryForList3.get(0).get("PRODUCTORDERNO") == null ? "" :queryForList3.get(0).get("PRODUCTORDERNO").toString();
			
			// 查询工单维护表 是否有对应的生产订单
			String workSql = "SELECT w.WORKORDER FROM WORKORDER w WHERE w.WORKORDER =:WORKORDER";
			Map<String, Object> hashMapW = new HashMap<String,Object> ();
			hashMapW.put("WORKORDER", productOrderNo);
			queryForListW = IDMFrameServiceProxy.getSqlTemplate().queryForList(workSql, hashMapW);
		}
		
		String type = materialShipRequest.getType();//Assign 备货 Dessign 解绑
		String materialPackingName = materialShipRequest.getMaterialPackingName();//标签
		String materialSpecName = materialShipRequest.getMaterialSpecName();
		String shipRequestName = materialShipRequest.getShipRequestName();
		String materialQuantity = materialShipRequest.getMaterialQuantity();
		String materialQuantity2 = materialShipRequest.getMaterialQuantity2();
		String sdk_id = materialShipRequest.getSdk_id() == null ? "" : materialShipRequest.getSdk_id();
		String phase = materialShipRequest.getPhase() == null ? "" :  materialShipRequest.getPhase();
		String user = materialShipRequest.getUser();
		//根据条码查询库存状态
		String judge = "SELECT\r\n" + 
				"	m.SDK_ID,MATERIALSPECNAME,\r\n" + 
				"	m.PHASE,\r\n" + 
				"	CASE WHEN MATERIALQUANTITY2 > 0 THEN MATERIALQUANTITY2    " + 
				"	 ELSE MATERIALQUANTITY   END    MATERIALQUANTITY2 , " + 
				"	m.SHIPREQUESTNAME,\r\n" + 
				"	m.STOCKSTATE,\r\n" + 
				"	m.PACKINGGRADE ,\r\n" + 
				"	CASE WHEN to_char(EXPIRINGDATE, 'yyyymmdd') >= to_char(SYSDATE, 'yyyymmdd') THEN 1\r\n" + 
				"	ELSE 0 END EXP, OQARESULTSTATE \r\n" + 
				"FROM\r\n" + 
				"	MATERIALPACKING m\r\n" + 
				"WHERE\r\n" + 
				"	m.MATERIALPACKINGNAME = :MATERIALPACKINGNAME";
		Map<String, Object> hashMap2 = new HashMap<String,Object> ();
		hashMap2.put("MATERIALPACKINGNAME", materialPackingName);
		List<Map<String, Object>> queryForList2 = IDMFrameServiceProxy.getSqlTemplate().queryForList(judge, hashMap2);
		if (queryForList2 != null && queryForList2.size() > 0) {
			materialSpecName = queryForList2.get(0).get("MATERIALSPECNAME") == null ? "" : queryForList2.get(0).get("MATERIALSPECNAME").toString();
			sdk_id = queryForList2.get(0).get("SDK_ID") == null ? "" : queryForList2.get(0).get("SDK_ID").toString();
			phase = queryForList2.get(0).get("PHASE") == null ? "" : queryForList2.get(0).get("PHASE").toString();
			materialQuantity2 = queryForList2.get(0).get("MATERIALQUANTITY2") == null ? "" : queryForList2.get(0).get("MATERIALQUANTITY2").toString();
			String  invoice = queryForList2.get(0).get("SHIPREQUESTNAME") == null ? "" : queryForList2.get(0).get("SHIPREQUESTNAME").toString();
			String stockState = queryForList2.get(0).get("STOCKSTATE") == null ? "" : queryForList2.get(0).get("STOCKSTATE").toString();
			String packinggrade  = queryForList2.get(0).get("PACKINGGRADE") == null ? "" : queryForList2.get(0).get("PACKINGGRADE").toString();
			String exp  = queryForList2.get(0).get("EXP") == null ? "" : queryForList2.get(0).get("EXP").toString();
			String oqaresultstate  = queryForList2.get(0).get("OQARESULTSTATE") == null ? "" : queryForList2.get(0).get("OQARESULTSTATE").toString();
			if (!"Stocked".equals(stockState)) {
				return AjaxResult.me().setSuccess(false).setMessage("条码不在库！");
			}
			if (!"OK".equals(packinggrade) && (!"55A2-02".equals(productOrderType) && !"55A2-Cxx-08".equals(productOrderType) 
					&& !"4455-Cxx-10".equals(productOrderType)   && !"4455-Cxx-01".equals(productOrderType) 
					 && !"5X-Cxx-07".equals(productOrderType)) && (queryForListW == null || queryForListW.size() < 1)) {//复卷，返工工单
				return AjaxResult.me().setSuccess(false).setMessage("单据类型不为复卷或 返工, 引膜领用和研发领用, 批次不合格不能出库！");
			}
			if (!"1".equals(exp)  && (!"55A2-02".equals(productOrderType) && !"55A2-Cxx-08".equals(productOrderType) 
					&& !"4455-Cxx-10".equals(productOrderType)   && !"4455-Cxx-01".equals(productOrderType) 
					 && !"5X-Cxx-07".equals(productOrderType)) && (queryForListW == null || queryForListW.size() < 1)) {//复卷，返工工单
				return AjaxResult.me().setSuccess(false).setMessage("单据类型不为复卷或 返工, 引膜领用和研发领用, 批次已过期！");
			}
			if (!"END".equals(oqaresultstate)) {
				return AjaxResult.me().setSuccess(false).setMessage("未完成质检！");
			}
//			if (!"".equals(invoice)) {
//				return AjaxResult.me().setSuccess(false).setMessage("条码已被" + invoice + "备货！");
//			}

			if (!"".equals(invoice) && "Assign".equals(type)) {
				return AjaxResult.me().setSuccess(false).setMessage("条码已被" + invoice + "备货！");
			}

		} else {
			return AjaxResult.me().setSuccess(false).setMessage("未找到对应的条码！");
		}
		//查询单据类型
		String sql = "SELECT\r\n" + 
				"	m.SHIPREQUESTTYPE ,\r\n" + 
				"	m.SHIPREQUESTDETAILTYPE,\r\n" + 
				"	t.MATERIALSPECNAME ,\r\n" + 
				"	SUM(t.REQUESTQUANTITY - t.ASSIGNEDQUANTITY) MINUSQTY,t.SDK_ID,t.PHASE\r\n" + 
				"FROM\r\n" + 
				"	MATERIALSHIPREQUESTDETAIL t\r\n" + 
				"LEFT JOIN MATERIALSHIPREQUEST m ON\r\n" + 
				"	t.SHIPREQUESTNAME = m.SHIPREQUESTNAME\r\n" + 
				"WHERE\r\n" + 
				"	m.SHIPREQUESTNAME = :SHIPREQUESTNAME\r\n" + 
				"	AND t.MATERIALSPECNAME = :MATERIALSPECNAME\r\n" + 
				"	AND (:SDK_ID IS NULL\r\n" + 
				"		OR t.SDK_ID = :SDK_ID)\r\n" + 
				"	AND (:PHASE IS NULL\r\n" + 
				"		OR t.PHASE = :PHASE)\r\n" + 
				"		GROUP BY m.SHIPREQUESTTYPE ,\r\n" + 
				"	m.SHIPREQUESTDETAILTYPE,\r\n" + 
				"	t.MATERIALSPECNAME,t.SDK_ID,t.PHASE\r\n" + 
				"ORDER BY\r\n" + 
				"	t.MATERIALSPECNAME ,\r\n" + 
				"	t.SDK_ID,\r\n" + 
				"	t.PHASE";
		Map<String, Object> hashMap = new HashMap<String,Object> ();
		hashMap.put("SHIPREQUESTNAME", shipRequestName);
		hashMap.put("MATERIALSPECNAME", materialSpecName);
		hashMap.put("SDK_ID", sdk_id);
		hashMap.put("PHASE", phase);
		List<Map<String, Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, hashMap);
		if(Objects.isNull(queryForList) || queryForList.isEmpty()) {
			return AjaxResult.me().setSuccess(false).setMessage("条码与单据无法匹配！");
		}
		String shipRequestType = queryForList.get(0).get("SHIPREQUESTTYPE").toString();
		String shipRequestDetailType = queryForList.get(0).get("SHIPREQUESTDETAILTYPE").toString();
		String minusQty = queryForList.get(0).get("MINUSQTY").toString();
		BigDecimal FminusQty = new BigDecimal(minusQty);
		BigDecimal Fzero = new BigDecimal(0);
		//"SHIPREQUESTDETAILTYPE": "4Y-Cxx-03",//跨组织领料调拨单 
		//"BLSQ4K".equals(shipRequestType) || "4Y".equals(shipRequestType) || "CKSQ4I".equals(shipRequestType)
		int dy0 = FminusQty.compareTo(Fzero);
		MaterialPackingKey materialPackingKey = new MaterialPackingKey("SDK",materialPackingName);
		com.cim.idm.wmspackage.materialpacking.management.data.MaterialPacking selectByKey = MaterialPackingServiceProxy.getMaterialPackingService().selectByKey(materialPackingKey);
		if ("Assign".equals(type)) {
			System.out.println("4Y".equals(shipRequestType) + "*****************");
			if ("4Y".equals(shipRequestType)) {//调拨单
				if ( "4Y-Cxx-03".equals(shipRequestDetailType)) {
					if (dy0 < 0) {//只能超发一个
						return AjaxResult.me().setSuccess(false).setMessage("只能超发一个不能出库了！");
					}
				} 
			} else if ("BLSQ4K".equals(shipRequestType)) {//领料
				if (dy0 < 0) {//只能超发一个
					return AjaxResult.me().setSuccess(false).setMessage("只能超发一个不能出库了！");
				}
			} else if ("CKSQ4I".equals(shipRequestType)) { //其他出库
				if (dy0 < 0) {//只能超发一个
					return AjaxResult.me().setSuccess(false).setMessage("只能超发一个不能出库了！");
				}
			} else if ("OEM".equals(shipRequestType)) { //委外出库
				if (dy0 < 0) {//只能超发一个
					return AjaxResult.me().setSuccess(false).setMessage("只能超发一个不能出库了！");
				}
			} else if ("4C".equals(shipRequestType)) {
				if (dy0 < 0) {//只能超发一个
					return AjaxResult.me().setSuccess(false).setMessage("销售不允许超发！");
				}
			}else {
				return AjaxResult.me().setSuccess(false).setMessage("单据类型不正确！");
			}
		} else if ("Dessign".equals(type)) {
			
		}
		try {
			InvoiceServiceImpl.Reserve(selectByKey, shipRequestName, materialSpecName,sdk_id, phase, type, "SDK", user,materialQuantity2,"N");
		} catch (CustomException e) {
			e.printStackTrace();
			AjaxResult.me().setSuccess(false).setMessage(e.toString());
		}
		
		return  AjaxResult.me().setSuccess(true).setMessage("备货或解绑成功");
	}

	/**
	 * BOPP备货取消
	 * @param in
	 * @return
	 */
	@Transactional
	@RequestMapping(value = "/BOPPDessignOrAssign", method = RequestMethod.POST)
	public AjaxResult BOPPDessignOrAssign(@RequestBody JSONObject in ){
		MaterialshipRequest materialShipRequest = JSON.toJavaObject(in, MaterialshipRequest.class);
		String shipRequestName2 = materialShipRequest.getShipRequestName();
		//根据单据类型判断生产订单类型，如果是领料单且单据类型是复卷和返工的不卡控批次质量状态
		String dTypeSql = "SELECT M.PRODUCTORDERTYPE FROM MATERIALSHIPREQUEST M WHERE M.SHIPREQUESTNAME = :SHIPREQUESTNAME";
		Map<String, Object> hashMap3 = new HashMap<String,Object> ();
		hashMap3.put("SHIPREQUESTNAME", shipRequestName2);
		String productOrderType = "";
		List<Map<String, Object>> queryForList3 = IDMFrameServiceProxy.getSqlTemplate().queryForList(dTypeSql, hashMap3);
		if (queryForList3 != null && queryForList3.size() > 0) {
			productOrderType = queryForList3.get(0).get("PRODUCTORDERTYPE") == null ? "" :queryForList3.get(0).get("PRODUCTORDERTYPE").toString();
		}
		String type = materialShipRequest.getType();//Assign 备货 Dessign 解绑
		String materialPackingName = materialShipRequest.getMaterialPackingName();//标签
		String materialSpecName = materialShipRequest.getMaterialSpecName();
		String shipRequestName = materialShipRequest.getShipRequestName();
		String materialQuantity = materialShipRequest.getMaterialQuantity();
		String materialQuantity2 = materialShipRequest.getMaterialQuantity2();
		String sdk_id = materialShipRequest.getSdk_id() == null ? "" : materialShipRequest.getSdk_id();
		String phase = materialShipRequest.getPhase() == null ? "" :  materialShipRequest.getPhase();
		String user = materialShipRequest.getUser();
		//根据条码查询库存状态
		String judge = "SELECT\r\n" + 
				"	m.SDK_ID,MATERIALSPECNAME,ERPLOCATION,\r\n" + 
				"	m.PHASE,\r\n" + 
				"	CASE WHEN MATERIALQUANTITY2 > 0 THEN MATERIALQUANTITY2    " + 
				"	 ELSE MATERIALQUANTITY   END    MATERIALQUANTITY2 , " + 
				"	m.SHIPREQUESTNAME,\r\n" + 
				"	m.STOCKSTATE,\r\n" + 
				"	m.PACKINGGRADE ,\r\n" + 
				"	CASE WHEN to_char(EXPIRINGDATE, 'yyyymmdd') >= to_char(SYSDATE, 'yyyymmdd') THEN 1\r\n" + 
				"	ELSE 0 END EXP, OQARESULTSTATE \r\n" + 
				"FROM\r\n" + 
				"	MATERIALPACKING m\r\n" + 
				"WHERE\r\n" + 
				"	m.MATERIALPACKINGNAME = :MATERIALPACKINGNAME";
		Map<String, Object> hashMap2 = new HashMap<String,Object> ();
		hashMap2.put("MATERIALPACKINGNAME", materialPackingName);
		List<Map<String, Object>> queryForList2 = IDMFrameServiceProxy.getSqlTemplate().queryForList(judge, hashMap2);
		String erpLocation = "";
		if (queryForList2 != null && queryForList2.size() > 0) {
			erpLocation = queryForList2.get(0).get("ERPLOCATION") == null ? "" : queryForList2.get(0).get("ERPLOCATION").toString();
			materialSpecName = queryForList2.get(0).get("MATERIALSPECNAME") == null ? "" : queryForList2.get(0).get("MATERIALSPECNAME").toString();
			sdk_id = queryForList2.get(0).get("SDK_ID") == null ? "" : queryForList2.get(0).get("SDK_ID").toString();
			phase = queryForList2.get(0).get("PHASE") == null ? "" : queryForList2.get(0).get("PHASE").toString();
			materialQuantity2 = queryForList2.get(0).get("MATERIALQUANTITY2") == null ? "" : queryForList2.get(0).get("MATERIALQUANTITY2").toString();
			String  invoice = queryForList2.get(0).get("SHIPREQUESTNAME") == null ? "" : queryForList2.get(0).get("SHIPREQUESTNAME").toString();
			String stockState = queryForList2.get(0).get("STOCKSTATE") == null ? "" : queryForList2.get(0).get("STOCKSTATE").toString();
			String packinggrade  = queryForList2.get(0).get("PACKINGGRADE") == null ? "" : queryForList2.get(0).get("PACKINGGRADE").toString();
			String exp  = queryForList2.get(0).get("EXP") == null ? "" : queryForList2.get(0).get("EXP").toString();
			String oqaresultstate  = queryForList2.get(0).get("OQARESULTSTATE") == null ? "" : queryForList2.get(0).get("OQARESULTSTATE").toString();
			if (!"Stocked".equals(stockState)) {
				return AjaxResult.me().setSuccess(false).setMessage("条码不在库！");
			}
			if (!"OK".equals(packinggrade) && (!"55A2-02".equals(productOrderType) && !"55A2-Cxx-08".equals(productOrderType) 
					&& !"4455-Cxx-10".equals(productOrderType)   && !"455-Cxx-01".equals(productOrderType) 
					 && !"5X-Cxx-07".equals(productOrderType))) {//复卷，返工工单
				return AjaxResult.me().setSuccess(false).setMessage("单据类型不为复卷或 返工, 引膜领用和研发领用, 批次不合格不能出库！");
			}
			if (!"1".equals(exp)  && (!"55A2-02".equals(productOrderType) && !"55A2-Cxx-08".equals(productOrderType) 
					&& !"4455-Cxx-10".equals(productOrderType)   && !"455-Cxx-01".equals(productOrderType) 
					 && !"5X-Cxx-07".equals(productOrderType))) {//复卷，返工工单
				return AjaxResult.me().setSuccess(false).setMessage("单据类型不为复卷或 返工, 引膜领用和研发领用, 批次已过期！");
			}
			if (!"END".equals(oqaresultstate)) {
				return AjaxResult.me().setSuccess(false).setMessage("未完成质检！");
			}
			if (!"".equals(invoice)) {
				return AjaxResult.me().setSuccess(false).setMessage("条码已被" + invoice + "备货！");
			}
		} else {
			return AjaxResult.me().setSuccess(false).setMessage("未找到对应的条码！");
		}
		//查询单据类型
		String sql = "SELECT\r\n" + 
				"	m.SHIPREQUESTTYPE ,\r\n" + 
				"	m.SHIPREQUESTDETAILTYPE,\r\n" + 
				"	t.MATERIALSPECNAME ,t.ERPLOCATION ,\r\n" + 
				"	SUM(t.REQUESTQUANTITY - t.ASSIGNEDQUANTITY) MINUSQTY,t.SDK_ID,t.PHASE\r\n" + 
				"FROM\r\n" + 
				"	MATERIALSHIPREQUESTDETAIL t\r\n" + 
				"LEFT JOIN MATERIALSHIPREQUEST m ON\r\n" + 
				"	t.SHIPREQUESTNAME = m.SHIPREQUESTNAME\r\n" + 
				"WHERE\r\n" + 
				"	m.SHIPREQUESTNAME = :SHIPREQUESTNAME\r\n" + 
				"	AND t.MATERIALSPECNAME = :MATERIALSPECNAME\r\n" + 
				"	AND (:SDK_ID IS NULL\r\n" + 
				"		OR t.SDK_ID = :SDK_ID)\r\n" + 
				"	AND (:PHASE IS NULL\r\n" + 
				"		OR t.PHASE = :PHASE)\r\n" + 
				"		GROUP BY m.SHIPREQUESTTYPE ,\r\n" + 
				"	m.SHIPREQUESTDETAILTYPE,\r\n" + 
				"	t.MATERIALSPECNAME,t.SDK_ID,t.PHASE,t.ERPLOCATION \r\n" + 
				"ORDER BY\r\n" + 
				"	t.MATERIALSPECNAME ,\r\n" + 
				"	t.SDK_ID,\r\n" + 
				"	t.PHASE";
		Map<String, Object> hashMap = new HashMap<String,Object> ();
		hashMap.put("SHIPREQUESTNAME", shipRequestName);
		hashMap.put("MATERIALSPECNAME", materialSpecName);
		hashMap.put("SDK_ID", sdk_id);
		hashMap.put("PHASE", phase);
		List<Map<String, Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, hashMap);
		if(Objects.isNull(queryForList) || queryForList.isEmpty()) {
			return AjaxResult.me().setSuccess(false).setMessage("条码与单据无法匹配！");
		}
		String shipRequestType = queryForList.get(0).get("SHIPREQUESTTYPE").toString();
		String djErpLocation = queryForList.get(0).get("ERPLOCATION") == null ? "" : queryForList.get(0).get("ERPLOCATION").toString();
		if (!erpLocation.equals(djErpLocation))  {
			return AjaxResult.me().setSuccess(false).setMessage("单据指定仓库和批次不一致 单据" + djErpLocation + "批次" +  erpLocation);
		}
		String shipRequestDetailType = queryForList.get(0).get("SHIPREQUESTDETAILTYPE").toString();
		String minusQty = queryForList.get(0).get("MINUSQTY").toString();
		BigDecimal FminusQty = new BigDecimal(minusQty);
		BigDecimal Fzero = new BigDecimal(0);
		//"SHIPREQUESTDETAILTYPE": "4Y-Cxx-03",//跨组织领料调拨单 
		//"BLSQ4K".equals(shipRequestType) || "4Y".equals(shipRequestType) || "CKSQ4I".equals(shipRequestType)
		int dy0 = FminusQty.compareTo(Fzero);
		MaterialPackingKey materialPackingKey = new MaterialPackingKey("SDK",materialPackingName);
		com.cim.idm.wmspackage.materialpacking.management.data.MaterialPacking selectByKey = MaterialPackingServiceProxy.getMaterialPackingService().selectByKey(materialPackingKey);
		if ("Assign".equals(type)) {
			System.out.println("4Y".equals(shipRequestType) + "*****************");
			if ("4Y".equals(shipRequestType)) {//调拨单
				if ( "4Y-Cxx-03".equals(shipRequestDetailType)) {
					if (dy0 < 0) {//只能超发一个
						return AjaxResult.me().setSuccess(false).setMessage("只能超发一个不能出库了！");
					}
				} 
			} else if ("BLSQ4K".equals(shipRequestType)) {//领料
				if (dy0 < 0) {//只能超发一个
					return AjaxResult.me().setSuccess(false).setMessage("只能超发一个不能出库了！");
				}
			} else if ("CKSQ4I".equals(shipRequestType)) { //其他出库
				if (dy0 < 0) {//只能超发一个
					return AjaxResult.me().setSuccess(false).setMessage("只能超发一个不能出库了！");
				}
			} else if ("4C".equals(shipRequestType)) {
				if (dy0 < 0) {//只能超发一个
					return AjaxResult.me().setSuccess(false).setMessage("销售不允许超发！");
				}
			}else {
				return AjaxResult.me().setSuccess(false).setMessage("单据类型不正确！");
			}
		} else if ("Dessign".equals(type)) {
			
		}
		try {
			InvoiceServiceImpl.Reserve(selectByKey, shipRequestName, materialSpecName,sdk_id, phase, type, "SDK", user,materialQuantity2,"N");
		} catch (CustomException e) {
			e.printStackTrace();
			AjaxResult.me().setSuccess(false).setMessage(e.toString());
		}
		
		return  AjaxResult.me().setSuccess(true).setMessage("备货成功");
	}
}
