using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.FriendlyException;
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
{
///
/// 订舱标签服务
///
[ApiDescriptionSettings("Application", Name = "BookingLabel", Order = 1)]
public class BookingLabelService : IBookingLabelService, IDynamicApiController, ITransient
{
private readonly SqlSugarRepository _rep;
private readonly SqlSugarRepository _repAllocation;
private readonly ISysCacheService _cache;
public BookingLabelService(SqlSugarRepository rep,
ISysCacheService cache,
SqlSugarRepository repAllocation)
{
_rep = rep;
_cache = cache;
_repAllocation = repAllocation;
}
///
/// 为舱位分配标签
///
///
[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 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;
}
}
///
/// 获取全部或指定范围类型的标签列表
///
/// 标签使用范围 空-全部 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/PageList")]
public async Task> PageList(BookingLabelPageListInput input)
{
var list = await _rep.AsQueryable()
.WhereIF(!string.IsNullOrEmpty(input.Name), x => x.Name.Contains(input.Name))
.WhereIF(input.Scope != null, x => x.Scope == input.Scope)
.ToPagedListAsync(input.PageNo, input.PageSize);
var result = list.Adapt>();
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().Filter(null,true).Where(a=>a.TenantId == UserManager.TENANT_ID).ToListAsync();
var cacheList = list.Adapt>();
await _cache.SetTimeoutAsync(CommonConst.CACHE_KEY_BOOKING_LABEL + ":" + UserManager.TENANT_ID, cacheList, TimeSpan.FromDays(3));
return cacheList;
}
}
}