using NFine.Data.Extensions; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using NFine.Code; using NFine.Repository; using System.Data.Common; using NFine.Domain._03_Entity.SRM; using ICS.Application.Entity; using Newtonsoft.Json; using System.Configuration; using System.Data.SqlClient; using ICS.Data; using Newtonsoft.Json.Linq; using System.Net; using System.IO; using NFine.Domain._03_Entity.WMS; namespace NFine.Application.WMS { public class ICSLotFreezeDHApp : RepositoryFactory { /// /// 点击委外退料生成条码 /// public string GetNewLotNo(string LotNO) { DataTable dt = new DataTable(); List parameter = new List(); //string sql = @"select max(LotNo) as NewLotNo from ICSInventoryLot where EATTRIBUTE1='{0}' "; string sql = @"SELECT TOP 1 LotNO as NewLotNo FROM ICSInventoryLot WHERE EATTRIBUTE1='{0}' ORDER BY CAST(SUBSTRING(LotNO, (LEN(LotNO)-CHARINDEX('-',REVERSE(LotNO))+1)+1,CHARINDEX('-',REVERSE(LotNO))-1) AS INT) DESC"; sql = string.Format(sql, LotNO); dt = Repository().FindTableBySql(sql.ToString()); if (dt == null || dt.Rows.Count == 0 || dt.Rows[0]["NewLotNo"].ToString() == "") { return LotNO + "-1"; } else { string newLotNO = dt.Rows[0]["NewLotNo"].ToString(); int COUNT = Convert.ToInt32(newLotNO.Substring(newLotNO.LastIndexOf('-') + 1)) + 1; return LotNO + "-" + COUNT.ToString(); } } public string Freeze(string LotNOList, string Memo) { string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location; string MUSER = NFine.Code.OperatorProvider.Provider.GetCurrent().UserCode; string MUSERNAME = NFine.Code.OperatorProvider.Provider.GetCurrent().UserName; string Identification = Guid.NewGuid().ToString(); string msg = ""; string sql = string.Empty; string LotList = ""; foreach (string LotNO in LotNOList.Split(',')) { LotList += "'" + LotNO + "',"; } try { LotList = LotList.TrimEnd(','); string switchsql = @"select CASE when Enable='true' Then '是' ELSE '否' END AS Switch from ICSConfiguration where Code ='LotFreezeToERP' and WorkPoint='{0}'"; switchsql = string.Format(switchsql, WorkPoint); DataTable switchdt = Repository().FindTableBySql(switchsql.ToString()); if (switchdt.Rows.Count == 0 || switchdt.Rows[0]["Switch"].ToString() == "否") //if (switchdt.Rows.Count != 0) { sql = @"update ICSWareHouseLotInfo set Freeze=1 where LotNo IN ({0}) and WorkPoint='{1}' insert into ICSWareHouseLotInfoLog (ID,Identification,LotNo,InvCode,FromWarehouseCode,FromLocationCode,Quantity,Memo,Lock,TransType,BusinessCode,ERPUpload ,MUSER,MUSERName,MTIME,WorkPoint) SELECT NEWID(),'{2}',LotNo,InvCode,WarehouseCode,LocationCode,Quantity,'{3}','0','10','52','0' ,'{4}','{5}',GETDATE(),'{1}' FROM ICSWareHouseLotInfo WHERE LotNo IN ({0}) AND WorkPoint='{1}'"; sql = string.Format(sql, LotList, WorkPoint, Identification, Memo, MUSER, MUSERNAME); if (SqlHelper.ExecuteNonQuery(sql) == LotNOList.Split(',').Count() * 2) { msg = ""; } else { msg = "冻结失败"; } } else { sql = @" update ICSWareHouseLotInfo set Freeze=1 where LotNo IN ({0}) and WorkPoint='{1}' insert into ICSWareHouseLotInfoLog (ID,Identification,LotNo,InvCode,FromWarehouseCode,FromLocationCode,Quantity,Memo,Lock,TransType,BusinessCode,ERPUpload ,MUSER,MUSERName,MTIME,WorkPoint) SELECT NEWID(),'{2}',LotNo,InvCode,WarehouseCode,LocationCode,Quantity,'{3}','0','10','52','0' ,'{4}','{5}',GETDATE(),'{6}' FROM ICSWareHouseLotInfo WHERE LotNo IN ({0}) AND WorkPoint='{1}' select '冻结' as Costre , '' as DepCode,d.WarehouseCode as InWhCode, (row_number() over(order by b.LotNo,d.WarehouseCode,b.InvCode)-1)/2+1 as GroupNO , d.WarehouseCode as OutWhCode,row_number() over(order by b.LotNo,d.WarehouseCode,b.InvCode) as Sequence,b.InvCode,'冻结' as [Type] ,d.WarehouseCode as WHCode,'false' AS UpdateStock, d.Quantity as Quantity ,b.Amount,'{4}' as [User],b.WorkPoint, isnull(c.BatchCode,'') as BatchCode ,isnull(c.ProjectCode,'') as ProjectCode,isnull(c.Version,'') as Version,isnull(c.Brand,'') as Brand,isnull(c.cFree1,'') as cFree1,isnull(c.cFree2,'') as cFree2,isnull(c.cFree3,'') as cFree3,isnull(c.cFree4,'') as cFree4,isnull(c.cFree5,'') as cFree5,isnull(c.cFree6,'') as cFree6, isnull(c.cFree7,'') as cFree7,isnull(c.cFree8,'') as cFree8,isnull(c.cFree9,'') as cFree9,isnull(c.cFree10,'') as cFree10 INTO #TempERP from ICSInventoryLot b left join ICSExtension c on b.ExtensionID=c.ID and b.WorkPoint=c.WorkPoint left join ICSWareHouseLotInfo d on d.LotNo=b.LotNo and d.WorkPoint=b.WorkPoint where b.LotNo in ({0}) and b.WorkPoint='{1}' select distinct Costre, DepCode,InWhCode,OutWhCode,[User],getdate() as MTime,WorkPoint,UpdateStock,'TransForm002' as TransferType FROM #TempERP select Costre, Sequence,GroupNO,[Type],InvCode,WHCode,BatchCode,ProjectCode,Quantity,Amount,Version,Brand,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10 FROM #TempERP DROP TABLE #TempERP "; sql = string.Format(sql, LotList, WorkPoint, Identification, Memo, MUSER, MUSERNAME, WorkPoint); msg = CmdExecuteData(sql); } } catch (Exception ex) { throw new Exception(ex.Message); } return msg; } public string UnFreeze(string LotNOList, string Memo) { string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location; string MUSER = NFine.Code.OperatorProvider.Provider.GetCurrent().UserCode; string MUSERNAME = NFine.Code.OperatorProvider.Provider.GetCurrent().UserName; string Identification = Guid.NewGuid().ToString(); string msg = ""; string sql = string.Empty; string LotList = ""; foreach (string LotNO in LotNOList.Split(',')) { LotList += "'" + LotNO + "',"; } try { LotList = LotList.TrimEnd(','); string switchsql = @"select CASE when Enable='true' Then '是' ELSE '否' END AS Switch from ICSConfiguration where Code ='LotFreezeToERP' and WorkPoint='{0}'"; switchsql = string.Format(switchsql, WorkPoint); DataTable switchdt = Repository().FindTableBySql(switchsql.ToString()); if (switchdt.Rows.Count == 0 || switchdt.Rows[0]["Switch"].ToString() == "否") { sql = @"update ICSWareHouseLotInfo set Freeze=0 where LotNo IN ({0}) and WorkPoint='{1}' insert into ICSWareHouseLotInfoLog (ID,Identification,LotNo,InvCode,FromWarehouseCode,FromLocationCode,Quantity,Memo,Lock,TransType,BusinessCode,ERPUpload ,MUSER,MUSERName,MTIME,WorkPoint) SELECT NEWID(),'{2}',LotNo,InvCode,WarehouseCode,LocationCode,Quantity,'{3}','0','10','53','0' ,'{4}','{5}',GETDATE(),'{1}' FROM ICSWareHouseLotInfo WHERE LotNo IN ({0}) AND WorkPoint='{1}'"; sql = string.Format(sql, LotList, WorkPoint, Identification, Memo, MUSER, MUSERNAME); if (SqlHelper.ExecuteNonQuery(sql) == LotNOList.Split(',').Count()*2) { msg = ""; } else { msg = "解除冻结失败"; } } else { sql = @" update ICSWareHouseLotInfo set Freeze=0 where LotNo IN ({0}) and WorkPoint='{1}' insert into ICSWareHouseLotInfoLog (ID,Identification,LotNo,InvCode,FromWarehouseCode,FromLocationCode,Quantity,Memo,Lock,TransType,BusinessCode,ERPUpload ,MUSER,MUSERName,MTIME,WorkPoint) SELECT NEWID(),'{2}',LotNo,InvCode,WarehouseCode,LocationCode,Quantity,'{3}','0','10','53','0' ,'{4}','{5}',GETDATE(),'{6}' FROM ICSWareHouseLotInfo WHERE LotNo IN ({0}) AND WorkPoint='{1}' select '解冻' as Costre , '' as DepCode,d.WarehouseCode as InWhCode, (row_number() over(order by b.LotNo,d.WarehouseCode,b.InvCode)-1)/2+1 as GroupNO , d.WarehouseCode as OutWhCode,row_number() over(order by b.LotNo,d.WarehouseCode,b.InvCode) as Sequence,b.InvCode,'解冻' as [Type] ,d.WarehouseCode as WHCode,'false' AS UpdateStock, d.Quantity as Quantity ,b.Amount,'{2}' as [User],b.WorkPoint, isnull(c.BatchCode,'') as BatchCode ,isnull(c.ProjectCode,'') as ProjectCode,isnull(c.Version,'') as Version,isnull(c.Brand,'') as Brand,isnull(c.cFree1,'') as cFree1,isnull(c.cFree2,'') as cFree2,isnull(c.cFree3,'') as cFree3,isnull(c.cFree4,'') as cFree4,isnull(c.cFree5,'') as cFree5,isnull(c.cFree6,'') as cFree6, isnull(c.cFree7,'') as cFree7,isnull(c.cFree8,'') as cFree8,isnull(c.cFree9,'') as cFree9,isnull(c.cFree10,'') as cFree10 INTO #TempERP from ICSInventoryLot b left join ICSExtension c on b.ExtensionID=c.ID and b.WorkPoint=c.WorkPoint left join ICSWareHouseLotInfo d on d.LotNo=b.LotNo and d.WorkPoint=b.WorkPoint where b.LotNo in ({0}) and b.WorkPoint='{1}' select distinct Costre, DepCode,InWhCode,OutWhCode,[User],getdate() as MTime,WorkPoint,UpdateStock,'TransForm002' as TransferType FROM #TempERP select Costre, Sequence,GroupNO,[Type],InvCode,WHCode,BatchCode,ProjectCode,Quantity,Amount,Version,Brand,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10 FROM #TempERP DROP TABLE #TempERP "; sql = string.Format(sql, LotList, WorkPoint, Identification, Memo, MUSER, MUSERNAME, WorkPoint); CmdExecuteData(sql); } } catch (Exception ex) { throw new Exception(ex.Message); } return msg; } public string CmdExecuteData(string sql) { string msg = ""; try { string connString = SqlHelper.DataCenterConnString; SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString); conn.Open(); SqlTransaction sqlTran = conn.BeginTransaction(); SqlCommand cmd = new SqlCommand(); cmd.Transaction = sqlTran; cmd.Connection = conn; try { DataSet DSet = SqlCommandHelper.SQlReturnDataSet(sql, cmd); string Inputstr = SqlHelper.DataSetToJson(DSet, "details", "Costre"); string APIURL = ConfigurationManager.ConnectionStrings["ERPAPIURL"].ConnectionString + "MorphologicalTransformationToFreezeDoc/Create"; string result = HttpPost(APIURL, Inputstr); JObject Obj = (JObject)JsonConvert.DeserializeObject(result);//或者JObject jo = JObject.Parse(jsonText); string MessAge = Obj["Message"].ToString(); string Success = Obj["Success"].ToString(); string ERPSql = ""; if (Success.ToUpper() == "FALSE") { throw new Exception(MessAge); } else { //try //{ // JArray res = (JArray)JsonConvert.DeserializeObject(Obj["Data"].ToString()); // foreach (var item in res) // { // JObject jo = (JObject)item; // JArray resdetail = (JArray)JsonConvert.DeserializeObject(jo["details"].ToString()); // foreach (var detail in resdetail) // { // JObject det = (JObject)detail; // ERPSql += @"UPDATE a set ERPID='{3}',ERPDetailID='{4}',ERPCode='{5}',ERPSequence='{6}',ERPUpload='1' // from ICSWareHouseLotInfoLog a // inner join ICSMTDOC b on a.LotNo=b.LotNo and a.WorkPoint=b.WorkPoint // where b.MTDOCCode='{0}' and b.Sequence='{1}' and a.Identification='{2}'"; // ERPSql += @" update ICSMTDOC set MTDOCID='{3}',MTDOCDetailID='{4}' where MTDOCCode='{0}' and Sequence='{1}'"; // ERPSql = string.Format(ERPSql, MTDOCCode, det["Sequence"].ToString(), Identification, jo["ID"].ToString(), det["DetailID"].ToString(), jo["MTCode"].ToString(), det["Sequence"].ToString()); // } // } // SqlCommandHelper.CmdExecuteNonQuery(ERPSql, cmd); //} //catch (Exception ex) //{ // msg = ex.Message; //} } cmd.Transaction.Commit(); //return dt; } catch (Exception ex) { cmd.Transaction.Rollback(); msg = ex.Message; } finally { if (conn.State == ConnectionState.Open) { conn.Close(); } conn.Dispose(); } } catch (Exception ex) { msg = ex.Message; } return msg; } //接口api解析 public static string HttpPost(string url, string body) { try { 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) { throw new Exception(ex.Message); } } /// /// 根据ID获取条码 /// /// public DataTable GetLotNoByID(string ID) { string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location; string sql = @"select '' as ID,'' as LotNo union all SELECT ID,LotNo FROM ICSWareHouseLotInfo WITH (NOLOCK) WHERE ID in ({0}) "; sql = string.Format(sql, ID.TrimEnd(',')); //string role = NFine.Code.OperatorProvider.Provider.GetCurrent().RoleEnCode; //if (role != "admin") //{ // sql += " and b.WorkPoint='" + WorkPoint + "'"; //} DataTable dt = SqlHelper.GetDataTableBySql(sql); return dt; } //合批 public string Combine(string LotNo, string ID) { string msg = ""; string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location; string MUSER = NFine.Code.OperatorProvider.Provider.GetCurrent().UserCode; string sql = @" select distinct LotNo,Quantity from ICSWareHouseLotInfo WHERE ID IN (" + ID.TrimEnd(',') + ")"; DataTable dt = Repository().FindTableBySql(sql.ToString()); List asn = new List(); LotNoCombineHead ass = new LotNoCombineHead(); for (int i = 0; i < dt.Rows.Count; i++) { if (LotNo == dt.Rows[i]["LotNo"].ToString()) { continue; } LotNoCombineBody DetailList = new LotNoCombineBody(); DetailList.CurrentLotNo = dt.Rows[i]["LotNo"].ToString(); DetailList.CurrentQuantity = dt.Rows[i]["Quantity"].ToString(); ass.detail.Add(DetailList); } ass.LotNo = LotNo; ass.User = MUSER; ass.WorkPoint = WorkPoint; ass.MTIME = System.DateTime.Now.ToString("s"); asn.Add(ass); string input = JsonConvert.SerializeObject(asn); string APIURL = ConfigurationManager.ConnectionStrings["APIURL"].ConnectionString + "LOTMerge/Create"; string result = HttpPost(APIURL, input); JObject Obj = (JObject)JsonConvert.DeserializeObject(result);//或者JObject jo = JObject.Parse(jsonText); string MessAge = Obj["Message"].ToString(); string Success = Obj["Success"].ToString(); if (Success.ToUpper() == "FALSE") { msg = MessAge; } return msg; } } }