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 { /// /// 订舱标签服务 /// public class BookingLabelService : IBookingLabelService, IDynamicApiController, ITransient { private readonly SqlSugarRepository _rep; private readonly ISysCacheService _cache; public BookingLabelService(SqlSugarRepository rep, ISysCacheService cache) { _rep = rep; _cache = cache; } /// /// 获取全部或指定范围类型的标签列表 /// /// 标签使用范围 空-全部 1-舱位管理台账 [HttpGet("/BookingLabel/List")] public async Task> List(int? scope) { List cacheList = await _cache.GetAsync>(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; } /// /// 新增或修改标签信息 /// [HttpPost("/BookingLabel/Save")] public async Task Save(BookingLabelBaseDto input) { var model = input.Adapt(); 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; } } /// /// 删除标签信息 /// [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> Cache() { var list = await _rep.AsQueryable().ToListAsync(); var cacheList = list.Adapt>(); await _cache.SetTimeoutAsync(CommonConst.CACHE_KEY_BOOKING_LABEL + ":" + UserManager.TENANT_ID, cacheList, TimeSpan.FromDays(3)); return cacheList; } } }