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.
BookingHeChuan/Myshipping.Core/Service/DjyTenantParam/DjyTenantParamService.cs

441 lines
16 KiB
C#

2 years ago
using Myshipping.Core;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
2 years ago
using Myshipping.Core.Entity;
2 years ago
using System;
2 years ago
using Furion.FriendlyException;
2 years ago
namespace Myshipping.Core.Service
{
/// <summary>
/// 租户参数
/// </summary>
[ApiDescriptionSettings(Name = "DJYTenantParam", Order = 1)]
public class DjyTenantParamService : IDjyTenantParamService, IDynamicApiController, ITransient
{
private readonly SqlSugarRepository<DjyTenantParam> _rep;
private readonly SqlSugarRepository<DjyTenantParamItem> _itemrep;
private readonly SqlSugarRepository<DjyTenantParamValue> _valuerep;
private readonly ILogger<DjyTenantParam> _logger;
2 years ago
private readonly ISysCacheService _cache;
8 months ago
private readonly SqlSugarRepository<SysTenant> _repTenant;
public DjyTenantParamService(SqlSugarRepository<DjyTenantParam> rep,
SqlSugarRepository<DjyTenantParamItem> itemrep,
SqlSugarRepository<DjyTenantParamValue> valuerep,
ISysCacheService cache, ILogger<DjyTenantParam> logger,
SqlSugarRepository<SysTenant> repTenant)
2 years ago
{
_rep = rep;
_logger = logger;
_itemrep = itemrep;
_valuerep = valuerep;
2 years ago
_cache = cache;
8 months ago
_repTenant = repTenant;
2 years ago
}
2 years ago
#region 类别
2 years ago
/// <summary>
/// 增加租户参数类别表
/// </summary>
/// <returns></returns>
[HttpPost("/DJYTenantParam/add")]
public async Task Add(AddDJYTenantParamInput input)
{
2 years ago
if (string.IsNullOrWhiteSpace(input.ParaCode))
{
2 years ago
throw Oops.Oh(ErrorCode.Param001);
}
2 years ago
if (string.IsNullOrWhiteSpace(input.ParaName))
{
2 years ago
throw Oops.Oh(ErrorCode.Param002);
}
2 years ago
if (string.IsNullOrWhiteSpace(input.Type))
{
throw Oops.Oh(ErrorCode.Param008);
}
2 years ago
var list = await _rep.FirstOrDefaultAsync(x => x.ParaCode == input.ParaCode);
2 years ago
if (list != null)
{
2 years ago
throw Oops.Oh(ErrorCode.Param004);
}
2 years ago
var entity = input.Adapt<DjyTenantParam>();
await _rep.InsertAsync(entity);
}
/// <summary>
/// 更新租户参数类别表
/// </summary>
/// <returns></returns>
[HttpPost("/DJYTenantParam/edit")]
public async Task Update(UpdateDJYTenantParamInput input)
{
2 years ago
if (input.Id == 0)
2 years ago
{
throw Oops.Oh(ErrorCode.Param000);
}
if (string.IsNullOrWhiteSpace(input.ParaCode))
{
throw Oops.Oh(ErrorCode.Param001);
}
if (string.IsNullOrWhiteSpace(input.ParaName))
{
throw Oops.Oh(ErrorCode.Param002);
}
2 years ago
if (string.IsNullOrWhiteSpace(input.Type))
{
throw Oops.Oh(ErrorCode.Param008);
}
2 years ago
var list = await _rep.FirstOrDefaultAsync(x => x.ParaCode == input.ParaCode && x.Id != input.Id);
2 years ago
if (list != null)
{
throw Oops.Oh(ErrorCode.Param004);
}
2 years ago
var entity = input.Adapt<DjyTenantParam>();
await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
}
/// <summary>
/// 删除租户参数类别表
/// </summary>
/// <returns></returns>
[HttpPost("/DJYTenantParam/delete")]
[SqlSugarUnitOfWork]
public async Task Delete(long Id)
{
2 years ago
if (Id == 0)
{
throw Oops.Oh(ErrorCode.Param000);
}
2 years ago
var entity = await _rep.FirstOrDefaultAsync(u => u.Id == Id);
await _itemrep.DeleteAsync(m => m.ParaCode == entity.ParaCode);
await _valuerep.DeleteAsync(m => m.ParaCode == entity.ParaCode);
await _rep.DeleteAsync(entity);
}
2 years ago
2 years ago
/// <summary>
/// 获取租户参数类别
8 months ago
/// 准备废弃,使用/DJYTenantParam/page替代
2 years ago
/// </summary>
/// <returns></returns>
2 years ago
[HttpGet("/DJYTenantParam/list")]
public async Task<List<DjyTenantParam>> List(string Type, string ParaName)
2 years ago
{
return await _rep.AsQueryable().WhereIF(!string.IsNullOrEmpty(Type), x => x.Type == Type).WhereIF(!string.IsNullOrEmpty(ParaName), x => x.ParaName.Contains(ParaName)).OrderBy(x => x.Sort).ToListAsync();
2 years ago
}
8 months ago
/// <summary>
/// 分页获取租户参数类别
/// </summary>
/// <returns></returns>
8 months ago
[HttpPost("/DJYTenantParam/page")]
8 months ago
public async Task<SqlSugarPagedList<DJYTenantParamOutput>> Page(QueryDJYTenantParamInput input)
{
var list = await _rep.AsQueryable()
.WhereIF(!string.IsNullOrEmpty(input.Type), x => x.Type == input.Type)
.WhereIF(!string.IsNullOrEmpty(input.ParaCode), x => x.ParaCode == input.ParaCode)
.WhereIF(!string.IsNullOrEmpty(input.ParaName), x => x.ParaName.Contains(input.ParaName))
.OrderBy(x => x.Sort)
.ToPagedListAsync(input.PageNo, input.PageSize);
var rtn = list.Adapt<SqlSugarPagedList<DJYTenantParamOutput>>();
var dict = await _cache.GetAllDictData();
foreach (var item in rtn.Items)
{
var d = dict.FirstOrDefault(x => x.TypeCode == "tenant_param_group" && x.Code == item.Type);
item.TypeName = d.Value;
}
return rtn;
}
2 years ago
#endregion
#region 参数
2 years ago
/// <summary>
/// 获取租户参数
/// </summary>
/// <returns></returns>
[HttpGet("/DJYTenantParam/getParamItem")]
public async Task<List<DjyTenantParamItem>> GetParamItem(string ParaCode)
{
2 years ago
return await _itemrep.AsQueryable().WhereIF(!string.IsNullOrEmpty(ParaCode), x => x.ParaCode == ParaCode).OrderBy(x => x.Sort).ToListAsync();
2 years ago
}
/// <summary>
/// 增加租户参数
/// </summary>
/// <returns></returns>
[HttpPost("/DJYTenantParam/addParamItem")]
public async Task AddParamItem(DjyTenantParamItem input)
{
2 years ago
if (string.IsNullOrWhiteSpace(input.ParaCode))
{
throw Oops.Oh(ErrorCode.Param001);
}
if (string.IsNullOrWhiteSpace(input.ItemCode))
{
throw Oops.Oh(ErrorCode.Param005);
}
if (string.IsNullOrWhiteSpace(input.ItemName))
{
throw Oops.Oh(ErrorCode.Param006);
}
var list = await _itemrep.FirstOrDefaultAsync(x => x.ParaCode == input.ParaCode && x.ItemCode == input.ItemCode);
if (list != null)
{
throw Oops.Oh(ErrorCode.Param004);
}
2 years ago
var entity = input.Adapt<DjyTenantParamItem>();
await _itemrep.InsertAsync(entity);
}
/// <summary>
/// 更新租户参数
/// </summary>
/// <returns></returns>
[HttpPost("/DJYTenantParam/editParamItem")]
public async Task UpdateParamItem(DjyTenantParamItem input)
{
2 years ago
if (input.Id == 0)
{
throw Oops.Oh(ErrorCode.Param000);
}
if (string.IsNullOrWhiteSpace(input.ParaCode))
{
throw Oops.Oh(ErrorCode.Param001);
}
if (string.IsNullOrWhiteSpace(input.ItemCode))
{
throw Oops.Oh(ErrorCode.Param005);
}
if (string.IsNullOrWhiteSpace(input.ItemName))
{
throw Oops.Oh(ErrorCode.Param006);
}
var list = await _itemrep.FirstOrDefaultAsync(x => x.ParaCode == input.ParaCode && x.ItemCode == input.ItemCode && x.Id != input.Id);
if (list != null)
{
throw Oops.Oh(ErrorCode.Param004);
}
2 years ago
var entity = input.Adapt<DjyTenantParamItem>();
await _itemrep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
2 years ago
2 years ago
}
2 years ago
/// <summary>
/// 删除租户参数类别表
/// </summary>
/// <returns></returns>
[HttpPost("/DJYTenantParam/deleteParamItem")]
[SqlSugarUnitOfWork]
public async Task DeleteParamItem(long Id)
{
2 years ago
if (Id == 0)
{
throw Oops.Oh(ErrorCode.Param000);
}
2 years ago
var entity = await _itemrep.FirstOrDefaultAsync(u => u.Id == Id);
await _valuerep.DeleteAsync(m => m.ItemCode == entity.ItemCode && m.ParaCode == entity.ParaCode);
await _itemrep.DeleteAsync(entity);
2 years ago
2 years ago
}
2 years ago
#endregion
#region 值
2 years ago
/// <summary>
8 months ago
/// 获取参数
/// 准备废弃,使用/DJYTenantParam/PageParamValue替代
2 years ago
/// </summary>
/// <returns></returns>
[HttpGet("/DJYTenantParam/getParamValue")]
2 years ago
public async Task<List<DjyTenantParamValue>> GetParamValue(long tenantId, string paraCode = null)
2 years ago
{
2 years ago
return await _valuerep.AsQueryable().Filter(null, true).WhereIF(paraCode != null, x => x.ParaCode == paraCode).OrderBy(x => x.Sort).ToListAsync();
2 years ago
}
8 months ago
/// <summary>
/// 分页获取参数值
/// </summary>
/// <returns></returns>
8 months ago
[HttpPost("/DJYTenantParam/PageParamValue")]
8 months ago
public async Task<SqlSugarPagedList<DjyTenantParamValueListOutput>> PageParamValue(QueryTenantParamValueInput input)
{
var list = await _valuerep.AsQueryable().Filter(null, true)
.WhereIF(!string.IsNullOrEmpty(input.ParaCode), x => x.ParaCode == input.ParaCode)
.WhereIF(input.TenantId.HasValue, x => x.TenantId == input.TenantId)
.OrderBy(x => x.Sort)
.ToPagedListAsync(input.PageNo, input.PageSize);
var rtn = list.Adapt<SqlSugarPagedList<DjyTenantParamValueListOutput>>();
var listParam = await _rep.AsQueryable().ToListAsync();
var listParamItem = await _itemrep.AsQueryable().ToListAsync();
var listTenant = await _repTenant.AsQueryable().Select(x => new { x.Id, x.Name }).ToListAsync();
foreach (var item in rtn.Items)
{
var p = listParam.FirstOrDefault(x => x.ParaCode == item.ParaCode);
if (p != null)
{
item.ParaName = p.ParaName;
}
8 months ago
var t = listTenant.FirstOrDefault(x => x.Id == item.TenantId);
if (t != null)
{
item.TenantName = t?.Name;
}
8 months ago
var it = listParamItem.FirstOrDefault(x => x.ParaCode == item.ParaCode && x.ItemCode == item.ItemCode);
8 months ago
if (it != null)
{
item.ItemCodeName = it.ItemName;
}
}
return rtn;
}
2 years ago
/// <summary>
/// 增加参数
/// </summary>
/// <returns></returns>
[HttpPost("/DJYTenantParam/addParamValue")]
public async Task AddParamValue(DjyTenantParamValue input)
{
var entity = input.Adapt<DjyTenantParamValue>();
2 years ago
if (string.IsNullOrWhiteSpace(input.ParaCode))
{
throw Oops.Oh(ErrorCode.Param001);
}
if (string.IsNullOrWhiteSpace(input.ItemCode))
{
throw Oops.Oh(ErrorCode.Param005);
}
2 years ago
if (input.TenantId == null)
2 years ago
{
throw Oops.Oh(ErrorCode.Param007);
}
2 years ago
var list = await _valuerep.FirstOrDefaultAsync(x => x.ParaCode == input.ParaCode && x.TenantId == input.TenantId);
2 years ago
if (list != null)
{
throw Oops.Oh(ErrorCode.Param004);
}
2 years ago
await _valuerep.InsertAsync(entity);
2 years ago
await CacheData();
2 years ago
}
2 years ago
2 years ago
/// <summary>
/// 更新参数
/// </summary>
/// <returns></returns>
[HttpPost("/DJYTenantParam/editParamValue")]
public async Task UpdateParamValue(DjyTenantParamValue input)
{
var entity = input.Adapt<DjyTenantParamValue>();
2 years ago
if (input.Id == 0)
{
throw Oops.Oh(ErrorCode.Param000);
}
if (string.IsNullOrWhiteSpace(input.ParaCode))
{
throw Oops.Oh(ErrorCode.Param001);
}
if (string.IsNullOrWhiteSpace(input.ItemCode))
{
throw Oops.Oh(ErrorCode.Param005);
}
if (input.TenantId == null)
{
throw Oops.Oh(ErrorCode.Param007);
}
2 years ago
var list = await _valuerep.FirstOrDefaultAsync(x => x.ParaCode == input.ParaCode && x.TenantId == input.TenantId && x.Id != input.Id);
2 years ago
if (list != null)
{
throw Oops.Oh(ErrorCode.Param004);
}
2 years ago
await _valuerep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
2 years ago
await CacheData();
2 years ago
}
/// <summary>
2 years ago
///删除参数
2 years ago
/// </summary>
/// <returns></returns>
[HttpPost("/DJYTenantParam/deleteParamValue")]
[SqlSugarUnitOfWork]
public async Task DeleteParamValue(long Id)
{
2 years ago
await _valuerep.DeleteAsync(u => u.Id == Id);
await CacheData();
2 years ago
}
#endregion
#region 类别+值
/// <summary>
/// 根据指定租户参数Code列表返回租户参数值
/// </summary>
/// <param name="paraCodes">参数Code列表</param>
[NonAction]
public async Task<List<DjyTenantParamValueOutput>> GetParaCodeWithValue(string[] paraCodes)
{
8 months ago
List<DjyTenantParamValueOutput> result = await _rep.AsQueryable()
.Filter(null, true)
.LeftJoin<DjyTenantParamValue>((t1, t2) => t1.ParaCode == t2.ParaCode)
.Where((t1, t2) => t2.TenantId == UserManager.TENANT_ID)
.WhereIF(paraCodes?.Any() == true, (t1, t2) => paraCodes.Contains(t1.ParaCode))
.Select((t1, t2) => new DjyTenantParamValueOutput()
{
ParaCode = t1.ParaCode,
ParaName = t1.ParaName,
ParaType = t1.Type,
ParaValue = t2.ItemCode
})
.ToListAsync();
return result;
}
#endregion
2 years ago
2 years ago
public async Task CacheData(bool flag = false)
2 years ago
{
2 years ago
if (flag)
{
if (!_cache.Exists(CommonConst.CACHE_KEY_COMMON_DB_TENANT_PARAM_VALUE))
{
var list = await _valuerep.AsQueryable().Filter(null, true).ToListAsync();
await _cache.SetAllTenantParam(list);
}
}
else
{
var list = await _valuerep.AsQueryable().Filter(null, true).ToListAsync();
2 years ago
await _cache.SetAllTenantParam(list);
}
2 years ago
}
2 years ago
2 years ago
}
}