新增任务分享

optimize
jianghaiqing 9 months ago
parent 63ef2607ac
commit 5fe5e56699

@ -0,0 +1,40 @@
using Myshipping.Application.Entity;
using Myshipping.Core.Entity;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Myshipping.Application
{
/// <summary>
/// 任务分享链接动态数据
/// </summary>
[SugarTable("task_share_link_dynamic_data")]
[Description("任务分享链接动态数据")]
public class TaskShareLinkDynamicDataInfo : DBEntityTenant
{
/// <summary>
/// 分享主键
/// </summary>
public string SHARE_ID { get; set; }
/// <summary>
/// 数据类型 DISCLAIMERS-免责声明
/// </summary>
public string DATA_TYPE { get; set; }
/// <summary>
/// 数据详情
/// </summary>
public string DATA_MSG { get; set; }
/// <summary>
/// 数据报文类型 JSONHTML
/// </summary>
public string DATA_MSG_TYPE { get; set; }
}
}

@ -1,4 +1,5 @@
using Myshipping.Application.Entity;
using Myshipping.Core.Entity;
using SqlSugar;
using System;
using System.Collections.Generic;
@ -15,7 +16,7 @@ namespace Myshipping.Application
/// </summary>
[SugarTable("task_share_link")]
[Description("任务分享链接")]
public class TaskShareLinkInfo : TaskManageDbEntity
public class TaskShareLinkInfo : DBEntityTenant
{
/// <summary>
/// 任务类型
@ -42,5 +43,19 @@ namespace Myshipping.Application
/// </summary>
public DateTime EXPIRE_DATE { get; set; }
/// <summary>
/// 状态 ACTIVE-有效的EXPIRE-失效CANCEL-取消REPEAT_KEY-重复KEY被取消
/// </summary>
public string STATUS { get; set; }
/// <summary>
/// 是否需要用户反馈意见 true-是 false-否
/// </summary>
public bool IS_USER_FEEDBACK { get; set; }
/// <summary>
/// 是否手工设定 true-是false-否
/// </summary>
public bool IS_MANUAL { get; set; }
}
}

@ -10,13 +10,12 @@ namespace Myshipping.Application
{
public class SuperShortLinkHelper
{
private readonly string _base62CharSet;
private readonly int _codeLength;
private readonly string _base62CharSet = "s9LFkgy5RovixI1aOf8UhdY3r4DMplQZJXPqebE0WSjBn7wVzmN2Gc6THCAKut";
private readonly int _codeLength = 6;
private readonly int _powMax = 62;
public SuperShortLinkHelper(IOptionsSnapshot<ShortLinkOptions> option)
public SuperShortLinkHelper()
{
_base62CharSet = option.Value.Secrect;
_codeLength = option.Value.CodeLength;
}
/// <summary>
@ -37,8 +36,8 @@ namespace Myshipping.Application
{
get
{
var max = (long)Math.Pow(62, _codeLength) - 1;
return (long)Math.Pow(10, max.ToString().Length - 1) - 1;
var max = (long)Math.Pow(_powMax, _codeLength) - 1;
return (long)Math.Pow(15, max.ToString().Length - 1) - 1;
}
}
@ -76,8 +75,8 @@ namespace Myshipping.Application
var sb = new StringBuilder();
do
{
sb.Insert(0, _base62CharSet[(int)(value % 62)]);
value /= 62;
sb.Insert(0, _base62CharSet[(int)(value % _powMax)]);
value /= _powMax;
} while (value > 0);
return sb.ToString();

@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Myshipping.Application
{
/// <summary>
/// 分享链接生成
/// </summary>
public class ShareLinkRequestDto
{
/// <summary>
/// 业务ID
/// </summary>
public string businessId { get; set; }
/// <summary>
/// 任务类型
/// </summary>
public string taskType { get; set; }
/// <summary>
/// 失效时间
/// </summary>
public string expireDate { get; set; }
/// <summary>
/// 是否需要用户反馈意见
/// </summary>
public bool isUserFeedBack { get; set; } = true;
}
}

@ -15,11 +15,9 @@ namespace Myshipping.Application
/// <summary>
/// 生成访问链接
/// </summary>
/// <param name="businessId">业务ID</param>
/// <param name="taskType">任务类型</param>
/// <param name="expireDate">失效时间</param>
/// <param name="model">创建分享链接请求</param>
/// <returns>返回回执</returns>
Task<TaskManageOrderResultDto> CreateShareLink(string businessId, string taskType, DateTime expireDate);
Task<TaskManageOrderResultDto> CreateShareLink(ShareLinkRequestDto model);
/// <summary>
/// 取消访问链接
@ -37,6 +35,11 @@ namespace Myshipping.Application
Task<TaskManageOrderResultDto> ValidateShareLink(string Url);
/// <summary>
/// 获取分享详情
/// </summary>
/// <param name="shareKey">链接分享KEY</param>
/// <returns>返回回执</returns>
Task<TaskManageOrderResultDto> GetInfo(string shareKey);
}
}

