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 DeleteASNInfo { public static Result Create(List entityList) { Result res = new Result(); try { string jsonstr = JsonConvert.SerializeObject(entityList); Log.WriteLogFile(jsonstr, "删除送货单日志"); StringBuilder sb = new StringBuilder();//接口返回Message foreach (DeleteASNInfoEntity 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.ASNCode)) { throw new Exception("存在送货单号为空!!!"); } if (string.IsNullOrWhiteSpace(entity.WorkPoint)) { throw new Exception("表头站点参数为空!!!"); } string WorkPoint = ICSHelper.GetConnectStringTest(entity.WorkPoint); if (WorkPoint == "NotExit") { throw new Exception("表头站点编码不存在!"); } //查询送货单 string sqlCheck = "Select ASNCode from ICSASN where workpoint='{0}' and ASNCode='{1}'"; sqlCheck = string.Format(sqlCheck, WorkPoint, entity.ASNCode); DataTable dtCheck = ICSHelper.SQlReturnData(sqlCheck, cmd); if (dtCheck != null && dtCheck.Rows.Count > 0) { //检查送货单是否绑定到货单 string sqlDN = "Select ASNCode from ICSDeliveryNotice Where workpoint='{0}' and ASNCode ='{1}'"; sqlDN = string.Format(sqlDN, WorkPoint, entity.ASNCode); DataTable dtDN = ICSHelper.SQlReturnData(sqlDN, cmd); if (dtDN != null && dtDN.Rows.Count > 0) { throw new Exception("送货单已绑定到货单,不能删除!"); } //删除送货单表头 string DeleteSql = "Delete ICSASN Where workpoint='{0}' and ASNCode ='{1}';"; DeleteSql = string.Format(DeleteSql, WorkPoint, entity.ASNCode); Log.WriteLogFile(DeleteSql, "删除送货单SQL日志"); if (!ICSHelper.ExecuteNonQuery(DeleteSql, cmd)) { throw new Exception("删除送货单表表头失败!"); } //删除送货单表体 string CheckDetailSql = "Select LotNO,workpoint from ICSASNDetail Where workpoint='{0}' and ASNCode ='{1}'"; CheckDetailSql = string.Format(CheckDetailSql, WorkPoint, entity.ASNCode); DataTable dtCheckDetail = ICSHelper.SQlReturnData(CheckDetailSql, cmd); if (dtCheckDetail != null && dtCheckDetail.Rows.Count > 0) { DeleteSql = "Delete ICSASNDetail Where workpoint='{0}' and ASNCode ='{1}';"; DeleteSql = string.Format(DeleteSql, WorkPoint, entity.ASNCode); Log.WriteLogFile(DeleteSql, "删除送货单SQL日志"); if (!ICSHelper.ExecuteNonQuery(DeleteSql, cmd)) { throw new Exception("删除送货单表表体失败!"); } foreach (DataRow dr in dtCheckDetail.Rows) { ////删除自由项表 //string sqlExten = "Select ExtensionID from ICSInventoryLot where workpoint='{0}' and LotNo = '{1}'"; //sqlExten = string.Format(sqlExten, WorkPoint, dr["LotNO"].ToString()); //DataTable dtExtension = ICSHelper.SQlReturnData(sqlExten, cmd); //if (dtExtension != null && dtExtension.Rows.Count > 0) //{ // string Extensql = "Select ID From ICSExtension where workpoint='{0}' and ID = '{1}'"; // Extensql = string.Format(Extensql, WorkPoint, dtExtension.Rows[0]["ExtensionID"].ToString()); // DataTable dtNew = ICSHelper.SQlReturnData(Extensql, cmd); // if (dtNew != null && dtNew.Rows.Count > 0) // { // DeleteSql = "Delete ICSExtension where workpoint='{0}' and ID = '{1}'"; // DeleteSql = string.Format(DeleteSql, WorkPoint, dtExtension.Rows[0]["ExtensionID"].ToString()); // Log.WriteLogFile(DeleteSql, "删除送货单SQL日志"); // if (!ICSHelper.ExecuteNonQuery(DeleteSql, cmd)) // { // throw new Exception("删除自由项表失败!"); // } // } //} //删除条码表表头 DeleteSql = "Delete ICSInventoryLot where workpoint='{0}' and LotNo = '{1}'"; DeleteSql = string.Format(DeleteSql, WorkPoint, dr["LotNO"].ToString()); Log.WriteLogFile(DeleteSql, "删除送货单SQL日志"); if (!ICSHelper.ExecuteNonQuery(DeleteSql, cmd)) { throw new Exception("删除条码表表头失败!"); } //删除条码表表体 DeleteSql = "Delete ICSInventoryLotDetail where workpoint='{0}' and LotNo = '{1}'"; DeleteSql = string.Format(DeleteSql, WorkPoint, dr["LotNO"].ToString()); Log.WriteLogFile(DeleteSql, "删除送货单SQL日志"); if (!ICSHelper.ExecuteNonQuery(DeleteSql, cmd)) { throw new Exception("删除条码表表体失败!"); } } } cmd.Transaction.Commit(); } } catch (Exception ex) { cmd.Transaction.Rollback(); sb.Append("执行报错,送货单号为:" + entity.ASNCode + ",报错信息:" + 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; } } } }