package com.cim.idm.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cim.idm.data.ErpMessageLog;
import com.cim.idm.framework.IDMFrameServiceProxy;
import com.cim.idm.framework.util.time.TimeStampUtil;
import com.cim.idm.util.MessageLogUtil;
import com.cim.idm.util.NCHttpUtil;
import com.cim.idm.util.QMSHttpUtil;
import com.cim.idm.wmsextend.generic.errorHandler.CustomException;
import org.springframework.beans.factory.annotation.Autowired;

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

/**
 * PMS接口实现类
 * @author zd
 *
 */
public class PMSServiceImpl {
	
	@Autowired
	QMSHttpUtil qmsHttpUtil;

	private static String  baseUrl = "http://192.168.1.72:9913";
	
	/**
	 * 备件出库信息发送QMS
	 * @throws CustomException 
	 */


	//通用的发送PMS消息。zd.2024.8.22
		public String PMSInfoSend(List<String> boxList,String siteName,String shipRequestName,String undoId) throws CustomException {
			
			String sql=" SELECT m.MATERIALSPECNAME,m.SHIPREQUESTNAME,m.SUPPLIERNAME SUPPLIERNO,max(m3.COSTCENTER) COSTCENTER,'' COSTUSERID,\r\n" + 
					" m2.MATERIALUNIT ,(SELECT s.SUPPLIERNAME FROM SUPPLIER s WHERE s.supplierno=m.SUPPLIERNAME)SUPPLIERNAME,\r\n" + 
					" to_char(SYSDATE,'YYYY-MM-DD HH24:mm:ss') STOCKOUTTIME,sum(m.MATERIALQUANTITY2) MATERIALQTY\r\n" + 
					" FROM MATERIALPACKING m ,MATERIALSPEC m2 ,MATERIALSHIPREQUESTDETAIL m3 \r\n" + 
					" WHERE m.MATERIALSPECNAME =m2.MATERIALSPECNAME \r\n" + 
					" AND m.SHIPREQUESTNAME =m3.SHIPREQUESTNAME \r\n" + 
					" AND m.MATERIALSPECNAME =m3.MATERIALSPECNAME\r\n" + 
					" AND m.MATERIALPACKINGNAME in(:BOXLIST)\r\n" + 
					" AND m.SHIPREQUESTNAME = :SHIPREQUESTNAME\r\n" + 
					" GROUP BY m.MATERIALSPECNAME,m.SHIPREQUESTNAME,m.SUPPLIERNAME,m2.MATERIALUNIT";
			
			Map<String, Object> hashMap = new HashMap<String, Object> ();
			hashMap.put("BOXLIST", boxList);
			hashMap.put("SITENAME", siteName);
			hashMap.put("SHIPREQUESTNAME", shipRequestName);
			List<Map<String, Object>> queryForList = IDMFrameServiceProxy.getSqlTemplate().queryForList(sql.toString(), hashMap);
			String code="";
			if(queryForList.size()>0) 
			{
				String makePMSSendJson = MakePMSSendJson(queryForList, undoId);
				String httpPost = NCHttpUtil.httpPost(baseUrl + "/pms", makePMSSendJson);
				org.json.JSONObject jsonObject = new org.json.JSONObject(httpPost);
				code = jsonObject.getString("code");
	//			if (!"00000".equals(code)) {
	//				throw new CustomException("ACTSTATEERROR","","");
	//			}
	//			String msg = jsonObject.getString("msg");
				//JSONObject object = (JSONObject) jsonObject.get("data");
	
				// 写log
				ErpMessageLog erplog = new ErpMessageLog();
				erplog.setEventUser("");
				erplog.setServerName("WmsToPMS");
				erplog.setEventName("PMSInfoSend");
				erplog.setInterfaceTime(TimeStampUtil.getCurrentTime(TimeStampUtil.FORMAT_DEFAULT));
				erplog.setMessageId(UUID.randomUUID().toString());
				erplog.setSendMsg2(makePMSSendJson);
				erplog.setReturnMsg2(httpPost.toString());
				erplog.setResultCode(code);
				MessageLogUtil.writeMessageLog(erplog);
			}
			return code;
			
		}
		
		//组装发送PMS的json
		public String MakePMSSendJson (List<Map<String, Object>> list,String undoId) {
			
			if (list == null || list.size() < 1) {
				return null;
			}
			
//			MATERIALSPECNAME	String	是	备件编码
//			MATERIALPACKINGNAME	String	是	BOX ID
//			MATERIALQTY	String	是	数量
//			UNIT	String	是	单位
//			COSTCENTER	String	是	成本中心
//			COSTUSERID	String	是	领用人ID
//			STOCKOUTTIME	String	是	备件出库时间
//			RECEIVEACTNO	String	否	物料凭证
//			SHIPREQUESTNAME	String	否	发货单号（预留号）
//			SUPPLIERNO	String	否	供应商代码
//			SUPPLIERNAME	String	否	供应商名称

			
			JSONArray jsonArray = new JSONArray();
			for (int i = 0; i < list.size(); i++) {
				JSONObject jsonObject = new JSONObject(true);	
				jsonObject.put("MATERIALSPECNAME", list.get(i).get("MATERIALSPECNAME"));
				jsonObject.put("MATERIALPACKINGNAME", TimeStampUtil.getCurrentEventTimeKey());
				jsonObject.put("MATERIALQTY", list.get(i).get("MATERIALQTY"));
				jsonObject.put("UNIT", list.get(i).get("MATERIALUNIT"));
				jsonObject.put("COSTCENTER", list.get(i).get("COSTCENTER"));
				jsonObject.put("COSTUSERID", list.get(i).get("COSTUSERID"));
				jsonObject.put("STOCKOUTTIME", list.get(i).get("STOCKOUTTIME"));
				jsonObject.put("RECEIVEACTNO", undoId);
				jsonObject.put("SHIPREQUESTNAME", list.get(i).get("SHIPREQUESTNAME"));
				jsonObject.put("SUPPLIERNO", list.get(i).get("SUPPLIERNO"));
				jsonObject.put("SUPPLIERNAME", list.get(i).get("SUPPLIERNAME"));
				jsonArray.add(jsonObject);
			}
	        
			return jsonArray.toJSONString();
			
		}

		
	
}
