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.model.MaterialPacking;
import com.cim.idm.model.po.MaterialReceiveRequest;
import com.cim.idm.model.MaterialshipRequest;
import com.cim.idm.utils.AjaxResult;
import com.cim.idm.utils.CommonUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
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.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import com.cim.idm.service.Impl.SaleReturnServiceImpl;

@Api("RMA接口")
@RestController
@RequestMapping("/SaleReturn")
@EnableAutoConfiguration
public class SaleReturnController {
	
	@Autowired
	private SaleReturnServiceImpl SaleReturnServiceImpl;
	
	CommonUtils untils=new CommonUtils();
	
	
	/******************************************************************************************************************************/
	/*****************************************销售退库单********************************************************************************/
	/******************************************************************************************************************************/
	/**
	 * 获取销售退待操作单号集合
	 * @param in
	 * @return
	 */
	@RequestMapping(value = "/getSaleReturnList", method = RequestMethod.POST)
	public AjaxResult getSaleReturnList(@RequestBody JSONObject in ){

		MaterialReceiveRequest materialReceiveRequest = JSON.toJavaObject(in, MaterialReceiveRequest.class);
		String receiveRequestName = materialReceiveRequest.getReceiveRequestName() == null ? "" : materialReceiveRequest.getReceiveRequestName();
		String receiveRequestType = materialReceiveRequest.getReceiveRequestType();
		String sql = "SELECT T.RECEIVEREQUESTNAME FROM MATERIALRECEIVEREQUEST T WHERE "
				+ " T.RECEIVEREQUESTNAME LIKE :RECEIVEREQUESTNAME AND T.RECEIVEREQUESTTYPE  = :RECEIVEREQUESTTYPE";
		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 ( list == null || list.size() < 1) {
			throw new GlobalException("未找到对应的数据！");
		}
		return AjaxResult.me().setResultObj(list);
	}


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

		MaterialReceiveRequest materialReceiveRequest = JSON.toJavaObject(in, MaterialReceiveRequest.class);
		String receiveRequestName = materialReceiveRequest.getReceiveRequestName() == null ? "" : materialReceiveRequest.getReceiveRequestName();
		String siteName = materialReceiveRequest.getSiteName()  == null ? "" : materialReceiveRequest.getSiteName();
		
