using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.AOP;
using WIDESEAWCS_Core.BaseRepository;
using WIDESEAWCS_Core.BaseServices;
using WIDESEAWCS_Core.Const;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_Core.Seed;
using WIDESEAWCS_ISystemRepository;
using WIDESEAWCS_ISystemServices;
using WIDESEAWCS_Model.Models;
namespace WIDESEAWCS_SystemServices
{
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);
}
}
}