yanjinhui
2025-03-07 aeb32ca2cc420266734c782df01b27be617e6943
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_ISerialPortRepository;
using WIDESEA_SerialPortRepository;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.BaseRepository;
using WIDESEAWCS_Core.BaseServices;
using WIDESEAWCS_DTO.SerialPort;
using WIDESEAWCS_ITaskInfoRepository;
using WIDESEAWCS_ITaskInfoService;
using WIDESEAWCS_Model.Models;
 
namespace WIDESEAWCS_TaskInfoService
{
    public class NjTaskServer : ServiceBase<Dt_NjTask, INjTaskRepository>, INjTaskServer
    {
        private readonly ITemplateRepository _templateRepository;
        private readonly INJtakeHistoryRepository _nJtakeHistoryRepository;
        private readonly IPutakeRepository _putakeRepository;
 
        private IUnitOfWorkManage _unitOfWorkManage;
 
 
 
        public NjTaskServer(INjTaskRepository BaseDal, ITemplateRepository templateRepository, INJtakeHistoryRepository nJtakeHistoryRepository,
            IPutakeRepository putakeRepository , IUnitOfWorkManage unitOfWorkManage) : base(BaseDal)
        {
            _templateRepository = templateRepository;
            _nJtakeHistoryRepository = nJtakeHistoryRepository;
            _putakeRepository = putakeRepository;
            _unitOfWorkManage = unitOfWorkManage;//数据库事务
            
        }
 
