wangxinhui
2026-03-19 5f53f4af369a1c7a8a09eb1f45c20a8581d107a9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
using HslCommunication.WebSocket;
using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Common.CommonEnum;
using WIDESEA_Common.LocationEnum;
using WIDESEA_Common.StockEnum;
using WIDESEA_Core;
using WIDESEA_Core.BaseRepository;
using WIDESEA_Core.BaseServices;
using WIDESEA_Core.Const;
using WIDESEA_Core.DB;
using WIDESEA_Core.Enums;
using WIDESEA_Core.Helper;
using WIDESEA_Core.Seed;
using WIDESEA_Core.Utilities;
using WIDESEA_DTO.Basic;
using WIDESEA_IBasicRepository;
using WIDESEA_IBasicService;
using WIDESEA_IStockRepository;
using WIDESEA_Model.Models;
 
namespace WIDESEA_BasicService
{
    public partial class LocationInfoService : ServiceBase<Dt_LocationInfo, ILocationInfoRepository>, ILocationInfoService
    {
        private readonly IUnitOfWorkManage _unitOfWorkManage;
        private readonly IBasicRepository _basicRepository;
        private readonly IStockInfoRepository _stockInfoRepository;
        private readonly IProStockInfoRepository _proStockInfoRepository;
        public ILocationInfoRepository Repository => BaseDal;
 
        public LocationInfoService(ILocationInfoRepository BaseDal, IUnitOfWorkManage unitOfWorkManage, IBasicRepository basicRepository, IStockInfoRepository stockInfoRepository, IProStockInfoRepository proStockInfoRepository) : base(BaseDal)
        {
            _unitOfWorkManage = unitOfWorkManage;
            _basicRepository = basicRepository;
            _stockInfoRepository = stockInfoRepository;
            _proStockInfoRepository = proStockInfoRepository;
        }
 
