服务端的TigerApi 框架,基于.NET6 2024 版本
Ben Lin
2024-09-22 6f0a6b40e86e2024c218d1d38cbdc5515fb95209
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
using Tiger.Model;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Rhea.Common;
using System.Net;
using System.Linq;
using Newtonsoft.Json;
using Tiger.IBusiness;
using Microsoft.AspNetCore.Http;
using Tiger.Model.Entitys.MES.BizMesWoBatch;
using Tiger.Model.Entitys.MES.BizMesWo;
using Tiger.Model.Entitys.MES.Position;
 
namespace Tiger.Business.MES
{
    public partial class BizMesWo : IBIZ_MES_WO
    {
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="wo"></param>
        /// <returns></returns>
        public async Task<ApiAction> SaveMesWo(BIZ_MES_WO wo)
        {
            var result = new ApiAction();
            try
            {
                var _wo = Biz.Db.Queryable<BIZ_MES_WO>().Where(x => x.ORDER_NO == wo.ORDER_NO).First();
                if (_wo != null && _wo.STATUS != (int)BIZ_MES_WO.STATUSs.Init)
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"工单已经存在且不是初始化状态,不能保存修改!");
                    return result;
                }
                var db = Biz.Db;
                var dbTran = db.UseTran(() =>
                {
                    var y = db.Storageable(wo)
                       .WhereColumns(t => new { t.ORDER_NO, t.GHOST_ROW })
                       .ToStorage();
                    y.AsInsertable.ExecuteCommand();
                    y.AsUpdateable.IgnoreColumns(x => x.ID).ExecuteCommand();
                });
                if (!dbTran.IsSuccess)
                {
                    result.IsSuccessed = false;
                    result.Message = $"保存工单异常";
                }
            }
            catch (Exception ex)
            {
                result.CatchExceptionWithLog(ex, "保存工单异常");
            }
            return await Task.FromResult(result);
        }
 
        /// <summary>
        /// 删除工单
        /// </summary>
        /// <param name="woId"></param>
        /// <returns></returns>
        public async Task<ApiAction> DeleteMesWo(string woId)
        {
            var result = new ApiAction();
            try
            {
                //查询是否已经有工单在用
                var _wo = Biz.Db.Queryable<BIZ_MES_WO>().Where(x => x.ID == woId).First();
                if (_wo != null && _wo.STATUS != (int)BIZ_MES_WO.STATUSs.Init)
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"工单不是初始化状态,不能删除!");
                    return result;
                }
                var db = Biz.Db;
                var dbTran = db.UseTran(() =>
                {
                    db.Deleteable<BIZ_MES_WO>().Where(x => x.ID == woId).ExecuteCommand();
                });
                if (!dbTran.IsSuccess)
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"删除工单异常");
                }
            }
            catch (Exception ex)
            {
                result.CatchExceptionWithLog(ex, "删除工单异常");
            }
            return await Task.FromResult(result);
        }
 
        /// <summary>
        /// 下发时保存批次工单信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<ApiAction> SaveMesBatchWo(SaveWoBatchInput input)
        {
            var result = new ApiAction();
            try
            {
                //保存前的判断,并生成批次号
                var _wo = await Biz.Db.Queryable<BIZ_MES_WO>().Where(x => x.ORDER_NO == input.Wo.ORDER_NO).FirstAsync();
                //if (_wo.STATUS > (int)BIZ_MES_WO.STATUSs.Init)
                //{
                //    result.IsSuccessed = false;
                //    result.LocaleMsg = new($"工单不是初始化状态,不能下发!");
                //    return result;
                //}
                if (_wo.STATUS > (int)BIZ_MES_WO.STATUSs.Working)
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"工单不是初始化状态,不能下发!");
                    return result;
                }
                var batchs = await Biz.Db.Queryable<BIZ_MES_WO_BATCH>().Where(x => x.ORDER_NO == input.Wo.ORDER_NO).ToListAsync();
                if (batchs.Count > 0 && batchs.Sum(x => x.PLAN_QTY) + input.WoBatch.PLAN_QTY > _wo.PLAN_QTY)
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"工单下发的数量超过工单计划数量,不能下发!");
                    return result;
                }
                if (Biz.Db.Queryable<BIZ_MES_WO_BATCH>().Any(x => x.ORDER_NO == input.Wo.ORDER_NO && x.ACT_LINE == input.WoBatch.ACT_LINE && x.STATUS> BIZ_MES_WO.STATUSs.Imported.GetValue() && x.STATUS< BIZ_MES_WO.STATUSs.Closed.GetValue()))
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"工单批次在线体[{input.WoBatch.ACT_LINE}]中状态为[{input.WoBatch.STATUS.GetEnumDesc<BIZ_MES_WO.STATUSs>()}],不能下发!");
                    return result;
                }
                WoContext.RemoveBatch(input.WoBatch.BATCH_NO);
 
                var _batchWos = await Biz.Db.Queryable<V_MES_WO_BATCH>().Where(x => x.ORDER_NO == input.WoBatch.ORDER_NO).ToListAsync();
                if (_batchWos.Count > 0)
                {
                    input.WoBatch.BATCH_NO = $"{input.WoBatch.ORDER_NO}-{(_batchWos.Max(x => x.BATCH) + 1).ToString("D2")}";
                }
                else
                {
                    input.WoBatch.BATCH_NO = $"{input.WoBatch.ORDER_NO}-01";
                }
 
                var db = Biz.Db;
                var dbTran = db.UseTran(() =>
                {
                    var y = db.Storageable(input.Wo)
                       .WhereColumns(t => new { t.ORDER_NO, t.GHOST_ROW })
                       .ToStorage();
                    y.AsInsertable.ExecuteCommand();
                    y.AsUpdateable.IgnoreColumns(x => x.ID).ExecuteCommand();
 
                    db.Insertable(input.WoBatch).ExecuteCommand();
                });
                if (!dbTran.IsSuccess)
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"保存批次工单异常");
                }
            }
            catch (Exception ex)
            {
                result.CatchExceptionWithLog(ex, "保存批次工单异常");
            }
            return result;
        }
 
        /// <summary>
        /// 更新工单状态
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiAction> UpdateWoStatus(BizMesWoInput input)
        {
            var result = new ApiAction();
            try
            {
                //保存前的判断,并生成批次号
                var _wo = await Biz.Db.Queryable<BIZ_MES_WO>().Where(x => x.ORDER_NO == input.WorkOrder).FirstAsync();
                if (_wo != null)
                {
                    _wo.STATUS = input.Status < 0 ? _wo.STATUS : input.Status;
                    _wo.ROUTE_STATUS = input.RouteStatus;
                }
                else
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"工单[{input.WorkOrder}]不存在");
                    return result;
                }
 
                var db = Biz.Db;
                var dbTran = db.UseTran(() =>
                {
                    db.Updateable(_wo, input.UserId).UpdateColumns(x => new { x.STATUS }).ExecuteCommand();
                });
                if (!dbTran.IsSuccess)
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"更新工单状态异常");
                }
            }
            catch (Exception ex)
            {
                result.CatchExceptionWithLog(ex, "更新工单状态异常");
            }
            return result;
        }
 
        /// <summary>
        /// 更新工单批次状态
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiAction> UpdateWoBatchStatus(BizMesWoInput input)
        {
            var result = new ApiAction();
            try
            {
                //保存前的判断
                var _woBatch = await Biz.Db.Queryable<BIZ_MES_WO_BATCH>().Where(x => x.BATCH_NO == input.WoBatch).FirstAsync();
                if (_woBatch != null)
                {
                    _woBatch.STATUS = input.Status < 0 ? _woBatch.STATUS : input.Status;
                    if (input.Status == BIZ_MES_WO_BATCH.STATUSs.Paused.GetValue()) {
                        WoContext.RemoveBatch(input.WoBatch);
                    }
                }
                else
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"工单批次[{input.WoBatch}]不存在");
                    return result;
                }
 
                var db = Biz.Db;
                var dbTran = db.UseTran(() =>
                {
                    db.Updateable(_woBatch, input.UserId).UpdateColumns(x=> new { x.STATUS}).ExecuteCommand();
                });
                if (!dbTran.IsSuccess)
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"更新工单批次状态异常");
                }
            }
            catch (Exception ex)
            {
                result.CatchExceptionWithLog(ex, "更新工单批次状态异常");
            }
            return result;
        }
 
        /// <summary>
        /// 工单模板变量修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<ApiAction> AddOrEditLabelVarByWorkOrder(BAS_LABEL_VAR_WO input)
        {
            var result = new ApiAction();
            try
            {
                //保存前的判断
                if (input.WORK_ORDER.IsNullOrEmpty())
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"工单号不能为空!");
                    return result;
                }
 
                var db = Biz.Db;
                var dbTran = db.UseTran(() =>
                {
                    var y = db.Storageable(input)
                       .WhereColumns(t => new { t.LABEL_ID, t.WORK_ORDER, t.VAR_NAME, t.GHOST_ROW })
                       .ToStorage();
                    y.AsInsertable.ExecuteCommand();
                    y.AsUpdateable.IgnoreColumns(x => x.ID).ExecuteCommand();
                });
                if (!dbTran.IsSuccess)
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"更新工单模板变量异常");
                }
            }
            catch (Exception ex)
            {
                result.CatchExceptionWithLog(ex, "更新工单模板变量异常");
            }
            return await Task.FromResult(result);
        }
 
        /// <summary>
        /// 获取工单模板变量
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<ApiAction<QueryAble<BAS_LABEL_VAR_WO>>> GetLabelVarByWorkOrder(BizLabelVarWoInput input)
        {
            var result = new ApiAction<QueryAble<BAS_LABEL_VAR_WO>>();
            try
            {
                QueryAble<BAS_LABEL_VAR_WO> query = new();
                if (input.WorkOrder.IsNullOrEmpty())
                {
                    result.IsSuccessed = false;
                    result.LocaleMsg = new($"工单号不能为空!");
                    return result;
                }
                query.page = await Biz.Db.Queryable<BAS_LABEL_VAR_WO>().Where(q=>q.WORK_ORDER.Equals(input.WorkOrder) && q.LABEL_ID.Equals(input.LabelId)).ToPageAsync(input.pageIndex, input.pageSize);
                query.Items = query.page.data;
                //如果不存在,则返回初始数据
                if (query.page.totals == 0) {
                    query.page = await Biz.Db.Queryable<BAS_LABEL_VAR_WO>().Where(q => SqlFunc.IsNullOrEmpty(q.WORK_ORDER) && q.LABEL_ID.Equals(input.LabelId)).ToPageAsync(input.pageIndex, input.pageSize);
                    query.Items = query.page.data;
                }
                result.Data = query;
            }
            catch (Exception ex)
            {
                result.CatchExceptionWithLog(ex, "获取工单模板变量异常");
            }
            return result;
        }
    }
}