接口加防抖
This commit is contained in:
1 parent
3eb8252e55
commit
2c178b1810
1 file changed
+93
-97
@@ -1,10 +1,13 @@
|
|||||||
using System.Collections.Generic;
|
using System;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using AutoMapper;
|
using AutoMapper;
|
||||||
using HHWMS.Web.Models;
|
using HHWMS.Web.Models;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using WalkingTec.Mvvm.Core;
|
using WalkingTec.Mvvm.Core;
|
||||||
|
using Microsoft.Extensions.Caching.Memory;
|
||||||
|
|
||||||
namespace HHWMS.Web
|
namespace HHWMS.Web
|
||||||
{
|
{
|
||||||
@@ -20,130 +23,123 @@ namespace HHWMS.Web
|
|||||||
{
|
{
|
||||||
private readonly IUnitWork _unitWork;
|
private readonly IUnitWork _unitWork;
|
||||||
private readonly IMapper _mapper;
|
private readonly IMapper _mapper;
|
||||||
|
private readonly IMemoryCache _cache;
|
||||||
|
private const int DEBOUNCE_INTERVAL_MS = 1000; // 防抖间隔1秒
|
||||||
|
|
||||||
public IWCSController(IUnitWork unitWork, IMapper mapper)
|
public IWCSController(IUnitWork unitWork, IMapper mapper, IMemoryCache cache)
|
||||||
{
|
{
|
||||||
_unitWork = unitWork;
|
_unitWork = unitWork;
|
||||||
_mapper = mapper;
|
_mapper = mapper;
|
||||||
|
_cache = cache;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 请求入库
|
/// 防抖处理装饰器
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
private async Task<IActionResult> WithDebounce(string key, Func<Task<IActionResult>> action)
|
||||||
[HttpPost]
|
|
||||||
public IActionResult RequestIn(WcsRequest request)
|
|
||||||
{
|
{
|
||||||
return ApiResult();
|
try
|
||||||
|
{
|
||||||
|
// 使用key作为请求标识
|
||||||
|
string requestKey = $"Debounce_{key}";
|
||||||
|
|
||||||
|
// 检查是否存在处理中的相同请求
|
||||||
|
if (_cache.TryGetValue(requestKey, out DateTime lastRequestTime))
|
||||||
|
{
|
||||||
|
var timeSinceLastRequest = DateTime.Now - lastRequestTime;
|
||||||
|
if (timeSinceLastRequest.TotalMilliseconds < DEBOUNCE_INTERVAL_MS)
|
||||||
|
{
|
||||||
|
var remainingSeconds = Math.Ceiling((DEBOUNCE_INTERVAL_MS - timeSinceLastRequest.TotalMilliseconds) / 1000);
|
||||||
|
return ApiResultError($"请求过于频繁,请等待{remainingSeconds}秒后重试");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 添加或更新缓存(设置防抖间隔时间)
|
||||||
|
var cacheEntryOptions = new MemoryCacheEntryOptions()
|
||||||
|
.SetAbsoluteExpiration(TimeSpan.FromMilliseconds(DEBOUNCE_INTERVAL_MS));
|
||||||
|
_cache.Set(requestKey, DateTime.Now, cacheEntryOptions);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// 执行实际的业务逻辑
|
||||||
|
return await action();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
// 处理失败时移除缓存,允许重试
|
||||||
|
_cache.Remove(requestKey);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return ApiResultError($"处理请求时发生错误: {ex.Message}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 状态返回
|
/// 请求入库(带防抖)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public IActionResult TaskStatusResult(WcsRequest request)
|
public Task<IActionResult> RequestIn(WcsRequest request)
|
||||||
{
|
{
|
||||||
return ApiResult();
|
if (request == null || string.IsNullOrEmpty(request.PalletNo))
|
||||||
|
{
|
||||||
|
return Task.FromResult<IActionResult>(ApiResultError("请求参数无效"));
|
||||||
|
}
|
||||||
|
|
||||||
|
return WithDebounce($"RequestIn_{request.PalletNo}", async () =>
|
||||||
|
{
|
||||||
|
await ProcessRequestIn(request);
|
||||||
|
return ApiResult(new { Message = "入库请求处理成功" });
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 请求空闲库位
|
/// 处理入库请求的具体业务逻辑
|
||||||
|
/// </summary>
|
||||||
|
private async Task ProcessRequestIn(WcsRequest request)
|
||||||
|
{
|
||||||
|
// TODO: 实现具体的入库处理逻辑
|
||||||
|
await Task.Delay(1000); // 模拟异步处理
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 状态返回(带防抖)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="request">包含容器编码的请求</param>
|
|
||||||
/// <returns>返回空闲库位信息</returns>
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
/*public IActionResult RequestLocation(WcsRequest request)
|
public Task<IActionResult> TaskStatusResult(WcsRequest request)
|
||||||
|
{
|
||||||
|
if (request == null || string.IsNullOrEmpty(request.PalletNo))
|
||||||
|
{
|
||||||
|
return Task.FromResult<IActionResult>(ApiResultError("请求参数无效"));
|
||||||
|
}
|
||||||
|
|
||||||
|
return WithDebounce($"TaskStatus_{request.PalletNo}", () => Task.FromResult<IActionResult>(ApiResult()));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 请求空闲库位(带防抖)
|
||||||
|
/// </summary>
|
||||||
|
[HttpPost]
|
||||||
|
public Task<IActionResult> RequestLocation(WcsRequest request)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(request?.PalletNo))
|
if (string.IsNullOrEmpty(request?.PalletNo))
|
||||||
{
|
{
|
||||||
return ApiResultError("容器编码不能为空");
|
return Task.FromResult<IActionResult>(ApiResultError("容器编码不能为空"));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 1. 优先查找里外仓位都为空的里仓位
|
return WithDebounce($"RequestLocation_{request.PalletNo}", () =>
|
||||||
var innerLocation = _unitwork.Find<Location>(x => 1 == 1)
|
|
||||||
.Join(_unitwork.Find<Location>(x => 1 == 1),
|
|
||||||
inner => inner.ContiguousCode,
|
|
||||||
outer => outer.Code,
|
|
||||||
(inner, outer) => new { Inner = inner, Outer = outer })
|
|
||||||
.Where(x => x.Inner.RowFlag == 1
|
|
||||||
&& x.Inner.Status == "empty"
|
|
||||||
&& x.Outer.Status == "empty")
|
|
||||||
.Select(x => x.Inner)
|
|
||||||
.FirstOrDefault();
|
|
||||||
|
|
||||||
if (innerLocation != null)
|
|
||||||
{
|
{
|
||||||
// 更新库位表的ContainerCode为请求的PalletNo
|
var vm = Wtm.CreateVM<LocationVM>();
|
||||||
innerLocation.ContainerCode = request.PalletNo;
|
var code = vm.GetLocation(request);
|
||||||
_unitwork.Update(innerLocation);
|
if (code.Status)
|
||||||
|
|
||||||
// 更新容器表的LocationCode为分配到的库位编码
|
|
||||||
var container = _unitwork.Find<Container>(x => x.Code == request.PalletNo).FirstOrDefault();
|
|
||||||
if (container != null)
|
|
||||||
{
|
{
|
||||||
container.LocationCode = innerLocation.Code;
|
UpdateLocationAndContainer(code.Data.LocationCode, request.PalletNo);
|
||||||
_unitwork.Update(container);
|
return Task.FromResult<IActionResult>(ApiResult(code.Data));
|
||||||
}
|
}
|
||||||
|
return Task.FromResult<IActionResult>(ApiResultError("没有符合条件的可用库位"));
|
||||||
return ApiResult(new
|
});
|
||||||
{
|
|
||||||
LocationCode = innerLocation.Code
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// 2. 查找里仓位有货,外仓位无货的外仓位
|
|
||||||
var outerLocation = _unitwork.Find<Location>(x => 1 == 1)
|
|
||||||
.Join(_unitwork.Find<Location>(x => 1 == 1),
|
|
||||||
outer => outer.ContiguousCode,
|
|
||||||
inner => inner.Code,
|
|
||||||
(outer, inner) => new { Outer = outer, Inner = inner })
|
|
||||||
.Where(x => x.Outer.RowFlag == 0
|
|
||||||
&& x.Outer.Status == "empty"
|
|
||||||
&& x.Inner.Status == "some")
|
|
||||||
.Select(x => x.Outer)
|
|
||||||
.FirstOrDefault();
|
|
||||||
|
|
||||||
if (outerLocation != null)
|
|
||||||
{
|
|
||||||
// 更新库位表的ContainerCode为请求的PalletNo
|
|
||||||
outerLocation.ContainerCode = request.PalletNo;
|
|
||||||
_unitwork.Update(outerLocation);
|
|
||||||
|
|
||||||
// 更新容器表的LocationCode为分配到的库位编码
|
|
||||||
var container = _unitwork.Find<Container>(x => x.Code == request.PalletNo).FirstOrDefault();
|
|
||||||
if (container != null)
|
|
||||||
{
|
|
||||||
container.LocationCode = outerLocation.Code;
|
|
||||||
_unitwork.Update(container);
|
|
||||||
}
|
|
||||||
|
|
||||||
return ApiResult(new
|
|
||||||
{
|
|
||||||
LocationCode = outerLocation.Code
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return ApiResultError("没有符合条件的可用库位");
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
public IActionResult RequestLocation(WcsRequest request)
|
|
||||||
{
|
|
||||||
if (string.IsNullOrEmpty(request?.PalletNo))
|
|
||||||
{
|
|
||||||
return ApiResultError("容器编码不能为空");
|
|
||||||
}
|
|
||||||
|
|
||||||
var vm = Wtm.CreateVM<LocationVM>();
|
|
||||||
var code = vm.GetLocation(request);
|
|
||||||
if (code.Status)
|
|
||||||
{
|
|
||||||
UpdateLocationAndContainer(code.Data.LocationCode, request.PalletNo);
|
|
||||||
return ApiResult(code.Data);
|
|
||||||
}
|
|
||||||
|
|
||||||
return ApiResultError("没有符合条件的可用库位");
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private IActionResult UpdateLocationAndContainer(string locationCode, string palletNo)
|
private IActionResult UpdateLocationAndContainer(string locationCode, string palletNo)
|
||||||
|
|||||||
Reference in new issue
Block a user