You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

206 lines
7.3 KiB
C#

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.Invoice.Dtos;
using DS.WMS.Core.Sys.Dtos;
using DS.WMS.Core.Sys.Entity;
using DS.WMS.Core.Sys.Interface;
using Mapster;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using SysTenantLink = DS.WMS.Core.Sys.Entity.SysTenantLink;
namespace DS.WMS.Core.Sys.Method;
public class VersionService : IVersionService
{
private readonly IServiceProvider _serviceProvider;
private readonly ISqlSugarClient db;
private readonly IUser user;
private readonly ISaasDbService saasService;
/// <summary>
///
/// </summary>
/// <param name="serviceProvider"></param>
public VersionService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
db = _serviceProvider.GetRequiredService<ISqlSugarClient>();
user = _serviceProvider.GetRequiredService<IUser>();
saasService = _serviceProvider.GetRequiredService<ISaasDbService>();
}
/// <summary>
/// 列表
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public DataResult<List<VersionRes>> GetListByPage(PageRequest request)
{
//序列化查询条件
var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition);
var data = db.Queryable<SysVersion>()
.Where(whereList)
.Select<VersionRes>().ToQueryPage(request.PageCondition);
return data;
}
/// <summary>
/// 编辑
/// </summary>
/// <param name="req"></param>
/// <returns></returns>
public DataResult EditVersion(VersionReq req)
{
if (req.Id == 0)
{
if (db.Queryable<SysVersion>().Where(x => x.Version == req.Version).Any())
{
return DataResult.Failed("系统版本信息已存在!", MultiLanguageConst.VersionExist);
}
var data = req.Adapt<SysVersion>();
var entity = db.Insertable(data).ExecuteReturnEntity();
return DataResult.Successed("添加成功!", entity.Id, MultiLanguageConst.DataCreateSuccess);
}
else
{
var info = db.Queryable<SysVersion>().Where(x => x.Id == req.Id).First();
info = req.Adapt(info);
db.Updateable(info).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommand();
return DataResult.Successed("更新成功!", MultiLanguageConst.DataUpdateSuccess);
}
}
/// <summary>
/// 详情
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public DataResult<VersionRes> GetVersionInfo(string id)
{
var data = db.Queryable<SysVersion>()
.Where(a => a.Id == long.Parse(id))
.Select<VersionRes>()
.First();
return DataResult<VersionRes>.Success(data, MultiLanguageConst.DataQuerySuccess);
}
/// <summary>
/// 更新租户表差异
/// </summary>
/// <returns></returns>
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<SysTenantLink>().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);
}
/// <summary>
/// 执行版本更新
/// </summary>
/// <param name="req">版本信息的Id</param>
/// <returns></returns>
public async Task<DataResult> ExecuteVersionUpdate(IdModel req)
{
var dbScope = (SqlSugarScope)db;
var version = await db.Queryable<SysVersion>()
.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<SysTenantLink>().ToListAsync();
foreach (var item in tenantList)
{
var tenantDb = saasService.GetBizDbScopeById(item.TenantId);
if (await db.Queryable<SysTenantVersion>()
.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();
await ex.LogAsync(db);
return await Task.FromResult(DataResult.Failed("审批失败!" + ",请联系管理员!"));
}
}
/// <summary/>
/// 获取版本已更新的租户列表
/// <param name="request"></param>
/// <returns></returns>
public DataResult<List<TenantVersionRes>> GetTenantVersionUpdateList(PageRequest request)
{
//序列化查询条件
var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition);
var data = db.Queryable<SysTenantVersion>()
.LeftJoin<SysTenant>((a,b)=>a.TenantId == b.Id)
.Where(whereList)
.Select<TenantVersionRes>().ToQueryPage(request.PageCondition);
return data;
}
}