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; using Myshipping.Core.Entity; using System; using Furion.FriendlyException; namespace Myshipping.Core.Service { /// /// 租户参数 /// [ApiDescriptionSettings(Name = "DJYTenantParam", Order = 1)] public class DjyTenantParamService : IDjyTenantParamService, IDynamicApiController, ITransient { private readonly SqlSugarRepository _rep; private readonly SqlSugarRepository _itemrep; private readonly SqlSugarRepository _valuerep; private readonly ILogger _logger; private readonly ISysCacheService _cache; private readonly SqlSugarRepository _repTenant; public DjyTenantParamService(SqlSugarRepository rep, SqlSugarRepository itemrep, SqlSugarRepository valuerep, ISysCacheService cache, ILogger logger, SqlSugarRepository repTenant) { _rep = rep; _logger = logger; _itemrep = itemrep; _valuerep = valuerep; _cache = cache; _repTenant = repTenant; } #region 类别 /// /// 增加租户参数类别表 /// /// [HttpPost("/DJYTenantParam/add")] public async Task Add(AddDJYTenantParamInput input) { if (string.IsNullOrWhiteSpace(input.ParaCode)) { throw Oops.Oh(ErrorCode.Param001); } if (string.IsNullOrWhiteSpace(input.ParaName)) { throw Oops.Oh(ErrorCode.Param002); } if (string.IsNullOrWhiteSpace(input.Type)) { throw Oops.Oh(ErrorCode.Param008); } var list = await _rep.FirstOrDefaultAsync(x => x.ParaCode == input.ParaCode); if (list != null) { throw Oops.Oh(ErrorCode.Param004); } var entity = input.Adapt(); await _rep.InsertAsync(entity); } /// /// 更新租户参数类别表 /// /// [HttpPost("/DJYTenantParam/edit")] public async Task Update(UpdateDJYTenantParamInput input) { if (input.Id == 0) { 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); } if (string.IsNullOrWhiteSpace(input.Type)) { throw Oops.Oh(ErrorCode.Param008); } var list = await _rep.FirstOrDefaultAsync(x => x.ParaCode == input.ParaCode && x.Id != input.Id); if (list != null) { throw Oops.Oh(ErrorCode.Param004); } var entity = input.Adapt(); await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync(); } /// /// 删除租户参数类别表 /// /// [HttpPost("/DJYTenantParam/delete")] [SqlSugarUnitOfWork] public async Task Delete(long Id) { if (Id == 0) { throw Oops.Oh(ErrorCode.Param000); } 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); } /// /// 获取租户参数类别 /// 准备废弃,使用/DJYTenantParam/page替代 /// /// [HttpGet("/DJYTenantParam/list")] public async Task> List(string Type, string ParaName) { 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(); } /// /// 分页获取租户参数类别 /// /// [HttpPost("/DJYTenantParam/page")] public async Task> 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>(); 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; } #endregion #region 参数 /// /// 获取租户参数 /// /// [HttpGet("/DJYTenantParam/getParamItem")] public async Task> GetParamItem(string ParaCode) { return await _itemrep.AsQueryable().WhereIF(!string.IsNullOrEmpty(ParaCode), x => x.ParaCode == ParaCode).OrderBy(x => x.Sort).ToListAsync(); } /// /// 增加租户参数 /// /// [HttpPost("/DJYTenantParam/addParamItem")] public async Task AddParamItem(DjyTenantParamItem input) { 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); } var entity = input.Adapt(); await _itemrep.InsertAsync(entity); } /// /// 更新租户参数 /// /// [HttpPost("/DJYTenantParam/editParamItem")] public async Task UpdateParamItem(DjyTenantParamItem input) { 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); } var entity = input.Adapt(); await _itemrep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync(); } /// /// 删除租户参数类别表 /// /// [HttpPost("/DJYTenantParam/deleteParamItem")] [SqlSugarUnitOfWork] public async Task DeleteParamItem(long Id) { if (Id == 0) { throw Oops.Oh(ErrorCode.Param000); } 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); } #endregion #region 值 /// /// 获取参数 /// 准备废弃,使用/DJYTenantParam/PageParamValue替代 /// /// [HttpGet("/DJYTenantParam/getParamValue")] public async Task> GetParamValue(long tenantId, string paraCode = null) { return await _valuerep.AsQueryable().Filter(null, true).WhereIF(paraCode != null, x => x.ParaCode == paraCode).OrderBy(x => x.Sort).ToListAsync(); } /// /// 分页获取参数值 /// /// [HttpPost("/DJYTenantParam/PageParamValue")] public async Task> 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>(); 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; } var t = listTenant.FirstOrDefault(x => x.Id == item.TenantId); if (t != null) { item.TenantName = t?.Name; } var it = listParamItem.FirstOrDefault(x => x.ParaCode == item.ParaCode && x.ItemCode == item.ItemCode); if (it != null) { item.ItemCodeName = it.ItemName; } } return rtn; } /// /// 增加参数 /// /// [HttpPost("/DJYTenantParam/addParamValue")] public async Task AddParamValue(DjyTenantParamValue input) { var entity = input.Adapt(); 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); } var list = await _valuerep.FirstOrDefaultAsync(x => x.ParaCode == input.ParaCode && x.TenantId == input.TenantId); if (list != null) { throw Oops.Oh(ErrorCode.Param004); } await _valuerep.InsertAsync(entity); await CacheData(); } /// /// 更新参数 /// /// [HttpPost("/DJYTenantParam/editParamValue")] public async Task UpdateParamValue(DjyTenantParamValue input) { var entity = input.Adapt(); 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); } var list = await _valuerep.FirstOrDefaultAsync(x => x.ParaCode == input.ParaCode && x.TenantId == input.TenantId && x.Id != input.Id); if (list != null) { throw Oops.Oh(ErrorCode.Param004); } await _valuerep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync(); await CacheData(); } /// ///删除参数 /// /// [HttpPost("/DJYTenantParam/deleteParamValue")] [SqlSugarUnitOfWork] public async Task DeleteParamValue(long Id) { await _valuerep.DeleteAsync(u => u.Id == Id); await CacheData(); } #endregion #region 类别+值 /// /// 根据指定租户参数Code列表返回租户参数值 /// /// 参数Code列表 [NonAction] public async Task> GetParaCodeWithValue(string[] paraCodes) { List result = await _rep.AsQueryable() .Filter(null, true) .LeftJoin((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 public async Task CacheData(bool flag = false) { 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(); await _cache.SetAllTenantParam(list); } } } }