		if(Objects.isNull(receiveRequestName)) {
			throw new GlobalException("下拉框选项不能为空！");
		}
		String sql ="WITH AA AS (\r\n" + 
				"SELECT\r\n" + 
				"	m5.RECEIVEREQUESTNAME,\r\n" + 
				"	m5.MATERIALSPECNAME ,\r\n" + 
				"	m5.SDK_ID,\r\n" + 
				"	m5.phase,\r\n" + 
				"	SUM(m5.MATERIALQUANTITY) QTY\r\n" + 
				"FROM\r\n" + 
				"	MATERIALPACKING m5\r\n" + 
				"WHERE\r\n" + 
				"	m5.RECEIVEREQUESTNAME = :RECEIVEREQUESTNAME\r\n" + 
				"	AND m5.STOCKSTATE IN ( 'Stocked','StockOut')\r\n" + 
				"GROUP BY\r\n" + 
				"	m5.MATERIALSPECNAME,\r\n" + 
				"	m5.RECEIVEREQUESTNAME,\r\n" + 
				"	m5.SDK_ID,\r\n" + 
				"	m5.phase\r\n" + 
				"   ),\r\n" + 
				"   BB AS (\r\n" + 
				"SELECT\r\n" + 
				"	m5.RECEIVEREQUESTNAME,\r\n" + 
				"	m5.MATERIALSPECNAME ,\r\n" + 
				"	m5.SDK_ID,\r\n" + 
				"	m5.phase,\r\n" + 
				"	SUM(m5.MATERIALQUANTITY) QTY,COUNT(m5.MATERIALPACKINGNAME) JS\r\n" + 
				"FROM\r\n" + 
				"	MATERIALPACKING m5\r\n" + 
				"WHERE\r\n" + 
				"	m5.RECEIVEREQUESTNAME = :RECEIVEREQUESTNAME\r\n" + 
				"	AND m5.STOCKSTATE = 'Created'\r\n" + 
				"GROUP BY\r\n" + 
				"	m5.MATERIALSPECNAME,\r\n" + 
				"	m5.RECEIVEREQUESTNAME,\r\n" + 
				"	m5.SDK_ID,\r\n" + 
				"	m5.phase\r\n" + 
				"   )\r\n" + 
				"   SELECT\r\n" + 
				"	'1' RECEIVEREQUESTDETAILNAME,\r\n" + 
				"	SUM(m.REQUESTQUANTITY) REQUESTQUANTITY,\r\n" + 
				"	SUM(m.RECEIVEDQUANTITY) RECEIVEDQUANTITY,\r\n" + 
				"	SUM(m.REQUESTQUANTITY - m.RECEIVEDQUANTITY) AS MINUSQTY,\r\n" + 
				"	NVL(BB.QTY,0) CREATEQTY,\r\n" + 
				"	NVL(AA.QTY,0) STOCKINQTY,\r\n" + 
				"	NVL(BB.JS, 0)  JS,\r\n" + 
				"	m3.DESC_CN ,\r\n" + 
				"	m.MATERIALUNIT ,\r\n" + 
				"	m.SDK_ID ,\r\n" + 
				"	ss.SPECNAME ,\r\n" + 
				"	m.PHASE ,\r\n" + 
				"	m.MATERIALSPECNAME,\r\n" + 
				"	m.SITENAME,\r\n" + 
				"	m.RECEIVEREQUESTNAME,\r\n" + 
				"	max(m.REMARK) REMARK,\r\n" + 
				"	max(m.SALESPERSON) SALESPERSON , max(m.CAREER_ASSISTANCE) CAREER_ASSISTANCE, max(m.BUSINESS_UNIT) BUSINESS_UNIT,\r\n" + 
				"	TO_CHAR(SYSDATE,'YYYY-MM-DD') RECEIVETIME,\r\n" + 
				"	TO_CHAR(SYSDATE,'YYYY-MM-DD') MAKEDATE\r\n" + 
				"FROM\r\n" + 
				"	MATERIALRECEIVEREQUESTDETAIL m\r\n" + 
				"LEFT JOIN MATERIALRECEIVEREQUEST m2 ON\r\n" + 
				"	m.RECEIVEREQUESTNAME = m2.RECEIVEREQUESTNAME\r\n" + 
				"LEFT JOIN MATERIALSPEC m3 ON\r\n" + 
				"	m.MATERIALSPECNAME = m3.MATERIALSPECNAME\r\n" + 
				"LEFT JOIN SDK_SPEC ss ON\r\n" + 
				"	m.SDK_ID = ss.SDK_ID\r\n" + 
				"LEFT JOIN SUPPLIER S ON\r\n" + 
				"	m2.SUPPLIERNO = S.SUPPLIERNO\r\n" + 
				"LEFT JOIN MATERIALUNIT MM ON\r\n" + 
				"	MM.FNUMBER = m.MATERIALUNIT\r\n" + 
				"LEFT JOIN BS_ERPLOCATION be ON\r\n" + 
				"	m.ERPLOCATION = be.ERPLOCATIONNAME\r\n" + 
				"LEFT JOIN ORG o ON\r\n" + 
				"	o.ORGNO = m2.STOCKORGNO\r\n" + 
				"LEFT JOIN STORAGESPEC sss ON\r\n" + 
				"	m.LOCATIONNAME = sss.STORAGENAME\r\n" + 
				"LEFT JOIN AA ON\r\n" + 
				"	AA. RECEIVEREQUESTNAME = m.RECEIVEREQUESTNAME\r\n" + 
				"	AND AA.MATERIALSPECNAME = m.MATERIALSPECNAME\r\n" + 
				"	AND AA.SDK_ID = m.SDK_ID\r\n" + 
				"	AND AA.PHASE = m.PHASE\r\n" + 
				"LEFT JOIN BB ON\r\n" + 
				"	BB. RECEIVEREQUESTNAME = m.RECEIVEREQUESTNAME\r\n" + 
				"	AND BB.MATERIALSPECNAME = m.MATERIALSPECNAME\r\n" + 
				"	AND BB.SDK_ID = m.SDK_ID\r\n" + 
				"	AND BB.PHASE = m.PHASE\r\n" + 
				"WHERE\r\n" + 
				"	1 = 1\r\n" + 
				"	AND m.RECEIVEREQUESTNAME = :RECEIVEREQUESTNAME\r\n" + 
				"	AND m.SITENAME = :SITENAME\r\n" + 
				"	AND m2.RECEIVEREQUESTTYPE IN ('4C')\r\n" + 
				"	AND (m2.RETURNFLAG IS NULL\r\n" + 
				"		OR m2.RETURNFLAG <> 'Y')\r\n" + 
				"	AND m3.IS_BATCH = 'Y'\r\n" + 
				"GROUP BY\r\n" + 
				"	m3.DESC_CN ,\r\n" + 
				"	m.MATERIALUNIT ,\r\n" + 
				"	m.SDK_ID ,\r\n" + 
				"	ss.SPECNAME ,\r\n" + 
				"	m.PHASE ,\r\n" + 
				"	m.MATERIALSPECNAME,\r\n" + 
				"	m.SITENAME,\r\n" + 
				"	m.RECEIVEREQUESTNAME,\r\n" + 
				"	BB.QTY,AA.QTY,BB.JS";
		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("RECEIVEREQUESTNAME", receiveRequestName);
		bindMap.put("SITENAME", siteName);
		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 = "/getSaleReturnBindBarCode", method = RequestMethod.POST)
	public AjaxResult getSaleReturnBindBarCode(@RequestBody JSONObject in) {

		MaterialshipRequest ms = JSON.toJavaObject(in, MaterialshipRequest.class);
		String shipRequestName = ms.getShipRequestName()  == null ? "" : ms.getSiteName();
		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.RECEIVEREQUESTNAME = :SHIPREQUESTNAME AND M.STOCKSTATE = 'StockOut'";
		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 = "/BindBarCodeToSaleReturn", method = RequestMethod.POST)
	public AjaxResult BindBarCodeToSaleReturn(@RequestBody JSONObject in ){

		MaterialPacking materialPacking = JSON.toJavaObject(in, MaterialPacking.class);
		String locationName = materialPacking.getLocationName();
		String charge = materialPacking.getCharge();
		String materialPackingName = materialPacking.getMaterialPackingName();
		String materialSpecName = materialPacking.getMaterialSpecName();
		String shipRequestName = materialPacking.getShipRequestName();
		String shipRequestDetailName = materialPacking.getShipRequestDetailName();
		String user = materialPacking.getUser();
		String materialQty=materialPacking.getMaterialQuantity();
		String erpLocation = materialPacking.getErpLocation();
		//恒温恒湿管控
				if(!(untils.JudgeMentERPLocationAndMaterialSpec(erpLocation,materialSpecName)))
				{
					throw new GlobalException("物料为:"+materialSpecName+" 仓库为:"+erpLocation+" ,不符合恒温恒湿管理");
				}
		//判断条码是否存在且状态是出库
		String sql = "SELECT * FROM MATERIALPACKING M WHERE M.CHARGE = :CHARGE "
				+ "AND M.MATERIALSPECNAME = :MATERIALSPECNAME  AND M.STOCKSTATE = 'StockOut' AND (M.RECEIVEREQUESTNAME <> :SHIPREQUESTNAME OR M.RECEIVEREQUESTNAME IS NULL )";
		
		Map<String, Object> bindMap = new HashMap<String, Object>();
		bindMap.put("MATERIALSPECNAME",materialSpecName); 
		bindMap.put("CHARGE",charge ); 
		bindMap.put("SHIPREQUESTNAME",shipRequestName ); 
		List<Map<String,Object>> list = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, bindMap);
		if(Objects.isNull(list) || list.isEmpty()) {
			throw new GlobalException("未找到对应的条码出库记录！");
		}
		if(list.size() > 1) {
			throw new GlobalException("批次找到不唯一记录,请联系IT处理！");
		}
		String sdk_id = list.get(0).get("SDK_ID") == null ? "" : list.get(0).get("SDK_ID").toString();
		String phase = list.get(0).get("PHASE") == null ? "" : list.get(0).get("PHASE").toString();
		//查找该单据该物料下的单据明细+需求数量+绑定数量
		String ShipDetailSql = "SELECT  SHIPREQUESTDETAILNAME ,REQUESTQUANTITY,ASSIGNEDQUANTITY  FROM MATERIALSHIPREQUESTDETAIL  m WHERE SHIPREQUESTNAME =:SHIPREQUESTNAME AND MATERIALSPECNAME =:MATERIALSPECNAME";
		Map<String, Object> ShipDetailMap = new HashMap<String, Object>();
		ShipDetailMap.put("SHIPREQUESTNAME",shipRequestName); 
		ShipDetailMap.put("MATERIALSPECNAME",materialSpecName ); 
		List<Map<String,Object>>  ShipDetaillist = IDMFrameServiceProxy.getSqlTemplate().queryForList(ShipDetailSql, ShipDetailMap);
		if(Objects.isNull(ShipDetaillist) || ShipDetaillist.isEmpty()) {
				throw new GlobalException("未找到对应的销售退货库明细！");
		}
		for(int i=0;i<ShipDetaillist.size();i++){
			String shipNo=(String) ShipDetaillist.get(i).get("SHIPREQUESTDETAILNAME");
			BigDecimal requestQtyBigDecimal = (BigDecimal) ShipDetaillist.get(i).get("REQUESTQUANTITY");  
			BigDecimal assQtyBigDecimal = (BigDecimal) ShipDetaillist.get(i).get("ASSIGNEDQUANTITY");  
			float requestQtyFloat = requestQtyBigDecimal.floatValue();  
			float assQtyFloat = assQtyBigDecimal.floatValue();  
			if(requestQtyFloat>=assQtyFloat+Float.parseFloat(materialQty)){
				shipRequestDetailName=shipNo;
				break;
			}
		}
		if("".equals(shipRequestDetailName)){
			throw new GlobalException("该销售退货单上可收数量低于标签数量");
		}
		boolean bindBarCodeToSaleReturn = SaleReturnServiceImpl.BindBarCodeToSaleReturn(materialPackingName, charge, materialSpecName, user, shipRequestName, shipRequestDetailName,locationName);
		if (!bindBarCodeToSaleReturn) {
			throw new GlobalException("绑定失败！");
		}
		return AjaxResult.me().setResultObj(list);
	}
	
	
	/**
	 * 销售退货 lz
	 * @param in
	 * @return 
	 * @throws Exception 
	 */
	@RequestMapping(value = "/ProductSaleReturn", method = RequestMethod.POST)
	public AjaxResult ProductSaleReturn(@RequestBody JSONObject in ) throws Exception{
		MaterialPacking materialPacking = JSON.toJavaObject(in, MaterialPacking.class);
		String locationName = materialPacking.getLocationName();
		String charge = materialPacking.getCharge();
		String materialPackingName = materialPacking.getMaterialPackingName();
		String materialSpecName = materialPacking.getMaterialSpecName();
		String shipRequestName = materialPacking.getShipRequestName();
		String shipRequestDetailName = materialPacking.getShipRequestDetailName();
		String user = materialPacking.getUser();
		String materialQuantity = "";
		String SDK_ID = "";
		String erpLocation = materialPacking.getErpLocation();
		String phase = materialPacking.getPhase(); 
		String receiveRequestName = materialPacking.getReceiveRequestName();
		String erpFactory = materialPacking.getErpFactory();
		
		
		String sqlString = "SELECT * \r\n"
				+ "FROM MATERIALPACKING m \r\n"
				+ "LEFT JOIN MATERIALSHIPREQUEST m2 ON m.SHIPREQUESTNAME = m2.SHIPREQUESTNAME\r\n"
				+ "WHERE m.STOCKSTATE ='StockOut'\r\n"
				+ "	AND m.MATERIALPACKINGNAME = :MATERIALPACKINGNAME\r\n"
				+ "	AND m2.SHIPREQUESTTYPE = '4C'";
		
		Map<String,Object> hashMap = new HashMap<String,Object>();
		hashMap.put("MATERIALPACKINGNAME", materialPackingName);
//		hashMap.put("SHIPREQUESTNAME", shipRequestName);
		List<Map<String,Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sqlString, hashMap);
		if(Objects.isNull(queryForList) || queryForList.isEmpty()) {
			return AjaxResult.me().setMessage("未找到对应的记录").setSuccess(false).setResultObj(null);
		}
		for (Map<String, Object> map : queryForList) {
			materialQuantity = map.get("MATERIALQUANTITY").toString();
			SDK_ID = map.get("SDK_ID") == null ? "" : map.get("SDK_ID").toString() ;
		}
		
