刘磊
2025-04-19 bd02cb3fa0fa75ffafb2cf17501929b1b1e0029b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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);
        }
    }
}