From ad5e768dd54cb3b772ff8e7dfc84107f63b43888 Mon Sep 17 00:00:00 2001 From: jianghaiqing Date: Mon, 19 Feb 2024 11:05:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=A2=84=E7=94=A9=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=E5=88=86=E4=BA=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../TaskManagePlat/TaskShareLinkInfo.cs | 5 ++ .../Helper/SuperShortLinkHelper.cs | 4 +- .../Dtos/ShareLink/ShareLinkRequestDto.cs | 5 ++ .../TaskManageShareLinkService.cs | 49 +++++++++++++++---- 4 files changed, 52 insertions(+), 11 deletions(-) diff --git a/Myshipping.Application/Entity/TaskManagePlat/TaskShareLinkInfo.cs b/Myshipping.Application/Entity/TaskManagePlat/TaskShareLinkInfo.cs index 671d3db8..55ddb5f6 100644 --- a/Myshipping.Application/Entity/TaskManagePlat/TaskShareLinkInfo.cs +++ b/Myshipping.Application/Entity/TaskManagePlat/TaskShareLinkInfo.cs @@ -57,5 +57,10 @@ namespace Myshipping.Application /// 是否手工设定 true-是,false-否 /// public bool IS_MANUAL { get; set; } + + /// + /// 自增KEY计数 + /// + public Nullable INCREMENT_KEY { get; set; } } } diff --git a/Myshipping.Application/Helper/SuperShortLinkHelper.cs b/Myshipping.Application/Helper/SuperShortLinkHelper.cs index b88bcfc2..2d86f6ae 100644 --- a/Myshipping.Application/Helper/SuperShortLinkHelper.cs +++ b/Myshipping.Application/Helper/SuperShortLinkHelper.cs @@ -32,12 +32,12 @@ namespace Myshipping.Application /// /// Code长度位数下能达到的最大值 /// - private long MaxValue + public long MaxValue { get { var max = (long)Math.Pow(_powMax, _codeLength) - 1; - return (long)Math.Pow(15, max.ToString().Length - 1) - 1; + return (long)Math.Pow(10, max.ToString().Length - 1) - 1; } } diff --git a/Myshipping.Application/Service/TaskManagePlat/Dtos/ShareLink/ShareLinkRequestDto.cs b/Myshipping.Application/Service/TaskManagePlat/Dtos/ShareLink/ShareLinkRequestDto.cs index ef954c2c..be8e7159 100644 --- a/Myshipping.Application/Service/TaskManagePlat/Dtos/ShareLink/ShareLinkRequestDto.cs +++ b/Myshipping.Application/Service/TaskManagePlat/Dtos/ShareLink/ShareLinkRequestDto.cs @@ -16,6 +16,11 @@ namespace Myshipping.Application /// public string businessId { get; set; } + /// + /// 业务类型,NOMI_DISPATCH-预甩调度(BUSI对应的预甩的调度主键) + /// + public string businessType { get; set; } = "NOMI_DISPATCH"; + /// /// 任务类型 /// diff --git a/Myshipping.Application/Service/TaskManagePlat/TaskManageShareLinkService.cs b/Myshipping.Application/Service/TaskManagePlat/TaskManageShareLinkService.cs index fda062b5..5e66bce3 100644 --- a/Myshipping.Application/Service/TaskManagePlat/TaskManageShareLinkService.cs +++ b/Myshipping.Application/Service/TaskManagePlat/TaskManageShareLinkService.cs @@ -25,14 +25,18 @@ namespace Myshipping.Application.Service.TaskManagePlat private readonly SqlSugarRepository _taskShareLinkInfoRepository; private readonly SqlSugarRepository _taskShareLinkDynamicDataInfoRepository; private readonly SqlSugarRepository _taskBaseRepository; + private readonly SqlSugarRepository _taskRollingNominationInfoRepository; + private readonly SqlSugarRepository _taskRollingNominationDispatchInfoRepository; - private const string SHARE_LINK_CACHE_KEY_TEMPLATE = "ShareLinkKey_"; + private const string SHARE_LINK_CACHE_KEY_TEMPLATE = "ShareLinkKeyIncrement"; public TaskManageShareLinkService(ISysCacheService cache, ILogger logger, SqlSugarRepository taskShareLinkInfoRepository, SqlSugarRepository taskShareLinkDynamicDataInfoRepository, - SqlSugarRepository taskBaseRepository) + SqlSugarRepository taskBaseRepository, + SqlSugarRepository taskRollingNominationInfoRepository, + SqlSugarRepository taskRollingNominationDispatchInfoRepository) { _cache = cache; _logger = logger; @@ -40,6 +44,8 @@ namespace Myshipping.Application.Service.TaskManagePlat _taskShareLinkInfoRepository = taskShareLinkInfoRepository; _taskShareLinkDynamicDataInfoRepository = taskShareLinkDynamicDataInfoRepository; _taskBaseRepository = taskBaseRepository; + _taskRollingNominationInfoRepository = taskRollingNominationInfoRepository; + _taskRollingNominationDispatchInfoRepository = taskRollingNominationDispatchInfoRepository; } #region 生成访问链接 @@ -71,6 +77,9 @@ namespace Myshipping.Application.Service.TaskManagePlat if (string.IsNullOrWhiteSpace(model.taskType)) throw Oops.Oh($"任务类型不能为空"); + if (string.IsNullOrWhiteSpace(model.businessType)) + throw Oops.Oh($"业务类型不能为空"); + if (string.IsNullOrWhiteSpace(model.expireDate)) throw Oops.Oh($"失效时间不能为空"); @@ -82,10 +91,22 @@ namespace Myshipping.Application.Service.TaskManagePlat if(expireDateTime <= DateTime.Now) throw Oops.Oh($"失效时间不能早于或等于当前时间"); - var taskInfo = _taskBaseRepository.AsQueryable().First(a => a.PK_ID == model.businessId); + if (!model.businessType.Equals("NOMI_DISPATCH", StringComparison.OrdinalIgnoreCase)) + { + throw Oops.Oh($"当前只支持预甩调度分享"); + } + + var nomDispatchList = _taskRollingNominationDispatchInfoRepository.AsQueryable().Filter(null, true) + .InnerJoin((dispatch,detail) => dispatch.DETAIL_ID == detail.PK_ID) + .Where((dispatch,detail) => dispatch.BATCH_ID == model.businessId + && detail.IsDeleted == false && dispatch.IsDeleted == false) + .Select((dispatch, detail) => new { Detail = detail, Dispatch = dispatch }).ToList(); - if (taskInfo == null) - throw Oops.Oh($"任务ID获取详情失败,已删除或不存在"); + if (nomDispatchList.Count == 0) + throw Oops.Oh($"预甩调度获取详情失败,已删除或不存在"); + + if (nomDispatchList.Any(a => !string.IsNullOrWhiteSpace(a.Dispatch.USER_OPINION))) + throw Oops.Oh($"当前预甩调度已有用户反馈,不能创建分享"); var shareInfo = _taskShareLinkInfoRepository.AsQueryable() .First(a => a.BUSI_ID == model.businessId && a.TASK_TYPE == model.taskType && a.STATUS == "ACTIVE"); @@ -115,30 +136,38 @@ namespace Myshipping.Application.Service.TaskManagePlat _logger.LogInformation($"写入分享记录表完成,id={taskShareLinkInfo.Id}"); + var autoIncrementKey = RedisHelper.IncrBy(SHARE_LINK_CACHE_KEY_TEMPLATE); //生成分享KEY SuperShortLinkHelper codeHelper = new SuperShortLinkHelper(); - shareKey = codeHelper.Confuse(taskShareLinkInfo.Id); + shareKey = codeHelper.Confuse(autoIncrementKey); _logger.LogInformation($"生成分享KEY完成,id={taskShareLinkInfo.Id} shareKey={shareKey}"); //更新分享表 var shareEntity = _taskShareLinkInfoRepository.AsQueryable().First(a => a.Id == taskShareLinkInfo.Id); + string taskId = nomDispatchList.FirstOrDefault().Dispatch.TASK_ID; + + var taskInfo = _taskBaseRepository.AsQueryable().First(a => a.PK_ID == taskId); + //写入redis缓存 - string cacheVal = $"{taskInfo.TASK_TYPE}_{taskInfo.TASK_NO}_{taskInfo.TenantName}_{model.expireDate}"; + string cacheVal = $"{taskInfo.TASK_TYPE}_{taskInfo.TASK_NO}_{nomDispatchList.FirstOrDefault().Detail.SHIPMENT}_{taskInfo.TenantName}_{model.expireDate}"; - var expireTimeSpan = new DateTimeOffset(expireDateTime).ToUnixTimeSeconds(); + var expireTimeSpan = expireDateTime.Subtract(nowDate).Duration(); + //new DateTimeOffset(expireDateTime).ToUnixTimeSeconds(); if (_cache.Exists(shareKey)) { shareEntity.SHARE_LINK_KEY = shareKey; shareEntity.STATUS = "REPEAT_KEY";//REPEAT_KEY-重复KEY被取消 + shareEntity.INCREMENT_KEY = autoIncrementKey; await _taskShareLinkInfoRepository.AsUpdateable(shareEntity) .UpdateColumns(it => new { it.SHARE_LINK_KEY, it.STATUS, + it.INCREMENT_KEY }).ExecuteCommandAsync(); _logger.LogInformation($"分享KEY存在重复终止生成分享(cache),id={taskShareLinkInfo.Id} shareKey={shareKey} cache={_cache.Get(shareKey)}"); @@ -148,14 +177,16 @@ namespace Myshipping.Application.Service.TaskManagePlat else { shareEntity.SHARE_LINK_KEY = shareKey; + shareEntity.INCREMENT_KEY = autoIncrementKey; await _taskShareLinkInfoRepository.AsUpdateable(shareEntity) .UpdateColumns(it => new { it.SHARE_LINK_KEY, + it.INCREMENT_KEY }).ExecuteCommandAsync(); - await _cache.SetTimeoutAsync(shareKey, cacheVal, new TimeSpan(expireTimeSpan)); + await _cache.SetTimeoutAsync(shareKey, cacheVal, expireTimeSpan); _logger.LogInformation($"分享KEY写入cache完成,id={taskShareLinkInfo.Id} shareKey={shareKey} cache={cacheVal}"); }