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.
 
 
 
 
 

215 lines
8.0 KiB

using System;
using System.Data.Entity;
using System.Data.Entity.Core;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using CK.SCP.Models.AppBoxEntity;
using CK.SCP.Models.Enums;
using CK.SCP.Models.UniApiEntity;
using ChangKeTec.Wms.Models.Wms;
using System.ComponentModel;
using CK.SCP.Utils;
namespace CK.SCP.Models
{
public class LogModel : INotifyPropertyChanged
{
public string LogTime { get; set; }
public string LogMessage { get; set; }
public string TaskState { get; set; }
public string TableName { get; set; }
public string TaskId { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
public void NotityPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public static class EntitiesFactory
{
static EntitiesFactory()
{
Database.SetInitializer<ScpEntities>(null);
Database.SetInitializer<WmsEntities>(null);
Database.SetInitializer<UniApiEntities>(null);
Database.SetInitializer<DataCenterContext>(null);
Database.SetInitializer<ExchangeCenterContext>(null);
}
public static ScpEntities CreateScpInstance()
{
try
{
// var strConn = setting == null ? GetEfConnectionString("Wms") : GetEfConnectionString(setting);
var strConn = GetEfConnectionString(GlobalConfig.ScpDatabase);
var db = new ScpEntities(strConn);
return db;
}
catch (SqlException ex)
{
throw new Exception($"系统无法连接到数据库:{GlobalConfig.ScpDatabase},请检查配置的服务器,数据库,用户名和密码等信息是否正确。" + Environment.NewLine + ex);
}
}
public static UniApiEntities CreateUniApiInstance()
{
try
{
// var strConn = setting == null ? GetEfConnectionString("UniApi") : GetEfConnectionString(setting);
var strConn = GetEfConnectionString(GlobalConfig.UniApiDatabase);
var db = new UniApiEntities(strConn);
return db;
}
catch (SqlException ex)
{
throw new Exception($"系统无法连接到数据库:{GlobalConfig.UniApiDatabase},请检查配置的服务器,数据库,用户名和密码等信息是否正确。" + Environment.NewLine + ex);
}
}
public static AppBoxContext CreateAppBoxInstance()
{
try
{
// var strConn = setting == null ? GetEfConnectionString("UniApi") : GetEfConnectionString(setting);
var strConn = GetEfConnectionString(GlobalConfig.AppBoxDatabase);
var db = new AppBoxContext(strConn);
return db;
}
catch (SqlException ex)
{
throw new Exception($"系统无法连接到数据库:{GlobalConfig.AppBoxDatabase},请检查配置的服务器,数据库,用户名和密码等信息是否正确。" + Environment.NewLine + ex);
}
}
public static WmsEntities CreateWmsInstance()
{
try
{
// var strConn = setting == null ? GetEfConnectionString("Wms") : GetEfConnectionString(setting);
var strConn = GetEfConnectionString(GlobalConfig.WmsDatabase);
var db = new WmsEntities(strConn);
return db;
}
catch (SqlException ex)
{
throw new Exception($"系统无法连接到数据库:{GlobalConfig.WmsDatabase},请检查配置的服务器,数据库,用户名和密码等信息是否正确。" + Environment.NewLine + ex);
}
}
public static DataCenterContext CreateDataCenterInstance()
{
var dbSetting = GlobalConfig.DataCenterDB;
try
{
DataCenterContext db;
var strConn = GetEfConnectionString(dbSetting);
db = new DataCenterContext(strConn);
return db;
}
catch (SqlException ex)
{
throw new Exception($"系统无法连接到数据库:{dbSetting},请检查配置的服务器,数据库,用户名和密码等信息是否正确。{Environment.NewLine}{ex}");
}
}
public static ExchangeCenterContext CreateExchangeCenterInstance()
{
var dbSetting = GlobalConfig.ExchangeCenterDB;
try
{
ExchangeCenterContext db;
var strConn = GetEfConnectionString(dbSetting);
db = new ExchangeCenterContext(strConn);
return db;
}
catch (SqlException ex)
{
throw new Exception($"系统无法连接到数据库:{dbSetting},请检查配置的服务器,数据库,用户名和密码等信息是否正确。{Environment.NewLine}{ex}");
}
}
public static string GetEfConnectionString(DbSetting dbSetting)
{
var sbConn = new StringBuilder();
if (string.IsNullOrEmpty(dbSetting.))
{
throw new Exception("配置文件错误,请检查");
}
switch (dbSetting.)
{
case "SQLServer":
sbConn.Append($"Data source ={dbSetting.服务器地址}");
sbConn.Append(dbSetting. == "0" ? ";" : $",{dbSetting.端口};");
sbConn.Append($"Initial catalog = {dbSetting.数据库名称};");
sbConn.Append($"User id = {dbSetting.用户名};");
sbConn.Append($"Password = {EncryptHelper.Decrypt(dbSetting.密码)};");
sbConn.Append("MultipleActiveResultSets = True;");
sbConn.Append("persist security info = True;");
sbConn.Append("App = EntityFramework;");
break;
case "MySql":
sbConn.Append($"server ={dbSetting.服务器地址};");
sbConn.Append($"port ={dbSetting.端口};");
sbConn.Append($"database = {dbSetting.数据库名称};");
sbConn.Append($"user id = {dbSetting.用户名};");
sbConn.Append($"password = {EncryptHelper.Decrypt(dbSetting.密码)};");
sbConn.Append("persistsecurityinfo =True;");
break;
}
// LogHelper.Write(sbConn.ToString());
return sbConn.ToString();
}
public static void SaveDb(DbContext db)
{
try
{
db.BulkSaveChanges();
}
catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)//捕获实体验证异常
{
var sb = new StringBuilder();
foreach (var error in dbEx.EntityValidationErrors.ToList())
{
error.ValidationErrors.ToList().ForEach(i =>
{
sb.AppendFormat("表:{0},字段:{1},信息:{2}\r\n", error.Entry.Entity.GetType().Name, i.PropertyName, i.ErrorMessage);
});
}
throw new ScpException(ResultCode.DbEntityValidationException, sb.ToString(), "字段验证失败" + sb.ToString());
}
catch (OptimisticConcurrencyException ex)//并发冲突异常
{
throw new ScpException(ResultCode.Exception, "9999", ex.ToString());
}
catch (ScpException ex)
{
if (ex.InnerException != null && ex.InnerException.GetType() == typeof(UpdateException))
{
var inner = (UpdateException)ex.InnerException;
throw new ScpException(ResultCode.Exception, "0000", ex.ToString());
}
else
{
if (ex.InnerException != null) throw ex.InnerException;
}
}
}
}
}