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.impl.ToSAPServiceImpl;
import com.cim.idm.utils.CommonUtils;
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service("NoInvoiceManager")
public class NoInvoiceManagerServiceImpl {
	@Autowired
	private ToSAPServiceImpl toSAPService;
	@Autowired
	private ToSAPServiceImpl toSAPServiceImpl;
	@Autowired
	private CommonUtils commonUtils;

	private SDKMaterialPackingServiceImpl SDKMaterialPackingServiceImpl = new SDKMaterialPackingServiceImpl();


	@Transactional
	public void QTR(List<MaterialPacking> list,String user,String commitDate,String opCode,
					String costName, String lifnr, String qtc) {
		String enentName = "QTR";
		if ("511".equals(qtc)) {
			enentName = "客供品入库";
		} else if ("542O".equals(qtc)) {
			enentName = "委外退料入库";
		} else if ("Z06".equals(qtc)) {
			enentName = "盘盈入库";
		}
		EventInfo makeEventInfo = EventInfoUtil.makeEventInfo(enentName, user, enentName, "", "");
		SetEventInfo setEventInfo = new SetEventInfo();
		List<Map<String, Object>> mapList = new ArrayList<>();
		HashMap<String, Object> hashMap = new HashMap<String,Object> ();
		List<String> strings = new ArrayList<>();
		hashMap.put("stockState", "Stocked");
		hashMap.put("packingState","Released");
		hashMap.put("oqaResultState","END");
		hashMap.put("oqaResult","OK");
		hashMap.put("packingGrade","OK");
		for (MaterialPacking materialPacking : list) {
			//判断标签是否存在
			String sql = "SELECT * FROM MATERIALPACKING m WHERE MATERIALPACKINGNAME = :MATERIALPACKINGNAME AND STOCKSTATE <> 'Stocked' ";
			Map<String, Object> hashMap2 = new HashMap<String,Object> ();
			String materialPackingName = materialPacking.getMaterialPackingName();
			MaterialPackingKey materialPackingKey = new MaterialPackingKey("SDK", materialPackingName);
			hashMap2.put("MATERIALPACKINGNAME", materialPackingName);
			List<Map<String, Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, hashMap2);
			if (queryForList == null || queryForList.size() < 1) {
				throw new GlobalException(materialPackingName + "标签未有系统流转记录！");
			}
			hashMap.put("locationName", materialPacking.getLocationName());
			hashMap.put("erpFactory", materialPacking.getErpFactory());
			hashMap.put("erpLocation", materialPacking.getErpLocation());
			setEventInfo.setObjectValueUserColumns(hashMap);
			MaterialPackingServiceProxy.getMaterialPackingService().setEvent(materialPackingKey, makeEventInfo, setEventInfo);

			Map<String, Object> map = new HashMap<>();
			map.put("MATERIALPACKINGNAME", materialPacking.getMaterialPackingName());
			map.put("CHARGE", materialPacking.getCharge());
			// 物料号
			map.put("MATERIALSPECNAME", materialPacking.getMaterialSpecName());
			map.put("UNIT", queryForList.get(0).get("UNIT"));
			map.put("PHASE", materialPacking.getPHASE());
			map.put("MATERIALQUANTITY", materialPacking.getMaterialQuantity());
			map.put("ERPFACTORY", materialPacking.getErpFactory());
			map.put("ERPLOCATION", materialPacking.getErpLocation());
			map.put("LOCATIONNAME", materialPacking.getLocationName());
			mapList.add(map);

			strings.add(materialPackingName);
		}
		String undoid = "";
//		undoid = NCWServiceImpl.qtrNoInvoice(list,user,commitDate,opCode);
		//调用SAP接口
		undoid = toSAPServiceImpl.qtcrNoInvoiceForCheckPlanApp(mapList, qtc, user,
				commitDate,opCode,costName,lifnr);
		SDKMaterialPackingServiceImpl.updateUndoIdInByList(list, undoid, makeEventInfo,opCode);
		//更新库存状态
		commonUtils.SaveUnDoInfo_ForSap_qtr(strings,undoid,makeEventInfo);
	}
	
	
	@Transactional
	public void ZK(List<MaterialPacking> list,String locationName,String user,String supplierCharge,String opCode) {
//		String undoid = toSAPService.zkNoInvoice(list,locationName,user,opCode);
		EventInfo makeEventInfo = EventInfoUtil.makeEventInfo("ZK", user, "ZK", "", "");
		SetEventInfo setEventInfo = new SetEventInfo();
		HashMap<String, Object> hashMap = new HashMap<String,Object> ();
		hashMap.put("LocationName", locationName);
		hashMap.put("supplierCharge", supplierCharge);
		setEventInfo.setObjectValueUserColumns(hashMap);
		for (MaterialPacking materialPacking : list) {
			String materialPackingName = materialPacking.getMaterialPackingName();
			MaterialPackingKey materialPackingKey = new MaterialPackingKey("SDK", materialPackingName);
			MaterialPackingServiceProxy.getMaterialPackingService().setEvent(materialPackingKey, makeEventInfo, setEventInfo);
		}
//		SDKMaterialPackingServiceImpl.updateUndoIdByList(list, undoid, makeEventInfo);
	}

	/**
	 *
	 * @param list
	 * @param user
	 * @param commitDate
	 * @param opCode
	 */
	@Transactional
	public void PDAShelf(List<MaterialPacking> list,String user,String commitDate,String opCode) {
		EventInfo makeEventInfo = EventInfoUtil.makeEventInfo("Shelf", user, "Shelf", "", "");
		SetEventInfo setEventInfo = new SetEventInfo();
		HashMap<String, Object> hashMap = new HashMap<String,Object> ();
		setEventInfo.setObjectValueUserColumns(hashMap);
		for (MaterialPacking materialPacking : list) {
			String materialPackingName = materialPacking.getMaterialPackingName();
			hashMap.put("LocationName",materialPacking.getLocationName() );
			hashMap.put("supplierCharge", materialPacking.getSupplierCharge());
			MaterialPackingKey materialPackingKey = new MaterialPackingKey("SDK", materialPackingName);
			MaterialPackingServiceProxy.getMaterialPackingService().setEvent(materialPackingKey, makeEventInfo, setEventInfo);
		}
	}

	@Transactional
	public void ChangeChargeName(List<MaterialPacking> list,String user) {
		EventInfo makeEventInfo = EventInfoUtil.makeEventInfo("ChangeSupplierCharge", user, "ChangeSupplierCharge", "", "");
		SetEventInfo setEventInfo = new SetEventInfo();
		Map<String, Object> hashMap = new HashMap<String,Object> ();
		for (MaterialPacking materialPacking : list) {
			String supplierCharge = materialPacking.getSupplierCharge();
			String materialPackingName = materialPacking.getMaterialPackingName();
			hashMap.put("supplierCharge", supplierCharge);
			setEventInfo.setUserColumns(hashMap);
			MaterialPackingKey materialPackingKey = new MaterialPackingKey("SDK", materialPackingName);
			MaterialPackingServiceProxy.getMaterialPackingService().setEvent(materialPackingKey, makeEventInfo, setEventInfo);
		}
	}

}
