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(); user = _serviceProvider.GetRequiredService(); saasService = _serviceProvider.GetRequiredService(); _seaExportService = _serviceProvider.GetRequiredService(); _redisService = _serviceProvider.GetRequiredService(); } #region 设定标签 /// /// 设定标签 /// /// 请求参数 /// 返回回执 public async Task> 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() .Where(x => input.BusinessIdArray.Contains(x.BusinessId)); recordList.ForEach(p => { tenantDb.Deleteable(p).ExecuteCommand(); }); 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 tenantDb.Insertable(list).ExecuteCommandAsync(); } } catch (Exception e) { Logger.Log(NLog.LogLevel.Error, $"设定标签失败,原因:{e.Message}"); DataResult.Failed(string.Format(MultiLanguageConst.GetDescription(nameof(MultiLanguageConst.BookingSlotSetLabelError)), e.Message)); } return DataResult.Success(string.Empty); } #endregion #region 获取全部或指定范围类型的标签列表 /// /// 获取全部或指定范围类型的标签列表 /// /// 标签使用范围 空-全部 1-舱位管理台账 /// 返回列表 public async Task>> List(int? scope) { List cacheList = _redisService.GetEntity>($"{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>.Success(result); } #endregion #region 标签管理台账 /// /// 标签管理台账 /// /// 查询条件 /// 返回台账列表 public async Task>> GetPageAsync(PageRequest QuerySearch) { var tenantDb = saasService.GetBizDbScopeById(user.TenantId); //序列化查询条件 var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(QuerySearch.QueryCondition); var data = tenantDb.Queryable() .Where(whereList) .Select().ToQueryPage(QuerySearch.PageCondition); return data; } #endregion #region 保存标签 /// /// 保存标签 /// /// 请求参数 /// 返回标签主键 public async Task> Save(BookingLabelBaseDto input) { var tenantDb = saasService.GetBizDbScopeById(user.TenantId); var model = input.Adapt(); if (input.Id is null or 0) { await tenantDb.Insertable(model).ExecuteReturnEntityAsync(); await Cache(); return DataResult.Success(model.Id); } else { var oldModel = await tenantDb.Queryable().FirstAsync(x => x.Id == input.Id); if (oldModel != null) { input.Adapt(oldModel); await tenantDb.Updateable(oldModel).ExecuteCommandAsync(); await Cache(); } return DataResult.Success(input.Id.Value); } } #endregion /// /// 删除标签信息 /// /// 标签主键组 /// public async Task> Delete([FromBody] long[] ids) { if (ids != null && ids.Length > 0) { var tenantDb = saasService.GetBizDbScopeById(user.TenantId); var recordList = tenantDb.Queryable() .Where(x => ids.Contains(x.Id)); recordList.ForEach(async p => { await tenantDb.Deleteable().ExecuteCommandAsync(); }); await Cache(); return DataResult.Success(string.Empty); } return DataResult.Failed(string.Empty); } /// /// 缓存数据 /// /// 返回标签列表 private async Task> Cache() { var tenantDb = saasService.GetBizDbScopeById(user.TenantId); var list = await tenantDb.Queryable().ToListAsync(); var cacheList = list.Adapt>(); _redisService.SetValue($"{CACHE_KEY_BOOKING_LABEL}:{user.TenantId}", JsonConvert.SerializeObject(list), 72 * 3600); return cacheList; } } }