圣珀
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.

189 lines
8.9 KiB

2 years ago
  1. using ICS.WCF.Base;
  2. using ICSSoft.Entity;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Data.SqlClient;
  9. namespace ICS.MaterialOutBoxGet
  10. {
  11. public class GetMaterialOutBox
  12. {
  13. /// <summary>
  14. /// 获取物料条码信息生成条码
  15. /// </summary>
  16. public FormICSCreatedArrivalNoticeModel CustGetMaterialOutBox(List<MaterialOutBox> lstm)
  17. {
  18. FormICSCreatedArrivalNoticeModel result = new FormICSCreatedArrivalNoticeModel();
  19. try
  20. {
  21. using (SqlConnection conn = new SqlConnection(Appconfig.GetMESStr()))
  22. {
  23. conn.Open();
  24. using (SqlTransaction trans = conn.BeginTransaction())
  25. {
  26. try
  27. {
  28. SqlCommand cmd = new SqlCommand();
  29. cmd.Connection = conn;
  30. cmd.Transaction = trans;
  31. DataTable flag2 = null;
  32. StringBuilder str = new StringBuilder();
  33. StringBuilder resultlog = new StringBuilder();
  34. foreach (MaterialOutBox m in lstm)
  35. {
  36. string SqlCartonNO = @"select * from ICSCarton where CartonNO = '" + m.CartonNO + "' ";
  37. DataTable flag1 = SQlReturnData(SqlCartonNO, cmd);
  38. if (flag1.Rows.Count <= 0)
  39. {
  40. foreach (LotNOAll Lotno in m.LotNOList)
  41. {
  42. string SqlLotNO = @"select * from ICSItemLot where LotNO = '" + Lotno.LotNO + "' ";
  43. flag2 = SQlReturnData(SqlLotNO, cmd);
  44. if (flag2.Rows.Count == 0)
  45. {
  46. throw new Exception("内箱条码:" + Lotno.LotNO + @"不存在,请查看日志");
  47. }
  48. }
  49. foreach (LotNOAll Lotno in m.LotNOList)
  50. {
  51. string sql = string.Format(@"insert into ICSITEMLot2Carton (ID,CartonNO,INVCODE,LotNo_ID,LotNO,Qty,OrderNO,MUSER,MUSERName,MTIME,WorkPoint)
  52. select NEWID(),'{0}',a.ItemCODE,a.ID,'{1}',a.LOTQTY,a.ORDERNO,'{3}','{4}',GETDATE(),'{2}'
  53. from ICSITEMLot a
  54. where WorkPoint = '{2}' and a.LotNO='{1}'",
  55. m.CartonNO, Lotno.LotNO, m.WorkPoint, m.MUSER, m.MUSERName);
  56. cmd.CommandText = sql;
  57. int dt = cmd.ExecuteNonQuery();
  58. if (dt == 0)
  59. {
  60. throw new Exception("外箱信息添加失败,请查看日志");
  61. }
  62. }
  63. string sql2 = string.Format(@"insert into ICSCarton (Serial,CartonNO,MUSER,MUSERName,MTIME,WorkPoint)
  64. Values
  65. (NEWID(),'{0}','{1}','{2}',GETDATE(),'{3}')"
  66. , m.CartonNO, m.MUSER, m.MUSERName, m.WorkPoint);
  67. cmd.CommandText = sql2;
  68. int dt2 = cmd.ExecuteNonQuery();
  69. if (dt2 > 0)
  70. {
  71. result.Code = 0;
  72. result.ResMsg = "物料外箱条码创建成功";
  73. result.ResData = "{}";
  74. result.IsCompress = false;
  75. result.IsSuccess = true;
  76. }
  77. else
  78. {
  79. throw new Exception("外箱条码创建失败,失败sql:" + sql2 + "");
  80. }
  81. }
  82. else
  83. {
  84. str.AppendLine("外箱条码:" + m.CartonNO + "已存在!");
  85. str.AppendLine("包含内箱条码:");
  86. foreach (LotNOAll LotNo in m.LotNOList)
  87. {
  88. str.AppendLine(LotNo.LotNO);
  89. }
  90. str.AppendLine("------------------------------------");
  91. continue;
  92. }
  93. }
  94. trans.Commit();
  95. Appconfig.WriteLogFile(str.ToString(), "物料外箱信息接口日志(无效数据)");
  96. }
  97. catch (Exception ex)
  98. {
  99. StringBuilder str = new StringBuilder();
  100. //foreach (MaterialOutBox m in lstm)
  101. //{
  102. // result.Code = -1;
  103. // result.ResMsg = ex.Message;
  104. // result.ResData = "{}";
  105. // result.IsCompress = false;
  106. // result.IsSuccess = false;
  107. // str.AppendLine("物料外箱信息获取失败");
  108. // str.AppendLine("外箱条码:" + m.CartonNO);
  109. // foreach (LotNOAll LotNo in m.LotNOList)
  110. // {
  111. // str.AppendLine("内箱条码:" + LotNo.LotNO);
  112. // }
  113. // str.AppendLine("传入站点:" + m.WorkPoint);
  114. // str.AppendLine("失败原因:" + ex.Message);
  115. //}
  116. result.Code = -1;
  117. result.ResMsg = ex.Message;
  118. result.ResData = "{}";
  119. result.IsCompress = false;
  120. result.IsSuccess = false;
  121. str.AppendLine("物料外箱信息获取失败");
  122. str.AppendLine("失败原因:" + ex.Message);
  123. Appconfig.WriteLogFile(str.ToString(), "物料外箱信息接口日志");
  124. trans.Rollback();
  125. }
  126. }
  127. conn.Close();
  128. }
  129. }
  130. catch (Exception ex)
  131. {
  132. StringBuilder str = new StringBuilder();
  133. result.Code = -1;
  134. result.ResMsg = ex.Message;
  135. result.ResData = "{}";
  136. result.IsCompress = false;
  137. result.IsSuccess = false;
  138. str.AppendLine("物料外箱信息获取失败");
  139. str.AppendLine("失败原因:" + ex.Message);
  140. Appconfig.WriteLogFile(str.ToString(), "物料外箱信息接口日志");
  141. }
  142. if (result.Code == 0 && result.IsSuccess == false)
  143. {
  144. result.Code = 0;
  145. result.ResMsg = "数据已存在,无需新增";
  146. result.ResData = "{}";
  147. result.IsCompress = false;
  148. result.IsSuccess = true;
  149. }
  150. return result;
  151. }
  152. /// <summary>
  153. /// 返回值
  154. /// </summary>
  155. public class FormICSCreatedArrivalNoticeModel
  156. {
  157. //0 :正常数据,-1:失败。
  158. public int Code { get; set; }
  159. public string ResMsg { get; set; }
  160. public Object ResData { get; set; }
  161. public bool IsCompress { get; set; }
  162. public bool IsSuccess { get; set; }
  163. }
  164. /// <summary>
  165. /// SQL执行方法
  166. /// </summary>
  167. /// <param name="SQl"></param>
  168. /// <param name="cmd"></param>
  169. /// <returns></returns>
  170. public static DataTable SQlReturnData(string SQl, SqlCommand cmd)
  171. {
  172. DataTable dt = new DataTable();
  173. cmd.CommandText = SQl;
  174. SqlDataAdapter dr = new System.Data.SqlClient.SqlDataAdapter();
  175. dr.SelectCommand = cmd;
  176. dr.Fill(dt);
  177. return dt;
  178. }
  179. }
  180. }