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 CreateVendorInventoryPrice { public static Result Create(List list) { Result res = new Result(); try { string jsonstr = JsonConvert.SerializeObject(list); Log.WriteLogFile(jsonstr, "创建价格表日志"); StringBuilder sb = new StringBuilder();//接口返回Message foreach (CreateVendorInventoryPriceEntity entity in list) { SqlConnection conn = new SqlConnection(ICSHelper.ReadConfig(ICSHelper.FileNameCompanyCon)["ERP"].ToString()); conn.Open(); SqlTransaction sqlTran = conn.BeginTransaction(); SqlCommand cmd = new SqlCommand(); cmd.Transaction = sqlTran; cmd.Connection = conn; try { if (string.IsNullOrWhiteSpace(entity.cVenCode)) { throw new Exception("存在供应商编码为空!"); } if (string.IsNullOrWhiteSpace(entity.cInvCode)) { throw new Exception("物料编码为空!"); } if (entity.bTaxCost == null) { throw new Exception("价格标识为空!"); } if (entity.iSupplyType == null) { throw new Exception("供应类型为空!"); } int iSupplyType = 1; if (entity.iSupplyType == 0) { iSupplyType = 2; } if (entity.iSupplyType == 2) { iSupplyType = 4; } if (string.IsNullOrWhiteSpace(entity.cExch_Name)) { throw new Exception("币种为空!"); } if (string.IsNullOrWhiteSpace(entity.WorkPoint)) { throw new Exception("表头站点为空!"); } string WorkPoint = ICSHelper.GetConnectStringTest(entity.WorkPoint); if (WorkPoint == "NotExit") { throw new Exception("站点编码不存在!"); } //检查供应商编码是否存在 string sqlVenCheck = "select cVenCode from {0}.dbo.Vendor where cVenCode='{1}'"; sqlVenCheck = string.Format(sqlVenCheck, WorkPoint, entity.cVenCode); DataTable dtVenCheck = ICSHelper.SQlReturnData(sqlVenCheck, cmd); if (dtVenCheck.Rows.Count < 1) { throw new Exception("供应商编码不存在!"); } //检查物料编码是否存在 string sqlInvCheck = "select cInvCode from {0}.dbo.Inventory where cInvCode='{1}'"; sqlInvCheck = string.Format(sqlInvCheck, WorkPoint, entity.cInvCode); DataTable dtInvCheck = ICSHelper.SQlReturnData(sqlInvCheck, cmd); if (dtInvCheck.Rows.Count < 1) { throw new Exception("物料编码不存在!"); } if (entity.list.Count > 0) { List listDownCheck = entity.list.Where(x => x.iLowerLimit == null).ToList(); if (listDownCheck.Count > 0) { throw new Exception("表体数量下限为空"); } List listDown = entity.list.Select(x => x.iLowerLimit).Distinct().ToList(); if (listDown.Count != entity.list.Count) { throw new Exception("表体数量下限存在重复"); } foreach (CreateVendorInventoryPriceDetailEntity detail in entity.list) { if (detail.iUpperLimit != null) { if (detail.iUpperLimit <= detail.iLowerLimit) { throw new Exception("表体数量下限大于数量上限"); } } } List listDownOrder = entity.list.OrderBy(x => x.iLowerLimit).ToList(); float? UpLimit = null; foreach (CreateVendorInventoryPriceDetailEntity item in listDownOrder) { if (UpLimit != null) { if (item.iLowerLimit <= UpLimit) { throw new Exception("表体数量下限与数量上限存在交叉"); } } if (item.iUpperLimit != null) { UpLimit = item.iUpperLimit; } else { UpLimit = null; } if (item.iUnitPrice == null) { throw new Exception("表体无税单价为空"); } if (item.iTaxRate == null) { throw new Exception("表体税率为空"); } if (item.iTaxUnitPrice == null) { throw new Exception("表体含税单价为空"); } if (item.WorkPoint == null) { throw new Exception("表体站点为空"); } if (item.dEnableDate == null) { throw new Exception("表体生效日期为空!"); } if (item.dDisableDate == null) { throw new Exception("表体失效日期为空!"); } } string UpdateSQL = ""; //将上个版本全部失效 string sqlLastCheck = @"Select Autoid,dEnableDate,dDisableDate from {0}.dbo.Ven_Inv_Price Where cVenCode ='{1}' and cInvCode ='{2}' and iSupplyType ='{3}' and (dDisableDate>='{4}' or dDisableDate is null)"; sqlLastCheck = string.Format(sqlLastCheck, WorkPoint, entity.cVenCode, entity.cInvCode, iSupplyType, entity.list[0].dEnableDate); Log.WriteLogFile(sqlLastCheck, "创建价格表SQL日志"); DataTable dtNew = ICSHelper.SQlReturnData(sqlLastCheck, cmd); if (dtNew != null && dtNew.Rows.Count > 0) { //将上个版本的失效日期改为当前生效日期的前一天 foreach (DataRow dr in dtNew.Rows) { UpdateSQL += " Update {0}.dbo.Ven_Inv_Price Set dDisableDate ='{1}',dEnableDate ='{1}' Where Autoid ='{2}';"; UpdateSQL = string.Format(UpdateSQL, WorkPoint, Convert.ToDateTime(entity.list[0].dEnableDate).AddDays(-1), dr["Autoid"].ToString()); } } foreach (CreateVendorInventoryPriceDetailEntity detail in entity.list) { if (detail.iUpperLimit == null) { UpdateSQL += @" Insert into {0}.dbo.Ven_Inv_Price(cVenCode,cInvCode,btaxcost,iSupplyType,cExch_Name,dEnableDate,dDisableDate,iLowerLimit, iUpperLimit,iUnitPrice,iTaxRate,iTaxUnitPrice,bPromotion) Values ('{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}',null,'{9}','{10}','{11}',0);"; UpdateSQL = string.Format(UpdateSQL, WorkPoint, entity.cVenCode, entity.cInvCode, entity.bTaxCost, iSupplyType, entity.cExch_Name, detail.dEnableDate, detail.dDisableDate, detail.iLowerLimit, detail.iUnitPrice, detail.iTaxRate, detail.iTaxUnitPrice); } else { UpdateSQL += @" Insert into {0}.dbo.Ven_Inv_Price(cVenCode,cInvCode,btaxcost,iSupplyType,cExch_Name,dEnableDate,dDisableDate,iLowerLimit, iUpperLimit,iUnitPrice,iTaxRate,iTaxUnitPrice,bPromotion) Values ('{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}',0);"; UpdateSQL = string.Format(UpdateSQL, WorkPoint, entity.cVenCode, entity.cInvCode, entity.bTaxCost, iSupplyType, entity.cExch_Name, detail.dEnableDate, detail.dDisableDate, detail.iLowerLimit, detail.iUpperLimit, detail.iUnitPrice, detail.iTaxRate, detail.iTaxUnitPrice); } } //if (iSupplyType == 1) //{ // float? MaxiUnitPrice = entity.list.Select(x => x.iUnitPrice).Max(); // decimal CkUnitPrice = (decimal)entity.list.Select(x => x.iUnitPrice).Max(); // if (entity.cExch_Name != "人民币") // { // string Year = DateTime.Now.Year.ToString(); // string Month = DateTime.Now.Month.ToString(); // string sqlEx = "select nflat from {0}.dbo.exch where cexch_name ='" + entity.cExch_Name + "' and iYear ='" + Year + "' and iperiod='" + Month + "' and itype =2"; // sqlEx = string.Format(sqlEx, WorkPoint); // DataTable dtEx = ICSHelper.SQlReturnData(sqlEx, cmd); // if (dtEx == null || dtEx.Rows.Count < 1) // { // throw new Exception("币种:" + entity.cExch_Name + ",在年份:" + Year + ",月份:" + Month + ",未维护汇率关系!"); // } // MaxiUnitPrice = MaxiUnitPrice * float.Parse(dtEx.Rows[0]["nflat"].ToString()); // CkUnitPrice = decimal.Round(CkUnitPrice * decimal.Parse(dtEx.Rows[0]["nflat"].ToString()), 2); // } // else // { // CkUnitPrice = decimal.Round(CkUnitPrice, 2); // } // UpdateSQL += " Update {0}.dbo.Inventory set cVenCode ='{1}',iInvRCost ='{3}',iInvSPrice ='{4}' Where cInvCode ='{2}'"; // UpdateSQL = String.Format(UpdateSQL, WorkPoint, entity.cVenCode, entity.cInvCode, MaxiUnitPrice, CkUnitPrice); //} //else //{ // UpdateSQL += " Update {0}.dbo.Inventory set cVenCode ='{1}' Where cInvCode ='{2}'"; // UpdateSQL = String.Format(UpdateSQL, WorkPoint, entity.cVenCode, entity.cInvCode); //} UpdateSQL += " Update {0}.dbo.Inventory set cVenCode ='{1}' Where cInvCode ='{2}'"; UpdateSQL = String.Format(UpdateSQL, WorkPoint, entity.cVenCode, entity.cInvCode); Log.WriteLogFile(UpdateSQL, "创建价格表SQL日志"); if (!ICSHelper.ExecuteNonQuery(UpdateSQL, cmd)) { throw new Exception("新增供应商料品价格表失败!"); } cmd.Transaction.Commit(); } else { throw new Exception("表体为空"); } } catch (Exception ex) { cmd.Transaction.Rollback(); sb.Append("执行报错,供应商编码为:" + entity.cVenCode + ",物料编码为:" + entity.cInvCode + ",报错信息:" + 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; } } } }