using ICSSoft.ERPWMS.Entity; using Microsoft.Data.SqlClient; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ICSSoft.ERPWMS.SQL { public class DeleteInventoryLot { public static Result Delete(List entityList) { Result res = new Result(); try { string jsonstr = JsonConvert.SerializeObject(entityList); Log.WriteLogFile(jsonstr, "删除条码日志"); StringBuilder sb = new StringBuilder();//接口返回Message foreach (DeleteInventoryEntity entity in entityList) { SqlConnection conn = new SqlConnection(ICSHelper.ReadConfig(ICSHelper.FileNameCompanyCon)["WMS"].ToString()); conn.Open(); SqlTransaction sqlTran = conn.BeginTransaction(); SqlCommand cmd = new SqlCommand(); cmd.Transaction = sqlTran; cmd.Connection = conn; try { if (string.IsNullOrWhiteSpace(entity.LotNo)) { throw new Exception("存在物料条码为空!"); } if (string.IsNullOrWhiteSpace(entity.WorkPoint)) { throw new Exception("站点为空!"); } string WorkPoint = ICSHelper.GetConnectStringTest(entity.WorkPoint); if (WorkPoint == "NotExit") { throw new Exception("站点编码不存在!"); } string sqlLot = "Select LotNo,ExtensionID from ICSInventoryLot where workpoint='{0}' and LotNo='{1}'"; sqlLot = string.Format(sqlLot, WorkPoint, entity.LotNo); DataTable dtLot = ICSHelper.SQlReturnData(sqlLot, cmd); if (dtLot != null && dtLot.Rows.Count > 0) { //检查条码是否绑定送货单,若绑定则不能删除 string sqlASN = "Select LotNo from ICSASNDetail Where workpoint='{0}' and LotNo ='{1}'"; sqlASN = string.Format(sqlASN, WorkPoint, entity.LotNo); DataTable dtASN = ICSHelper.SQlReturnData(sqlASN, cmd); if (dtASN != null && dtASN.Rows.Count > 0) { throw new Exception("条码已绑定送货单,不能删除!"); } //删除条码表表头 string sqlLotDelete = "Delete ICSInventoryLot where workpoint='{0}' and LotNo='{1}'"; sqlLotDelete = string.Format(sqlLotDelete, WorkPoint, dtLot.Rows[0]["LotNo"].ToString()); Log.WriteLogFile(sqlLotDelete, "删除条码SQL日志"); if (!ICSHelper.ExecuteNonQuery(sqlLotDelete, cmd)) { throw new Exception("删除条码表表头失败!"); } //删除条码表表体 string sqlLotDetailDelete = "Delete ICSInventoryLotDetail where workpoint='{0}' and LotNo='{1}'"; sqlLotDetailDelete = string.Format(sqlLotDetailDelete, WorkPoint, dtLot.Rows[0]["LotNo"].ToString()); Log.WriteLogFile(sqlLotDetailDelete, "删除条码SQL日志"); if (!ICSHelper.ExecuteNonQuery(sqlLotDetailDelete, cmd)) { throw new Exception("删除条码表表体失败!"); } //删除自由项表 //string sqlCheck = "Select * from ICSInventoryLot where ExtensionID ='" + dtLot.Rows[0]["ExtensionID"].ToString() + "'"; //DataTable dtCheck = ICSHelper.SQlReturnData(sqlCheck, cmd); //if (dtCheck.Rows.Count<1) //{ // string sqlFreeDelete = "Delete ICSExtension where workpoint='{0}' and ID ='{1}'"; // sqlFreeDelete = string.Format(sqlFreeDelete, WorkPoint, dtLot.Rows[0]["ExtensionID"].ToString()); // Log.WriteLogFile(sqlFreeDelete, "删除条码SQL日志"); // if (!ICSHelper.ExecuteNonQuery(sqlFreeDelete, cmd)) // { // throw new Exception("删除自由项表失败!"); // } //} } cmd.Transaction.Commit(); } catch (Exception ex) { cmd.Transaction.Rollback(); sb.Append("执行报错,条码为:" + entity.LotNo + ",报错信息:" + ex.Message + "!!!"); continue; } finally { if (conn.State == ConnectionState.Open) { conn.Close(); } conn.Dispose(); } } if (sb.Length > 0) { res.IsSuccess = false; res.Message = sb.ToString(); } else { res.IsSuccess = true; res.Message = "执行成功!"; } return res; } catch (Exception ex) { res.IsSuccess = false; res.Message = ex.Message; return res; } } } }