using ICSSoft.Base.Config.AppConfig; using ICSSoft.Frame.Data.Entity; using ICSSoft.Frame.Data.Entity.OM_PO; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Configuration; using System.IO; using System.Linq; using System.Net; using System.Text; namespace ICSSoft.Frame.Data.DAL { public static class ICSIntSupCalDAL { public static void Delete(List id, string conn) { FramDataContext frame = new FramDataContext(conn); frame.Connection.Open(); frame.Transaction = frame.Connection.BeginTransaction(); try { var cal = frame.ICSIntSupCalLog.Where(a => id.Contains(a.id)).Select(a => a); frame.ICSIntSupCalLog.DeleteAllOnSubmit(cal); frame.SubmitChanges(); frame.Transaction.Commit(); } catch (Exception ex) { frame.Transaction.Rollback(); throw ex; } } #region 内部供应商结算 public static void Calculate(List list,string conn) { FramDataContext db = new FramDataContext(conn); db.Connection.Open(); db.Transaction = db.Connection.BeginTransaction(); try { string cIWhCode = ConfigurationManager.AppSettings["WHICode"].ToString(); string cOWhCode = ConfigurationManager.AppSettings["WHOCode"].ToString(); List headList = new List(); OM_POmain head = new OM_POmain(); string key = ""; foreach (var info in list) { var MO = db.ICSMO.Where(a => a.MOCODE == info.Mocode && a.MOSEQ == info.Moseq).Select(a => a).FirstOrDefault(); if (headList.Where(a => a.MODId == MO.ID && a.POID == info.Pocode.Split('-')[0]).FirstOrDefault() == null) { head = new OM_POmain(); head.UserCode = AppConfig.UserName; head.POID = info.Pocode.Split('-')[0]; head.cIWhCode = cIWhCode; head.cOWhCode = cOWhCode; head.MODId = MO.ID; head.list = new List(); headList.Add(head); } else { head = headList.Where(a => a.MODId == MO.ID && a.POID == info.Pocode.Split('-')[0]).FirstOrDefault(); } OM_PODetails body = new OM_PODetails(); body.ID = info.Pocode.Split('-')[1]; body.cInvCode = MO.ITEMCODE+"_"+info.Opcode; body.iQuantity = Convert.ToDecimal(info.Prqty); body.iNum = Decimal.Parse(info.Moseq); head.list.Add(body); } string Istr = JsonConvert.SerializeObject(headList); string iresult = HttpPost(System.Configuration.ConfigurationSettings.AppSettings["APIURL"].ToString() + "APIOM_POMain1", Istr); //string iresult = HttpPost("http://localhost:51182/api/" + "APIOM_POMain1", Istr); Result INVOUTResult = new Result(); INVOUTResult = JsonConvert.DeserializeObject(iresult); if (INVOUTResult.code != "200") { throw new Exception(INVOUTResult.msg); } //内部供应商结算 List id = list.Select(a => a.id).ToList(); var c=db.ICSIntSupCalLog.Where(a => id.Contains(a.id)); foreach (ICSIntSupCalLog sup in c) { sup.ISInput = true; db.SubmitChanges(); } db.Transaction.Commit(); } catch (Exception ex) { db.Transaction.Rollback(); throw ex; } } #endregion public static string HttpPost(string url, string body) { try { System.Text.Encoding encoding = Encoding.UTF8; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.Accept = "application/json, text/javascript, */*"; //"text/html, application/xhtml+xml, */*"; request.ContentType = "application/json; charset=utf-8"; byte[] buffer = encoding.GetBytes(body); request.ContentLength = buffer.Length; request.GetRequestStream().Write(buffer, 0, buffer.Length); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding)) { return reader.ReadToEnd(); } } catch (WebException ex) { var res = (HttpWebResponse)ex.Response; StringBuilder sb = new StringBuilder(); StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8); sb.Append(sr.ReadToEnd()); throw new Exception(sb.ToString()); } } } }