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.
BookingHeChuan/Myshipping.Application/Service/TaskManagePlat/TaskManageShareLinkService.cs

271 lines
12 KiB
C#

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;
using System.Text;
using System.Threading.Tasks;
namespace Myshipping.Application.Service.TaskManagePlat
{
/// <summary>
/// 任务分享链接服务
/// </summary>
[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 readonly SqlSugarRepository<TaskRollingNominationInfo> _taskRollingNominationInfoRepository;
private readonly SqlSugarRepository<TaskRollingNominationDispatchInfo> _taskRollingNominationDispatchInfoRepository;
private const string SHARE_LINK_CACHE_KEY_TEMPLATE = "ShareLinkKeyIncrement";
public TaskManageShareLinkService(ISysCacheService cache, ILogger<TaskManageShareLinkService> logger,
SqlSugarRepository<TaskShareLinkInfo> taskShareLinkInfoRepository,
SqlSugarRepository<TaskShareLinkDynamicDataInfo> taskShareLinkDynamicDataInfoRepository,
SqlSugarRepository<TaskBaseInfo> taskBaseRepository,
SqlSugarRepository<TaskRollingNominationInfo> taskRollingNominationInfoRepository,
SqlSugarRepository<TaskRollingNominationDispatchInfo> taskRollingNominationDispatchInfoRepository)
{
_cache = cache;
_logger = logger;
_taskShareLinkInfoRepository = taskShareLinkInfoRepository;
_taskShareLinkDynamicDataInfoRepository = taskShareLinkDynamicDataInfoRepository;
_taskBaseRepository = taskBaseRepository;
_taskRollingNominationInfoRepository = taskRollingNominationInfoRepository;
_taskRollingNominationDispatchInfoRepository = taskRollingNominationDispatchInfoRepository;
}
#region 生成访问链接
/// <summary>
/// 生成访问链接
/// </summary>
/// <param name="model">创建分享链接请求</param>
/// <returns>返回回执</returns>
[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.businessType))
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($"失效时间不能早于或等于当前时间");
if (!model.businessType.Equals("NOMI_DISPATCH", StringComparison.OrdinalIgnoreCase))
{
throw Oops.Oh($"当前只支持预甩调度分享");
}
var nomDispatchList = _taskRollingNominationDispatchInfoRepository.AsQueryable().Filter(null, true)
.InnerJoin<TaskRollingNominationDetailInfo>((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 (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");
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}");
var autoIncrementKey = RedisHelper.IncrBy(SHARE_LINK_CACHE_KEY_TEMPLATE);
//生成分享KEY
SuperShortLinkHelper codeHelper = new SuperShortLinkHelper();
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}_{nomDispatchList.FirstOrDefault().Detail.SHIPMENT}_{taskInfo.TenantName}_{model.expireDate}";
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<string>(shareKey)}");
throw Oops.Oh($"已有分享记录不能重复生成");
}
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, 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>
/// <param name="pkId">访问链接主键</param>
/// <returns>返回回执</returns>
public async Task<TaskManageOrderResultDto> CancelShareLink(string pkId)
{
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
return result;
}
#endregion
#region 校验成访问链接
/// <summary>
/// 校验成访问链接
/// </summary>
/// <param name="Url">请求链接</param>
/// <returns>返回回执</returns>
public async Task<TaskManageOrderResultDto> ValidateShareLink(string Url)
{
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
return result;
}
#endregion
#region 访问链接
/// <summary>
/// 访问链接
/// </summary>
/// <param name="Url">请求链接</param>
/// <returns>返回回执</returns>
public async Task<TaskManageOrderResultDto> QueryShareLink(string Url)
{
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
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
}
}