587 lines
24 KiB
C#
587 lines
24 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Threading.Tasks;
|
||
using System.ComponentModel.DataAnnotations;
|
||
using WalkingTec.Mvvm.Core;
|
||
using WalkingTec.Mvvm.Core.Extensions;
|
||
using HHWMS.Web.Models;
|
||
using Microsoft.EntityFrameworkCore;
|
||
|
||
|
||
namespace HHWMS.Web
|
||
{
|
||
public partial class LocationVM : BaseCRUDVM<Location>
|
||
{
|
||
public List<ComboSelectListItem> AllLocationZones { get; set; }
|
||
|
||
[Display(Name = "Location.BeginLine", Description = "起始列")]
|
||
public short BeginLine { get; set; }
|
||
|
||
[Display(Name = "Location.BeginLayer", Description = "起始层")]
|
||
public short BeginLayer { get; set; }
|
||
|
||
[Display(Name = "Location.RoadwayType", Description = "巷道类型")]
|
||
[Required(ErrorMessage = "Validate.{0}required")]
|
||
public short RoadwayType { get; set; }
|
||
|
||
public List<ComboSelectListItem> RoadwayTypes { get; set; }
|
||
|
||
|
||
public LocationVM()
|
||
{
|
||
SetInclude(x => x.Zone);
|
||
}
|
||
|
||
protected override void InitVM()
|
||
{
|
||
AllLocationZones = DC.Set<LocationZone>().GetSelectListItems(Wtm, y => y.Name);
|
||
|
||
var currentCulture = Wtm.CurrentCulture;
|
||
var dictData = DC.Set<SysDictData>().ToList();
|
||
RoadwayTypes = dictData.Where(x => x.DictType == "RoadwayType").Select(x => new ComboSelectListItem
|
||
{
|
||
Text = currentCulture == "zh" ? x.DictLabel : x.DictLabelUs,
|
||
Value = x.DictValue
|
||
}).ToList();
|
||
}
|
||
|
||
#region base
|
||
|
||
public override void DoAdd()
|
||
{
|
||
base.DoAdd();
|
||
// 单个新增库位时,库位状态默认空仓位(ContainerStatus = "empty")
|
||
Entity.ContainerStatus = LocationStatus.空仓位;
|
||
Entity.Status = LocationStatusEnum.未锁;
|
||
Entity.IsStop = false;
|
||
}
|
||
|
||
public override void DoEdit(bool updateAllFields = false)
|
||
{
|
||
base.DoEdit(updateAllFields);
|
||
}
|
||
|
||
public override void DoDelete()
|
||
{
|
||
base.DoDelete();
|
||
}
|
||
|
||
#endregion base
|
||
|
||
/// <summary>
|
||
/// 查找单伸位巷道的空仓位
|
||
/// </summary>
|
||
private string GetEmptyLocationBySingle(int? roadway)
|
||
{
|
||
var locations = DC.Set<Location>()
|
||
.Where(l => l.RoadWay == roadway
|
||
&& !l.IsStop
|
||
&& l.Status == LocationStatusEnum.未锁
|
||
&& l.ContainerStatus == LocationStatus.空仓位
|
||
&& l.RowFlag == 0) // 单伸位只查找外仓位
|
||
.OrderBy(l => l.Layer)
|
||
.ThenBy(l => l.Line)
|
||
.AsNoTracking()
|
||
.FirstOrDefault();
|
||
|
||
return locations?.Code;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 查找双伸位巷道的空仓位
|
||
/// </summary>
|
||
private string GetEmptyLocationByDouble(int roadway, LocationTypeEnum locationType, string inPort,
|
||
decimal? height = 0)
|
||
{
|
||
// 一次性获取所需的所有数据,避免多次查询数据库
|
||
var locationPairs = DC.Set<Location>()
|
||
.Where(l => l.Type == locationType
|
||
&& l.Status == LocationStatusEnum.未锁
|
||
&& !l.IsStop
|
||
&& l.Height >= height)
|
||
.AsNoTracking()
|
||
.ToDictionary(l => l.Code);
|
||
if (locationPairs.Count <= 20)
|
||
{
|
||
return "";
|
||
}
|
||
|
||
// 1. 优先查找里外仓位都为空的里仓位
|
||
/// 1 7 8 优先1巷道
|
||
/// 4 9 10 优先2 巷道
|
||
var availableLocation = locationPairs.Values
|
||
.Where(l => l.RowFlag == RowFlagEnum.Inner
|
||
&& l.ContainerStatus == LocationStatus.空仓位
|
||
&& locationPairs.TryGetValue(l.ContiguousCode, out var outer)
|
||
&& outer.ContainerStatus == LocationStatus.空仓位)
|
||
.OrderBy(l => l.Height)
|
||
.ToList();
|
||
|
||
if (availableLocation.Any())
|
||
{
|
||
return GetSortedLocationCode(availableLocation, inPort) ?? availableLocation.First().Code;
|
||
}
|
||
|
||
// 2. 查找里仓位有货,外仓位为空的外仓位
|
||
availableLocation = locationPairs.Values
|
||
.Where(l => l.RowFlag == RowFlagEnum.Outer
|
||
&& l.Status == LocationStatusEnum.未锁
|
||
&& l.ContainerStatus == LocationStatus.空仓位
|
||
&& locationPairs.TryGetValue(l.ContiguousCode, out var inner)
|
||
&& inner.Status == LocationStatusEnum.未锁
|
||
&& inner.ContainerStatus == LocationStatus.有货)
|
||
.OrderBy(l => l.Height)
|
||
.ToList();
|
||
|
||
return GetSortedLocationCode(availableLocation, inPort) ?? availableLocation.First().Code;
|
||
}
|
||
|
||
private string GetSortedLocationCode(List<Location> locations, string inPort)
|
||
{
|
||
if (locations == null || !locations.Any())
|
||
return null;
|
||
|
||
// 定义端口组与排序规则的映射(键:端口数组,值:排序函数)
|
||
var portSortMappings = new Dictionary<string[], Func<IEnumerable<Location>, IOrderedEnumerable<Location>>>
|
||
{
|
||
{
|
||
new[] { "EnterExit_P1001" },
|
||
locs => locs.OrderBy(x => x.Height).ThenBy(x => x.RoadWay).ThenBy(l => l.Layer).ThenByDescending(x => x.Line)
|
||
},
|
||
{
|
||
new[] { "EnterExit_P1007", "EnterExit_P1008" },
|
||
locs => locs.OrderBy(x => x.Height).ThenBy(x => x.RoadWay).ThenBy(l => l.Layer).ThenBy(x => x.Line)
|
||
},
|
||
{
|
||
new[] { "EnterExit_P1004" },
|
||
locs => locs.OrderBy(x => x.Height).ThenByDescending(x => x.RoadWay).ThenBy(l => l.Layer).ThenByDescending(x => x.Line)
|
||
},
|
||
{
|
||
new[] { "EnterExit_P1009", "EnterExit_P1010" },
|
||
locs => locs.OrderBy(x => x.Height).ThenByDescending(x => x.RoadWay).ThenBy(l => l.Layer).ThenBy(x => x.Line)
|
||
}
|
||
};
|
||
|
||
// 查找匹配的端口组,应用排序规则
|
||
foreach (var (ports, sortFunc) in portSortMappings)
|
||
{
|
||
if (ports.Contains(inPort))
|
||
{
|
||
return sortFunc(locations).FirstOrDefault()?.Code;
|
||
}
|
||
}
|
||
|
||
// 无匹配端口时,返回默认排序的第一个
|
||
return locations.FirstOrDefault()?.Code;
|
||
}
|
||
/// <summary>
|
||
/// 根据巷道类型查找可用仓位
|
||
/// </summary>
|
||
public ResponseResult<dynamic> GetLocation(WcsRequest request)
|
||
{
|
||
if (!request.RoadWay.HasValue)
|
||
{
|
||
return ResponseResult<dynamic>.Error("巷道号不能为空");
|
||
}
|
||
|
||
// 获取巷道类型
|
||
var roadwayType = GetRoadwayType(request.RoadWay);
|
||
if (roadwayType == null)
|
||
{
|
||
return ResponseResult<dynamic>.Error("未找到对应的巷道类型");
|
||
}
|
||
|
||
string locationCode;
|
||
switch (roadwayType)
|
||
{
|
||
case 1: // 单边位
|
||
case 2: // 单伸位
|
||
locationCode = GetEmptyLocationBySingle(request.RoadWay.Value);
|
||
break;
|
||
case 4: // 双伸位
|
||
locationCode =
|
||
GetEmptyLocationByDouble(request.RoadWay.Value, request.LocationType, request.InPort,
|
||
request.Height);
|
||
break;
|
||
default:
|
||
return ResponseResult<dynamic>.Error("未知的巷道类型");
|
||
}
|
||
|
||
if (string.IsNullOrWhiteSpace(locationCode))
|
||
{
|
||
return ResponseResult<dynamic>.Error("未找到可用的空仓位");
|
||
}
|
||
|
||
return ResponseResult<dynamic>.Success(new { LocationCode = locationCode });
|
||
}
|
||
|
||
/// <summary>
|
||
/// 线边库查找空仓位
|
||
/// </summary>
|
||
public string GetEmptyLocationFromXb(int beginLine, int endLine)
|
||
{
|
||
var location = DC.Set<Location>()
|
||
.Where(l => l.Type == LocationTypeEnum.X
|
||
&& !l.IsStop
|
||
&& l.Status == LocationStatusEnum.未锁
|
||
&& l.ContainerStatus == LocationStatus.空仓位
|
||
&& l.RowFlag == 0
|
||
&& (l.Line >= beginLine && l.Line <= endLine)) // 单伸位只查找外仓位
|
||
.OrderBy(l => l.Layer)
|
||
.ThenBy(l => l.Line)
|
||
.AsNoTracking()
|
||
.FirstOrDefault();
|
||
// 没查找合适的就找靠近的此区域的
|
||
if (location == null)
|
||
{
|
||
location = DC.Set<Location>()
|
||
.Where(l => l.Type == LocationTypeEnum.X
|
||
&& !l.IsStop
|
||
&& l.Status == LocationStatusEnum.未锁
|
||
&& l.ContainerStatus == LocationStatus.空仓位
|
||
&& l.RowFlag == 0) // 单伸位只查找外仓位
|
||
.OrderBy(l => Math.Abs(l.Line - (endLine - beginLine) / 2))
|
||
.ThenBy(l => l.Layer)
|
||
.AsNoTracking()
|
||
.FirstOrDefault();
|
||
}
|
||
|
||
return location?.Code;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 根据巷道名称获取巷道类型
|
||
/// </summary>
|
||
/// <param name="roadwayName">巷道名称</param>
|
||
/// <returns>巷道类型(2代表单伸位,4代表双伸位)</returns>
|
||
private int? GetRoadwayType(int? roadwayName)
|
||
{
|
||
var dictData = DC.Set<SysDictData>()
|
||
.FirstOrDefault(x => x.DictType == "RoadwayConfig" && x.DictLabel == roadwayName.ToString());
|
||
|
||
if (dictData != null)
|
||
{
|
||
if (int.TryParse(dictData.DictValue, out int roadwayType))
|
||
{
|
||
return roadwayType;
|
||
}
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取所有巷道的可用仓位
|
||
/// </summary>
|
||
public ResponseResult<dynamic> GetAllAvailableLocations()
|
||
{
|
||
// 获取所有巷道配置
|
||
var roadwayConfigs = DC.Set<SysDictData>()
|
||
.Where(x => x.DictType == "RoadwayConfig")
|
||
.Select(x => new
|
||
{
|
||
Roadway = x.DictLabel,
|
||
Type = x.DictValue
|
||
})
|
||
.ToDictionary(x => x.Roadway, x => x.Type);
|
||
|
||
if (!roadwayConfigs.Any())
|
||
{
|
||
return ResponseResult<dynamic>.Error("未找到巷道配置");
|
||
}
|
||
|
||
// 一次性获取所有库位数据
|
||
var allLocations = DC.Set<Location>()
|
||
.Select(l => new
|
||
{
|
||
l.Code,
|
||
l.ContiguousCode,
|
||
l.Status,
|
||
l.ContainerStatus,
|
||
l.RowFlag,
|
||
l.Layer,
|
||
l.Line,
|
||
l.RoadWay
|
||
})
|
||
.ToDictionary(l => l.Code);
|
||
|
||
var availableLocations = new List<object>();
|
||
|
||
// 按巷道类型分组处理
|
||
foreach (var config in roadwayConfigs)
|
||
{
|
||
var roadway = int.Parse(config.Key);
|
||
var locationType = config.Value;
|
||
|
||
// 获取当前巷道的所有仓位
|
||
var roadwayLocations = allLocations.Values
|
||
.Where(l => l.RoadWay == roadway)
|
||
.ToList();
|
||
|
||
if (!roadwayLocations.Any()) continue;
|
||
|
||
// 根据巷道类型处理
|
||
switch (locationType)
|
||
{
|
||
case "2": // 单伸位
|
||
var singleLocations = roadwayLocations
|
||
.Where(l => l.RowFlag == 0
|
||
&& l.Status == LocationStatusEnum.未锁
|
||
&& l.ContainerStatus == LocationStatus.空仓位)
|
||
.OrderBy(l => l.Layer)
|
||
.ThenBy(l => l.Line)
|
||
.Select(l => new
|
||
{
|
||
LocationCode = l.Code,
|
||
l.RoadWay,
|
||
l.Layer,
|
||
l.Line,
|
||
Type = "单伸位"
|
||
});
|
||
availableLocations.AddRange(singleLocations);
|
||
break;
|
||
|
||
case "4": // 双伸位
|
||
// 1. 查找里外都为空的里仓位
|
||
var emptyInnerLocations = roadwayLocations
|
||
.Where(l => l.RowFlag == RowFlagEnum.Inner
|
||
&& l.Status == LocationStatusEnum.未锁
|
||
&& l.ContainerStatus == LocationStatus.空仓位
|
||
&& allLocations.TryGetValue(l.ContiguousCode, out var outer)
|
||
&& outer.Status == LocationStatusEnum.未锁
|
||
&& outer.ContainerStatus == LocationStatus.空仓位)
|
||
.OrderBy(l => l.Layer)
|
||
.ThenBy(l => l.Line)
|
||
.Select(l => new
|
||
{
|
||
LocationCode = l.Code,
|
||
l.RoadWay,
|
||
l.Layer,
|
||
l.Line,
|
||
Type = "双伸位-里仓位"
|
||
});
|
||
availableLocations.AddRange(emptyInnerLocations);
|
||
|
||
// 2. 查找里仓位有货,外仓位为空的外仓位
|
||
var availableOuterLocations = roadwayLocations
|
||
.Where(l => l.RowFlag == 0
|
||
&& l.Status == LocationStatusEnum.未锁
|
||
&& l.ContainerStatus == LocationStatus.空仓位
|
||
&& allLocations.TryGetValue(l.ContiguousCode, out var inner)
|
||
&& inner.Status == LocationStatusEnum.未锁
|
||
&& inner.ContainerStatus == LocationStatus.有货)
|
||
.OrderBy(l => l.Layer)
|
||
.ThenBy(l => l.Line)
|
||
.Select(l => new
|
||
{
|
||
LocationCode = l.Code,
|
||
l.RoadWay,
|
||
l.Layer,
|
||
l.Line,
|
||
Type = "双伸位-外仓位"
|
||
});
|
||
availableLocations.AddRange(availableOuterLocations);
|
||
break;
|
||
}
|
||
}
|
||
|
||
if (!availableLocations.Any())
|
||
{
|
||
return ResponseResult<dynamic>.Error("未找到可用的空仓位");
|
||
}
|
||
|
||
return ResponseResult<dynamic>.Success(new
|
||
{
|
||
Locations = availableLocations,
|
||
Total = availableLocations.Count
|
||
});
|
||
}
|
||
|
||
/// <summary>
|
||
/// 下发任务到wcs
|
||
/// </summary>
|
||
/// <param name="taskHeader"></param>
|
||
/// <returns></returns>
|
||
public ResponseResult<dynamic> SendWcsTask(TaskHeader task)
|
||
{
|
||
// 下发任务到wcs
|
||
// 1.入库任务直接下发
|
||
// 2.出库任务 判断是否为双伸位库位
|
||
// 2.1 单伸位:直接下发
|
||
// 2.2 双伸位:判断出库库位是否为里仓位
|
||
// 2.2.1 里仓位,判断是否需要移库,先生成移库任务,再下发出库任务
|
||
// 2.2.2 外仓位,直接下发
|
||
|
||
var _unitWork = new UnitWork(Wtm);
|
||
if (task.TaskType != TaskTypeEnum.入库)
|
||
{
|
||
var loc = DC.Set<Location>().Where(x => x.Code == task.FromLocation)
|
||
.AsNoTracking()
|
||
.FirstOrDefault();
|
||
// 双伸位 并且里仓位
|
||
if (loc != null && loc.RowFlag == RowFlagEnum.Inner && !string.IsNullOrWhiteSpace(loc.ContiguousCode))
|
||
{
|
||
var outter = DC.Set<Location>().FirstOrDefault(x => x.Code == loc.ContiguousCode);
|
||
if (outter != null && outter.Status == LocationStatusEnum.未锁 &&
|
||
outter.ContainerStatus != LocationStatus.空仓位)
|
||
{
|
||
// 查找移库仓位
|
||
var moveloc = GetMoveLocation(outter);
|
||
if (moveloc.Status)
|
||
{
|
||
string code = moveloc.Data.LocationCode;
|
||
var movelocation = _unitWork.FindSingle<Location>(x => x.Code == code);
|
||
|
||
// 生成outter移库任务
|
||
var outterTask = new TaskHeader()
|
||
{
|
||
ZoneId = outter.ZoneId,
|
||
TaskNo = _unitWork.GetSerialNumber("Move"),
|
||
TaskType = TaskTypeEnum.移库,
|
||
RoadWay = 2,
|
||
ContainerCode = outter.ContainerCode,
|
||
FromLocation = outter.Code,
|
||
ToLocation = movelocation.Code,
|
||
Status = TaskStatusEnum.待下发,
|
||
CreateTime = DateTime.Now,
|
||
CreateBy = Wtm?.LoginUserInfo?.ITCode ?? "WMS",
|
||
};
|
||
var result = SendWcsTask(outterTask);
|
||
if (result.Status)
|
||
{
|
||
movelocation.Status = LocationStatusEnum.锁定;
|
||
_unitWork.Update(movelocation);
|
||
|
||
outter.Status = LocationStatusEnum.锁定;
|
||
_unitWork.Update(outter);
|
||
}
|
||
else
|
||
{
|
||
return result;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
return moveloc;
|
||
}
|
||
}
|
||
else if (outter != null && outter.Status == LocationStatusEnum.锁定)
|
||
{
|
||
return ResponseResult<dynamic>.Error("外侧仓位已被锁定!请稍后再尝试!");
|
||
}
|
||
}
|
||
}
|
||
|
||
// 下发任务到wcs
|
||
// 成品库下发WCS入库,目的仓位给固定WareCell
|
||
var postData = new
|
||
{
|
||
TaskNo = task.TaskNo,
|
||
PalletNo = task.ContainerCode,
|
||
FromLocation = task.FromLocation,
|
||
ToLocation = (task.TaskType == TaskTypeEnum.入库 && task.ZoneId != 1027) ? "WareCell" : task.ToLocation
|
||
};
|
||
|
||
try
|
||
{
|
||
var domain = task.ZoneId == 1027 ? "WCSXB" : "WCSCP";
|
||
var res = Wtm.CallAPI<ApiJsonResult<dynamic>>(domain, "TaskAssign", HttpMethodEnum.POST, postData)
|
||
.Result?.Data;
|
||
|
||
if (res != null && res.Success)
|
||
{
|
||
task.Status = TaskStatusEnum.已下发;
|
||
task.UpdateTime = DateTime.Now;
|
||
task.UpdateBy = "WMS";
|
||
if (task.ID == 0)
|
||
{
|
||
_unitWork.Add(task);
|
||
}
|
||
else
|
||
{
|
||
_unitWork.Update(task);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
task.ErrorMsg = res?.Msg;
|
||
return ResponseResult<dynamic>.Error(res?.Msg);
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
task.ErrorMsg = ex.Message;
|
||
return ResponseResult<dynamic>.Error("任务下发失败!");
|
||
}
|
||
|
||
return ResponseResult<dynamic>.Success();
|
||
}
|
||
|
||
public ResponseResult<dynamic> GetMoveLocation(Location outter)
|
||
{
|
||
// 查找移库仓位
|
||
// 一次性获取所需的所有数据,避免多次查询数据库
|
||
var locationPairs = DC.Set<Location>()
|
||
.Where(l => l.Type == outter.Type
|
||
&& !l.IsStop
|
||
&& l.Status == LocationStatusEnum.未锁
|
||
&& l.Height >= outter.Height)
|
||
.Select(l => new
|
||
{
|
||
l.RoadWay,
|
||
l.Code,
|
||
l.ContiguousCode,
|
||
l.Status,
|
||
l.ContainerStatus,
|
||
l.RowFlag,
|
||
l.Height,
|
||
l.Layer,
|
||
l.Line
|
||
})
|
||
.AsNoTracking()
|
||
.ToDictionary(l => l.Code);
|
||
|
||
// 1. 优先查找里外仓位都为空的里仓位
|
||
var moveloc = locationPairs.Values
|
||
.Where(l => l.RowFlag == RowFlagEnum.Inner
|
||
&& l.ContainerStatus == LocationStatus.空仓位
|
||
&& locationPairs.TryGetValue(l.ContiguousCode, out var outer)
|
||
&& outer.ContainerStatus == LocationStatus.空仓位)
|
||
.OrderBy(l => Math.Abs((decimal)outter.Height - (decimal)l.Height))
|
||
.ThenBy(l => Math.Abs(outter.RoadWay - l.RoadWay))
|
||
.ThenBy(l => Math.Abs(outter.Line - l.Line))
|
||
.ThenBy(l => l.Layer)
|
||
.FirstOrDefault();
|
||
|
||
if (moveloc == null)
|
||
{
|
||
// 2. 查找里仓位有货,外仓位为空的外仓位
|
||
moveloc = locationPairs.Values
|
||
.Where(l => l.RowFlag == RowFlagEnum.Outer
|
||
&& l.ContainerStatus == LocationStatus.空仓位
|
||
&& locationPairs.TryGetValue(l.ContiguousCode, out var inner)
|
||
&& inner.Status == LocationStatusEnum.未锁
|
||
&& inner.ContainerStatus == LocationStatus.有货)
|
||
.OrderBy(l => Math.Abs((decimal)outter.Height - (decimal)l.Height))
|
||
.ThenBy(l => Math.Abs(outter.RoadWay - l.RoadWay))
|
||
.ThenBy(l => Math.Abs(outter.Line - l.Line))
|
||
.ThenBy(l => l.Layer)
|
||
.FirstOrDefault();
|
||
if (moveloc == null)
|
||
{
|
||
return ResponseResult<dynamic>.Error("找不到可用的移库仓位!");
|
||
}
|
||
else
|
||
{
|
||
return ResponseResult<dynamic>.Success(new { LocationCode = moveloc.Code });
|
||
}
|
||
}
|
||
else
|
||
{
|
||
return ResponseResult<dynamic>.Success(new { LocationCode = moveloc.Code });
|
||
}
|
||
}
|
||
}
|
||
}
|