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.

223 lines
8.1 KiB
C#

using Amazon.Runtime.Internal.Util;
using DS.Module.Core;
using DS.Module.Core.Log;
using DS.Module.SqlSugar;
using DS.Module.UserModule;
using DS.WMS.Core.Op.Dtos;
using DS.WMS.Core.Op.Entity;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using DS.WMS.Core.Op.Interface;
using SqlSugar.IOC;
using System.Text.Json.Nodes;
using NLog;
using DS.Module.RedisModule;
using MySqlConnector.Logging;
using Newtonsoft.Json;
using DS.Module.Core.Extensions;
namespace DS.WMS.Core.Op.Method
{
public class BookingLabelService : IBookingLabelService
{
private readonly IServiceProvider _serviceProvider;
private readonly ISqlSugarClient db;
private readonly IUser user;
private readonly ISaasDbService saasService;
private readonly ISeaExportService _seaExportService;
private readonly IRedisService _redisService;
const string CACHE_KEY_BOOKING_LABEL = "BookingLabelList";
private static readonly NLog.Logger Logger = LogManager.GetCurrentClassLogger();
public BookingLabelService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
db = _serviceProvider.GetRequiredService<ISqlSugarClient>();
user = _serviceProvider.GetRequiredService<IUser>();
saasService = _serviceProvider.GetRequiredService<ISaasDbService>();
_seaExportService = _serviceProvider.GetRequiredService<ISeaExportService>();
_redisService = _serviceProvider.GetRequiredService<IRedisService>();
}
#region 设定标签
/// <summary>
/// 设定标签
/// </summary>
/// <param name="input">请求参数</param>
/// <returns>返回回执</returns>
public async Task<DataResult<string>> SetLabel(BindLabelDto input)
{
if (input.BusinessIdArray?.Any() != true)
{
//业务主键为空,请选中需要标记的业务信息
throw new Exception(MultiLanguageConst.GetDescription(nameof(MultiLanguageConst.BookingSlotLabelBusiNoNull)));
}
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
try
{
var recordList = tenantDb.Queryable<BookingLabelAllocation>()
.Where(x => input.BusinessIdArray.Contains(x.BusinessId));
recordList.ForEach(p =>
{
tenantDb.Deleteable<BookingLabelAllocation>(p).ExecuteCommand();
});
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 tenantDb.Insertable<BookingLabelAllocation>(list).ExecuteCommandAsync();
}
}
catch (Exception e)
{
Logger.Log(NLog.LogLevel.Error, $"设定标签失败,原因:{e.Message}");
DataResult<string>.Failed(string.Format(MultiLanguageConst.GetDescription(nameof(MultiLanguageConst.BookingSlotSetLabelError)), e.Message));
}
return DataResult<string>.Success(string.Empty);
}
#endregion
#region 获取全部或指定范围类型的标签列表
/// <summary>
/// 获取全部或指定范围类型的标签列表
/// </summary>
/// <param name="scope">标签使用范围 空-全部 1-舱位管理台账</param>
/// <returns>返回列表</returns>
public async Task<DataResult<List<BookingLabelBaseDto>>> List(int? scope)
{
List<BookingLabelBaseDto> cacheList = _redisService.GetEntity<List<BookingLabelBaseDto>>($"{CACHE_KEY_BOOKING_LABEL}:{user.TenantId}");
if (cacheList?.Any() != true)
{
cacheList = await Cache();
}
var result = scope == null
? cacheList
: cacheList.Where(x => x.Scope == scope).ToList();
return DataResult<List<BookingLabelBaseDto>>.Success(result);
}
#endregion
#region 标签管理台账
/// <summary>
/// 标签管理台账
/// </summary>
/// <param name="QuerySearch">查询条件</param>
/// <returns>返回台账列表</returns>
public async Task<DataResult<List<BookingLabelBaseDto>>> GetPageAsync(PageRequest QuerySearch)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
//序列化查询条件
var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(QuerySearch.QueryCondition);
var data = tenantDb.Queryable<BookingLabel>()
.Where(whereList)
.Select<BookingLabelBaseDto>().ToQueryPage(QuerySearch.PageCondition);
return data;
}
#endregion
#region 保存标签
/// <summary>
/// 保存标签
/// </summary>
/// <param name="input">请求参数</param>
/// <returns>返回标签主键</returns>
public async Task<DataResult<long>> Save(BookingLabelBaseDto input)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var model = input.Adapt<BookingLabel>();
if (input.Id is null or 0)
{
await tenantDb.Insertable<BookingLabel>(model).ExecuteReturnEntityAsync();
await Cache();
return DataResult<long>.Success(model.Id);
}
else
{
var oldModel = await tenantDb.Queryable<BookingLabel>().FirstAsync(x => x.Id == input.Id);
if (oldModel != null)
{
input.Adapt(oldModel);
await tenantDb.Updateable<BookingLabel>(oldModel).ExecuteCommandAsync();
await Cache();
}
return DataResult<long>.Success(input.Id.Value);
}
}
#endregion
/// <summary>
/// 删除标签信息
/// </summary>
/// <param name="ids">标签主键组</param>
/// <returns></returns>
public async Task<DataResult<string>> Delete([FromBody] long[] ids)
{
if (ids != null && ids.Length > 0)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var recordList = tenantDb.Queryable<BookingLabel>()
.Where(x => ids.Contains(x.Id));
recordList.ForEach(async p =>
{
await tenantDb.Deleteable<BookingLabel>().ExecuteCommandAsync();
});
await Cache();
return DataResult<string>.Success(string.Empty);
}
return DataResult<string>.Failed(string.Empty);
}
/// <summary>
/// 缓存数据
/// </summary>
/// <returns>返回标签列表</returns>
private async Task<List<BookingLabelBaseDto>> Cache()
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var list = await tenantDb.Queryable<BookingLabel>().ToListAsync();
var cacheList = list.Adapt<List<BookingLabelBaseDto>>();
_redisService.SetValue($"{CACHE_KEY_BOOKING_LABEL}:{user.TenantId}", JsonConvert.SerializeObject(list), 72 * 3600);
return cacheList;
}
}
}