@ -45,7 +45,7 @@ namespace Myshipping.Application
SqlSugarRepository<TaskRollingNominationShipInfo> taskRollingNominationShipInfoRepository,
SqlSugarRepository<TaskShareLinkInfo> taskShareLinkInfoRepository,
SqlSugarRepository<BookingOrder> bookingOrderRepository,
SqlSugarRepository<BookingCtn> bookingCtnRepository)
SqlSugarRepository<BookingCtn> bookingCtnRepository, ISysCacheService cache, ILogger<TaskManageRollingNominationService> logger)
{
_taskBaseRepository = taskBaseRepository;
_taskRollingNominationInfoRepository = taskRollingNominationInfoRepository;
@ -55,6 +55,9 @@ namespace Myshipping.Application
_taskShareLinkInfoRepository = taskShareLinkInfoRepository;
_bookingOrderRepository = bookingOrderRepository;
_bookingCtnRepository = bookingCtnRepository;
_cache = cache;
_logger = logger;
}
#region 获取预甩详情
@ -587,7 +590,7 @@ namespace Myshipping.Application
try
{
/*
BOOKING_IDSHIPMENT()
BOOKING_IDSHIPMENT(),
*/
}

@ -1,5 +1,11 @@
using Furion.DynamicApiController;
using Furion.DistributedIDGenerator;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Myshipping.Application.Entity;
using Myshipping.Core;
using Myshipping.Core.Service;
using System;
using System.Collections.Generic;
using System.Linq;
@ -14,20 +20,162 @@ namespace Myshipping.Application.Service.TaskManagePlat
[ApiDescriptionSettings("Application", Name = "TaskManageShareLink", Order = 10)]
public class TaskManageShareLinkService : ITaskManageShareLinkService, IDynamicApiController
{
private readonly ISysCacheService _cache;
private readonly ILogger<TaskManageShareLinkService> _logger;
private readonly SqlSugarRepository<TaskShareLinkInfo> _taskShareLinkInfoRepository;
private readonly SqlSugarRepository<TaskShareLinkDynamicDataInfo> _taskShareLinkDynamicDataInfoRepository;
private readonly SqlSugarRepository<TaskBaseInfo> _taskBaseRepository;
private const string SHARE_LINK_CACHE_KEY_TEMPLATE = "ShareLinkKey_";
public TaskManageShareLinkService(ISysCacheService cache, ILogger<TaskManageShareLinkService> logger,
SqlSugarRepository<TaskShareLinkInfo> taskShareLinkInfoRepository,
SqlSugarRepository<TaskShareLinkDynamicDataInfo> taskShareLinkDynamicDataInfoRepository,
SqlSugarRepository<TaskBaseInfo> taskBaseRepository)
{
_cache = cache;
_logger = logger;
_taskShareLinkInfoRepository = taskShareLinkInfoRepository;
_taskShareLinkDynamicDataInfoRepository = taskShareLinkDynamicDataInfoRepository;
_taskBaseRepository = taskBaseRepository;
}
#region 生成访问链接
/// <summary>
/// 生成访问链接
/// </summary>
/// <param name="businessId">业务ID</param>
/// <param name="taskType">任务类型</param>
/// <param name="expireDate">失效时间</param>
/// <param name="model">创建分享链接请求</param>
/// <returns>返回回执</returns>
public async Task<TaskManageOrderResultDto> CreateShareLink(string businessId, string taskType, DateTime expireDate)
[HttpPost("/TaskManageShareLink/CreateShareLink")]
public async Task<TaskManageOrderResultDto> CreateShareLink([FromBody] ShareLinkRequestDto model)
{
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
/*
1businessIdSTATUS=ACTIVE
2
3
4SHARE_LINK_KEYredisexpireDate
5SHARE_LINK_KEY
*/
try
{
string shareKey = string.Empty;
if (string.IsNullOrWhiteSpace(model.businessId))
throw Oops.Oh($"业务ID不能为空");
if (string.IsNullOrWhiteSpace(model.taskType))
throw Oops.Oh($"任务类型不能为空");
if (string.IsNullOrWhiteSpace(model.expireDate))
throw Oops.Oh($"失效时间不能为空");
DateTime expireDateTime = DateTime.MinValue;
if(!DateTime.TryParse(model.expireDate, out expireDateTime))
throw Oops.Oh($"失效时间格式错误请参考格式yyyy-MM-dd HH:mm:ss");
if(expireDateTime <= DateTime.Now)
throw Oops.Oh($"失效时间不能早于或等于当前时间");
var taskInfo = _taskBaseRepository.AsQueryable().First(a => a.PK_ID == model.businessId);
if (taskInfo == null)
throw Oops.Oh($"任务ID获取详情失败已删除或不存在");
var shareInfo = _taskShareLinkInfoRepository.AsQueryable()
.First(a => a.BUSI_ID == model.businessId && a.TASK_TYPE == model.taskType && a.STATUS == "ACTIVE");
if (shareInfo != null)
{
throw Oops.Oh($"已有分享记录不能重复生成");
}
DateTime nowDate = DateTime.Now;
TaskShareLinkInfo taskShareLinkInfo = new TaskShareLinkInfo {
EXPIRE_DATE = expireDateTime,
STATUS = "ACTIVE",
BUSI_ID = model.businessId,
IS_MANUAL = false,
CreatedTime = nowDate,
UpdatedTime = nowDate,
CreatedUserId = UserManager.UserId,
CreatedUserName = UserManager.Name,
TASK_TYPE = model.taskType,
IS_USER_FEEDBACK = model.isUserFeedBack,
};
//写入分享记录
await _taskShareLinkInfoRepository.InsertAsync(taskShareLinkInfo);
_logger.LogInformation($"写入分享记录表完成id={taskShareLinkInfo.Id}");
//生成分享KEY
SuperShortLinkHelper codeHelper = new SuperShortLinkHelper();
shareKey = codeHelper.Confuse(taskShareLinkInfo.Id);
_logger.LogInformation($"生成分享KEY完成id={taskShareLinkInfo.Id} shareKey={shareKey}");
//更新分享表
var shareEntity = _taskShareLinkInfoRepository.AsQueryable().First(a => a.Id == taskShareLinkInfo.Id);
//写入redis缓存
string cacheVal = $"{taskInfo.TASK_TYPE}_{taskInfo.TASK_NO}_{taskInfo.TenantName}_{model.expireDate}";
var expireTimeSpan = new DateTimeOffset(expireDateTime).ToUnixTimeSeconds();
if (_cache.Exists(shareKey))
{
shareEntity.SHARE_LINK_KEY = shareKey;
shareEntity.STATUS = "REPEAT_KEY";//REPEAT_KEY-重复KEY被取消
await _taskShareLinkInfoRepository.AsUpdateable(shareEntity)
.UpdateColumns(it => new
{
it.SHARE_LINK_KEY,
it.STATUS,
}).ExecuteCommandAsync();
_logger.LogInformation($"分享KEY存在重复终止生成分享(cache)id={taskShareLinkInfo.Id} shareKey={shareKey} cache={_cache.Get<string>(shareKey)}");
throw Oops.Oh($"已有分享记录不能重复生成");
}
else
{
shareEntity.SHARE_LINK_KEY = shareKey;
await _taskShareLinkInfoRepository.AsUpdateable(shareEntity)
.UpdateColumns(it => new
{
it.SHARE_LINK_KEY,
}).ExecuteCommandAsync();
await _cache.SetTimeoutAsync(shareKey, cacheVal, new TimeSpan(expireTimeSpan));
_logger.LogInformation($"分享KEY写入cache完成id={taskShareLinkInfo.Id} shareKey={shareKey} cache={cacheVal}");
}
result.succ = true;
result.ext = shareKey;
}
catch (Exception ex)
{
_logger.LogError($"获取预甩详情异常,原因:{ex.Message}");
result.succ = false;
result.msg = $"获取预甩详情异常,原因:{ex.Message}";
}
return result;
}
#endregion
#region 取消访问链接
/// <summary>
/// 取消访问链接
/// </summary>
@ -39,8 +187,9 @@ namespace Myshipping.Application.Service.TaskManagePlat
return result;
}
#endregion
#region 校验成访问链接
/// <summary>
/// 校验成访问链接
/// </summary>
@ -52,8 +201,9 @@ namespace Myshipping.Application.Service.TaskManagePlat
return result;
}
#endregion
#region 访问链接
/// <summary>
/// 访问链接
/// </summary>
@ -65,5 +215,25 @@ namespace Myshipping.Application.Service.TaskManagePlat
return result;
}
#endregion
#region 获取分享详情
/// <summary>
/// 获取分享详情
/// </summary>
/// <param name="shareKey">链接分享KEY</param>
/// <returns>返回回执</returns>
public async Task<TaskManageOrderResultDto> GetInfo(string shareKey)
{
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
/*
1shareKey
2
3
*/
return result;
}
#endregion
}
}

Loading…
Cancel
Save