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.

135 lines
6.1 KiB

6 months ago
  1. using ICSSoft.ERPWMS.Entity;
  2. using Microsoft.Data.SqlClient;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace ICSSoft.ERPWMS.SQL
  11. {
  12. public class DeleteInventoryLot
  13. {
  14. public static Result Delete(List<DeleteInventoryEntity> entityList)
  15. {
  16. Result res = new Result();
  17. try
  18. {
  19. string jsonstr = JsonConvert.SerializeObject(entityList);
  20. Log.WriteLogFile(jsonstr, "删除条码日志");
  21. StringBuilder sb = new StringBuilder();//接口返回Message
  22. foreach (DeleteInventoryEntity entity in entityList)
  23. {
  24. SqlConnection conn = new SqlConnection(ICSHelper.ReadConfig(ICSHelper.FileNameCompanyCon)["WMS"].ToString());
  25. conn.Open();
  26. SqlTransaction sqlTran = conn.BeginTransaction();
  27. SqlCommand cmd = new SqlCommand();
  28. cmd.Transaction = sqlTran;
  29. cmd.Connection = conn;
  30. try
  31. {
  32. if (string.IsNullOrWhiteSpace(entity.LotNo))
  33. {
  34. throw new Exception("存在物料条码为空!");
  35. }
  36. if (string.IsNullOrWhiteSpace(entity.WorkPoint))
  37. {
  38. throw new Exception("站点为空!");
  39. }
  40. string WorkPoint = ICSHelper.GetConnectStringTest(entity.WorkPoint);
  41. if (WorkPoint == "NotExit")
  42. {
  43. throw new Exception("站点编码不存在!");
  44. }
  45. string sqlLot = "Select LotNo,ExtensionID from ICSInventoryLot where workpoint='{0}' and LotNo='{1}'";
  46. sqlLot = string.Format(sqlLot, WorkPoint, entity.LotNo);
  47. DataTable dtLot = ICSHelper.SQlReturnData(sqlLot, cmd);
  48. if (dtLot != null && dtLot.Rows.Count > 0)
  49. {
  50. //检查条码是否绑定送货单,若绑定则不能删除
  51. string sqlASN = "Select LotNo from ICSASNDetail Where workpoint='{0}' and LotNo ='{1}'";
  52. sqlASN = string.Format(sqlASN, WorkPoint, entity.LotNo);
  53. DataTable dtASN = ICSHelper.SQlReturnData(sqlASN, cmd);
  54. if (dtASN != null && dtASN.Rows.Count > 0)
  55. {
  56. throw new Exception("条码已绑定送货单,不能删除!");
  57. }
  58. //删除条码表表头
  59. string sqlLotDelete = "Delete ICSInventoryLot where workpoint='{0}' and LotNo='{1}'";
  60. sqlLotDelete = string.Format(sqlLotDelete, WorkPoint, dtLot.Rows[0]["LotNo"].ToString());
  61. Log.WriteLogFile(sqlLotDelete, "删除条码SQL日志");
  62. if (!ICSHelper.ExecuteNonQuery(sqlLotDelete, cmd))
  63. {
  64. throw new Exception("删除条码表表头失败!");
  65. }
  66. //删除条码表表体
  67. string sqlLotDetailDelete = "Delete ICSInventoryLotDetail where workpoint='{0}' and LotNo='{1}'";
  68. sqlLotDetailDelete = string.Format(sqlLotDetailDelete, WorkPoint, dtLot.Rows[0]["LotNo"].ToString());
  69. Log.WriteLogFile(sqlLotDetailDelete, "删除条码SQL日志");
  70. if (!ICSHelper.ExecuteNonQuery(sqlLotDetailDelete, cmd))
  71. {
  72. throw new Exception("删除条码表表体失败!");
  73. }
  74. //删除自由项表
  75. //string sqlCheck = "Select * from ICSInventoryLot where ExtensionID ='" + dtLot.Rows[0]["ExtensionID"].ToString() + "'";
  76. //DataTable dtCheck = ICSHelper.SQlReturnData(sqlCheck, cmd);
  77. //if (dtCheck.Rows.Count<1)
  78. //{
  79. // string sqlFreeDelete = "Delete ICSExtension where workpoint='{0}' and ID ='{1}'";
  80. // sqlFreeDelete = string.Format(sqlFreeDelete, WorkPoint, dtLot.Rows[0]["ExtensionID"].ToString());
  81. // Log.WriteLogFile(sqlFreeDelete, "删除条码SQL日志");
  82. // if (!ICSHelper.ExecuteNonQuery(sqlFreeDelete, cmd))
  83. // {
  84. // throw new Exception("删除自由项表失败!");
  85. // }
  86. //}
  87. }
  88. cmd.Transaction.Commit();
  89. }
  90. catch (Exception ex)
  91. {
  92. cmd.Transaction.Rollback();
  93. sb.Append("执行报错,条码为:" + entity.LotNo + ",报错信息:" + ex.Message + "!!!");
  94. continue;
  95. }
  96. finally
  97. {
  98. if (conn.State == ConnectionState.Open)
  99. {
  100. conn.Close();
  101. }
  102. conn.Dispose();
  103. }
  104. }
  105. if (sb.Length > 0)
  106. {
  107. res.IsSuccess = false;
  108. res.Message = sb.ToString();
  109. }
  110. else
  111. {
  112. res.IsSuccess = true;
  113. res.Message = "执行成功!";
  114. }
  115. return res;
  116. }
  117. catch (Exception ex)
  118. {
  119. res.IsSuccess = false;
  120. res.Message = ex.Message;
  121. return res;
  122. }
  123. }
  124. }
  125. }