//		String sqlString2 = "SELECT SDK_ID FROM MATERIALRECEIVEREQUESTDETAIL m WHERE RECEIVEREQUESTNAME = :RECEIVEREQUESTNAME";
//		HashMap<String,Object> hashMap3 = new HashMap<String , Object>();
//		hashMap3.put("RECEIVEREQUESTNAME", receiveRequestName);
//		List<Map<String,Object>> queryForList3 = IDMFrameServiceProxy.getSqlTemplate().queryForList(sqlString2, hashMap3);
//		int count = 0;
//		for (Map<String, Object> map : queryForList3) {
//			if(map.get("SDK_ID").toString().equals(SDK_ID)){
//				count++;
//			}
//		}
//		if(count == 0) {
//			return AjaxResult.me().setSuccess(false).setMessage("未找到相对应的单据规格").setResultObj(null);
//		}
		String invoiceSql = "SELECT	M.REQUESTQUANTITY - M.RECEIVEDQUANTITY QTY,	M.RECEIVEREQUESTNAME,M.RECEIVEREQUESTDETAILNAME,M.SDK_ID \r\n"
				+ "FROM	MATERIALRECEIVEREQUESTDETAIL M\r\n"
				+ "WHERE	M.MATERIALSPECNAME = :MATERIALSPECNAME\r\n"
				+ "	AND (:PHASE IS NULL	OR PHASE = :PHASE)\r\n"
				+ "--	AND (:SDK_ID IS NULL OR SDK_ID = :SDK_ID)\r\n"
				+ "	AND M.REQUESTQUANTITY - M.RECEIVEDQUANTITY > 0\r\n"
				+ "	AND RECEIVEREQUESTNAME = :RECEIVEREQUESTNAME";
		Map<String,Object> binMap = new HashMap<String ,Object>();
		binMap.put("MATERIALSPECNAME", materialSpecName);
		binMap.put("PHASE", phase);
		binMap.put("SDK_ID", SDK_ID);
		binMap.put("RECEIVEREQUESTNAME", receiveRequestName);
		List<Map<String, Object>> queryForList2 = IDMFrameServiceProxy.getSqlTemplate().queryForList(invoiceSql, binMap);
		if (Objects.isNull(queryForList2) || queryForList2.isEmpty()) {
			return AjaxResult.me().setSuccess(false).setMessage("未找到对应的订单").setResultObj(null);
		}
		boolean bindBarProductSaleReturn = SaleReturnServiceImpl.BindBarProductSaleReturn( materialSpecName, phase,queryForList2,materialQuantity, materialPackingName,user,
				erpFactory,erpLocation, locationName,receiveRequestName, charge);
	
		
		
		