        /// <summary>
        /// 查询货位对应的RFID及库存信息
        /// </summary>
        /// <param name="locationCodes"></param>
        /// <returns></returns>
        public WebResponseContent GetRfid(string[] locationCodes, int warehouseId = 0)
        {
            try
            {
                // 参数验证
                if (locationCodes == null || locationCodes.Length == 0)
                {
                    return WebResponseContent.Instance.Error("货位编号不能为空");
                }
 
                if (warehouseId == 1)
                {
                    // 查询原料库Dt_StockInfo表,获取RFID及库存信息
                    var rawMaterialRfidList = _stockInfoRepository.QueryData()
                        .Where(x => locationCodes.Contains(x.LocationCode))
                        .Select(x => new {
                            locationCode = x.LocationCode,
                            rfidCode = x.RfidCode,
                            paperRoll = x.MaterielName, // 纸卷名称
                            width = x.MaterielWide, // 门幅(幅宽)
                            barcode = x.PalletCode, // 纸卷条码
                            status = x.StockStatus, // 状态
                            inDate = x.CreateDate // 入库时间(创建时间)
                        })
                        .ToList();
 
                    // 返回结果
                    return WebResponseContent.Instance.OK(data: rawMaterialRfidList);
                }
 
                if (warehouseId == 2)
                {
                    // 替换原有的 .Select(x => new { ... Detail = x.proStockInfoDetails?.FirstOrDefault() ... }) 代码块
                    var finishedProductRfidList = _proStockInfoRepository.Db.Queryable<Dt_ProStockInfo>()
                        .Includes(x => x.proStockInfoDetails)
                        .Where(x => locationCodes.Contains(x.LocationCode))
                        .ToList()
                        .Select(x => new
                        {
                            locationCode = x.LocationCode,
                            rfidCode = x.PalletCode,
                            paperRoll = (x.proStockInfoDetails != null && x.proStockInfoDetails.Count > 0) ? x.proStockInfoDetails[0].ProductName : string.Empty,
                            productName = (x.proStockInfoDetails != null && x.proStockInfoDetails.Count > 0) ? x.proStockInfoDetails[0].ProductName : string.Empty,
                            width = (x.proStockInfoDetails != null && x.proStockInfoDetails.Count > 0) ? x.proStockInfoDetails[0].StockQty : 0,
                            quantity = (x.proStockInfoDetails != null && x.proStockInfoDetails.Count > 0) ? x.proStockInfoDetails[0].StockQty : 0,
                            barcode = x.PalletCode,
                            status = x.StockStatus,
                            inDate = x.CreateDate
                        })
                        .ToList();
 
                    // 返回结果
                    return WebResponseContent.Instance.OK(data: finishedProductRfidList);
                }
                // 如果没有匹配的仓库ID,返回空列表
                return WebResponseContent.Instance.OK(data: new List<object>());
            }
            catch (Exception ex)
            {
                Console.WriteLine($"GetRfid 异常: {ex.Message}");
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
 
        /// <summary>
        /// 批量启用货位
        /// </summary>
        /// <param name="keys">货位主键数组</param>
        /// <returns></returns>
        public WebResponseContent LocationEnableStatus(int[] keys)
        {
            List<Dt_LocationInfo> locationInfos = Repository.QueryData(x => keys.Contains(x.Id));
            locationInfos.ForEach(x =>
            {
                x.EnableStatus = EnableStatusEnum.Normal.ObjToInt();
            });
            Repository.UpdateData(locationInfos);
 
            return WebResponseContent.Instance.OK();
        }
 
        /// <summary>
        /// 批量禁用货位
        /// </summary>
        /// <param name="keys">货位主键数组</param>
        /// <returns></returns>
        public WebResponseContent LocationDisableStatus(int[] keys)
        {
            List<Dt_LocationInfo> locationInfos = Repository.QueryData(x => keys.Contains(x.Id));
            locationInfos.ForEach(x =>
            {
                x.EnableStatus = EnableStatusEnum.Disable.ObjToInt();
            });
            Repository.UpdateData(locationInfos);
 
            return WebResponseContent.Instance.OK();
        }
 
        /// <summary>
        /// 单个启用货位
        /// </summary>
        /// <param name="key">货位主键</param>
        /// <returns></returns>
        public WebResponseContent LocationEnableStatus(int key)
        {
            return LocationEnableStatus(new int[] { key });
        }
 
        /// <summary>
        /// 单个禁用货位
        /// </summary>
        /// <param name="key">货位主键</param>
        /// <returns></returns>
        public WebResponseContent LocationDisableStatus(int key)
        {
            return LocationDisableStatus(new int[] { key });
        }
        /// <summary>
        /// 初始化货位
        /// </summary>
        /// <param name="initializationLocationDTO"></param>
        /// <returns></returns>
        public WebResponseContent InitializationLocation(InitializationLocationDTO initializationLocationDTO)
        {
            try
            {
                (bool, string, object?) result = ModelValidate.ValidateModelData(initializationLocationDTO);
                if (!result.Item1) return WebResponseContent.Instance.Error(result.Item2);
 
                int depth = initializationLocationDTO.Depth;
                List<Dt_LocationInfo> locationInfos = new List<Dt_LocationInfo>();
                for (int i = 0; i < initializationLocationDTO.MaxRow; i++)
                {
                    if ((i + 1) % initializationLocationDTO.MaxRow == 1)
                    {
                        depth = initializationLocationDTO.Depth;
                    }
                    else if ((i + 1) % initializationLocationDTO.MaxRow == initializationLocationDTO.Depth + 1)
                    {
                        depth = 1;
                    }
                    else if ((i + 1) % initializationLocationDTO.MaxRow > 1 && (i + 1) % initializationLocationDTO.MaxRow <= initializationLocationDTO.Depth)
                    {
                        depth -= 1;
                    }
                    else
                    {
                        depth += 1;
                    }
                    for (int j = 0; j < initializationLocationDTO.MaxColumn; j++)
                    {
                        for (int k = 0; k < initializationLocationDTO.MaxLayer; k++)
                        {
                            Dt_LocationInfo locationInfo = new Dt_LocationInfo()
                            {
                                WarehouseId = 0,
                                Columns = j + 1,
                                EnableStatus = EnableStatusEnum.Normal.ObjToInt(),
                                Layer = k + 1,
                                LocationStatus = LocationStatusEnum.Free.ObjToInt(),
                                LocationType = LocationTypeEnum.Undefined.ObjToInt(),
                                RoadwayNo = $"{initializationLocationDTO.Roadway.ToString()}",
                                Row = i + 1,
                                Depth = depth,
                            };
                            locationInfo.LocationCode = $"{locationInfo.RoadwayNo}-{locationInfo.Row.ToString().PadLeft(3, '0')}-{locationInfo.Columns.ToString().PadLeft(3, '0')}-{locationInfo.Layer.ToString().PadLeft(3, '0')}-{locationInfo.Depth.ToString().PadLeft(2, '0')}";
                            locationInfo.LocationName = $"{locationInfo.RoadwayNo}巷道{locationInfo.Row.ToString().PadLeft(3, '0')}行{locationInfo.Columns.ToString().PadLeft(3, '0')}列{locationInfo.Layer.ToString().PadLeft(3, '0')}层{locationInfo.Depth.ToString().PadLeft(2, '0')}深";
                            locationInfos.Add(locationInfo);
                        }
                    }
                }
                BaseDal.AddData(locationInfos);
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
    }
}