using System; using System.Collections.Generic; using System.Linq; using System.Text; using UFIDA.U9.WK.PurchasePullChangeUIModel; using System.Data; using UFSoft.UBF.Util.Context; using UFSoft.UBF.Util.DataAccess; using System.Net; using System.IO; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System.Web.Script.Serialization; namespace UFIDA.U9.WK.PRUIToMesBottleneckOrder { public partial class PRMainUIFormWebPart : UFSoft.UBF.UI.Custom.ExtendedPartBase { public override void AfterInit(UFSoft.UBF.UI.IView.IPart part, EventArgs args) { this.Do_AfterInit(part, args); } public override void AfterDataLoad(UFSoft.UBF.UI.IView.IPart Part) { base.AfterDataLoad(Part); } public override void AfterEventProcess(UFSoft.UBF.UI.IView.IPart Part, string eventName, object sender, EventArgs args) { UFSoft.UBF.UI.WebControlAdapter.UFWebButton4ToolbarAdapter webButton = sender as UFSoft.UBF.UI.WebControlAdapter.UFWebButton4ToolbarAdapter; _strongPart = Part as UFIDA.U9.SCM.PM.PRUIModel.PRMainUIFormWebPart; String ipAddress = ""; try { ipAddress = System.Web.HttpContext.Current.Request.Url.Host; } catch { ipAddress = "192.168.0.98"; } String ipSQl = "select MESAddress from mesInterAddress where U9Address='" + ipAddress + "'"; DataSet IPDS = new DataSet(); DataAccessor.RunSQL(DataAccessor.GetConn(), ipSQl, null, out IPDS); if (this._strongPart.Model.PR.FocusedRecord.Org == 1001610070356046 || this._strongPart.Model.PR.FocusedRecord.Org == 1001610070374699 || this._strongPart.Model.PR.FocusedRecord.Org == 1001610070370367) { if (webButton != null && webButton.Action == "ApproveClick") {//FieldDescFlexField_PrivateDescSeg30该字段存的是MES瓶颈待办表里面的ID(MES请购单推送过来的) if (string.IsNullOrEmpty(this._strongPart.Model.PR.FocusedRecord.DescFlexField_PrivateDescSeg30.ToString())) { return; } string url = IPDS.Tables[0].Rows[0]["MESAddress"] + "/api/MES/UpdateBottleneckOrder?"; // string url = "http://10.16.4.165:8091/api/MES/UpdateBottleneckOrder?";//测试用 url += string.Format("Org={0}&BottleneckOrderId={1}&", this._strongPart.Model.PR.FocusedRecord.Org, this._strongPart.Model.PR.FocusedRecord.DescFlexField_PrivateDescSeg30.ToString()); if (!string.IsNullOrEmpty(this._strongPart.Model.PR.FocusedRecord.DocNo)) { url += string.Format("DocNo={0}", this._strongPart.Model.PR.FocusedRecord.DocNo); } string str = HttpGet(url); if (str != @"""true""") { throw new Exception("回写MES瓶颈订单请购单状态失败"); } //StringBuilder sb = new StringBuilder(); //List<MES_Bus_SD_PurchaseOrder> list = new List<MES_Bus_SD_PurchaseOrder>(); //foreach (UFIDA.U9.PM.PurchaseOrderUIModel.PurchaseOrder_POLinesRecord lin in _strongPart.Model.PR_PRLineList.Records) //{ // if (!string.IsNullOrEmpty(lin.DescFlexSegments_PrivateDescSeg2) && lin.DescFlexSegments_PrivateDescSeg2.Length == 11) // { // MES_Bus_SD_PurchaseOrder mb = new MES_Bus_SD_PurchaseOrder(); // if (this._strongPart.Model.PR.FocusedRecord.Org == 1001610070356046) // { // mb.Produce_company = "1004";//天津 // } // else if (this._strongPart.Model.PR.FocusedRecord.Org == 1001610070370367) // { // mb.Produce_company = "1003";//丽水 // } // else if (this._strongPart.Model.PR.FocusedRecord.Org == 1001610070374699) // { // mb.Produce_company = "1002";//成都 // } // if (lin.Status == 0) // { // mb.status = "开立"; // } // else if (lin.Status == 1) // { // mb.status = "审核中"; // } // else if (lin.Status == 2) // { // mb.status = "已审核"; // } // else if (lin.Status == 3) // { // mb.status = "自然关闭"; // } // else if (lin.Status == 4) // { // mb.status = "短额关闭"; // } // else if (lin.Status == 5) // { // mb.status = "超额关闭"; // } // mb.DocNo = this._strongPart.Model.PR.FocusedRecord.DocNo; // mb.Contract_i_no = lin.DescFlexSegments_PrivateDescSeg2; // mb.MaterialCode = lin.ItemInfo_ItemCode; // mb.MaterialName = lin.ItemInfo_ItemName; // mb.MaterialSpecs = lin.ItemInfo_ItemID_SPECS; // mb.PurQtyCU = lin.PurQtyTU; // mb.DELETE_FLAG = 0; // list.Add(mb); // } //if (list != null && list.Count > 0) //{ // if (IPDS.Tables[0].Rows.Count > 0) // { // //string json = JsonConvert.SerializeObject(list); // string url = IPDS.Tables[0].Rows[0]["MESAddress"] + "/api/MES/UpdataPurchase"; // string result = HttpRequestPostJson(url, list); // JObject obj = (JObject)JsonConvert.DeserializeObject(result); // string code = obj["code"].ToString(); // if (code != "200") // { // throw new Exception("调用接口失败"); // } // } //} } } } public override void BeforeEventProcess(UFSoft.UBF.UI.IView.IPart Part, string eventName, object sender, EventArgs args, out bool executeDefault) { executeDefault = false; base.BeforeEventProcess(Part, eventName, sender, args, out executeDefault); return; UFSoft.UBF.UI.WebControlAdapter.UFWebButton4ToolbarAdapter webButton = sender as UFSoft.UBF.UI.WebControlAdapter.UFWebButton4ToolbarAdapter; _strongPart = Part as UFIDA.U9.SCM.PM.PRUIModel.PRMainUIFormWebPart; String ipAddress = ""; try { ipAddress = System.Web.HttpContext.Current.Request.Url.Host; } catch { ipAddress = "192.168.0.98"; } String ipSQl = "select MESAddress from mesInterAddress where U9Address='" + ipAddress + "'"; DataSet IPDS = new DataSet(); DataAccessor.RunSQL(DataAccessor.GetConn(), ipSQl, null, out IPDS); if (this._strongPart.Model.PR.FocusedRecord.Org == 1001610070356046 || this._strongPart.Model.PR.FocusedRecord.Org == 1001610070374699 || this._strongPart.Model.PR.FocusedRecord.Org == 1001610070370367) { //if (null!=sb) //{ // String ipAddress = System.Web.HttpContext.Current.Request.Url.Host; // String ipSQl = "select OMSAddress from interAddress where U9Address='" + ipAddress + "'"; // DataSet IPDS = new DataSet(); // DataAccessor.RunSQL(DataAccessor.GetConn(), ipSQl, null, out IPDS); // string url = IPDS.Tables[0].Rows[0]["OMSAddress"] + "/API/U9/DetailStatus?MarkFlag=1" + sb.ToString(); // string str = HttpGet(url); // if (str != "true") // { // throw new Exception("回写销售订单状态失败"); // } //} //if ((webButton != null && webButton.Action == "DeApprovedClick") || (webButton != null && webButton.Action == "DeleteClick")) //{ // List<MES_Bus_SD_PurchaseOrder> list = new List<MES_Bus_SD_PurchaseOrder>(); // foreach (UFIDA.U9.PM.PurchaseOrderUIModel.PurchaseOrder_POLinesRecord lin in _strongPart.Model.PurchaseOrder_POLines.Records) // { // if (!string.IsNullOrEmpty(lin.DescFlexSegments_PrivateDescSeg2) && lin.DescFlexSegments_PrivateDescSeg2.Length==11) // { // MES_Bus_SD_PurchaseOrder mb = new MES_Bus_SD_PurchaseOrder(); // if (this._strongPart.Model.PurchaseOrder.FocusedRecord.Org == 1001610070356046) // { // mb.Produce_company = "1004";//天津 // } // else if (this._strongPart.Model.PurchaseOrder.FocusedRecord.Org == 1001610070370367) // { // mb.Produce_company = "1003";//丽水 // } // else if (this._strongPart.Model.PurchaseOrder.FocusedRecord.Org == 1001610070374699) // { // mb.Produce_company = "1002";//成都 // } // if (lin.Status == 0) // { // mb.status = "开立"; // } // else if (lin.Status == 1) // { // mb.status = "审核中"; // } // else if (lin.Status == 2) // { // mb.status = "已审核"; // } // else if (lin.Status == 3) // { // mb.status = "自然关闭"; // } // else if (lin.Status == 4) // { // mb.status = "短额关闭"; // } // else if (lin.Status == 5) // { // mb.status = "超额关闭"; // } // mb.DocNo = this._strongPart.Model.PurchaseOrder.FocusedRecord.DocNo; // mb.Contract_i_no = lin.DescFlexSegments_PrivateDescSeg2; // mb.MaterialCode = lin.ItemInfo_ItemCode; // mb.MaterialName = lin.ItemInfo_ItemName; // mb.MaterialSpecs = lin.ItemInfo_ItemID_SPECS; // mb.PurQtyCU = lin.PurQtyTU; // mb.DELETE_FLAG = 1; // list.Add(mb); // } // if (list != null && list.Count > 0) // { // if (IPDS.Tables[0].Rows.Count > 0) // { // //string json = JsonConvert.SerializeObject(list); // string url = IPDS.Tables[0].Rows[0]["MESAddress"] + "/api/MES/UpdataPurchase"; // string result = HttpRequestPostJson(url,list); // JObject obj = (JObject)JsonConvert.DeserializeObject(result); // string code = obj["code"].ToString(); // if (code != "200") // { // throw new Exception("调用接口失败"); // } // } // } // } //} } base.BeforeEventProcess(Part, eventName, sender, args, out executeDefault); } /// <summary> /// HttpRequestPostJson /// </summary> /// <param name="requestURL"></param> /// <param name="data"></param> /// <returns></returns> private static string HttpRequestPostJson(string requestURL, object data) { try { string postDataString = JsonSerialize2(data); byte[] postDataBytes = Encoding.UTF8.GetBytes(postDataString); HttpWebRequest request = HttpWebRequest.Create(requestURL) as HttpWebRequest; //request.Headers["authorization"] = "mAmoli09ly0ilI0amQ0T1Emzmp1wnomg";//这里是身份验证字符串 request.Accept = "application/json, text/javascript, */*; q=0.01"; request.ContentType = "application/json; charset=UTF-8"; request.Method = "POST"; request.ContentLength = postDataBytes.Length; using (Stream requestStream = request.GetRequestStream()) { requestStream.Write(postDataBytes, 0, postDataBytes.Length); } using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) using (Stream responseStream = response.GetResponseStream()) using (StreamReader reader = new StreamReader(responseStream)) { return reader.ReadToEnd(); } } catch (Exception exp) { throw new AggregateException(exp.Message); } } /// <summary> /// 将对象装换为Json字符串 /// </summary> /// <param name="obj"></param> /// <returns></returns> public static string JsonSerialize2(object obj) { if (obj == null) { return string.Empty; } // JavaScriptSerializer jss = new JavaScriptSerializer(); //string myJson = jss.Serialize(linesobj); //string json = JsonConvert.SerializeObject(obj); JavaScriptSerializer serializer = new JavaScriptSerializer(); string myJson = serializer.Serialize(obj); //return serializer.Deserialize<List<MES_Bus_SD_PurchaseOrder>>(myJson); return myJson; } public string HttpGet(string Url) { System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(Url); request.Method = "GET"; request.ContentType = "text/html;charset=UTF-8"; System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse(); System.IO.Stream myResponseStream = response.GetResponseStream(); System.IO.StreamReader myStreamReader = new System.IO.StreamReader(myResponseStream, System.Text.Encoding.GetEncoding("utf-8")); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); return retString; } //private static string HttpRequestPostJson(string requestURL, object data) //{ // try // { // //string postDataString = JsonSerialize2(data); // string postDataString = JsonConvert.SerializeObject(data); // byte[] postDataBytes = Encoding.UTF8.GetBytes(postDataString); // HttpWebRequest request = HttpWebRequest.Create(requestURL) as HttpWebRequest; // //request.Headers["authorization"] = "mAmoli09ly0ilI0amQ0T1Emzmp1wnomg";//这里是身份验证字符串 // request.Accept = "application/json, text/javascript, */*; q=0.01"; // request.ContentType = "application/json; charset=UTF-8"; // request.Method = "POST"; // request.ContentLength = postDataBytes.Length; // using (Stream requestStream = request.GetRequestStream()) // { // requestStream.Write(postDataBytes, 0, postDataBytes.Length); // } // using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) // using (Stream responseStream = response.GetResponseStream()) // using (StreamReader reader = new StreamReader(responseStream)) // { // return reader.ReadToEnd(); // } // } // catch (Exception exp) // { // throw new AggregateException(exp.Message); // } //} /// <summary> /// 将对象装换为Json字符串 /// </summary> /// <param name="obj"></param> /// <returns></returns> //public static string JsonSerialize2(object obj) //{ // if (obj == null) // { // return string.Empty; // } // JavaScriptSerializer serializer = new JavaScriptSerializer(); // return serializer.Serialize(obj); //} //public static string SendHttpRequest(string requestURI, string requestMethod, string json) //{ // //json格式请求数据 // string requestData = json; // //拼接URL // string serviceUrl = requestURI;//string.Format("{0}/{1}", requestURI, requestMethod); // HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(serviceUrl); // //post请求 // myRequest.Method = requestMethod; // //utf-8编码 // byte[] buf = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(requestData); // myRequest.ContentLength = buf.Length; // myRequest.Timeout = 5000; // //指定为json否则会出错 // myRequest.ContentType = "application/json"; // myRequest.MaximumAutomaticRedirections = 1; // myRequest.AllowAutoRedirect = true; // Stream newStream = myRequest.GetRequestStream(); // newStream.Write(buf, 0, buf.Length); // newStream.Close(); // //获得接口返回值 // HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); // StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); // string ReqResult = reader.ReadToEnd(); // reader.Close(); // myResponse.Close(); // return ReqResult; //} //public string HttpGet(string Url) //{ // System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(Url); // request.Method = "GET"; // request.ContentType = "text/html;charset=UTF-8"; // System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse(); // System.IO.Stream myResponseStream = response.GetResponseStream(); // System.IO.StreamReader myStreamReader = new System.IO.StreamReader(myResponseStream, System.Text.Encoding.GetEncoding("utf-8")); // string retString = myStreamReader.ReadToEnd(); // myStreamReader.Close(); // myResponseStream.Close(); // return retString; //} } }