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.Application/Service/Fee/FeeCodeService.cs

187 lines
6.1 KiB
C#

10 months ago
using Furion;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.EventBus;
using Furion.FriendlyException;
using Furion.LinqBuilder;
using Mapster;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Myshipping.Application.ConfigOption;
using Myshipping.Application.Entity;
using Myshipping.Application.Event;
using Myshipping.Application.Service.BookingOrder.Dto;
using Myshipping.Application.Service.BookingSlot.Dto;
using Myshipping.Application.Service.Fee.Dto;
using Myshipping.Core;
using Myshipping.Core.Service;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Yitter.IdGenerator;
namespace Myshipping.Application
{
/// <summary>
/// 费用代码
/// </summary>
[ApiDescriptionSettings("Application", Name = "FeeCode", Order = 1)]
public class FeeCodeService : IDynamicApiController, ITransient
{
private readonly SqlSugarRepository<FeeCode> _repCode;
private readonly ILogger<FeeCodeService> _logger;
private readonly ISysCacheService _cache;
private readonly IEventPublisher _publisher;
public FeeCodeService(SqlSugarRepository<FeeCode> repCode,
ILogger<FeeCodeService> logger,
ISysCacheService cache,
IEventPublisher publisher)
{
_repCode = repCode;
_logger = logger;
_cache = cache;
_publisher = publisher;
}
/// <summary>
/// 费用代码查询
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("/FeeCode/Page")]
public async Task<dynamic> Page(FeeCodePageInput input)
{
var entities = await _repCode.AsQueryable()
.WhereIF(!string.IsNullOrEmpty(input.Code), u => u.Code.Contains(input.Code))
.WhereIF(!string.IsNullOrEmpty(input.Name), u => u.Name.Contains(input.Name))
.ToPagedListAsync(input.PageNo, input.PageSize);
var result = entities.Adapt<SqlSugarPagedList<FeeCodePageOutput>>();
return result.XnPagedResult();
}
/// <summary>
/// 保存
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("/FeeCode/Save")]
public async Task<FeeCodeSaveDto> Save(FeeCodeSaveDto input)
{
if (input == null)
{
throw Oops.Bah("请传入正常数据!");
}
FeeCode entity = null;
if (input.Id == 0)
{
entity = input.Adapt<FeeCode>();
entity.Id = YitIdHelper.NextId();
await _repCode.InsertAsync(entity);
}
else
{
entity = await _repCode.AsQueryable().Filter(null, true).FirstAsync(x => x.Id == input.Id);
entity = input.Adapt(entity);
await _repCode.UpdateAsync(entity);
}
await CacheFeeCode();
10 months ago
return entity.Adapt<FeeCodeSaveDto>();
}
/// <summary>
/// 获取详情
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet("/FeeCode/Detail")]
public async Task<FeeCodeSaveDto> Detail(long id)
{
var entity = await _repCode.AsQueryable().Filter(null, true).FirstAsync(x => x.Id == id);
return entity.Adapt<FeeCodeSaveDto>();
}
/// <summary>
/// 删除
/// </summary>
/// <param name="ids"></param>
/// <returns></returns>
[HttpPost("/FeeCode/Delete")]
public async Task Delete(List<long> ids)
{
var list = await _repCode.AsQueryable().Filter(null, true).Where(x => ids.Contains(x.Id)).ToListAsync();
list.ForEach(x => x.IsDeleted = true);
await _repCode.AsUpdateable(list).ExecuteCommandAsync();
await CacheFeeCode();
}
/// <summary>
/// 获取费用代码列表
/// </summary>
/// <returns></returns>
[HttpGet("/FeeCode/List")]
public async Task<List<FeeCodeDto>> List()
{
var result = await _cache.GetAsync<List<FeeCodeDto>>(CommonConst.CACHE_KEY_FEE_CODE + "_" + UserManager.TENANT_ID);
if (result?.Any() != true)
{
result = await CacheFeeCode();
}
return result;
}
/// <summary>
/// 缓存费用代码列表
/// </summary>
[NonAction]
public async Task<List<FeeCodeDto>> CacheFeeCode()
{
if (UserManager.IsSuperAdmin)
{
// 如果登陆人是SuperAdmin则分别缓存所有租户的费用代码
var allFeeCode = await _repCode.AsQueryable().Filter(null, true).Where(x => !x.IsDeleted).ToListAsync();
var groupFeeCodeCache = allFeeCode.GroupBy(x => x.TenantId);
foreach (var item in groupFeeCodeCache)
{
var item2 = item.Adapt<List<FeeCodeDto>>();
await _cache.SetTimeoutAsync(CommonConst.CACHE_KEY_FEE_CODE + "_" + item.Key, item2, new TimeSpan(6, 0, 0));
}
var result = groupFeeCodeCache.FirstOrDefault(x => x.Key == UserManager.TENANT_ID)?.Adapt<List<FeeCodeDto>>();
return result;
}
else
{
// 否则只缓存当前租户的费用代码
var tenantFeeCode = await _repCode.AsQueryable().Filter(null, true).Where(x => !x.IsDeleted && x.TenantId == UserManager.TENANT_ID).ToListAsync();
await _cache.SetTimeoutAsync(CommonConst.CACHE_KEY_FEE_CODE + "_" + UserManager.TENANT_ID, tenantFeeCode, new TimeSpan(6, 0, 0));
var result = tenantFeeCode.Adapt<List<FeeCodeDto>>();
return result;
}
10 months ago
}
}
}