337 lines
12 KiB
C#
337 lines
12 KiB
C#
using System.Collections.Generic;
|
||
using System.ComponentModel.DataAnnotations;
|
||
using System.Linq;
|
||
using HHWMS.Web.Models;
|
||
using Microsoft.EntityFrameworkCore;
|
||
using Microsoft.EntityFrameworkCore.SqlServer.Query.Internal;
|
||
using WalkingTec.Mvvm.Core;
|
||
using WalkingTec.Mvvm.Core.Extensions;
|
||
|
||
namespace HHWMS.Web
|
||
{
|
||
public partial class LocationVM : BaseCRUDVM<Location>
|
||
{
|
||
public LocationVM()
|
||
{
|
||
}
|
||
|
||
public InventoryListVM Inventorys { get; set; } = new InventoryListVM();
|
||
|
||
[Display(Name = "Location.BeginLine", Description = "起始列")]
|
||
[Required(ErrorMessage = "Validate.{0}required")]
|
||
public short BeginLine { get; set; }
|
||
|
||
[Display(Name = "Location.BeginLayer", Description = "起始层")]
|
||
[Required(ErrorMessage = "Validate.{0}required")]
|
||
public short BeginLayer { get; set; }
|
||
|
||
[Display(Name = "Location.RoadwayType", Description = "巷道类型")]
|
||
[Required(ErrorMessage = "Validate.{0}required")]
|
||
public short RoadwayType { get; set; }
|
||
|
||
private IUnitWork _unitWork { get; set; }
|
||
|
||
public List<ComboSelectListItem> RoadwayTypes { get; set; }
|
||
public List<ComboSelectListItem> ZoneCodes { get; set; }
|
||
public List<ComboSelectListItem> Types { get; set; }
|
||
public List<ComboSelectListItem> LocationStatus { get; set; }
|
||
|
||
protected override void InitVM()
|
||
{
|
||
Inventorys.CopyContext(this);
|
||
Inventorys.Searcher.LocationCode = Entity.Code;
|
||
|
||
//var tt = DC.Set<TaskHeader>().Where(x => x.ID > 1).Include(x => x.TaskDetails);
|
||
//_unitWork = new UnitWork(Wtm);
|
||
//var task = _unitWork.Find<TaskHeader>(x => x.ID > 1)
|
||
// .Include(x => x.TaskDetails)
|
||
// .FirstOrDefault();
|
||
//var details = task.TaskDetails;
|
||
|
||
var CurrentCulture = Wtm.CurrentCulture;
|
||
var dictDatas = DC.Set<SysDictData>().ToList();
|
||
RoadwayTypes = dictDatas.Where(x => x.DictType == "RoadwayType").Select(x => new ComboSelectListItem
|
||
{
|
||
Text = CurrentCulture == "zh" ? x.DictLabel : x.DictLabelUs,
|
||
Value = x.DictValue
|
||
}).ToList();
|
||
ZoneCodes = dictDatas.Where(x => x.DictType == "ZoneCode").Select(x => new ComboSelectListItem
|
||
{
|
||
Text = CurrentCulture == "zh" ? x.DictLabel : x.DictLabelUs,
|
||
Value = x.DictValue
|
||
}).ToList();
|
||
Types = dictDatas.Where(x => x.DictType == "LocationType").Select(x => new ComboSelectListItem
|
||
{
|
||
Text = CurrentCulture == "zh" ? x.DictLabel : x.DictLabelUs,
|
||
Value = x.DictValue
|
||
}).ToList();
|
||
LocationStatus = dictDatas.Where(x => x.DictType == "LocationStatus").Select(x => new ComboSelectListItem
|
||
{
|
||
Text = CurrentCulture == "zh" ? x.DictLabel : x.DictLabelUs,
|
||
Value = x.DictValue
|
||
}).ToList();
|
||
}
|
||
|
||
public override void DoAdd()
|
||
{
|
||
base.DoAdd();
|
||
}
|
||
|
||
public override void DoEdit(bool updateAllFields = false)
|
||
{
|
||
base.DoEdit(updateAllFields);
|
||
}
|
||
|
||
public override void DoDelete()
|
||
{
|
||
base.DoDelete();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 查找单伸位巷道的空仓位
|
||
/// </summary>
|
||
private string GetEmptyLocationBySingle(int? roadway)
|
||
{
|
||
var locations = DC.Set<Location>()
|
||
.Where(l => l.RoadWay == roadway
|
||
&& l.Status == "empty"
|
||
&& l.RowFlag == 0) // 单伸位只查找外仓位
|
||
.OrderBy(l => l.Layer)
|
||
.ThenBy(l => l.Line)
|
||
.FirstOrDefault();
|
||
|
||
return locations?.Code;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 查找双伸位巷道的空仓位
|
||
/// </summary>
|
||
private string GetEmptyLocationByDouble(int roadway)
|
||
{
|
||
// 一次性获取所需的所有数据,避免多次查询数据库
|
||
var locationPairs = DC.Set<Location>()
|
||
.Where(l => l.RoadWay == roadway)
|
||
.Select(l => new
|
||
{
|
||
l.Code,
|
||
l.ContiguousCode,
|
||
l.Status,
|
||
l.RowFlag,
|
||
l.Layer,
|
||
l.Line
|
||
})
|
||
.ToDictionary(l => l.Code);
|
||
|
||
// 1. 优先查找里外仓位都为空的里仓位
|
||
var emptyInnerLocation = locationPairs.Values
|
||
.Where(l => l.RowFlag == 1
|
||
&& l.Status == "empty"
|
||
&& locationPairs.TryGetValue(l.ContiguousCode, out var outer)
|
||
&& outer.Status == "empty")
|
||
.OrderBy(l => l.Layer)
|
||
.ThenBy(l => l.Line)
|
||
.FirstOrDefault();
|
||
|
||
if (emptyInnerLocation != null)
|
||
{
|
||
return emptyInnerLocation.Code;
|
||
}
|
||
|
||
// 2. 查找里仓位有货,外仓位为空的外仓位
|
||
var availableOuterLocation = locationPairs.Values
|
||
.Where(l => l.RowFlag == 0
|
||
&& l.Status == "empty"
|
||
&& locationPairs.TryGetValue(l.ContiguousCode, out var inner)
|
||
&& inner.Status == "some")
|
||
.OrderBy(l => l.Layer)
|
||
.ThenBy(l => l.Line)
|
||
.FirstOrDefault();
|
||
|
||
return availableOuterLocation?.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);
|
||
break;
|
||
default:
|
||
return ResponseResult<dynamic>.Error("未知的巷道类型");
|
||
}
|
||
|
||
if (locationCode == null)
|
||
{
|
||
return ResponseResult<dynamic>.Error("未找到可用的空仓位");
|
||
}
|
||
|
||
return ResponseResult<dynamic>.Success(new { LocationCode = locationCode });
|
||
}
|
||
|
||
/// <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.RowFlag,
|
||
l.Layer,
|
||
l.Line,
|
||
l.RoadWay,
|
||
l.ZoneCode
|
||
})
|
||
.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 == "empty")
|
||
.OrderBy(l => l.Layer)
|
||
.ThenBy(l => l.Line)
|
||
.Select(l => new
|
||
{
|
||
LocationCode = l.Code,
|
||
l.ZoneCode,
|
||
l.RoadWay,
|
||
l.Layer,
|
||
l.Line,
|
||
Type = "单伸位"
|
||
});
|
||
availableLocations.AddRange(singleLocations);
|
||
break;
|
||
|
||
case "4": // 双伸位
|
||
// 1. 查找里外都为空的里仓位
|
||
var emptyInnerLocations = roadwayLocations
|
||
.Where(l => l.RowFlag == 1
|
||
&& l.Status == "empty"
|
||
&& allLocations.TryGetValue(l.ContiguousCode, out var outer)
|
||
&& outer.Status == "empty")
|
||
.OrderBy(l => l.Layer)
|
||
.ThenBy(l => l.Line)
|
||
.Select(l => new
|
||
{
|
||
LocationCode = l.Code,
|
||
l.ZoneCode,
|
||
l.RoadWay,
|
||
l.Layer,
|
||
l.Line,
|
||
Type = "双伸位-里仓位"
|
||
});
|
||
availableLocations.AddRange(emptyInnerLocations);
|
||
|
||
// 2. 查找里仓位有货,外仓位为空的外仓位
|
||
var availableOuterLocations = roadwayLocations
|
||
.Where(l => l.RowFlag == 0
|
||
&& l.Status == "empty"
|
||
&& allLocations.TryGetValue(l.ContiguousCode, out var inner)
|
||
&& inner.Status == "some")
|
||
.OrderBy(l => l.Layer)
|
||
.ThenBy(l => l.Line)
|
||
.Select(l => new
|
||
{
|
||
LocationCode = l.Code,
|
||
l.ZoneCode,
|
||
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
|
||
});
|
||
}
|
||
}
|
||
} |