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/BookingLabel/BookingLabelService.cs

98 lines
3.2 KiB
C#

6 months ago
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Myshipping.Application.Entity;
using Myshipping.Application.Service.BookingLabel.Dto;
using Myshipping.Core;
using Myshipping.Core.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Myshipping.Application
{
/// <summary>
/// 订舱标签服务
/// </summary>
public class BookingLabelService : IBookingLabelService, IDynamicApiController, ITransient
{
private readonly SqlSugarRepository<BookingLabel> _rep;
private readonly ISysCacheService _cache;
public BookingLabelService(SqlSugarRepository<BookingLabel> rep,
ISysCacheService cache)
{
_rep = rep;
_cache = cache;
}
/// <summary>
/// 获取全部或指定范围类型的标签列表
/// </summary>
/// <param name="scope">标签使用范围 空-全部 1-舱位管理台账</param>
[HttpGet("/BookingLabel/List")]
public async Task<List<BookingLabelBaseDto>> List(int? scope)
{
List<BookingLabelBaseDto> cacheList = await _cache.GetAsync<List<BookingLabelBaseDto>>(CommonConst.CACHE_KEY_BOOKING_LABEL + ":" + UserManager.TENANT_ID);
if (cacheList?.Any() != true)
{
cacheList = await Cache();
}
var result = scope == null
? cacheList
: cacheList.Where(x => x.Scope == scope).ToList();
return result;
}
/// <summary>
/// 新增或修改标签信息
/// </summary>
[HttpPost("/BookingLabel/Save")]
public async Task<long> Save(BookingLabelBaseDto input)
{
var model = input.Adapt<BookingLabel>();
if (input.Id is null or 0)
{
await _rep.InsertAsync(model);
await Cache();
return model.Id;
}
else
{
var oldModel = await _rep.FirstOrDefaultAsync(x => x.Id == input.Id);
if (oldModel != null)
{
input.Adapt(oldModel);
await _rep.UpdateAsync(oldModel);
await Cache();
}
return (long)input.Id;
}
}
/// <summary>
/// 删除标签信息
/// </summary>
[HttpPost("/BookingLabel/Delete")]
public async Task Delete([FromBody] long[] ids)
{
if (ids != null && ids.Length > 0)
{
await _rep.DeleteAsync(x => ids.Contains(x.Id));
await Cache();
}
}
[NonAction]
private async Task<List<BookingLabelBaseDto>> Cache()
{
var list = await _rep.AsQueryable().ToListAsync();
var cacheList = list.Adapt<List<BookingLabelBaseDto>>();
await _cache.SetTimeoutAsync(CommonConst.CACHE_KEY_BOOKING_LABEL + ":" + UserManager.TENANT_ID, cacheList, TimeSpan.FromDays(3));
return cacheList;
}
}
}