//		boolean bindBarProductSaleReturn = SaleReturnServiceImpl.BindBarProductSaleReturn( materialSpecName, phase,"",materialQuantity, materialPackingName,user,erpFactory,
//				 erpLocation, locationName,receiveRequestName, charge);
//		if(!bindBarProductSaleReturn) {
//			return AjaxResult.me().setSuccess(false).setMessage("未找到相对应的单据规格").setResultObj(null);
//		}
		
		String sqString = "SELECT  m.MATERIALPACKINGNAME,m.CHARGE,m.MATERIALSPECNAME,m2.DESC_CN,ss.SPECNAME,m.SDK_ID,m.PHASE,m.MATERIALQUANTITY,o.ORGNAME,be.DESCRIPTION,s.DESCRIPTION DESCRIPTION1\r\n"
				+ "FROM MaterialPacking m\r\n"
				+ "LEFT JOIN MATERIALSPEC m2 ON m.MATERIALSPECNAME = m2.MATERIALSPECNAME\r\n"
				+ "LEFT JOIN SDK_SPEC ss ON m.SDK_ID = ss.SDK_ID\r\n"
				+ "LEFT JOIN ORG o ON m.ERPFACTORY = o.ORGNO\r\n"
				+ "LEFT JOIN BS_ERPLOCATION be ON m.ERPLOCATION = be.ERPLOCATIONNAME\r\n"
				+ "LEFT JOIN STORAGESPEC s ON m.locationName = s.STORAGENAME AND m.ERPLOCATION = s.ERPLOCATION \r\n"
				+ "WHERE 1=1 \r\n"
				+ "	AND m.STOCKSTATE = 'Created'\r\n"
				+ "	AND m.RECEIVEREQUESTNAME = :RECEIVEREQUESTNAME	";
		HashMap<String,Object> hashMap2 = new HashMap<String,Object >();
		hashMap2.put("RECEIVEREQUESTNAME", receiveRequestName);	
