wanshenmean
2026-02-11 e6b190354191122069b1a0518f050d6504f7ec5e
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
using SqlSugar;
using WIDESEA_Common.CommonEnum;
using WIDESEA_Common.LocationEnum;
using WIDESEA_Core;
using WIDESEA_Core.BaseRepository;
using WIDESEA_Core.BaseServices;
using WIDESEA_Core.Utilities;
using WIDESEA_DTO.Basic;
using WIDESEA_IBasicService;
using WIDESEA_Model.Models;
 
namespace WIDESEA_BasicService
{
    public partial class LocationInfoService : ServiceBase<Dt_LocationInfo, IRepository<Dt_LocationInfo>>, ILocationInfoService
    {
        public IRepository<Dt_LocationInfo> Repository => BaseDal;
 
        public LocationInfoService(IRepository<Dt_LocationInfo> BaseDal) : base(BaseDal)
        {
        }
 
        /// <summary>
        /// 批量启用货位
        /// </summary>
        public WebResponseContent LocationEnableStatus(int[] keys)
        {
            var locationInfos = Repository.QueryData(x => keys.Contains(x.Id));
            locationInfos.ForEach(x => x.EnableStatus = EnableStatusEnum.Normal.GetHashCode());
            Repository.UpdateData(locationInfos);
            return WebResponseContent.Instance.OK();
        }
 
        /// <summary>
        /// 批量禁用货位
        /// </summary>
        public WebResponseContent LocationDisableStatus(int[] keys)
        {
            var locationInfos = Repository.QueryData(x => keys.Contains(x.Id));
            locationInfos.ForEach(x => x.EnableStatus = EnableStatusEnum.Disable.GetHashCode());
            Repository.UpdateData(locationInfos);
            return WebResponseContent.Instance.OK();
        }
 
        /// <summary>
        /// 单个启用货位
        /// </summary>
        public WebResponseContent LocationEnableStatus(int key) => LocationEnableStatus(new[] { key });
 
        /// <summary>
        /// 单个禁用货位
        /// </summary>
        public WebResponseContent LocationDisableStatus(int key) => LocationDisableStatus(new[] { key });
 
        /// <summary>
        /// 初始化货位
        /// </summary>
        public WebResponseContent InitializationLocation(InitializationLocationDTO dto)
        {
            try
            {
                var (isValid, errorMsg, _) = ModelValidate.ValidateModelData(dto);
                if (!isValid) return WebResponseContent.Instance.Error(errorMsg);
 
                var locationInfos = new List<Dt_LocationInfo>();
                int depth = dto.Depth;
 
                for (int row = 1; row <= dto.MaxRow; row++)
                {
                    depth = CalculateDepth(row, dto.MaxRow, dto.Depth, depth);
 
                    for (int col = 1; col <= dto.MaxColumn; col++)
                    {
                        for (int layer = 1; layer <= dto.MaxLayer; layer++)
                        {
                            var location = CreateLocationInfo(dto.Roadway, row, col, layer, depth);
                            locationInfos.Add(location);
                        }
                    }
                }
 
                BaseDal.AddData(locationInfos);
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
 
        private static int CalculateDepth(int row, int maxRow, int maxDepth, int currentDepth)
        {
            int mod = row % maxRow;
            if (mod == 1) return maxDepth;
            if (mod == maxDepth + 1) return 1;
            if (mod > 1 && mod <= maxDepth) return currentDepth - 1;
            return currentDepth + 1;
        }
 
        private static Dt_LocationInfo CreateLocationInfo(string roadwayNo, int row, int col, int layer, int depth)
        {
            return new Dt_LocationInfo
            {
                WarehouseId = 0,
                Row = row,
                Column = col,
                Layer = layer,
                Depth = depth,
                RoadwayNo = roadwayNo,
                EnableStatus = EnableStatusEnum.Normal.GetHashCode(),
                LocationStatus = LocationStatusEnum.Free.GetHashCode(),
                LocationType = LocationTypeEnum.Undefined.GetHashCode(),
                LocationCode = $"{roadwayNo}-{row:D3}-{col:D3}-{layer:D3}-{depth:D2}",
                LocationName = $"{roadwayNo}巷道{row:D3}行{col:D3}列{layer:D3}层{depth:D2}深"
            };
        }
 
 
 
        /// <summary>
        /// 获取空闲货位信息(根据巷道查询)
        /// </summary>
        public async Task<Dt_LocationInfo?> GetLocationInfo(string roadwayNo)
        {
            var locations = await BaseDal.QueryDataAsync(x =>
                x.EnableStatus == EnableStatusEnum.Normal.GetHashCode() &&
                x.RoadwayNo == roadwayNo &&
                x.LocationStatus == LocationStatusEnum.Free.GetHashCode());
 
            return locations?
                .OrderBy(x => x.Layer)
                .ThenBy(x => x.Depth)
                .ThenBy(x => x.Column)
                .ThenBy(x => x.Row)
                .FirstOrDefault();
        }
 
        /// <summary>
        /// 获取货位信息(根据巷道和货位编码查询)
        /// </summary>
        public async Task<Dt_LocationInfo?> GetLocationInfo(string roadwayNo, string locationCode)
        {
            return await BaseDal.QueryFirstAsync(x => x.RoadwayNo == roadwayNo && x.LocationCode == locationCode);
        }
 
        /// <summary>
        /// 更新货位信息
        /// </summary>
        public async Task<bool> UpdateLocationInfoAsync(Dt_LocationInfo locationInfo)
        {
            return await BaseDal.UpdateDataAsync(locationInfo);
        }
    }
}