🐛 修复多库数据库偶先bug

This commit is contained in:
不做码农 2023-06-23 16:36:00 +08:00
parent dea937033e
commit a9547257e6
10 changed files with 35 additions and 41 deletions

View File

@ -2,6 +2,7 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System;
using System.Security.Claims;
@ -9,6 +10,11 @@ namespace Infrastructure
{
public static class App
{
/// <summary>
/// 全局配置文件
/// </summary>
public static OptionsSetting OptionsSetting => CatchOrDefault(() => ServiceProvider?.GetService<IOptions<OptionsSetting>>()?.Value);
/// <summary>
/// 服务提供器
/// </summary>

View File

@ -1,14 +1,15 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Infrastructure
namespace Infrastructure
{
/// <summary>
/// 全局静态常量
/// </summary>
public class GlobalConstant
{
/// <summary>
/// 代码生成常量
/// </summary>
public static readonly string CodeGenDbConfig;
/// <summary>
/// 管理员权限
/// </summary>

View File

@ -19,6 +19,7 @@ namespace Infrastructure
public JwtSettings JwtSettings { get; set; }
public Gen Gen { get; set; }
public List<DbConfigs> DbConfigs { get; set; }
public DbConfigs CodeGenDbConfig { get; set; }
}
/// <summary>
/// 发送邮件数据配置
@ -94,10 +95,6 @@ namespace Infrastructure
public int DbType { get; set; }
public string ConfigId { get; set; }
public bool IsAutoCloseConnection { get; set; }
/// <summary>
/// 是否代码生成使用库
/// </summary>
public bool IsGenerateDb { get; set; }
public string DbName { get; set; }
}

View File

@ -2,7 +2,6 @@
using Infrastructure.Attribute;
using Infrastructure.Enums;
using Infrastructure.Extensions;
using IP2Region.Ex.Models;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
@ -13,7 +12,6 @@ using ZR.CodeGenerator.Model;
using ZR.CodeGenerator.Service;
using ZR.Common;
using ZR.Model;
using ZR.Model.System;
using ZR.Model.System.Dto;
using ZR.Model.System.Generate;
using ZR.Service.System.IService;
@ -153,7 +151,7 @@ namespace ZR.Admin.WebApi.Controllers
{
throw new CustomException("表不能为空");
}
var dbConfig = AppSettings.Get<List<DbConfigs>>("dbConfigs").FirstOrDefault(f => f.IsGenerateDb);
DbConfigs dbConfig = AppSettings.Get<DbConfigs>(nameof(GlobalConstant.CodeGenDbConfig));
string[] tableNames = tables.Split(',', StringSplitOptions.RemoveEmptyEntries);
int result = 0;
foreach (var tableName in tableNames)
@ -218,29 +216,25 @@ namespace ZR.Admin.WebApi.Controllers
/// <summary>
/// 预览代码
/// </summary>
/// <param name="dto"></param>
/// <param name="tableId"></param>
/// <param name="VueVersion"></param>
/// <returns></returns>
[HttpPost("preview/{tableId}")]
[ActionPermissionFilter(Permission = "tool:gen:preview")]
public IActionResult Preview(long tableId = 0, int VueVersion = 0)
public IActionResult Preview([FromQuery] GenerateDto dto, [FromRoute] int tableId = 0)
{
GenerateDto dto = new()
{
TableId = tableId,
VueVersion = VueVersion
};
dto.TableId = tableId;
if (dto == null || dto.TableId <= 0)
{
throw new CustomException(ResultCode.CUSTOM_ERROR, "请求参数为空");
}
var genTableInfo = GenTableService.GetGenTableInfo(dto.TableId);
var dbConfig = AppSettings.Get<List<DbConfigs>>("dbConfigs").FirstOrDefault(f => f.IsGenerateDb);
var dbConfig = AppSettings.Get<DbConfigs>(nameof(GlobalConstant.CodeGenDbConfig));
dto.DbType = dbConfig.DbType;
dto.GenTable = genTableInfo;
dto.IsPreview = true;
//生成代码
CodeGeneratorTool.Generate(dto);
return SUCCESS(dto.GenCodes);
@ -261,7 +255,7 @@ namespace ZR.Admin.WebApi.Controllers
throw new CustomException(ResultCode.CUSTOM_ERROR, "请求参数为空");
}
var genTableInfo = GenTableService.GetGenTableInfo(dto.TableId);
var dbConfig = AppSettings.Get<List<DbConfigs>>("dbConfigs").FirstOrDefault(f => f.IsGenerateDb);
var dbConfig = AppSettings.Get<DbConfigs>(nameof(GlobalConstant.CodeGenDbConfig));
dto.DbType = dbConfig.DbType;
dto.GenTable = genTableInfo;

View File

@ -36,7 +36,7 @@ namespace ZR.Admin.WebApi.Extensions
List<DbConfigs> dbConfigs = Configuration.GetSection("DbConfigs").Get<List<DbConfigs>>();
var iocList = new List<IocConfig>();
foreach (var item in dbConfigs.FindAll(f => !f.IsGenerateDb))
foreach (var item in dbConfigs)
{
iocList.Add(new IocConfig()
{

View File

@ -62,7 +62,6 @@ builder.Services.AddAuthentication(options =>
};
});
//InternalApp.InternalServices = builder.Services;
builder.Services.AddSingleton(new AppSettings(builder.Configuration));
builder.Services.AddAppService();
//开启计划任务

View File

@ -12,18 +12,17 @@
"DbType": 1, // MySql = 0, SqlServer = 1, Oracle = 3PgSql = 4
"ConfigId": "0", //
"IsAutoCloseConnection": true
},
{
//{dbName}
"Conn": "Data Source=LAPTOP-STKF2M8H\\SQLEXPRESS;User ID=admin;Password=admin123;Initial Catalog={dbName};",
"DbType": 1,
"ConfigId": "0",
"IsAutoCloseConnection": true,
"DbName": "ZrAdmin", //
"IsGenerateDb": true //使
}
//...
],
//
"CodeGenDbConfig": {
//{dbName}
"Conn": "Data Source=LAPTOP-STKF2M8H\\SQLEXPRESS;User ID=admin;Password=admin123;Initial Catalog={dbName};",
"DbType": 1,
"IsAutoCloseConnection": true,
"DbName": "ZrAdmin" //
},
"urls": "http://localhost:8888", //urldevServer
"corsUrls": "http://localhost:8887", //","
"JwtSettings": {

View File

@ -414,9 +414,9 @@ namespace ZR.CodeGenerator
OptionsSetting optionsSetting = new();
var gen = AppSettings.Get<Gen>("gen");
var dbConfigs = AppSettings.Get<List<DbConfigs>>("dbConfigs");
var dbConfig = dbConfigs.FirstOrDefault(f => f.IsGenerateDb);
optionsSetting.DbConfigs = dbConfigs;
var dbConfig = AppSettings.Get<DbConfigs>("CodeGenDbConfig");
optionsSetting.CodeGenDbConfig = dbConfig;
optionsSetting.Gen = gen ?? throw new CustomException("代码生成节点配置异常");
optionsSetting.Gen.GenDbConfig = dbConfig ?? throw new CustomException("代码生成节点数据配置异常");
List<GenTableColumn> genTableColumns = new();

View File

@ -20,9 +20,8 @@ namespace ZR.CodeGenerator
/// <returns></returns>
public SqlSugarClient GetSugarDbContext(string dbName = "")
{
List<DbConfigs> dbConfigs = AppSettings.Get<List<DbConfigs>>("dbConfigs");
DbConfigs configs = AppSettings.Get<DbConfigs>("CodeGenDbConfig");
DbConfigs configs = dbConfigs.Find(f => f.IsGenerateDb == true);
string connStr = configs.Conn;
if (!string.IsNullOrEmpty(dbName))

View File

@ -17,8 +17,7 @@ namespace ZR.CodeGenerator.Service
{
var db = GetSugarDbContext();
//Oracle库特殊处理
List<DbConfigs> dbConfigs = AppSettings.Get<List<DbConfigs>>("dbConfigs");
DbConfigs configs = dbConfigs.Find(f => f.IsGenerateDb == true);
DbConfigs configs = AppSettings.Get<DbConfigs>(nameof(GlobalConstant.CodeGenDbConfig));
if (configs.DbType == 3)
{
return new List<string>() { configs?.DbName };