package com.cim.idm.service.Impl;

import com.cim.idm.exception.GlobalException;
import com.cim.idm.framework.IDMFrameServiceProxy;
import com.cim.idm.framework.data.EventInfo;
import com.cim.idm.service.MoveInService;
import com.cim.idm.service.impl.ToSAPServiceImpl;
import com.cim.idm.utils.EventInfoUtil;
import com.cim.idm.wmspackage.materialpacking.MaterialPackingServiceProxy;
import com.cim.idm.wmspackage.materialpacking.management.data.MaterialPacking;
import com.cim.idm.wmspackage.materialpacking.management.data.MaterialPackingKey;
import com.cim.idm.wmspackage.materialpacking.management.info.SetEventInfo;
import com.cim.idm.wmspackage.storage.StorageServiceProxy;
import com.cim.idm.wmspackage.storage.management.data.StorageSpec;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("MoveIn")
public class MoveInServiceImpl implements MoveInService {

	private EventInfo makeEventInfo;

	@Autowired
	private ToSAPServiceImpl toSAPServiceImpl;

	/**
	 * 调拨入库绑定条码
	 */
	@Transactional
	@Override
	public boolean BindBarCodeToMoveIn(String materialPackingName,String charge,String materialspecName,String user,
			String shipRequestName,String shipRequestDetailName,String locationName) throws GlobalException {
		//解析条码,根据远条码信息更新目标条码数量和库存状态
		String condition="WHERE CHARGE=? AND MATERIALSPECNAME=? ";
		String[] bindSet={charge,materialspecName};
		List<MaterialPacking> MaterialPackingList = MaterialPackingServiceProxy.getMaterialPackingService().select(condition, bindSet);
		SetEventInfo setEventInfo = new SetEventInfo();
		EventInfoUtil eventInfoUtil = new EventInfoUtil();
		makeEventInfo = eventInfoUtil.makeEventInfo("BarCodeToMoveIn", user, "BarCodeToMoveIn", "", "");
		String sql = "select * from STORAGESPEC WHERE STORAGENAME = :STORAGENAME" ;
		Map<String, Object> hashMap2 = new HashMap<String,Object> ();
		hashMap2.put("STORAGENAME", locationName) ;
		List<Map<String, Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, hashMap2);
		if (queryForList == null || queryForList.size() < 1) {
			throw new GlobalException(locationName + "库位不存在！");
		}
		//根据货位找到ERP库位和组织
		condition="WHERE STORAGENAME=? ";
		String[] indSet={locationName};
		List<StorageSpec> select = StorageServiceProxy.getStorageSpecService().select(condition, indSet);
		String erpFactory = "";
		String erpLocation = "";
		for (StorageSpec storageSpec : select) {
			erpLocation = storageSpec.getErpLocation();
			erpFactory = storageSpec.getErpFactory();
		}
		if ("".equals(erpLocation) || "".equals(erpFactory)) {
			throw new GlobalException(locationName + "库位不存在！");
		}
		String[] split = materialPackingName.split("\\|");
		for (MaterialPacking materialPacking : MaterialPackingList) {
			//更新批次，数量，库位状态
			Map<String, Object> hashMap = new HashMap<String,Object> ();
			hashMap.put("shipRequestName", shipRequestName);
			hashMap.put("shipRequestDetailName", shipRequestDetailName);
			hashMap.put("erpLocation", erpLocation);
			hashMap.put("erpFactory", erpFactory);
			hashMap.put("locationName", locationName);
			setEventInfo.setUserColumns(hashMap);
			MaterialPackingServiceProxy.getMaterialPackingService().setEvent(materialPacking.getKey(), makeEventInfo, setEventInfo);
			
			String sql2 = "UPDATE MATERIALSHIPREQUESTDETAIL T SET T.ASSIGNEDQUANTITY = T.ASSIGNEDQUANTITY + :materialQuantity "
					+ "WHERE T.SHIPREQUESTNAME = :shipRequestName AND T.SHIPREQUESTDETAILNAME  = :shipRequestDetailName ";
			hashMap.put("materialQuantity", split[7]);
			IDMFrameServiceProxy.getSqlTemplate().update(sql2, hashMap);
			
		}
		return true;
	}

	/**
	 * pc端移库绑定条码
	 */
	@Transactional
	@Override
	public boolean BindBarCodeToMoveIn2(List<MaterialPacking> boxList, String materialPackingName, String user,
										String shipRequestName, String shipRequestDetailName, String locationName) throws GlobalException {
		//解析条码,根据远条码信息更新目标条码数量和库存状态
		String condition="WHERE CHARGE=? AND MATERIALSPECNAME=? ";
//		String[] bindSet={charge,materialspecName};
//		List<MaterialPacking> MaterialPackingList = MaterialPackingServiceProxy.getMaterialPackingService().select(condition, bindSet);
		SetEventInfo setEventInfo = new SetEventInfo();
		EventInfoUtil eventInfoUtil = new EventInfoUtil();
		makeEventInfo = eventInfoUtil.makeEventInfo("BarCodeToMoveIn", user, "BarCodeToMoveIn", "", "");
		String sql = "select * from STORAGESPEC WHERE STORAGENAME = :STORAGENAME" ;
		Map<String, Object> hashMap2 = new HashMap<String,Object> ();
		hashMap2.put("STORAGENAME", locationName);
		List<Map<String, Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, hashMap2);
		if (queryForList == null || queryForList.size() < 1) {
			throw new GlobalException(locationName + "库位不存在！");
		}
		//根据货位找到ERP库位和组织
		condition="WHERE STORAGENAME=? ";
		String[] indSet={locationName};
		List<StorageSpec> select = StorageServiceProxy.getStorageSpecService().select(condition, indSet);
		String erpFactory = "";
		String erpLocation = "";
		for (StorageSpec storageSpec : select) {
			erpLocation = storageSpec.getErpLocation();
			erpFactory = storageSpec.getErpFactory();
		}
		if ("".equals(erpLocation) || "".equals(erpFactory)) {
			throw new GlobalException(locationName + "库位不存在！");
		}
