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.

417 lines
20 KiB

1 year ago
  1. using ICSSoft.Common;
  2. using ICSSoft.Entity;
  3. using ICSSoft.Entity;
  4. using Newtonsoft.Json;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Data;
  8. using System.Data.SqlClient;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace ICSSoft.DataProject
  13. {
  14. public class OutsourcingSevice
  15. {
  16. private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  17. private static string connString = System.Configuration.ConfigurationManager.AppSettings["ConnStr"];
  18. private static string ERPDB = System.Configuration.ConfigurationManager.AppSettings["ERPDB"];
  19. DataTable table = null;
  20. DataTable tableInfo = null;
  21. SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
  22. string sql = string.Empty;
  23. string sqlInfo = string.Empty;
  24. /// <summary>
  25. /// 委外送货单
  26. /// </summary>
  27. /// <param name="JsonData"></param>
  28. /// <returns></returns>
  29. public OutsourcingModelInfo Get(OutsourcingModel model)
  30. {
  31. conn.Open();
  32. SqlTransaction sqlTran = conn.BeginTransaction();
  33. SqlCommand cmd = new SqlCommand();
  34. cmd.Transaction = sqlTran;
  35. cmd.Connection = conn;
  36. try
  37. {
  38. sql = @"
  39. select
  40. a.ID,
  41. a.OASNCode,
  42. b.VenCode ,
  43. b.VenName,
  44. a.MUSER,
  45. a.MTIME
  46. from ICSOASN a
  47. left join ICSVendor b on a.VenCode=b.VenCode where 1=1 ";
  48. if (!string.IsNullOrEmpty(model.OASNCode))
  49. {
  50. sql += " and a.OASNCode={0}";
  51. }
  52. if (!string.IsNullOrEmpty(model.User))
  53. {
  54. sql += " and a.MUSER={1}";
  55. }
  56. if (model.MTime > new DateTime(2000, 01, 01))
  57. {
  58. //throw new Exception("请输入正确的操作时间:" + model.MTime);
  59. sql += " and a.MTIME={2}";
  60. }
  61. sql = string.Format(sql, model.OASNCode, model.User, model.MTime);
  62. table = DBHelper.SQlReturnData(sql, cmd);
  63. sqlInfo = @"select
  64. a.Sequence,
  65. a.LotNo,
  66. a.Quantity,
  67. a.Amount,
  68. b.InvCode from ICSOASNDetail a
  69. left join ICSInventoryLot b on a.LotNo=b.LotNo where 1=1";
  70. if (!string.IsNullOrEmpty(model.OASNCode))
  71. {
  72. sqlInfo += " and a.OASNCode=(select OASNCode from ICSOASN where OASNCode={0} )";
  73. }
  74. sqlInfo = string.Format(sqlInfo, model.OASNCode);
  75. tableInfo = DBHelper.SQlReturnData(sqlInfo, cmd);
  76. string json = JsonConvert.SerializeObject(table);
  77. string jsonInfo = JsonConvert.SerializeObject(tableInfo);
  78. string jsonList = null;
  79. if (table.Rows.Count > 0)
  80. {
  81. foreach (DataRow item in table.Rows)
  82. {
  83. OutsourcingModelInfo main = new OutsourcingModelInfo();
  84. main.ID = item["ID"].ToString();
  85. main.User = item["MUSER"].ToString();
  86. main.VenName = item["VenName"].ToString();
  87. main.VenCode = item["VenCode"].ToString();
  88. main.MTime = Convert.ToDateTime(item["MTime"]);
  89. List<OutsourcingModelInfos> list = new List<OutsourcingModelInfos>();
  90. foreach (DataRow dt in tableInfo.Rows)
  91. {
  92. OutsourcingModelInfos detail = new OutsourcingModelInfos();
  93. detail.LotNO = dt["LotNO"].ToString();
  94. detail.InvCode = dt["InvCode"].ToString();
  95. detail.Quantity = dt["Quantity"].ToString();
  96. detail.Sequence = dt["Sequence"].ToString();
  97. list.Add(detail);
  98. }
  99. main.detail = list;
  100. jsonList = JsonConvert.SerializeObject(main);
  101. }
  102. var deliveryNotrModelInfo = JsonConvert.DeserializeObject<OutsourcingModelInfo>(jsonList);
  103. cmd.Transaction.Commit();
  104. return deliveryNotrModelInfo;
  105. }
  106. return null;
  107. }
  108. catch (Exception ex)
  109. {
  110. cmd.Transaction.Rollback();
  111. log.Error(ex.Message);
  112. throw new Exception(ex.Message);
  113. }
  114. finally
  115. {
  116. if (conn.State == ConnectionState.Open)
  117. {
  118. conn.Close();
  119. }
  120. conn.Dispose();
  121. }
  122. }
  123. /// <summary>
  124. /// 添加
  125. /// </summary>
  126. /// <param name="model"></param>
  127. /// <returns></returns>
  128. public int Create(List<NoticeCreateIModel> model)
  129. {
  130. string sqlERP = string.Empty;//ERP的表头
  131. string sqlERPDetil = string.Empty;//ERP的表体
  132. string jsonERP = string.Empty;///erpjson
  133. string IstrErp = string.Empty;
  134. string iresult = string.Empty;//接口调用
  135. string WorkPoint = string.Empty;
  136. string res = string.Empty;
  137. conn.Open();
  138. SqlTransaction sqlTran = conn.BeginTransaction();
  139. SqlCommand cmd = new SqlCommand();
  140. cmd.Transaction = sqlTran;
  141. cmd.Connection = conn;
  142. int num = 0;
  143. Random Rdm = new Random();
  144. int iRdm = Rdm.Next(0, 100);
  145. try
  146. {
  147. foreach (var item in model)
  148. {
  149. string ISsqlInfos = @"select * from ICSODeliveryNotice where OASNCode='{0}' ";
  150. ISsqlInfos = string.Format(ISsqlInfos, item.TransCode);
  151. DataTable dataInfos = DBHelper.SQlReturnData(ISsqlInfos, cmd);
  152. if (dataInfos.Rows.Count > 0)
  153. {
  154. throw new Exception("请勿重复到货!");
  155. }
  156. foreach (var itemInfo in item.Detail)
  157. {
  158. WorkPoint = itemInfo.WorkPoint;
  159. string ISsql = @"select * from ICSOASN a left join ICSOASNDetail b on a.OASNCode=b.OASNCode where b.LotNo='{0}' and B.WorkPoint='{1}' ";
  160. ISsql = string.Format(ISsql, itemInfo.LotNO, itemInfo.WorkPoint);
  161. DataTable data = DBHelper.SQlReturnData(ISsql, cmd);
  162. if (data.Rows.Count <= 0)
  163. {
  164. throw new Exception("送货单不存在!");
  165. }
  166. else
  167. {
  168. string ISsqlInfo = @"select * from ICSOASN a left join ICSOASNDetail b on a.OASNCode=b.OASNCode where b.LotNo='{0}' and B.WorkPoint='{1}' and a.Status='2'";
  169. ISsqlInfo = string.Format(ISsqlInfo, itemInfo.LotNO, itemInfo.WorkPoint);
  170. DataTable dataInfo = DBHelper.SQlReturnData(ISsqlInfo, cmd);
  171. if (dataInfo.Rows.Count <= 0)
  172. {
  173. throw new Exception("送货单未审核!");
  174. }
  175. else
  176. {
  177. //修改子表
  178. string sql = @"UPDATE ICSOASNDetail set ODNQuantity=(select sum(ODNQuantity) from ICSOASNDetail where LotNo='{0}' and WorkPoint='{1}') +'{2}' where LotNo='{0}' and WorkPoint='{1}' ";
  179. sql = string.Format(sql, itemInfo.LotNO, itemInfo.WorkPoint, itemInfo.Quantity);
  180. cmd.CommandText = sql;
  181. num = cmd.ExecuteNonQuery();
  182. }
  183. }
  184. }
  185. string Date = DateTime.Now.ToString("yy");
  186. string Pre = "ODN" + iRdm + Date;
  187. ///添加到到货单表
  188. string sqlInfo = @"
  189. INSERT INTO [dbo].[ICSODeliveryNotice]([ID],[ODNCode],[Sequence],[VenCode],[DepCode],[ODNType],[InvCode],[Quantity],[Amount] ,[RCVQuantity],[UnitPrice] ,[Currency],[Status] ,[CreatePerson] ,[CreateDateTime],[POID]
  190. ,[PODetailID] ,[ODNID] ,[ODNDetailID] ,[ExtensionID] ,[MUSER] ,[MUSERName] ,[MTIME] ,[WorkPoint] ,[EATTRIBUTE1] ,[OASNCode])
  191. (SELECT NEWID(),'{0}',row_number() OVER (ORDER BY b.InvCode),d.VenCode,d.DepCode,'1',b.InvCode,SUM(a.ODNQuantity),'0','0',d.UnitPrice,d.Currency,'2'
  192. ,'{1}',GETDATE(),d.OOID,d.OODetailID,'0','0',b.ExtensionID,'{1}',e.F_RealName,GETDATE(),'{2}','',a.OASNCode
  193. FROM ICSOASNDetail a
  194. INNER JOIN ICSInventoryLot b ON a.LotNo=b.LotNo AND a.WorkPoint=b.WorkPoint
  195. INNER JOIN ICSInventoryLotDetail c ON b.LotNo=c.LotNo AND b.WorkPoint=c.WorkPoint
  196. INNER JOIN ICSOutsourcingOrder d ON c.TransCode=d.OOCode AND c.TransSequence=d.Sequence AND c.WorkPoint=d.WorkPoint
  197. INNER JOIN Sys_SRM_User e ON e.F_Account='{1}' AND e.F_Location='{2}'
  198. WHERE a.OASNCode='{3}'
  199. GROUP BY d.OOID,d.OODetailID,d.VenCode,d.UnitPrice,d.Currency,d.DepCode,b.ExtensionID,b.InvCode,a.OASNCode,e.F_RealName) ";
  200. sqlInfo = string.Format(sqlInfo, Pre, item.User, WorkPoint, item.TransCode);
  201. //sqlInfo = string.Format(sqlInfo, Pre, item.VenCode, itemInfo.DepCode, itemInfo.Amount, itemInfo.Quantity, itemInfo.UnitPrice, itemInfo.Currency, item.User, DateTime.Now, itemInfo.WorkPoint, item.TransCode, itemInfo.Sequence, itemInfo.InvCode);
  202. cmd.CommandText = sqlInfo;
  203. num = cmd.ExecuteNonQuery();
  204. #region ERP
  205. sqlERP = @"select a.VenCode,a.DepCode,b.OOCode,a.MUSER,(getdate())as Mtime from ICSODeliveryNotice a
  206. left join ICSOutsourcingOrder b on a.poid = b.OOID and a.PODetailID = b.OODetailID
  207. left join ICSOASNDetail c on c.OASNCode = a.OASNCode
  208. where a.OASNCode = '{0}'and A.WorkPoint = '{1}'
  209. group by a.VenCode,a.DepCode,b.OOCode,a.MUSER";
  210. sqlERP = string.Format(sqlERP, item.TransCode, WorkPoint);
  211. table = DBHelper.SQlReturnData(sqlERP, cmd);
  212. sqlERPDetil = @"select a.Sequence,a.InvCode,sum(a.RCVQuantity) as Quantity ,a.Amount,isnull((a.UnitPrice),0) as UnitPrice,a.Currency,PODetailID ,
  213. cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10 from
  214. ICSODeliveryNotice A
  215. left join ICSOutsourcingOrder b on a.poid=b.OOID and a.PODetailID=b.OODetailID
  216. left join ICSOASNDetail c on c.OASNCode=a.OASNCode
  217. LEFT JOIN ICSExtension d ON A.ExtensionID=B.ID
  218. where a.OASNCode='{0}'and A.WorkPoint='{1}'
  219. group by a.Sequence,a.InvCode,a.Amount,a.UnitPrice,a.Currency,PODetailID,
  220. cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10 ";
  221. sqlERPDetil = string.Format(sqlERPDetil, item.TransCode, WorkPoint);
  222. DataTable ERPtable = DBHelper.SQlReturnData(sqlERPDetil, cmd);
  223. List<ERPDeliveryNotice> docmodel = new List<ERPDeliveryNotice>();
  224. foreach (DataRow dtIn in table.Rows)
  225. {
  226. ERPDeliveryNotice doc = new ERPDeliveryNotice();
  227. doc.VenCode = dtIn["VenCode"].ToString();
  228. doc.DepCode = dtIn["DepCode"].ToString();
  229. doc.OOCode = dtIn["OOCode"].ToString();
  230. doc.User = dtIn["MUSER"].ToString();
  231. doc.MTime = dtIn["Mtime"].ToString();
  232. List<ERPDeliveryNoticeList> list = new List<ERPDeliveryNoticeList>();
  233. foreach (DataRow dtList in ERPtable.Rows)
  234. {
  235. ERPDeliveryNoticeList docc = new ERPDeliveryNoticeList();
  236. docc.Sequence = dtList["Sequence"].ToString();
  237. docc.InvCode = dtList["InvCode"].ToString();
  238. docc.Quantity = dtList["Quantity"].ToString();
  239. docc.Amount = dtList["Amount"].ToString();
  240. docc.Currency = dtList["Currency"].ToString();
  241. docc.UnitPrice = dtList["UnitPrice"].ToString();
  242. docc.OODetailID = dtList["PODetailID"].ToString();
  243. docc.cFree1 = dtList["cFree1"].ToString();
  244. docc.cFree2 = dtList["cFree2"].ToString();
  245. docc.cFree3 = dtList["cFree3"].ToString();
  246. docc.cFree4 = dtList["cFree4"].ToString();
  247. docc.cFree5 = dtList["cFree5"].ToString();
  248. docc.cFree6 = dtList["cFree6"].ToString();
  249. docc.cFree7 = dtList["cFree7"].ToString();
  250. docc.cFree8 = dtList["cFree8"].ToString();
  251. docc.cFree9 = dtList["cFree9"].ToString();
  252. docc.cFree10 = dtList["cFree10"].ToString();
  253. list.Add(docc);
  254. }
  255. doc.details = list;
  256. docmodel.Add(doc);
  257. }
  258. IstrErp = JsonConvert.SerializeObject(docmodel);
  259. log.Info("委外到货ERPJSON" + IstrErp);
  260. iresult = HttpPost("委外到货", ERPUrl.ODeliveryNoticeURL, IstrErp);
  261. Result OtherOutResult = new Result();
  262. OtherOutResult = JsonConvert.DeserializeObject<Result>(iresult);
  263. if (OtherOutResult.Success == false)
  264. {
  265. throw new Exception(OtherOutResult.Message);
  266. }
  267. else
  268. {
  269. List<ERPJsonRetun> list = new List<ERPJsonRetun>();
  270. //string nn = OtherOutResult.Data.ToString();//LOTStockModel
  271. list = JsonConvert.DeserializeObject<List<ERPJsonRetun>>(OtherOutResult.Data.ToString());
  272. foreach (var items in list)
  273. {
  274. string ERPupdate = @"update ICSODeliveryNotice set ODNCode='{0}',Sequence='{1}',ODNID='{2}',ODNDetailID='{3}',ODNType='1'
  275. where OASNCode='{4}' ";
  276. ERPupdate = string.Format(ERPupdate, items.ODNCode, items.Sequence, items.ID, items.DetailID, item.TransCode, DateTime.Now);
  277. cmd.CommandText = ERPupdate;
  278. num = cmd.ExecuteNonQuery();
  279. }
  280. }
  281. #endregion
  282. }
  283. num = cmd.ExecuteNonQuery();
  284. if (num > 0)
  285. {
  286. cmd.Transaction.Commit();
  287. }
  288. else
  289. {
  290. cmd.Transaction.Rollback();
  291. }
  292. return num;
  293. }
  294. catch (Exception ex)
  295. {
  296. // cmd.Transaction.Rollback();
  297. log.Error(ex.Message);
  298. throw new Exception(ex.Message);
  299. }
  300. finally
  301. {
  302. if (conn.State == ConnectionState.Open)
  303. {
  304. conn.Close();
  305. }
  306. conn.Dispose();
  307. }
  308. }
  309. /// <summary>
  310. /// 删除委外数据(物理删除)
  311. /// </summary>
  312. /// <param name="model"></param>
  313. /// <returns></returns>
  314. public string Delete(NoticeDelete model)
  315. {
  316. string res = string.Empty;
  317. conn.Open();
  318. SqlTransaction sqlTran = conn.BeginTransaction();
  319. SqlCommand cmd = new SqlCommand();
  320. cmd.Transaction = sqlTran;
  321. cmd.Connection = conn;
  322. try
  323. {
  324. //委外送货详情表删除
  325. sql = @" DELETE FROM ICSOASNDetail
  326. WHERE OASNCode=(select OASNCode from ICSOASN where id='{0}') ";
  327. sqlInfo = @" DELETE FROM ICSOASN where 1=1 ";
  328. if (model.ID != null)
  329. {
  330. sqlInfo += " and id='{0}'";
  331. }
  332. if (model.MTime != null)
  333. {
  334. sqlInfo += " and MTIME='{1}'";
  335. }
  336. if (model.User != null)
  337. {
  338. sqlInfo += " and MUSER='{2}'";
  339. }
  340. sql = string.Format(sql, model.ID);
  341. sqlInfo = string.Format(sqlInfo, model.ID, model.MTime, model.User);
  342. DBHelper.CmdExecuteNonQuery(sql, cmd, "送货单:" + model.ID + "未查询到对应数据!");
  343. DBHelper.CmdExecuteNonQuery(sqlInfo, cmd, "送货单:" + model.ID + "未查询到对应数据!");
  344. cmd.Transaction.Commit();
  345. return res;
  346. }
  347. catch (Exception ex)
  348. {
  349. cmd.Transaction.Rollback();
  350. log.Error(ex.Message);
  351. throw new Exception(ex.Message);
  352. }
  353. finally
  354. {
  355. if (conn.State == ConnectionState.Open)
  356. {
  357. conn.Close();
  358. }
  359. conn.Dispose();
  360. }
  361. }
  362. #region 接口调用
  363. public static string HttpPost(string apiName, string url, string body)
  364. {
  365. try
  366. {
  367. Encoding encoding = Encoding.UTF8;
  368. System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
  369. request.Method = "POST";
  370. request.Accept = "application/json, text/javascript, */*"; //"text/html, application/xhtml+xml, */*";
  371. request.ContentType = "application/json; charset=utf-8";
  372. byte[] buffer = encoding.GetBytes(body);
  373. request.ContentLength = buffer.Length;
  374. request.GetRequestStream().Write(buffer, 0, buffer.Length);
  375. System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
  376. using (System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream(), encoding))
  377. {
  378. return reader.ReadToEnd();
  379. }
  380. }
  381. catch (System.Net.WebException ex)
  382. {
  383. throw new Exception(apiName + "调用失败," + ex.Message);
  384. }
  385. }
  386. #endregion
  387. }
  388. }