chengsc
2025-04-12 3a3c50e8a4f1bca47daaadd03f4138a5d4a2227c
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Model.InterFaceModel;
using Model.ModelDto.LogDto;
using Model.ModelDto.SysDto;
using SqlSugar;
using WMS.BLL.LogServer;
using WMS.Entity.Context;
using WMS.Entity.DataEntity;
using WMS.Entity.SysEntity;
using WMS.IBLL.ILogServer;
using WMS.IBLL.ISysServer;
using WMS.IDAL.ISysInterface;
 
namespace WMS.BLL.SysServer
{
    public class PalletsServer : IPalletsServer
    {
        public IPalletsRepository PalletsRst { get; set; }
        private readonly IOperationSysServer _operation; //操作日志
        public PalletsServer(IPalletsRepository palletsRst, IOperationSysServer operation)
        {
            PalletsRst = palletsRst;
            _operation = operation;
        }
        /// <summary>
        /// 查询托盘表信息
        /// </summary>
        /// <param name="palletNo">托盘码</param>
        /// <param name="status">状态 0未使用 1使用中</param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<PalletsDto> GetPalletsList(string palletNo, string status, int page, int limit, out int count)
        {
            try
            {
                //创建表达式
                Expression<Func<SysPallets, bool>> item = Expressionable.Create<SysPallets>()
                    .AndIF(!string.IsNullOrWhiteSpace(palletNo), it => it.PalletNo.Contains(palletNo.Trim()))
                    .AndIF(!string.IsNullOrWhiteSpace(status), it => it.Status == status)
                    .ToExpression();//注意 这一句 不能少
 
 
                var data = PalletsRst.GetAllByOrderPageAsync(item, limit, page, out int counts)
                    .Includes(x => x.CreateUserInfo)
                    .ToList();
                count = counts;
                return data.Select(m => new PalletsDto()
                {
                    Id = m.Id,
                    PalletNo = m.PalletNo,
                    Standard = m.Standard,
                    Type = m.Type == "0" ? "托盘" : m.Type == "1" ? "中转箱" : "",
                    Status = m.Status,
                    LastUse = m.LastUse,
                    CreateTime = m.CreateTime,
                    CreateUserName = m.CreateUserInfo == null ? "" : m.CreateUserInfo.RealName
                }).ToList();
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
 
        public string GetPalletsNo(string palletNo)
        {
            try
            {
                var time = DateTime.Now.ToString("yyyy");
                var time2 = time.Substring(2, 2);
                int codeId;
 
                if (!string.IsNullOrWhiteSpace(palletNo))
                {
                    var code = PalletsRst.GetAllWhereAsync(m => m.PalletNo == palletNo).First();
                    return code.PalletNo;
                }
                else
                {
                    var code = PalletsRst.GetAllAsync().OrderByDescending(m => m.PalletNo).First();
 
                    if (code != null)
                    {
                        string riQi = code.PalletNo.Substring(1, 2);
                        if (riQi == time2)
                        {
                            codeId = int.Parse(code.PalletNo.Substring(3, 5)) + 1;
                        }
                        else
                        {
                            codeId = int.Parse("00001");
                        }
                    }
                    else
                    {
                        codeId = int.Parse("00001");
                    }
                    int liuShuiId = codeId;
                    var pallet = "T"+time2 + Convert.ToString(liuShuiId).PadLeft(5, '0');
                    return pallet;
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
 
        /// <summary>
        /// 添加桶信息并增加库存
        /// </summary>
        /// <param name="palletNo"></param>
        /// <param name="locatNo"></param>
        /// <param name="userId"></param>
        /// <exception cref="Exception"></exception>
        public void AddPallets(string palletNo,string locatNo, string deviceCode, int userId)
        {
            var db = DataContext.Db;
            try
            {                
                if (string.IsNullOrEmpty(palletNo))
                {
                    throw new Exception("请输入桶号");
                }
                if (string.IsNullOrEmpty(locatNo))
                {
                    throw new Exception("请输入储位地址");
                }
                var palletModel = db.Queryable<SysPallets>().First(w => w.PalletNo == palletNo && w.IsDel == "0");
                if (palletModel != null)
                {
                    throw new Exception("该桶号已存在");
                }
                var locatModel= db.Queryable<SysStorageLocat>().First(w => w.LocatNo == locatNo && w.IsDel == "0");
                if (locatModel == null)
                {
                    throw new Exception("储位地址不存在");
                }
                if (locatModel.Status != "0")
                {
                    throw new Exception("该储位地址非空闲状态");
                }
                var areaModel = db.Queryable<SysStorageArea>().First(w => w.IsDel == "0" && w.AreaNo == locatModel.AreaNo);
                if (areaModel == null)
                {
                    throw new Exception("储位地址所属区域不存在");
                }
                if (!areaModel.AreaName.Contains("净桶"))
                {
                    throw new Exception("新添加的桶只能绑定净桶区");
                }
                if (!string.IsNullOrEmpty(deviceCode))
                {
                    var areaInfo = db.Queryable<SysStorageArea>().First(w => w.IsDel == "0" && w.DeviceCode.Contains(deviceCode));
                    if (areaInfo == null)
                    {
                        throw new Exception("未查到设备所属区域信息");
                    }
                    //125设备是接料和混料一体机
                    if (deviceCode != "125")
                    {
                        if (!areaInfo.AreaName.Contains("接料设备"))
                        {
                            throw new Exception("只能绑定接料设备");
                        }
                        if (areaInfo.WareHouseNo != "M03")
                        {
                            throw new Exception("只有大单体车间才能绑定设备");
                        }
                    }                    
                }
                //开启事务
                db.BeginTran();
 
                palletModel = new SysPallets();
                palletModel.PalletNo = palletNo;
                palletModel.Type = "0";
                palletModel.Status = "0";
                palletModel.CreateUser = userId;
                //添加桶
                db.Insertable(palletModel).ExecuteCommand();
 
                var comTime = DateTime.Now;
                var model = new DataStockDetail()
                {
                    ASNNo = "",
                    Qty = 0,
                    LockQty = 0,
                    FrozenQty = 0,
                    InspectQty = 0,
                    WareHouseNo = locatModel.WareHouseNo,//所属仓库
                    RoadwayNo = "",//所属巷道
                    AreaNo = locatModel.AreaNo,//所属区域
                    LocatNo = locatModel.LocatNo,//储位地址
                    PalletNo = palletNo,
                    PalletNo2 = "",
                    PalletNo3 = "",
                    PalletTags = "0",
                    CompleteTime = comTime,
                    ProductionTime = null,
                    ExpirationTime = null,
                    Status = "0",
                    InspectMark = "0",
                    InspectStatus = "0",//待检验
                    BitPalletMark = "0",
                    PackagNo = "",
                    IsBale = "0",
                    IsBelt = "0",
 
                    SkuNo = "",
                    SkuName = "",
                    LotNo = "",
 
                    PalletStatus = "0",//净桶
 
                    UDF1 = deviceCode,
 
                    IsDel = "0",
                    CreateUser = userId,
                    CreateTime = comTime,
                    UpdateTime=comTime,                   
                };
                //插入库存明细
                db.Insertable(model).ExecuteCommand();
 
                //更新储位状态
                locatModel.Status = "1";//有物品
                db.Updateable(locatModel).ExecuteCommand();
 
                //提交事务
                db.CommitTran();
            }
            catch (Exception e)
            {
                //回滚事务
                db.RollbackTran();
                throw new Exception(e.Message);
            }
        }
 
    }
}