//		String[] split = materialPackingName.split("\\|");
		for (MaterialPacking materialPacking : boxList) {
			//更新批次，数量，库位状态
			Map<String, Object> hashMap = new HashMap<String,Object> ();
//			hashMap.put("shipRequestName", shipRequestName);
//			hashMap.put("shipRequestDetailName", shipRequestDetailName);
			hashMap.put("erpLocation", erpLocation);
			hashMap.put("erpFactory", erpFactory);
			hashMap.put("locationName", locationName);
			setEventInfo.setUserColumns(hashMap);
			MaterialPackingKey key = new MaterialPackingKey("SDK",materialPacking.getMaterialPackingName());
//			MaterialPackingServiceProxy.getMaterialPackingService().setEvent(materialPacking.getKey(), makeEventInfo, setEventInfo);
			MaterialPackingServiceProxy.getMaterialPackingService().setEvent(key, makeEventInfo, setEventInfo);

//			String sql2 = "UPDATE MATERIALSHIPREQUESTDETAIL T SET T.ASSIGNEDQUANTITY = T.ASSIGNEDQUANTITY + :materialQuantity "
//					+ "WHERE T.SHIPREQUESTNAME = :shipRequestName AND T.SHIPREQUESTDETAILNAME  = :shipRequestDetailName ";
//			hashMap.put("materialQuantity", split[7]);
//			IDMFrameServiceProxy.getSqlTemplate().update(sql2, hashMap);

		}
		return true;
	}
	@Override
	public boolean CommitMoveInInvoice(String shipRequestName,String user,String commitDate,String opCode) throws Exception {
		EventInfoUtil eventInfoUtil = new EventInfoUtil();
		makeEventInfo = eventInfoUtil.makeEventInfo("CommitMoveInInvoice", user, "CommitMoveInInvoice", "", "");
		SetEventInfo setEventInfo = new SetEventInfo();
		String condition1="WHERE SHIPREQUESTNAME=? AND STOCKSTATE='Stocked' ";
		String[] bindSet2 = {shipRequestName};
		List<MaterialPacking> MaterialPackingList = MaterialPackingServiceProxy.getMaterialPackingService().select(condition1, bindSet2);
		for (MaterialPacking materialPacking : MaterialPackingList) {
			//更新批次，数量，库位状态
			Map<String, Object> hashMap = new HashMap<String,Object> ();
			hashMap.put("shipRequestName", "");
			hashMap.put("shipRequestDetailName", "");
			setEventInfo.setUserColumns(hashMap);
			MaterialPackingServiceProxy.getMaterialPackingService().setEvent(materialPacking.getKey(), makeEventInfo, setEventInfo);
		}
		return true;
	}


	/**
	 *
	 * @param undoid
	 * @param commitDate
	 */
	public void SaveUnDoInfo( String undoid,String commitDate) {
		String insertUnDOMaterialPackingInfo = "insert into BS_MATERIALPACKINGUNDOINFO (unDoID,preundoid,UNDODETAILNAME,undoidstate,UNDOIDDATE,"
				+ "SITENAME, MATERIALPACKINGNAME, MATERIALPACKINGTYPE, CONTENTMATERIALTYPE,"
				+ "MATERIALTYPE, MATERIALSPECNAME, SUPERMATERIALPACKINGNAME, PACKINGGRADE, SUBPACKINGQUANTITY, MATERIALCREATEQUANTITY,"
				+ "MATERIALQUANTITY, PACKINGSTATE, OLDPACKINGSTATE, STOCKSTATE, OLDSTOCKSTATE, HOLDSTATE, AREANAME, LOCATIONNAME,"
				+ "DURABLENAME, MATERIALPROCESSGROUPNAME, LASTEVENTNAME, LASTEVENTTIMEKEY, LASTEVENTTIME, LASTEVENTUSER,"
				+ "LASTEVENTCOMMENT, CREATETIME, CREATEUSER, RECEIVEREQUESTNAME, RECEIVEREQUESTDETAILNAME,"
				+ "MAKEDATE, RECEIVEACTNO, STOCKINTYPE, RECEIVETIME, RECEIVEUSER, SHIPREQUESTNAME, SHIPREQUESTDETAILNAME, SHIPTIME,"
				+ "SHIPUSER, REASONCODETYPE, OQARESULTSTATE, OQARESULT, ERPLOCATION, ERPFACTORY, REASONCODE, CHARGE, SUPPLIERNAME,ETCSTATE,ERPEVENTTIME,NPFLAG)"
				+ "select unDoID,'' as preundoid,UNDODETAILNAME,'Completed' as undoidstate,:commitDate AS UNDOIDDATE," //
				+ "SITENAME, MATERIALPACKINGNAME, MATERIALPACKINGTYPE, CONTENTMATERIALTYPE,"
				+ "MATERIALTYPE, MATERIALSPECNAME, SUPERMATERIALPACKINGNAME, PACKINGGRADE, SUBPACKINGQUANTITY, MATERIALCREATEQUANTITY,"
				+ "MATERIALQUANTITY, PACKINGSTATE, OLDPACKINGSTATE, STOCKSTATE, OLDSTOCKSTATE, HOLDSTATE, AREANAME, LOCATIONNAME,"
				+ "DURABLENAME, MATERIALPROCESSGROUPNAME, LASTEVENTNAME, LASTEVENTTIMEKEY, LASTEVENTTIME, LASTEVENTUSER,"
				+ "LASTEVENTCOMMENT, CREATETIME, CREATEUSER, RECEIVEREQUESTNAME, RECEIVEREQUESTDETAILNAME,"
				+ "MAKEDATE, RECEIVEACTNO, STOCKINTYPE, RECEIVETIME, RECEIVEUSER, SHIPREQUESTNAME, SHIPREQUESTDETAILNAME, SHIPTIME,"
				+ "SHIPUSER, REASONCODETYPE, OQARESULTSTATE, OQARESULT, ERPLOCATION, ERPFACTORY, REASONCODE, CHARGE, SUPPLIERNAME,ETCSTATE,ERPEVENTTIME,NPFLAG"
				+ " from MATERIALPACKING  "
				+ " where unDoID=:UNDOID";
		Map<String, Object> bindMap_insert = new HashMap<String, Object>();
		bindMap_insert.put("UNDOID", undoid);
		bindMap_insert.put("commitDate", commitDate);
		IDMFrameServiceProxy.getSqlTemplate()
				.update(insertUnDOMaterialPackingInfo, bindMap_insert);
	}
}
