using HslCommunication.WebSocket; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using System.Collections.Generic; using WIDESEA_Common.CommonEnum; using WIDESEA_Common.LocationEnum; using WIDESEA_Core; using WIDESEA_Core.BaseController; using WIDESEA_Core.Enums; using WIDESEA_DTO.Basic; using WIDESEA_IBasicRepository; using WIDESEA_IBasicService; using WIDESEA_Model.Models; namespace WIDESEA_WMSServer.Controllers.Basic { /// /// 货位 /// [Route("api/LocationInfo")] [ApiController] public class LocationInfoController : ApiBaseController { private readonly ILocationInfoRepository _repository; public LocationInfoController(ILocationInfoService service, ILocationInfoRepository repository) : base(service) { _repository = repository; } /// /// 初始化货位 /// /// /// [HttpPost, Route("InitializationLocation"), AllowAnonymous] public WebResponseContent InitializationLocation([FromBody] InitializationLocationDTO initializationLocationDTO) { return Service.InitializationLocation(initializationLocationDTO); } /// /// 货位分配 /// /// /// /// [HttpPost, HttpGet, Route("AssignLocation"), AllowAnonymous] public Dt_LocationInfo? AssignLocation(string roadwayNo, int palletType, int warehouseId) { return Service.AssignLocation(roadwayNo, palletType, warehouseId); } /// /// 启用货位 /// /// /// [HttpPost, Route("LocationEnableStatus")] public WebResponseContent LocationEnableStatus([FromBody] int[] keys) { return Service.LocationEnableStatus(keys); ; } /// /// 禁用货位 /// /// /// [HttpPost, Route("LocationDisableStatus")] public WebResponseContent LocationDisableStatus([FromBody] int[] keys) { return Service.LocationDisableStatus(keys); ; } [HttpPost, Route("UpdateLocationStatus"), AllowAnonymous] public WebResponseContent UpdateLocationStatus(string locationCode, int palletType, LocationStatusEnum locationStatus, int warehousId) { try { Service.UpdateLocationStatus(locationCode, palletType, locationStatus, warehousId); return WebResponseContent.Instance.OK(); } catch (Exception e) { return WebResponseContent.Instance.Error(e.Message); } } /// /// 查询条件货位 /// /// /// /// [HttpPost, HttpGet, Route("GetLocationStatus")] public WebResponseContent GetLocationStatus(string roadwayNo, int warehouseId = 0) { // 定义返回数据结构 var result = new { row1 = new List(), // 巷道第1排(东面) row2 = new List() // 巷道第2排(西面) }; // 查询巷道下所有货位 var locations = _repository.QueryData(x => x.RoadwayNo == roadwayNo); // 获取所有层,按从高到低排序 var layers = locations.Select(x => x.Layer).Distinct().OrderByDescending(x => x).ToList(); // 处理每一层 foreach (var layer in layers) { // 获取当前层的货位 var layerLocations = locations.Where(x => x.Layer == layer); // 第1排数据(东面) var row1Locations = layerLocations.Where(x => x.Row == 1) .OrderBy(x => x.Columns) .Select(x => new { layer = x.Layer.ToString().PadLeft(2, '0'), row = x.Row.ToString().PadLeft(2, '0'), column = x.Columns.ToString().PadLeft(2, '0'), locationCode = x.LocationCode, location_lock = x.LocationStatus }).ToList(); // 第2排数据(西面) var row2Locations = layerLocations.Where(x => x.Row == 2) .OrderBy(x => x.Columns) .Select(x => new { layer = x.Layer.ToString().PadLeft(2, '0'), row = x.Row.ToString().PadLeft(2, '0'), column = x.Columns.ToString().PadLeft(2, '0'), locationCode = x.LocationCode, location_lock = x.LocationStatus }).ToList(); // 添加到结果中 result.row1.Add(new { layer, locationObj = row1Locations }); result.row2.Add(new { layer, locationObj = row2Locations }); } return WebResponseContent.Instance.OK("成功", result); } /// /// 查询全部巷道 /// /// [HttpPost, HttpGet, Route("GetRow")] public WebResponseContent GetRow() { // 保持兼容旧接口,返回巷道列表 List roadwayList = _repository.QueryData().Select(x => x.RoadwayNo).Distinct().ToList(); return WebResponseContent.Instance.OK("成功", roadwayList); } /// /// 查询全部巷道 /// /// [HttpPost, HttpGet, Route("GetRoadway")] public WebResponseContent GetRoadway() { List roadwayList = _repository.QueryData().Select(x => x.RoadwayNo).Distinct().ToList(); return WebResponseContent.Instance.OK("成功", roadwayList); } /// /// 查询货位RFID /// /// /// [HttpPost, Route("GetRfid")] public WebResponseContent GetRfid([FromBody] dynamic requestData) { try { if (requestData == null) { return WebResponseContent.Instance.Error("请求数据为空"); } // 检查locationCodes字段是否存在且不为null(兼容locationCode) if (requestData.locationCodes == null && requestData.locationCode == null) { return WebResponseContent.Instance.Error("货位编号数组不能为空"); } // 转换为string[],优先使用locationCodes string[] locationCode = null; var locationCodeField = requestData.locationCodes ?? requestData.locationCode; if (locationCodeField is Newtonsoft.Json.Linq.JArray) { locationCode = ((Newtonsoft.Json.Linq.JArray)locationCodeField).ToObject(); } else { return WebResponseContent.Instance.Error("货位编号必须是数组格式"); } // 检查warehouseId字段是否存在 int warehouseId = 0; if (requestData.warehouseId != null) { warehouseId = Convert.ToInt32(requestData.warehouseId); } return Service.GetRfid(locationCode, warehouseId); } catch (Exception ex) { return WebResponseContent.Instance.Error($"参数解析失败: {ex.Message}"); } } } }