| using SqlSugar; | 
| using System; | 
| using System.Collections.Generic; | 
| using System.Linq; | 
| using System.Text; | 
| using System.Threading.Tasks; | 
| using WIDESEA_Core; | 
| using WIDESEA_Core.AOP; | 
| using WIDESEA_Core.BaseRepository; | 
| using WIDESEA_Core.BaseServices; | 
| using WIDESEA_Core.Const; | 
| using WIDESEA_Core.Helper; | 
| using WIDESEA_Core.Seed; | 
| using WIDESEA_IRepository; | 
| using WIDESEA_IServices; | 
| using WIDESEA_Model.Models; | 
|   | 
| namespace WIDESEA_Services | 
| { | 
|     public class Sys_TenantService : ServiceBase<Sys_Tenant, ISys_TenantRepository>, ISys_TenantService | 
|     { | 
|         private IUnitOfWorkManage _unitOfWorkManage; | 
|         public Sys_TenantService(ISys_TenantRepository BaseDal, IUnitOfWorkManage unitOfWorkManage) : base(BaseDal) | 
|         { | 
|             _unitOfWorkManage = unitOfWorkManage; | 
|         } | 
|   | 
|         public WebResponseContent InitTenantInfo(string tenantName, int tenantType) | 
|         { | 
|             WebResponseContent content = new WebResponseContent(); | 
|             try | 
|             { | 
|                 Sys_Tenant tenant = new Sys_Tenant() | 
|                 { | 
|                     ConnectionString = TenantConst.DBConStr, | 
|                     DbType = 1, | 
|                     Status = 1, | 
|                     TenantName = tenantName, | 
|                     TenantType = tenantType, | 
|                     CreateDate = DateTime.Now, | 
|                     Creater = "1111" | 
|                 }; | 
|   | 
|                 int tenantId = BaseDal.Db.Insertable(tenant).ExecuteReturnIdentity(); | 
|   | 
|                 tenant = BaseDal.QueryFirst(x => x.TenantId == tenantId); | 
|   | 
|                 tenant.ConnectionString = string.Format(TenantConst.DBConStr, ".", "Tenant_WMSDB_" + tenantId, "sa", "P@ssw0rd").EncryptDES(AppSecret.DB); | 
|   | 
|                 BaseDal.UpdateData(tenant); | 
|   | 
|                 InitTenantDb(tenant); | 
|                 content = WebResponseContent.Instance.OK(); | 
|             } | 
|             catch (Exception ex) | 
|             { | 
|                 content = WebResponseContent.Instance.Error(ex.Message); | 
|             } | 
|             return content; | 
|         } | 
|   | 
|         public void InitTenantDb(Sys_Tenant tenant) | 
|         { | 
|             string conStr = tenant.ConnectionString.DecryptDES(AppSecret.DB); | 
|   | 
|             ConnectionConfig connectionConfig = new ConnectionConfig() | 
|             { | 
|                 ConfigId = tenant.TenantId, | 
|                 ConnectionString = conStr, | 
|                 DbType = (DbType)tenant.DbType, | 
|                 IsAutoCloseConnection = true, | 
|                 AopEvents = new AopEvents() | 
|                 { | 
|                     DataExecuting = SqlSugarAop.DataExecuting | 
|                 } | 
|             }; | 
|             DBSeed.InitTenantSeedAsync(Db.AsTenant(), connectionConfig); | 
|         } | 
|     } | 
| } |