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 CreateOrDeleteASNInfo { 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 (CreateASNEntity 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.ID)) { throw new Exception("表头ID为空!!!"); } if (string.IsNullOrWhiteSpace(entity.VenCode)) { throw new Exception("供应商编码为空!!!"); } if (string.IsNullOrWhiteSpace(entity.Status)) { throw new Exception("状态为空!!!"); } if (string.IsNullOrWhiteSpace(entity.WorkPoint)) { throw new Exception("表头站点参数为空!!!"); } string WorkPoint = ICSHelper.GetConnectStringTest(entity.WorkPoint); if (WorkPoint == "NotExit") { throw new Exception("表头站点编码不存在!"); } //检查供应商编码是否存在 string Vensql = "Select * from ICSVendor where Vencode ='{0}' and workpoint ='{1}'"; Vensql = string.Format(Vensql, entity.VenCode, WorkPoint); DataTable dtVen = ICSHelper.SQlReturnData(Vensql, cmd); if (dtVen.Rows.Count < 1) { 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(); } else { //if (!string.IsNullOrWhiteSpace(entity.MUSER)) //{ // string sqlUser = "Select F_RealName from Sys_SRM_User where f_location='{0}' and F_Account='{1}'"; // sqlUser = string.Format(sqlUser, WorkPoint, entity.MUSER); // DataTable dtUser = ICSHelper.SQlReturnData(sqlUser, cmd); // if (dtUser != null && dtUser.Rows.Count > 0) // { // if (!string.IsNullOrWhiteSpace(entity.MUSERName)) // { // if (dtUser.Rows[0]["F_RealName"].ToString() != entity.MUSERName) // { // throw new Exception("表头操作人编码对应的人员名称与传入人员名称不一致!"); // } // else // { // entity.MUSERName = dtUser.Rows[0]["F_RealName"].ToString(); // } // } // } // else // { // throw new Exception("表头操作人不存在!"); // } //} //新增送货单表头 string sql = "Insert into ICSASN (ID,ASNCode,VenCode,Status,MUSER,MUSERName,WorkPoint,MTIME,EATTRIBUTE1)"; sql += " Values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}');"; sql = string.Format(sql, entity.ID, entity.ASNCode, entity.VenCode, entity.Status, entity.MUSER, entity.MUSERName, WorkPoint, entity.MTIME, entity.EATTRIBUTE1); Log.WriteLogFile(sql, "创建删除送货单SQL日志"); if (!ICSHelper.ExecuteNonQuery(sql, cmd)) { throw new Exception("新增送货单表头失败!"); } if (entity.list.Count > 0) { bool InserFlag = true; string sqldetail = ""; int m = 1; foreach (CreateASNDetailEntity detail in entity.list) { if (string.IsNullOrWhiteSpace(detail.ASNCode)) { InserFlag = false; sb.Append("存在表体送货单号为空,表头送货单号为:" + entity.ASNCode + "!!!"); break; } if (detail.ASNCode != entity.ASNCode) { InserFlag = false; sb.Append("存在表体送货单与表头送货单不同,表头送货单号为:" + entity.ASNCode + ",表体送货单号为:" + detail.ASNCode + "!!!"); break; } if (string.IsNullOrWhiteSpace(detail.ID)) { InserFlag = false; sb.Append("表体ID为空,送货单号为:" + detail.ASNCode + "!!!"); break; } //if (string.IsNullOrWhiteSpace(detail.Sequence)) //{ // InserFlag = false; // sb.Append("表体行号为空,送货单号为:" + detail.ASNCode + "!!!"); // break; //} if (string.IsNullOrWhiteSpace(detail.LotNo)) { InserFlag = false; sb.Append("表体条码为空,送货单号为:" + detail.ASNCode + "!!!"); break; } if (detail.Quantity == null) { InserFlag = false; sb.Append("表体数量为空,送货单号为:" + detail.ASNCode + "!!!"); break; } if (string.IsNullOrWhiteSpace(detail.WorkPoint)) { InserFlag = false; sb.Append("表体站点参数为空,送货单号为:" + detail.ASNCode + "!!!"); break; } string WorkPointdetail = ICSHelper.GetConnectStringTest(detail.WorkPoint); if (WorkPointdetail == "NotExit") { InserFlag = false; sb.Append("表体站点编码不存在,送货单号为:" + detail.ASNCode + "!!!"); break; } //if (!string.IsNullOrWhiteSpace(detail.MUSER)) //{ // string sqlUser = "Select F_RealName from Sys_SRM_User where f_location='{0}' and F_Account='{1}'"; // sqlUser = string.Format(sqlUser, WorkPointdetail, detail.MUSER); // DataTable dtUser = ICSHelper.SQlReturnData(sqlUser, cmd); // if (dtUser != null && dtUser.Rows.Count > 0) // { // if (!string.IsNullOrWhiteSpace(detail.MUSERName)) // { // if (dtUser.Rows[0]["F_RealName"].ToString() != detail.MUSERName) // { // InserFlag = false; // sb.Append("表体操作人编码对应的人员名称与传入人员名称不一致!,送货单号为:" + detail.ASNCode + "!!!"); // break; // } // else // { // detail.MUSERName = dtUser.Rows[0]["F_RealName"].ToString(); // } // } // } // else // { // InserFlag = false; // sb.Append("表体操作人不存在!,送货单号为:" + detail.ASNCode + "!!!"); // break; // } //} //检查条码是否存在 string sqlLot = "Select Lotno from ICSInventoryLot Where workpoint='{0}' and Lotno ='{1}'"; sqlLot = string.Format(sqlLot, WorkPointdetail, detail.LotNo); DataTable dtLot = ICSHelper.SQlReturnData(sqlLot, cmd); if (dtLot.Rows.Count < 1) { InserFlag = false; sb.Append("表体条码不存在,送货单号为:" + detail.ASNCode + ",条码为:" + detail.LotNo + "!!!"); break; } //新增送货单表体 sqldetail += " Insert into ICSASNDetail (ID,ASNCode,Sequence,LotNo,Quantity,Amount,DNQuantity,DNAmount,MUSER,MUSERName,WorkPoint,MTIME)"; sqldetail += " Values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}')"; sqldetail = string.Format(sqldetail, detail.ID, detail.ASNCode, m, detail.LotNo, detail.Quantity, detail.Amount, detail.DNQuantity, detail.DNAmount, detail.MUSER, detail.MUSERName, WorkPointdetail, detail.MTIME); m++; } if (InserFlag) { Log.WriteLogFile(sqldetail, "创建删除送货单SQL日志"); if (!ICSHelper.ExecuteNonQuery(sqldetail, cmd)) { throw new Exception("新增送货单表体失败!"); } cmd.Transaction.Commit(); } else { cmd.Transaction.Rollback(); } } else { cmd.Transaction.Rollback(); sb.Append("表体为空,表头送货单号为:" + entity.ASNCode + "!!!"); continue; } } } 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; } } } }