using ICSSoft.ERPWMS.Entity; using Microsoft.Data.SqlClient; using Microsoft.IdentityModel.Protocols; using Newtonsoft.Json; using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ICSSoft.ERPWMS.SQL { public class CreateVendorInfo { 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 (CreateVendorEntity entity in entitylist) { 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("存在表头供应商编码为空!"); } else { //if (entity.dStopDate == null) //{ // throw new Exception("停用日期参数为空!"); //} if (string.IsNullOrWhiteSpace(entity.WorkPoint)) { throw new Exception("站点参数为空!"); } string WorkPoint = ICSHelper.GetConnectStringTest(entity.WorkPoint); if (WorkPoint == "NotExit") { throw new Exception("站点编码不存在!"); } string 停用日期 = "null"; if (entity.dStopDate!=null) { 停用日期 = "'" + entity.dStopDate + "'"; } #region 验证供应商编码是否重复 string sqlCheck = "Select cVenCode from {0}.dbo.Vendor where cVenCode ='{1}'"; sqlCheck = string.Format(sqlCheck, WorkPoint, entity.cVenCode); DataTable dtCheck = ICSHelper.SQlReturnData(sqlCheck, cmd); if (dtCheck != null && dtCheck.Rows.Count > 0) { throw new Exception("供应商编码已存在!"); } #endregion #region 表头数据 string sql = ""; if (string.IsNullOrWhiteSpace(entity.cDCCode) && !string.IsNullOrWhiteSpace(entity.cVCCode)) { sql = @"Insert into {16}.dbo.Vendor(cVenCode,cVenName,cVenAbbName,cVCCode,cVenRegCode,cVenHeadCode,fRegistFund,cVenExch_name,bVenTax,iVenTaxRate, cVenPUOMProtocol,cVenIType,cDCCode,dEndDate,cCreatePerson,dVenCreateDatetime,bVenHomeBranch,iVenGSPType,bVenAccPeriodMng,bLicenceDate, bBusinessDate,bProxyDate,bPassGMP,bVenCargo,bProxyForeign,bVenService,dVenDevDate) Values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}', '{8}','{9}','{10}','{11}',null,{13},'{14}','{15}',0,0,0,0,0,0,0,1,0,0,'{17}');"; } if (string.IsNullOrWhiteSpace(entity.cDCCode) && string.IsNullOrWhiteSpace(entity.cVCCode)) { sql = @"Insert into {16}.dbo.Vendor(cVenCode,cVenName,cVenAbbName,cVCCode,cVenRegCode,cVenHeadCode,fRegistFund,cVenExch_name,bVenTax,iVenTaxRate, cVenPUOMProtocol,cVenIType,cDCCode,dEndDate,cCreatePerson,dVenCreateDatetime,bVenHomeBranch,iVenGSPType,bVenAccPeriodMng,bLicenceDate, bBusinessDate,bProxyDate,bPassGMP,bVenCargo,bProxyForeign,bVenService,dVenDevDate) Values ('{0}','{1}','{2}',null,'{4}','{5}','{6}','{7}', '{8}','{9}','{10}','{11}',null,{13},'{14}','{15}',0,0,0,0,0,0,0,1,0,0,'{17}');"; } if (!string.IsNullOrWhiteSpace(entity.cDCCode) && string.IsNullOrWhiteSpace(entity.cVCCode)) { sql = @"Insert into {16}.dbo.Vendor(cVenCode,cVenName,cVenAbbName,cVCCode,cVenRegCode,cVenHeadCode,fRegistFund,cVenExch_name,bVenTax,iVenTaxRate, cVenPUOMProtocol,cVenIType,cDCCode,dEndDate,cCreatePerson,dVenCreateDatetime,bVenHomeBranch,iVenGSPType,bVenAccPeriodMng,bLicenceDate, bBusinessDate,bProxyDate,bPassGMP,bVenCargo,bProxyForeign,bVenService,dVenDevDate) Values ('{0}','{1}','{2}',null,'{4}','{5}','{6}','{7}', '{8}','{9}','{10}','{11}','{12}',{13},'{14}','{15}',0,0,0,0,0,0,0,1,0,0,'{17}');"; } if (!string.IsNullOrWhiteSpace(entity.cDCCode) && !string.IsNullOrWhiteSpace(entity.cVCCode)) { sql = @"Insert into {16}.dbo.Vendor(cVenCode,cVenName,cVenAbbName,cVCCode,cVenRegCode,cVenHeadCode,fRegistFund,cVenExch_name,bVenTax,iVenTaxRate, cVenPUOMProtocol,cVenIType,cDCCode,dEndDate,cCreatePerson,dVenCreateDatetime,bVenHomeBranch,iVenGSPType,bVenAccPeriodMng,bLicenceDate, bBusinessDate,bProxyDate,bPassGMP,bVenCargo,bProxyForeign,bVenService,dVenDevDate) Values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}', '{8}','{9}','{10}','{11}','{12}',{13},'{14}','{15}',0,0,0,0,0,0,0,1,0,0,'{17}');"; } sql = string.Format(sql, entity.cVenCode, entity.cVenName, entity.cVenAbbName, entity.cVCCode, entity.cVenRegCode, entity.cVenHeadCode, entity.fRegistFund, entity.cVenExch_name, entity.bVenTax, entity.iVenTaxRate, entity.cVenPUOMProtocol, entity.cVenIType, entity.cDCCode, 停用日期, entity.cCreatePerson, entity.dVenCreateDatetime, WorkPoint, entity.dVenCreateDatetime); Log.WriteLogFile(sql, "创建供应商SQL日志"); if (!ICSHelper.ExecuteNonQuery(sql, cmd)) { throw new Exception("新增供应商表头失败!"); } #endregion #region 表体数据 if (entity.list.Count > 0) { //验证表体中是否存在默认银行设置 List listBool = entity.list.Where(x => x.bDefault == true).ToList(); if (listBool.Count == 0) { throw new Exception("默认银行未设置!"); } //验证表体中默认银行设置是否大于1个 if (listBool.Count > 1) { throw new Exception("默认银行设置数大于1!"); } //获取表体最大ID //int Sort = 0; //string sqlID = "select max(ID) ID from {0}.dbo.VendorBank"; //sqlID = string.Format(sqlID, entity.WorkPoint); //DataTable dtID = U8Helper.SQlReturnData(sqlID, cmd); //if (dtID != null && dtID.Rows.Count > 0) //{ // Sort = Convert.ToInt32(dtID.Rows[0]["ID"].ToString()) + 1;//首条ID //} bool InserFlag = true; string sqlDetail = ""; foreach (CreateVendorDetailEntity detail in entity.list) { if (string.IsNullOrWhiteSpace(detail.cVenCode)) { InserFlag = false; sb.Append("存在表体供应商编码为空!!!"); break; } if (detail.cVenCode != entity.cVenCode) { InserFlag = false; sb.Append("表体供应商编码与表头不一致,表头供应商编码为:" + entity.cVenCode + ",表体供应商编码为:" + detail.cVenCode + "!!!"); break; } if (string.IsNullOrWhiteSpace(detail.cAccountNum)) { InserFlag = false; sb.Append("银行账号为空,供应商编码为:" + detail.cVenCode + "!!!"); break; } //验证所属银行 string sqlBank = "Select cbankcode from {0}.dbo.AA_Bank Where cbankname ='{1}'"; sqlBank = string.Format(sqlBank, WorkPoint, detail.cBank); DataTable dtBank = ICSHelper.SQlReturnData(sqlBank, cmd); if (dtBank!=null&&dtBank.Rows.Count>0) { detail.cBank = dtBank.Rows[0]["cbankcode"].ToString(); } else { InserFlag = false; sb.Append("所属银行不存在,供应商编码为:" + detail.cVenCode + "!!!"); break; } sqlDetail += " Insert into {0}.dbo.VendorBank(cVenCode,cBranch,cBank,cAccountNum,cAccountName,bDefault) Values ('{1}','{2}','{3}','{4}','{5}','{6}');"; sqlDetail = string.Format(sqlDetail, WorkPoint, detail.cVenCode, detail.cBranch, detail.cBank, detail.cAccountNum, detail.cAccountName, detail.bDefault); //Sort++; } 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.cVenCode + "!!!"); continue; } #endregion } } catch (Exception ex) { cmd.Transaction.Rollback(); sb.Append("执行报错,供应商编码为:" + entity.cVenCode + ",报错信息:" + 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; } } } }