//		List<Map<String,Object>> queryForList2 = IDMFrameServiceProxy.getSqlTemplate().queryForList(sqString, hashMap2);
		
		return AjaxResult.me().setSuccess(true).setResultObj(queryForList2);
	}
	
	
	/**
	 * 销售退货 lz
	 * @param in
	 * @return 
	 */
	@RequestMapping(value = "/getSaleReturnLists", method = RequestMethod.POST)
	public AjaxResult getSaleReturnLists(@RequestBody JSONObject in ){
		MaterialPacking materialPacking = JSON.toJavaObject(in, MaterialPacking.class);
		String receiveRequestName = materialPacking.getReceiveRequestName();
		String sqlString = "SELECT  m.MATERIALPACKINGNAME,m.CHARGE,m.MATERIALSPECNAME,m2.DESC_CN,ss.SPECNAME,m.SDK_ID,m.PHASE,m.MATERIALQUANTITY,o.ORGNAME,be.DESCRIPTION,s.DESCRIPTION DESCRIPTION1\r\n"
				+ "FROM MaterialPacking m\r\n"
				+ "LEFT JOIN MATERIALSPEC m2 ON m.MATERIALSPECNAME = m2.MATERIALSPECNAME\r\n"
				+ "LEFT JOIN SDK_SPEC ss ON m.SDK_ID = ss.SDK_ID\r\n"
				+ "LEFT JOIN ORG o ON m.ERPFACTORY = o.ORGNO\r\n"
				+ "LEFT JOIN BS_ERPLOCATION be ON m.ERPLOCATION = be.ERPLOCATIONNAME\r\n"
				+ "LEFT JOIN STORAGESPEC s ON m.locationName = s.STORAGENAME AND m.ERPLOCATION = s.ERPLOCATION \r\n"
				+ "WHERE 1=1 \r\n"
				+ "	AND m.STOCKSTATE = 'Created'\r\n"
				+ "	AND m.RECEIVEREQUESTNAME = :RECEIVEREQUESTNAME	";
		HashMap<String,Object> hashMap = new HashMap<String,Object>();
		hashMap.put("RECEIVEREQUESTNAME", receiveRequestName);
		List<Map<String,Object>> queryForList2 = IDMFrameServiceProxy.getSqlTemplate().queryForList(sqlString, hashMap);
		return AjaxResult.me().setSuccess(true).setResultObj(queryForList2);
	}
	

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

		MaterialPacking materialPacking = JSON.toJavaObject(in, MaterialPacking.class);
		String locationName = materialPacking.getLocationName();
		String charge = materialPacking.getCharge();
		String materialPackingName = materialPacking.getMaterialPackingName();
		String materialSpecName = materialPacking.getMaterialSpecName();
		String shipRequestName = materialPacking.getShipRequestName();
		String shipRequestDetailName = materialPacking.getShipRequestDetailName();
		String user = materialPacking.getUser();
		String commitDate = materialPacking.getCommitDate();
		String opCode = materialPacking.getOpCode();
		boolean commitSaleReturnInvoice = SaleReturnServiceImpl.CommitSaleReturnInvoice(shipRequestName, user,commitDate,opCode);
		if (!commitSaleReturnInvoice) {
			throw new GlobalException("过账失败！");
		}
		return AjaxResult.me().setResultObj(null);

	}
	


}
