using System.Reflection;
using DS.Module.Core;
using DS.Module.Core.Data;
using DS.Module.Core.Extensions;
using DS.Module.SqlSugar;
using DS.Module.UserModule;
using DS.WMS.Core.System.Dtos;
using DS.WMS.Core.System.Entity;
using DS.WMS.Core.System.Interface;
using Mapster;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using SysTenantLink = DS.WMS.Core.System.Entity.SysTenantLink;
namespace DS.WMS.Core.System.Method;
public class VersionService : IVersionService
{
private readonly IServiceProvider _serviceProvider;
private readonly ISqlSugarClient db;
private readonly IUser user;
private readonly ISaasDbService saasService;
///
///
///
///
public VersionService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
db = _serviceProvider.GetRequiredService();
user = _serviceProvider.GetRequiredService();
saasService = _serviceProvider.GetRequiredService();
}
///
/// 列表
///
///
///
public DataResult> GetListByPage(PageRequest request)
{
//序列化查询条件
var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition);
var data = db.Queryable()
.Where(whereList)
.Select().ToQueryPage(request.PageCondition);
return data;
}
///
/// 编辑
///
///
///
public DataResult EditVersion(VersionReq req)
{
if (req.Id == 0)
{
if (db.Queryable().Where(x => x.Version == req.Version).Any())
{
return DataResult.Failed("系统版本信息已存在!", MultiLanguageConst.VersionExist);
}
var data = req.Adapt();
var entity = db.Insertable(data).ExecuteReturnEntity();
return DataResult.Successed("添加成功!", entity.Id, MultiLanguageConst.DataCreateSuccess);
}
else
{
var info = db.Queryable().Where(x => x.Id == req.Id).First();
info = req.Adapt(info);
db.Updateable(info).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommand();
return DataResult.Successed("更新成功!", MultiLanguageConst.DataUpdateSuccess);
}
}
///
/// 详情
///
///
///
public DataResult GetVersionInfo(string id)
{
var data = db.Queryable()
.Where(a => a.Id == long.Parse(id))
.Select()
.First();
return DataResult.Success(data, MultiLanguageConst.DataQuerySuccess);
}
///
/// 更新租户表差异
///
///
public DataResult UpdateSaasTableInfo()
{
var path = AppDomain.CurrentDomain.RelativeSearchPath ?? AppDomain.CurrentDomain.BaseDirectory;
var referencedAssemblies = Directory.GetFiles(path, "DS.WMS.Core.dll").Select(Assembly.LoadFrom).ToArray();
var types = referencedAssemblies
.SelectMany(a => a.DefinedTypes)
.Select(type => type.AsType())
.Where(it => it.FullName.Contains("DS.WMS.Core.Code.Entity.")||it.FullName.Contains("DS.WMS.Core.Info.Entity.")||it.FullName.Contains("DS.WMS.Core.Op.Entity.")).ToArray();
var tenantList = db.Queryable().ToList();
foreach (var item in tenantList)
{
var tenantDb = saasService.GetBizDbScopeById(item.TenantId);
StaticConfig.CodeFirst_MySqlCollate = "utf8mb4_0900_ai_ci"; //较高版本支持
tenantDb.CodeFirst.InitTables(types);
}
return DataResult.Successed("更新租户表信息成功!", MultiLanguageConst.DataUpdateSuccess);
}
///
/// 执行版本更新
///
/// 版本信息的Id
///
public async Task ExecuteVersionUpdate(IdModel req)
{
var dbScope = (SqlSugarScope)db;
var version = await db.Queryable()
.FirstAsync(a => a.Id == long.Parse(req.Id) && a.Status == StatusEnum.Enable);
if (version.IsNull())
{
return await Task.FromResult(DataResult.Failed("系统版本信息已存在!", MultiLanguageConst.VersionExist));
}
if (version.Executed)
{
return await Task.FromResult(DataResult.Failed("系统版本更新已执行!", MultiLanguageConst.VersionExecuted));
}
try
{
//开启事务
await dbScope.Ado.BeginTranAsync();
if (version.MainContent.IsNotEmptyOrNull())
{
db.Ado.ExecuteCommandWithGo(version.MainContent);
}
if (version.TenantContent.IsNotEmptyOrNull())
{
var tenantList = await db.Queryable().ToListAsync();
foreach (var item in tenantList)
{
var tenantDb = saasService.GetBizDbScopeById(item.TenantId);
if (await db.Queryable()
.Where(x => x.TenantId == item.TenantId && x.VersionId == version.Id).AnyAsync())
{
continue;
}
else
{
tenantDb.Ado.ExecuteCommandWithGo(version.TenantContent);
var tenantVersion = new SysTenantVersion()
{
TenantId = item.TenantId,
VersionId = version.Id,
UpdateTime = DateTime.Now
};
await db.Insertable(tenantVersion).ExecuteCommandAsync();
}
}
}
version.Executed = true;
await db.Updateable(version).ExecuteCommandAsync();
await dbScope.Ado.CommitTranAsync();
return await Task.FromResult(DataResult.Successed("执行版本更新成功!", MultiLanguageConst.DataUpdateSuccess));
}
catch (Exception ex)
{
await dbScope.Ado.RollbackTranAsync();
return await Task.FromResult(DataResult.Failed("审批失败!" + ",请联系管理员!" + ex.ToString()));
}
}
///
/// 获取版本已更新的租户列表
///
///
public DataResult> GetTenantVersionUpdateList(PageRequest request)
{
//序列化查询条件
var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition);
var data = db.Queryable()
.LeftJoin((a,b)=>a.TenantId == b.Id)
.Where(whereList)
.Select().ToQueryPage(request.PageCondition);
return data;
}
}