You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

381 lines
19 KiB

3 weeks ago
  1. using NFine.Data.Extensions;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using NFine.Code;
  9. using NFine.Repository;
  10. using System.Data.Common;
  11. using NFine.Domain._03_Entity.SRM;
  12. using ICS.Application.Entity;
  13. using Newtonsoft.Json;
  14. using System.Configuration;
  15. using System.Data.SqlClient;
  16. using ICS.Data;
  17. using Newtonsoft.Json.Linq;
  18. using System.Net;
  19. using System.IO;
  20. using NFine.Domain._03_Entity.WMS;
  21. namespace NFine.Application.WMS
  22. {
  23. public class ICSLotFreezeDHApp : RepositoryFactory<ICSVendor>
  24. {
  25. /// <summary>
  26. /// 点击委外退料生成条码
  27. /// </summary>
  28. public string GetNewLotNo(string LotNO)
  29. {
  30. DataTable dt = new DataTable();
  31. List<DbParameter> parameter = new List<DbParameter>();
  32. //string sql = @"select max(LotNo) as NewLotNo from ICSInventoryLot where EATTRIBUTE1='{0}' ";
  33. string sql = @"SELECT TOP 1 LotNO as NewLotNo FROM ICSInventoryLot WHERE EATTRIBUTE1='{0}'
  34. ORDER BY CAST(SUBSTRING(LotNO, (LEN(LotNO)-CHARINDEX('-',REVERSE(LotNO))+1)+1,CHARINDEX('-',REVERSE(LotNO))-1) AS INT) DESC";
  35. sql = string.Format(sql, LotNO);
  36. dt = Repository().FindTableBySql(sql.ToString());
  37. if (dt == null || dt.Rows.Count == 0 || dt.Rows[0]["NewLotNo"].ToString() == "")
  38. {
  39. return LotNO + "-1";
  40. }
  41. else
  42. {
  43. string newLotNO = dt.Rows[0]["NewLotNo"].ToString();
  44. int COUNT = Convert.ToInt32(newLotNO.Substring(newLotNO.LastIndexOf('-') + 1)) + 1;
  45. return LotNO + "-" + COUNT.ToString();
  46. }
  47. }
  48. public string Freeze(string LotNOList, string Memo)
  49. {
  50. string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location;
  51. string MUSER = NFine.Code.OperatorProvider.Provider.GetCurrent().UserCode;
  52. string MUSERNAME = NFine.Code.OperatorProvider.Provider.GetCurrent().UserName;
  53. string Identification = Guid.NewGuid().ToString();
  54. string msg = "";
  55. string sql = string.Empty;
  56. string LotList = "";
  57. foreach (string LotNO in LotNOList.Split(','))
  58. {
  59. LotList += "'" + LotNO + "',";
  60. }
  61. try
  62. {
  63. LotList = LotList.TrimEnd(',');
  64. string switchsql = @"select CASE when Enable='true' Then '是' ELSE '否' END AS Switch from ICSConfiguration
  65. where Code ='LotFreezeToERP' and WorkPoint='{0}'";
  66. switchsql = string.Format(switchsql, WorkPoint);
  67. DataTable switchdt = Repository().FindTableBySql(switchsql.ToString());
  68. if (switchdt.Rows.Count == 0 || switchdt.Rows[0]["Switch"].ToString() == "否")
  69. //if (switchdt.Rows.Count != 0)
  70. {
  71. sql = @"update ICSWareHouseLotInfo set Freeze=1
  72. where LotNo IN ({0}) and WorkPoint='{1}'
  73. insert into ICSWareHouseLotInfoLog
  74. (ID,Identification,LotNo,InvCode,FromWarehouseCode,FromLocationCode,Quantity,Memo,Lock,TransType,BusinessCode,ERPUpload
  75. ,MUSER,MUSERName,MTIME,WorkPoint)
  76. SELECT NEWID(),'{2}',LotNo,InvCode,WarehouseCode,LocationCode,Quantity,'{3}','0','10','52','0'
  77. ,'{4}','{5}',GETDATE(),'{1}' FROM ICSWareHouseLotInfo
  78. WHERE LotNo IN ({0}) AND WorkPoint='{1}'";
  79. sql = string.Format(sql, LotList, WorkPoint, Identification, Memo, MUSER, MUSERNAME);
  80. if (SqlHelper.ExecuteNonQuery(sql) == LotNOList.Split(',').Count() * 2)
  81. {
  82. msg = "";
  83. }
  84. else
  85. {
  86. msg = "冻结失败";
  87. }
  88. }
  89. else
  90. {
  91. sql = @" update ICSWareHouseLotInfo set Freeze=1
  92. where LotNo IN ({0}) and WorkPoint='{1}'
  93. insert into ICSWareHouseLotInfoLog
  94. (ID,Identification,LotNo,InvCode,FromWarehouseCode,FromLocationCode,Quantity,Memo,Lock,TransType,BusinessCode,ERPUpload
  95. ,MUSER,MUSERName,MTIME,WorkPoint)
  96. SELECT NEWID(),'{2}',LotNo,InvCode,WarehouseCode,LocationCode,Quantity,'{3}','0','10','52','0'
  97. ,'{4}','{5}',GETDATE(),'{6}' FROM ICSWareHouseLotInfo
  98. WHERE LotNo IN ({0}) AND WorkPoint='{1}'
  99. 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 ,
  100. 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,
  101. d.Quantity as Quantity ,b.Amount,'{4}' as [User],b.WorkPoint,
  102. 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,
  103. isnull(c.cFree7,'') as cFree7,isnull(c.cFree8,'') as cFree8,isnull(c.cFree9,'') as cFree9,isnull(c.cFree10,'') as cFree10
  104. INTO #TempERP
  105. from ICSInventoryLot b
  106. left join ICSExtension c on b.ExtensionID=c.ID and b.WorkPoint=c.WorkPoint
  107. left join ICSWareHouseLotInfo d on d.LotNo=b.LotNo and d.WorkPoint=b.WorkPoint
  108. where b.LotNo in ({0}) and b.WorkPoint='{1}'
  109. select distinct Costre, DepCode,InWhCode,OutWhCode,[User],getdate() as MTime,WorkPoint,UpdateStock,'TransForm002' as TransferType FROM #TempERP
  110. 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
  111. DROP TABLE #TempERP ";
  112. sql = string.Format(sql, LotList, WorkPoint, Identification, Memo, MUSER, MUSERNAME, WorkPoint);
  113. msg = CmdExecuteData(sql);
  114. }
  115. }
  116. catch (Exception ex)
  117. {
  118. throw new Exception(ex.Message);
  119. }
  120. return msg;
  121. }
  122. public string UnFreeze(string LotNOList, string Memo)
  123. {
  124. string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location;
  125. string MUSER = NFine.Code.OperatorProvider.Provider.GetCurrent().UserCode;
  126. string MUSERNAME = NFine.Code.OperatorProvider.Provider.GetCurrent().UserName;
  127. string Identification = Guid.NewGuid().ToString();
  128. string msg = "";
  129. string sql = string.Empty;
  130. string LotList = "";
  131. foreach (string LotNO in LotNOList.Split(','))
  132. {
  133. LotList += "'" + LotNO + "',";
  134. }
  135. try
  136. {
  137. LotList = LotList.TrimEnd(',');
  138. string switchsql = @"select CASE when Enable='true' Then '是' ELSE '否' END AS Switch from ICSConfiguration
  139. where Code ='LotFreezeToERP' and WorkPoint='{0}'";
  140. switchsql = string.Format(switchsql, WorkPoint);
  141. DataTable switchdt = Repository().FindTableBySql(switchsql.ToString());
  142. if (switchdt.Rows.Count == 0 || switchdt.Rows[0]["Switch"].ToString() == "否")
  143. {
  144. sql = @"update ICSWareHouseLotInfo set Freeze=0
  145. where LotNo IN ({0}) and WorkPoint='{1}'
  146. insert into ICSWareHouseLotInfoLog
  147. (ID,Identification,LotNo,InvCode,FromWarehouseCode,FromLocationCode,Quantity,Memo,Lock,TransType,BusinessCode,ERPUpload
  148. ,MUSER,MUSERName,MTIME,WorkPoint)
  149. SELECT NEWID(),'{2}',LotNo,InvCode,WarehouseCode,LocationCode,Quantity,'{3}','0','10','53','0'
  150. ,'{4}','{5}',GETDATE(),'{1}' FROM ICSWareHouseLotInfo
  151. WHERE LotNo IN ({0}) AND WorkPoint='{1}'";
  152. sql = string.Format(sql, LotList, WorkPoint, Identification, Memo, MUSER, MUSERNAME);
  153. if (SqlHelper.ExecuteNonQuery(sql) == LotNOList.Split(',').Count()*2)
  154. {
  155. msg = "";
  156. }
  157. else
  158. {
  159. msg = "解除冻结失败";
  160. }
  161. }
  162. else
  163. {
  164. sql = @" update ICSWareHouseLotInfo set Freeze=0
  165. where LotNo IN ({0}) and WorkPoint='{1}'
  166. insert into ICSWareHouseLotInfoLog
  167. (ID,Identification,LotNo,InvCode,FromWarehouseCode,FromLocationCode,Quantity,Memo,Lock,TransType,BusinessCode,ERPUpload
  168. ,MUSER,MUSERName,MTIME,WorkPoint)
  169. SELECT NEWID(),'{2}',LotNo,InvCode,WarehouseCode,LocationCode,Quantity,'{3}','0','10','53','0'
  170. ,'{4}','{5}',GETDATE(),'{6}' FROM ICSWareHouseLotInfo
  171. WHERE LotNo IN ({0}) AND WorkPoint='{1}'
  172. 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 ,
  173. 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,
  174. d.Quantity as Quantity ,b.Amount,'{2}' as [User],b.WorkPoint,
  175. 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,
  176. isnull(c.cFree7,'') as cFree7,isnull(c.cFree8,'') as cFree8,isnull(c.cFree9,'') as cFree9,isnull(c.cFree10,'') as cFree10
  177. INTO #TempERP
  178. from ICSInventoryLot b
  179. left join ICSExtension c on b.ExtensionID=c.ID and b.WorkPoint=c.WorkPoint
  180. left join ICSWareHouseLotInfo d on d.LotNo=b.LotNo and d.WorkPoint=b.WorkPoint
  181. where b.LotNo in ({0}) and b.WorkPoint='{1}'
  182. select distinct Costre, DepCode,InWhCode,OutWhCode,[User],getdate() as MTime,WorkPoint,UpdateStock,'TransForm002' as TransferType FROM #TempERP
  183. 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
  184. DROP TABLE #TempERP ";
  185. sql = string.Format(sql, LotList, WorkPoint, Identification, Memo, MUSER, MUSERNAME, WorkPoint);
  186. CmdExecuteData(sql);
  187. }
  188. }
  189. catch (Exception ex)
  190. {
  191. throw new Exception(ex.Message);
  192. }
  193. return msg;
  194. }
  195. public string CmdExecuteData(string sql)
  196. {
  197. string msg = "";
  198. try
  199. {
  200. string connString = SqlHelper.DataCenterConnString;
  201. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  202. conn.Open();
  203. SqlTransaction sqlTran = conn.BeginTransaction();
  204. SqlCommand cmd = new SqlCommand();
  205. cmd.Transaction = sqlTran;
  206. cmd.Connection = conn;
  207. try
  208. {
  209. DataSet DSet = SqlCommandHelper.SQlReturnDataSet(sql, cmd);
  210. string Inputstr = SqlHelper.DataSetToJson(DSet, "details", "Costre");
  211. string APIURL = ConfigurationManager.ConnectionStrings["ERPAPIURL"].ConnectionString + "MorphologicalTransformationToFreezeDoc/Create";
  212. string result = HttpPost(APIURL, Inputstr);
  213. JObject Obj = (JObject)JsonConvert.DeserializeObject(result);//或者JObject jo = JObject.Parse(jsonText);
  214. string MessAge = Obj["Message"].ToString();
  215. string Success = Obj["Success"].ToString();
  216. string ERPSql = "";
  217. if (Success.ToUpper() == "FALSE")
  218. {
  219. throw new Exception(MessAge);
  220. }
  221. else
  222. {
  223. //try
  224. //{
  225. // JArray res = (JArray)JsonConvert.DeserializeObject(Obj["Data"].ToString());
  226. // foreach (var item in res)
  227. // {
  228. // JObject jo = (JObject)item;
  229. // JArray resdetail = (JArray)JsonConvert.DeserializeObject(jo["details"].ToString());
  230. // foreach (var detail in resdetail)
  231. // {
  232. // JObject det = (JObject)detail;
  233. // ERPSql += @"UPDATE a set ERPID='{3}',ERPDetailID='{4}',ERPCode='{5}',ERPSequence='{6}',ERPUpload='1'
  234. // from ICSWareHouseLotInfoLog a
  235. // inner join ICSMTDOC b on a.LotNo=b.LotNo and a.WorkPoint=b.WorkPoint
  236. // where b.MTDOCCode='{0}' and b.Sequence='{1}' and a.Identification='{2}'";
  237. // ERPSql += @" update ICSMTDOC set MTDOCID='{3}',MTDOCDetailID='{4}' where MTDOCCode='{0}' and Sequence='{1}'";
  238. // ERPSql = string.Format(ERPSql, MTDOCCode, det["Sequence"].ToString(), Identification, jo["ID"].ToString(), det["DetailID"].ToString(), jo["MTCode"].ToString(), det["Sequence"].ToString());
  239. // }
  240. // }
  241. // SqlCommandHelper.CmdExecuteNonQuery(ERPSql, cmd);
  242. //}
  243. //catch (Exception ex)
  244. //{
  245. // msg = ex.Message;
  246. //}
  247. }
  248. cmd.Transaction.Commit();
  249. //return dt;
  250. }
  251. catch (Exception ex)
  252. {
  253. cmd.Transaction.Rollback();
  254. msg = ex.Message;
  255. }
  256. finally
  257. {
  258. if (conn.State == ConnectionState.Open)
  259. {
  260. conn.Close();
  261. }
  262. conn.Dispose();
  263. }
  264. }
  265. catch (Exception ex)
  266. {
  267. msg = ex.Message;
  268. }
  269. return msg;
  270. }
  271. //接口api解析
  272. public static string HttpPost(string url, string body)
  273. {
  274. try
  275. {
  276. Encoding encoding = Encoding.UTF8;
  277. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  278. request.Method = "POST";
  279. request.Accept = "application/json, text/javascript, */*"; //"text/html, application/xhtml+xml, */*";
  280. request.ContentType = "application/json; charset=utf-8";
  281. byte[] buffer = encoding.GetBytes(body);
  282. request.ContentLength = buffer.Length;
  283. request.GetRequestStream().Write(buffer, 0, buffer.Length);
  284. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  285. using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding))
  286. {
  287. return reader.ReadToEnd();
  288. }
  289. }
  290. catch (WebException ex)
  291. {
  292. throw new Exception(ex.Message);
  293. }
  294. }
  295. /// <summary>
  296. /// 根据ID获取条码
  297. /// </summary>
  298. /// <returns></returns>
  299. public DataTable GetLotNoByID(string ID)
  300. {
  301. string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location;
  302. string sql = @"select '' as ID,'' as LotNo union all
  303. SELECT ID,LotNo FROM ICSWareHouseLotInfo WITH (NOLOCK) WHERE ID in ({0}) ";
  304. sql = string.Format(sql, ID.TrimEnd(','));
  305. //string role = NFine.Code.OperatorProvider.Provider.GetCurrent().RoleEnCode;
  306. //if (role != "admin")
  307. //{
  308. // sql += " and b.WorkPoint='" + WorkPoint + "'";
  309. //}
  310. DataTable dt = SqlHelper.GetDataTableBySql(sql);
  311. return dt;
  312. }
  313. //合批
  314. public string Combine(string LotNo, string ID)
  315. {
  316. string msg = "";
  317. string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location;
  318. string MUSER = NFine.Code.OperatorProvider.Provider.GetCurrent().UserCode;
  319. string sql = @" select distinct LotNo,Quantity from ICSWareHouseLotInfo WHERE ID IN (" + ID.TrimEnd(',') + ")";
  320. DataTable dt = Repository().FindTableBySql(sql.ToString());
  321. List<LotNoCombineHead> asn = new List<LotNoCombineHead>();
  322. LotNoCombineHead ass = new LotNoCombineHead();
  323. for (int i = 0; i < dt.Rows.Count; i++)
  324. {
  325. if (LotNo == dt.Rows[i]["LotNo"].ToString())
  326. {
  327. continue;
  328. }
  329. LotNoCombineBody DetailList = new LotNoCombineBody();
  330. DetailList.CurrentLotNo = dt.Rows[i]["LotNo"].ToString();
  331. DetailList.CurrentQuantity = dt.Rows[i]["Quantity"].ToString();
  332. ass.detail.Add(DetailList);
  333. }
  334. ass.LotNo = LotNo;
  335. ass.User = MUSER;
  336. ass.WorkPoint = WorkPoint;
  337. ass.MTIME = System.DateTime.Now.ToString("s");
  338. asn.Add(ass);
  339. string input = JsonConvert.SerializeObject(asn);
  340. string APIURL = ConfigurationManager.ConnectionStrings["APIURL"].ConnectionString + "LOTMerge/Create";
  341. string result = HttpPost(APIURL, input);
  342. JObject Obj = (JObject)JsonConvert.DeserializeObject(result);//或者JObject jo = JObject.Parse(jsonText);
  343. string MessAge = Obj["Message"].ToString();
  344. string Success = Obj["Success"].ToString();
  345. if (Success.ToUpper() == "FALSE")
  346. {
  347. msg = MessAge;
  348. }
  349. return msg;
  350. }
  351. }
  352. }