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

146 lines
5.0 KiB
C#

6 months ago
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.FriendlyException;
6 months ago
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>
[ApiDescriptionSettings("Application", Name = "BookingLabel", Order = 1)]
6 months ago
public class BookingLabelService : IBookingLabelService, IDynamicApiController, ITransient
{
private readonly SqlSugarRepository<BookingLabel> _rep;
private readonly SqlSugarRepository<BookingLabelAllocation> _repAllocation;
6 months ago
private readonly ISysCacheService _cache;
public BookingLabelService(SqlSugarRepository<BookingLabel> rep,
ISysCacheService cache,
SqlSugarRepository<BookingLabelAllocation> repAllocation)
6 months ago
{
_rep = rep;
_cache = cache;
_repAllocation = repAllocation;
6 months ago
}
/// <summary>
/// 为舱位分配标签
/// </summary>
/// <returns></returns>
[HttpPost("/BookingLabel/Bind")]
public async Task SetLabel(BindLabelDto input)
{
if (input.BusinessIdArray?.Any() != true)
{
throw Oops.Oh("请检查参数");
}
_rep.CurrentBeginTran();
try
{
await _repAllocation.DeleteAsync(x => input.BusinessIdArray.Contains(x.BusinessId));
if (input.LabelIdArray != null && input.LabelIdArray.Length > 0)
{
List<BookingLabelAllocation> list = new();
foreach (var item in input.LabelIdArray)
{
foreach (var businessId in input.BusinessIdArray)
{
list.Add(new BookingLabelAllocation
{
LabelId = item,
BusinessId = businessId,
//TenantId = UserManager.TENANT_ID
});
}
}
await _repAllocation.InsertAsync(list);
}
_rep.CurrentCommitTran();
}
catch (Exception)
{
_rep.CurrentRollbackTran();
throw;
}
}
6 months ago
/// <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;
}
}
}