        public WebResponseContent Fuzzyquery(string trainkind, string trainnum, string track, string coachnum, string bogie, string processdept)
        {
            try
            {
                var query = BaseDal.Db.Queryable<Dt_NjTask>();
                // 生成 OR 查询条件 即任意字段都可以查看
                if (!string.IsNullOrEmpty(trainkind) || !string.IsNullOrEmpty(trainnum) ||
                    !string.IsNullOrEmpty(track) || !string.IsNullOrEmpty(coachnum) ||
                    !string.IsNullOrEmpty(bogie) || !string.IsNullOrEmpty(processdept))
                {
                    query = query.Where(x =>
                        (!string.IsNullOrEmpty(trainkind) && x.trainKind.Contains(trainkind)) ||
                        (!string.IsNullOrEmpty(trainnum) && x.trainNum.Contains(trainnum)) ||
                        (!string.IsNullOrEmpty(track) && x.track.Contains(track)) ||
                        (!string.IsNullOrEmpty(coachnum) && x.coachNum.Contains(coachnum)) ||
                        (!string.IsNullOrEmpty(bogie) && x.bogie.Contains(bogie)) ||
                        (!string.IsNullOrEmpty(processdept) && x.processDept.Contains(processdept))
                    );
                }
 
                var result = query.ToList(); // 执行查询 .ToList()查询列表, .InSingle(1); // 根据查询单个实体
                return new WebResponseContent { Status = true, Data = result };
            }
            catch (Exception ex)
            {
 
                return new WebResponseContent { Status = false, Message = "查看失败:" + ex.Message };
            }
 
        }
 
       
        //保存模板
        public WebResponseContent Submtandsave(NjtakeDTO njtakeDTO)
        {
            try
            {
 
                #region 自定义命名给Njtaskid
                // 获取今天的日期部分,如 "KH0306"
                string todayPrefix = $"KH{DateTime.Now:MMdd}";
 
                // 查询数据库,找出当天最大的 NJtaskID
                var lastTask = BaseDal.QueryData(i => i.NJtaskID.StartsWith(todayPrefix))
                                      .OrderByDescending(i => i.NJtaskID)
                                      .FirstOrDefault();
 
                int nextNumber = 1; // 默认编号
 
                if (lastTask != null)
                {
                    // 获取后两位分钟编号
                    string lastNumberPart = lastTask.NJtaskID.Substring(6, 2);
                    if (int.TryParse(lastNumberPart, out int lastNumber))
                    {
                        nextNumber = lastNumber + 1;
                    }
                }
 
                // 生成 NJtaskID,例如 KH030601, KH030602...
                string CustomizeID = $"{todayPrefix}{nextNumber:D2}";
                #endregion
 
 
 
                //更具模板id来查询是否存在该条数据
                var existtepm = _templateRepository.QueryFirst(i => i.TemplateID == njtakeDTO.Tpid);
                var Temp = new Dt_Template(); //如果你要更新那么要先找到,否则你这样是创建一个新的对象      
                //如果没有模板id那么就之间创建模板
                if (existtepm == null)
                {
                               
                        Temp.TemplateName = njtakeDTO.takename;
                        Temp.TakeName = njtakeDTO.takename;
                        Temp.jiShuYuan = njtakeDTO.jishuyuan;
                        Temp.gongZhang = njtakeDTO.gongzhang;
                        Temp.zhiJianYuan_Dq = njtakeDTO.zhijianyuan_dq;
                        Temp.zhiJianYuan_Lc = njtakeDTO.zhijianyuan_lc;
                        Temp.zhiJianYuan_Dg = njtakeDTO.zhijianyuan_dg;
                        Temp.liJu_Dg = njtakeDTO.liju_dq;
                        Temp.liJu_Dq = njtakeDTO.liju_dq;
                        Temp.liJu_Lc = njtakeDTO.liju_lc;
                        Temp.fuZhu_Dg = njtakeDTO.fuzhu_dg;
                        Temp.fuZhu_Dq = njtakeDTO.fuzhu_dq;
                        Temp.fuZhu_Lc = njtakeDTO.fuzhu_lc;
                           
                 
 
                }
                //如果有就在模板上进行更改
                else
                {
                    existtepm.zhiJianYuan_Dq = njtakeDTO.zhijianyuan_dq;
                    existtepm.zhiJianYuan_Lc = njtakeDTO.zhijianyuan_lc;
                    existtepm.zhiJianYuan_Dg = njtakeDTO.zhijianyuan_dg;
                    existtepm.liJu_Dg = njtakeDTO.liju_dq;
                    existtepm.liJu_Dq = njtakeDTO.liju_dq;
                    existtepm.liJu_Lc = njtakeDTO.liju_lc;
                    existtepm.fuZhu_Dg = njtakeDTO.fuzhu_dg;
                    existtepm.fuZhu_Dq = njtakeDTO.fuzhu_dq;
                    existtepm.fuZhu_Lc = njtakeDTO.fuzhu_lc;
                 
                }
                //先根据id判断是否有该任务
                var task = BaseDal.QueryFirst(i => i.NJtaskID == njtakeDTO.Njtakeid);
                if (task == null)
                {
                    return new WebResponseContent { Status = false, Message = "没有找到" };
                }
 
                task.Taskstatus = "已派工";
 
                if (existtepm == null)
                {
                    task.NJtaskID = CustomizeID;
                    task.TakeName = njtakeDTO.takename;
                    task.jiShuYuan = njtakeDTO.jishuyuan;
                    task.zhiJianYuan_DG = njtakeDTO.zhijianyuan_dg;
                    task.zhiJianYuan_DQ = njtakeDTO.zhijianyuan_dq;
                    task.zhiJianYuan_LC = njtakeDTO.zhijianyuan_lc;
                    task.gongZhang = njtakeDTO.gongzhang;
                    task.liJu_DG = njtakeDTO.liju_dg;
                    task.liJu_DQ = njtakeDTO.liju_dq;
                    task.liJu_LC = njtakeDTO.liju_lc;
                    task.fuZhu_DG = njtakeDTO.fuzhu_dg;
                    task.fuZhu_DQ = njtakeDTO.fuzhu_dq;
                    task.fuZhu_LC = njtakeDTO.fuzhu_lc;
                    task.dispatchTime = DateTime.Now;
                }
                else
                {
                    task.TakeName = existtepm.TakeName; //从模板中拿值
                    task.jiShuYuan = existtepm.jiShuYuan;
                    task.zhiJianYuan_DG = njtakeDTO.zhijianyuan_dg;
                    task.zhiJianYuan_DQ = njtakeDTO.zhijianyuan_dq;
                    task.zhiJianYuan_LC = njtakeDTO.zhijianyuan_lc;
                    task.gongZhang = existtepm.gongZhang;
                    task.liJu_DG = njtakeDTO.liju_dg;
                    task.liJu_DQ = njtakeDTO.liju_dq;
                    task.liJu_LC = njtakeDTO.liju_lc;
                    task.fuZhu_DG = njtakeDTO.fuzhu_dg;
                    task.fuZhu_DQ = njtakeDTO.fuzhu_dq;
                    task.fuZhu_LC = njtakeDTO.fuzhu_lc;
                    task.dispatchTime = DateTime.Now;
                }
 
 
                // 插入到任务历史表
                var history = new Dt_NJtakeHistory
                {
                    taskID = task.id,
                    trainKind = task.trainKind,
                    TakeName = task.TakeName,
                    trainNum = task.trainNum,
                    track = task.track,
                    coachNum = task.coachNum,
                    bogie = task.bogie,
                    processDept = task.processDept,
                    jiShuYuan = task.jiShuYuan,
                    zhiJianYuan_LC = task.zhiJianYuan_LC,
                    zhiJianYuan_DG = task.zhiJianYuan_DG,
                    zhiJianYuan_DQ = task.zhiJianYuan_DQ,
                    gongZhang = task.gongZhang,
                    liJu_LC = task.liJu_LC,
                    liJu_DG = task.liJu_DG,
                    liJu_DQ = task.liJu_DQ,
                    fuZhu_LC = task.fuZhu_LC,
                    fuZhu_DQ = task.fuZhu_DQ,
                    fuZhu_DG=task.fuZhu_DG,
                    createTime = task.createTime,
                    dispatchTime = task.dispatchTime,
                    startTime = task.startTime,
                    startTime_LC = task.startTime_LC,
                    endTime_LC = task.endTime_LC,
                    startTime_DG = task.startTime_DG,
                    endTime_DG = task.endTime_DG,
                    startTime_DQ = task.startTime_DQ,
                    endTime_DQ = task.endTime_DQ,
                    endTime = task.endTime,
                    confirmTime_JS = task.confirmTime_JS,
                    confirmTime_GZ = task.confirmTime_GZ,
                    confirmTime_LC = task.confirmTime_LC,
                    confirmTime_DG = task.confirmTime_DG,
                    confirmTime_DQ = task.confirmTime_DQ
                };
 
 
 
              
                List<Dt_Putake> listp = new List<Dt_Putake>();
 
                // 定义任务拆解的组信息
                var groups = new[]
                {
                    new { Grouptype= "电气",Pustatus="待领筒",Zhijianyuan = task.zhiJianYuan_DQ, Lijuzouyeyuan = task.liJu_DQ, Fuzyuan = task.fuZhu_DQ },
                    new { Grouptype = "机械",Pustatus="待领筒", Zhijianyuan = task.zhiJianYuan_LC, Lijuzouyeyuan = task.liJu_LC, Fuzyuan = task.fuZhu_LC },
                    new { Grouptype = "地沟",Pustatus="待领筒", Zhijianyuan = task.zhiJianYuan_DG, Lijuzouyeyuan = task.liJu_DG, Fuzyuan = task.fuZhu_DG }
                 };
 
 
                // 通过循环创建对象
                foreach (var g in groups)
                {
                    listp.Add(new Dt_Putake
                    {
                        Njtakeid = task.NJtaskID,
                        takename = task.TakeName,
                        jishuyuan = task.jiShuYuan,
                        gonzhang = task.gongZhang,
 
                        zhijianyuan = g.Zhijianyuan,
                        lijuzouyeyuan = g.Lijuzouyeyuan,
                        fuzyuan = g.Fuzyuan,
                       Grouptype=g.Grouptype,
                       Pustatus=g.Pustatus,
                       Dispatchtime=task.dispatchTime,
                    });
                }
 
 
                _unitOfWorkManage.BeginTran();//开启事务(在增删改查前,业务后)
 
                if (existtepm == null)//如果不存在id
                {
                    
                    _templateRepository.AddData(Temp);//添加模板
 
                    //db.Insertable(task).ExecuteCommand();
                    BaseDal.UpdateData(task);//将模板表中修改的插入进任务表中
 
                    // db.Insertable(history).ExecuteCommand(); 
                    _nJtakeHistoryRepository.AddData(history);// 插入历史记录
 
                    _putakeRepository.AddData(listp);//下发三条任务分解表数据
                  //  return new WebResponseContent { Status = true, Message = "下发了任务并保存了模板"};
                }
                else
                {
 
                    //获取并返回插入记录的自增主键值,该值被存储在 templatID 变量中
                    //int templatID = _templateRepository.Db.Insertable(Temp).ExecuteReturnIdentity();
                    //task.tempID = templatID;
 
                    //修改模板表
                    _templateRepository.UpdateData(existtepm);
 
                    //将模板号插入这条任务表中
                    task.tempID = existtepm.TemplateID;
 
                    BaseDal.UpdateData(task);//将任务表中的人员插入进任务表中
 
 
                    _nJtakeHistoryRepository.AddData(history);// 插入历史记录
 
                    _putakeRepository.AddData(listp);//下发三条任务分解表数据
                  
                }
                _unitOfWorkManage.CommitTran();//提交事务
                return new WebResponseContent { Status = true, Data = task };
            }
            catch (Exception ex)
            {
 
                //db.Ado.RollbackTran(); // 回滚事务
                _unitOfWorkManage.RollbackTran();
                return new WebResponseContent { Status = false, Message = "添加失败:" + ex.Message };
            }
        }
 
 
 
