You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

288 lines
14 KiB

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 UpdateVendorInfo
{
public static Result Update(List<UpdateVendorEntity> list)
{
Result res = new Result();
try
{
string jsonstr = JsonConvert.SerializeObject(list);
Log.WriteLogFile(jsonstr, "更新供应商日志");
StringBuilder sb = new StringBuilder();//接口返回Message
foreach (UpdateVendorEntity 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("存在表头供应商编码为空!!!");
}
else
{
if (string.IsNullOrWhiteSpace(entity.WorkPoint))
{
throw new Exception("站点参数为空!!!");
}
string WorkPoint = ICSHelper.GetConnectStringTest(entity.WorkPoint);
if (WorkPoint == "NotExit")
{
throw new Exception("站点编码不存在!");
}
#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.Rows.Count < 1)
{
throw new Exception("供应商编码不存在!!!");
}
#endregion
#region 表头数据
string sql = @"Update {0}.dbo.Vendor Set ";
sql = string.Format(sql, WorkPoint);
if (!string.IsNullOrWhiteSpace(entity.cVenName))
{
sql += " cVenName='{0}',";
sql = string.Format(sql, entity.cVenName);
}
if (!string.IsNullOrWhiteSpace(entity.cVenAbbName))
{
sql += " cVenAbbName='{0}',";
sql = string.Format(sql, entity.cVenAbbName);
}
if (!string.IsNullOrWhiteSpace(entity.cVCCode))
{
sql += " cVCCode='{0}',";
sql = string.Format(sql, entity.cVCCode);
}
if (!string.IsNullOrWhiteSpace(entity.cVenHeadCode))
{
sql += " cVenHeadCode='{0}',";
sql = string.Format(sql, entity.cVenHeadCode);
}
if (!string.IsNullOrWhiteSpace(entity.cVenExch_name))
{
sql += " cVenExch_name='{0}',";
sql = string.Format(sql, entity.cVenExch_name);
}
if (entity.dVenDevDate!=null)
{
sql += " dVenDevDate='{0}',";
sql = string.Format(sql, entity.dVenDevDate);
}
if (entity.dStopDate!=null)
{
sql += " dEndDate='{0}',";
sql = string.Format(sql, entity.dStopDate);
}
if (entity.bVenTax != null)
{
sql += " bVenTax='{0}',";
sql = string.Format(sql, entity.bVenTax);
}
if (entity.iVenTaxRate != null)
{
sql += " iVenTaxRate='{0}',";
sql = string.Format(sql, entity.iVenTaxRate);
}
if (!string.IsNullOrWhiteSpace(entity.cVenPUOMProtocol))
{
sql += " cVenPUOMProtocol='{0}',";
sql = string.Format(sql, entity.cVenPUOMProtocol);
}
if (!string.IsNullOrWhiteSpace(entity.cVenIType))
{
sql += " cVenIType='{0}',";
sql = string.Format(sql, entity.cVenIType);
}
if (!string.IsNullOrWhiteSpace(entity.cDCCode))
{
sql += " cDCCode='{0}',";
sql = string.Format(sql, entity.cDCCode);
}
if (!string.IsNullOrWhiteSpace(entity.cModifyPerson))
{
sql += " cModifyPerson='{0}',";
sql = string.Format(sql, entity.cModifyPerson);
}
if (entity.dModifyDate != null)
{
sql += " dModifyDate='{0}',";
sql = string.Format(sql, entity.dModifyDate);
}
sql = sql.TrimEnd(',');
sql += " Where cVenCode ='{0}'";
sql = string.Format(sql, entity.cVenCode);
Log.WriteLogFile(sql, "更新供应商SQL日志");
if (!ICSHelper.ExecuteNonQuery(sql, cmd))
{
throw new Exception("更新供应商表头失败!");
}
#endregion
#region 表体数据
if (entity.list.Count > 0)
{
//验证是否有银行信息
string VenCheck = "Select * from {0}.dbo.VendorBank Where cVenCode ='{1}'";
VenCheck = string.Format(VenCheck, WorkPoint, entity.cVenCode);
DataTable dtVenCheck = ICSHelper.SQlReturnData(VenCheck, cmd);
if (dtVenCheck!=null&&dtVenCheck.Rows.Count>0)
{
string sqlDelete = "Delete {0}.dbo.VendorBank Where cVenCode ='{1}'";
sqlDelete = string.Format(sqlDelete, WorkPoint, entity.cVenCode);
Log.WriteLogFile(sqlDelete, "更新供应商SQL日志");
if (!ICSHelper.ExecuteNonQuery(sqlDelete, cmd))
{
throw new Exception("删除供应商表体失败!");
}
}
//验证表体中是否存在默认银行设置
List<UpdateVendorDetailEntity> 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!");
}
string sqlBankUpt = "Update {0}.dbo.Vendor Set cVenAccount='{1}',cVenBank ='{3}' Where cVenCode ='{2}'";
sqlBankUpt = string.Format(sqlBankUpt, WorkPoint, listBool[0].cAccountNum, entity.cVenCode, listBool[0].cBranch);
Log.WriteLogFile(sqlBankUpt, "更新供应商SQL日志");
if (!ICSHelper.ExecuteNonQuery(sqlBankUpt, cmd))
{
throw new Exception("更新供应商表头银行信息失败!");
}
string sqlDetail = "";
bool InserFlag = true;
foreach (UpdateVendorDetailEntity 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();
}
}
#endregion
else
{
cmd.Transaction.Commit();
}
}
}
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;
}
}
}
}