        //不保存模板
        public WebResponseContent Submit(NotempDTO notempDTO)
        {
            try
            {
                #region 自定义命名给Njtaskid
                // 获取今天的日期部分,如 "KH0306"
                string todayPrefix = $"KH{DateTime.Now:MMdd}";
 
                // 查询数据库,找出当天最大的 NJtaskID
                var lastTask = BaseDal.QueryData(i => i.NJtaskID.StartsWith(todayPrefix))
                                      .OrderByDescending(i => i.NJtaskID)
                                      .FirstOrDefault();
 
                int nextNumber = 1; // 默认编号
 
                if (lastTask != null)
                {
                    // 获取后两位分钟编号
                    string lastNumberPart = lastTask.NJtaskID.Substring(6, 2);
                    if (int.TryParse(lastNumberPart, out int lastNumber))
                    {
                        nextNumber = lastNumber + 1;
                    }
                }
 
                // 生成 NJtaskID,例如 KH030601, KH030602...
                string CustomizeID = $"{todayPrefix}{nextNumber:D2}";
                #endregion
 
 
                //先根据id判断是否有该任务
                var task = BaseDal.QueryFirst(i => i.NJtaskID == notempDTO.Njtakeid);
                if (task == null)
                {
                    return new WebResponseContent { Status = false, Message = "没有找到" };
                }
 
                task.Taskstatus = "已派工";
 
                     task.NJtaskID = CustomizeID;
                    task.TakeName = notempDTO.takename;
                    task.jiShuYuan = notempDTO.jishuyuan;
                    task.zhiJianYuan_DG = notempDTO.zhijianyuan_dg;
                    task.zhiJianYuan_DQ = notempDTO.zhijianyuan_dq;
                    task.zhiJianYuan_LC = notempDTO.zhijianyuan_lc;
                    task.gongZhang = notempDTO.gongzhang;
                    task.liJu_DG = notempDTO.liju_dg;
                    task.liJu_DQ = notempDTO.liju_dq;
                    task.liJu_LC = notempDTO.liju_lc;
                    task.fuZhu_DG = notempDTO.fuzhu_dg;
                    task.fuZhu_DQ = notempDTO.fuzhu_dq;
                    task.fuZhu_LC = notempDTO.fuzhu_lc;
                    task.dispatchTime=DateTime.Now;
                    //task.NJtaskID=
                   // task.startTime =DateTime.Now;
               
 
 
                // 插入到任务历史表
                var history = new Dt_NJtakeHistory
                {
                    taskID = task.id,
                    trainKind = task.trainKind,
                    TakeName = task.TakeName,
                    trainNum = task.trainNum,
                    track = task.track,
                    coachNum = task.coachNum,
                    bogie = task.bogie,
                    processDept = task.processDept,
                    jiShuYuan = task.jiShuYuan,
                    zhiJianYuan_LC = task.zhiJianYuan_LC,
                    zhiJianYuan_DG = task.zhiJianYuan_DG,
                    zhiJianYuan_DQ = task.zhiJianYuan_DQ,
                    gongZhang = task.gongZhang,
                    liJu_LC = task.liJu_LC,
                    liJu_DG = task.liJu_DG,
                    liJu_DQ = task.liJu_DQ,
                    fuZhu_LC = task.fuZhu_LC,
                    fuZhu_DQ = task.fuZhu_DQ,
                    fuZhu_DG = task.fuZhu_DG,
                    createTime = task.createTime,
                    dispatchTime = task.dispatchTime,
                    startTime = task.startTime,
                    startTime_LC = task.startTime_LC,
                    endTime_LC = task.endTime_LC,
                    startTime_DG = task.startTime_DG,
                    endTime_DG = task.endTime_DG,
                    startTime_DQ = task.startTime_DQ,
                    endTime_DQ = task.endTime_DQ,
                    endTime = task.endTime,
                    confirmTime_JS = task.confirmTime_JS,
                    confirmTime_GZ = task.confirmTime_GZ,
                    confirmTime_LC = task.confirmTime_LC,
                    confirmTime_DG = task.confirmTime_DG,
                    confirmTime_DQ = task.confirmTime_DQ
                };
 
 
 
 
                List<Dt_Putake> listp = new List<Dt_Putake>();
 
                // 定义任务拆解的组信息
                var groups = new[]
                {
                    new { Grouptype= "电气",Pustatus="待领筒",Zhijianyuan = task.zhiJianYuan_DQ, Lijuzouyeyuan = task.liJu_DQ, Fuzyuan = task.fuZhu_DQ },
                    new { Grouptype = "机械",Pustatus="待领筒", Zhijianyuan = task.zhiJianYuan_LC, Lijuzouyeyuan = task.liJu_LC, Fuzyuan = task.fuZhu_LC },
                    new { Grouptype = "地沟",Pustatus="待领筒", Zhijianyuan = task.zhiJianYuan_DG, Lijuzouyeyuan = task.liJu_DG, Fuzyuan = task.fuZhu_DG }
                 };
 
                // 通过循环创建对象
                foreach (var g in groups)
                {
                    listp.Add(new Dt_Putake
                    {
                        Njtakeid = task.NJtaskID,
                        takename = task.TakeName,
                        jishuyuan = task.jiShuYuan,
                        gonzhang = task.gongZhang,
                        zhijianyuan = g.Zhijianyuan,
                        lijuzouyeyuan = g.Lijuzouyeyuan,
                        fuzyuan = g.Fuzyuan,
                        Grouptype = g.Grouptype,
                        Pustatus = g.Pustatus,
                        Dispatchtime = task.dispatchTime,
 
                    });
                }
 
 
                _unitOfWorkManage.BeginTran();//开启事务(在增删改查前,业务后)
 
                    BaseDal.UpdateData(task);//将模板表中修改的插入进任务表中
 
                    // db.Insertable(history).ExecuteCommand(); 
                    _nJtakeHistoryRepository.AddData(history);// 插入历史记录
 
                    _putakeRepository.AddData(listp);//下发三条任务分解表数据
                                                     //  return new WebResponseContent { Status = true, Message = "下发了任务并保存了模板"};
                
                _unitOfWorkManage.CommitTran();//提交事务(增删改查后)
                return new WebResponseContent { Status = true, Data = task };
            }
            catch (Exception ex)
            {
 
                //db.Ado.RollbackTran(); // 回滚事务
                _unitOfWorkManage.RollbackTran();
                return new WebResponseContent { Status = false, Message = "添加失败:" + ex.Message };
            }
        }
 
 
    }
}