qinchulong
2025-03-03 ea3b8223434673ca6d911bfcd6cdcafa773cb5f0
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
using Autofac.Core;
using HslCommunication;
using HslCommunication.Core;
using HslCommunication.WebSocket;
using Microsoft.Extensions.Hosting;
using Microsoft.VisualBasic.FileIO;
using Newtonsoft.Json;
using Quartz;
using SixLabors.ImageSharp.PixelFormats;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using WIDESEAWCS_Common.LocationEnum;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Communicator;
using WIDESEAWCS_Core.BaseRepository;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_ISystemServices;
using WIDESEAWCS_ITaskInfoService;
using WIDESEAWCS_IWMSPart;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_QuartzJob.DeviceBase;
using WIDESEAWCS_QuartzJob.StackerCrane.Enum;
using WIDESEAWCS_TaskInfoService;
using WIDESEAWCS_Tasks.OHT;
 
namespace WIDESEAWCS_Tasks
{
    [DisallowConcurrentExecution]
    public class GZJJob : JobBase, IJob
    {
        private readonly ITaskService _taskService;
        private readonly Idt_ErrormsginfoService _ErrormsginfoService;
        private readonly IRepository<dt_stationInfo> _dt_stationInfoRepository;
        private readonly IRepository<Dt_Task> _dt_taskRepositiry;
        private readonly ILocationInfoService _locationInfoService;
        private readonly IRepository<dt_storagemode> _storagemodeRepository;
        private readonly IRepository<dt_outstockinfo> _outStockRepository;
        private readonly IRepository<dt_batchInfo> _batchInfoRepository;
        private readonly IRepository<Dt_StockInfo> _StockInfoRepository;
        private readonly IRepository<Dt_LocationInfo> _LocationInfoRepository;
        private readonly IUnitOfWorkManage _unitOfWorkManage;
 
        WebSocketServer _webSocketServer;
        public GZJJob(ITaskService taskService, WebSocketServer webSocketServer, Idt_ErrormsginfoService errormsginfoService,
            Idt_StationinfoService stationinfoService,
            IRepository<dt_stationInfo> dt_stationInfoRepository,
            IRepository<Dt_Task> dt_taskRepositiry,
            ILocationInfoService locationInfoService,
            IRepository<dt_outstockinfo> outStockRepository,
            IRepository<dt_storagemode> storagemodeRepository,
            IRepository<dt_batchInfo> batchInfoRepository,
            IRepository<Dt_StockInfo> StockInfoRepository,
            IRepository<Dt_LocationInfo> LocationInfoRepository,
            IUnitOfWorkManage unitOfWorkManage)
        {
            _taskService = taskService;//注入
            _webSocketServer = webSocketServer;
            _ErrormsginfoService = errormsginfoService;
            _dt_stationInfoRepository = dt_stationInfoRepository;
            _dt_taskRepositiry = dt_taskRepositiry;
            _locationInfoService = locationInfoService;
            _storagemodeRepository = storagemodeRepository;
            _outStockRepository = outStockRepository;
            _batchInfoRepository = batchInfoRepository;
            _StockInfoRepository = StockInfoRepository;
            _LocationInfoRepository = LocationInfoRepository;
            _unitOfWorkManage = unitOfWorkManage;
        }
 
        public Task Execute(IJobExecutionContext context)
        {
           
            bool flag = context.JobDetail.JobDataMap.TryGetValue("JobParams", out object? value);
            if (flag && value != null)
            {
                OtherDevice device = (OtherDevice)value;
                try
                {
                    #region 生成入库任务
                    if (!OHTJob.oHTReadData.R_GZJ_isWork)
                    {
                        throw new Exception($"规整机投入使用信号为false");
                    }
                    Dt_Task task = new Dt_Task();
                    var storagemode = _storagemodeRepository.QueryFirst(v=>v.Id==1);
                    if (OHTJob.oHTReadData.R_RGVMode && OHTJob.oHTReadData.R_DK_isready)//RGV上料模式,且东跨架子有上料信号
                    {
                        var station=_dt_stationInfoRepository.QueryFirst(v=>v.msg== "东跨架子");
                        task.SourceAddress = station.Row + "-" + station.Column + "-1";
                        task.TargetAddress = _locationInfoService.GetInLocation(storagemode.storagemode).LocationCode;
                        
                        task.Grade = 1;
                        task.Remark = (int)MateTypeEnum.ZiChan;
                    }
                    else if (OHTJob.oHTReadData.R_RGVMode && OHTJob.oHTReadData.R_XK_isready)//RGV上料模式,且西跨架子有上料信号
                    {
                        var station = _dt_stationInfoRepository.QueryFirst(v => v.msg == "西跨架子");
                        task.SourceAddress = station.Row + "-" + station.Column + "-1";
                        task.TargetAddress = _locationInfoService.GetInLocation(storagemode.storagemode).LocationCode;
                        var DKstation = _dt_stationInfoRepository.QueryFirst(v => v.msg == "东跨架子");
                        string address= DKstation.Row + "-" + DKstation.Column + "-1";
                        var oldtask = _dt_taskRepositiry.QueryFirst(v=>v.SourceAddress== address);
                        if (oldtask!=null && oldtask.TaskState== (int)TaskInStatusEnum.InNew)
                        {
                            task.Grade = 2;
                        }
                        else
                        {
                            task.Grade = 1;
                        }
                        task.Remark = (int)MateTypeEnum.ZiChan;
                    }
                    else if (OHTJob.oHTReadData.R_CCMode && OHTJob.oHTReadData.R_XK_isready)//人工叉车上料模式,且西跨架子有上料信号
                    {
                        var station = _dt_stationInfoRepository.QueryFirst(v => v.msg == "西跨架子");
                        task.SourceAddress = station.Row + "-" + station.Column + "-1";
                        task.TargetAddress = _locationInfoService.GetInLocation(storagemode.storagemode).LocationCode;
                        task.Grade = 1;
                        task.Remark = (int)MateTypeEnum.WaiGou;
                    }
                    else if (OHTJob.oHTReadData.R_CCMode && OHTJob.oHTReadData.R_DK_isready)//人工叉车上料模式,且东跨架子有上料信号
                    {
                        var station = _dt_stationInfoRepository.QueryFirst(v => v.msg == "东跨架子");
                        task.SourceAddress = station.Row + "-" + station.Column + "-1";
                        task.TargetAddress = _locationInfoService.GetInLocation(storagemode.storagemode).LocationCode;
                        var XKstation = _dt_stationInfoRepository.QueryFirst(v => v.msg == "西跨架子");
                        string address= XKstation.Row + "-" + XKstation.Column + "-1";
                        var oldtask = _dt_taskRepositiry.QueryFirst(v => v.SourceAddress == address);
                        if (oldtask != null && oldtask.TaskState == (int)TaskInStatusEnum.InNew)
                        {
                            task.Grade = 2;
                        }
                        else
                        {
                            task.Grade = 1;
                        }
                        task.Remark = (int)MateTypeEnum.WaiGou;
                    }
                    else
                    {
                        throw new Exception($"规整机当前信号不满足入库任务生成条件");
                    }
                    //查找相邻的库位
                    Dt_LocationInfo Towlocation = new Dt_LocationInfo();
                    var Onelocation = _LocationInfoRepository.QueryFirst(v => v.LocationCode == task.TargetAddress);
                    if (Onelocation.Depth == 1)
                    {
                        Towlocation = _LocationInfoRepository.QueryFirst(v => v.Row == Onelocation.Row && v.Column == Onelocation.Column && v.Depth == 2);
                    }
                    else
                    {
                        Towlocation = _LocationInfoRepository.QueryFirst(v => v.Row == Onelocation.Row && v.Column == Onelocation.Column && v.Depth == 1);
                    }
                    task.NextAddress = Towlocation.LocationCode;
                    task.Roadway = "TC01";
                    task.TaskType = (int)TaskInboundTypeEnum.Inbound;
                    task.TaskState = (int)TaskInStatusEnum.InNew;
                    task.PalletCode = "text";
                    task.CurrentAddress = "text";
                    _dt_taskRepositiry.AddData(task);
 
                    #endregion
                }
                catch (Exception ex)
                {
                    _ErrormsginfoService.UpdateErrorMsg(ex.Message, 2);
                }
 
                try
                {
                    #region 生成出库任务
                    if (!OHTJob.oHTReadData.R_ZXJ_isWork)
                    {
                        throw new Exception($"整形机投入使用信号为false");
                    }
                    var isout = _outStockRepository.QueryFirst(v => v.Id == 1);
                    if (isout.isout != 1)
                    {
                        throw new Exception($"当前系统设置为不出库模式");
                    }
                    if (!OHTJob.oHTReadData.R_ZXJ_TCMode || !OHTJob.oHTReadData.R_HC_isReadyWork)
                    {
                        throw new Exception($"整形机不属于天车上料模式或天车放板信号为false");
                    }
                    //查询当前任务池中是否存在出库任务,出库任务只能存在一条
                    var oldtask = _dt_taskRepositiry.QueryFirst(v => v.TaskType == (int)TaskOutboundTypeEnum.Outbound);
                    if (oldtask != null)
                    {
                        throw new Exception($"当前任务池中已存在一条出库任务");
                    }
                    //查询当前需要出库的批次号,以及出库物料类型
                    var batch = _batchInfoRepository.QueryFirst(v => v.Id == 1);
                    //查询库存物料库存信息
                    var stocks = _StockInfoRepository.QueryData(v => v.Remark == batch.materType && v.BatchNo == batch.OutBatch && v.StockStatus==0)
                        .OrderBy(v => v.CreateDate).ToList();
                    if (stocks.Count <= 0)
                    {
                        throw new Exception($"当前库存中没有【{batch.materType}】(0自产 1外购)且批次为【{batch.OutBatch}】的物料");
                    }
                    //查询货位
                    var locaOne=_LocationInfoRepository.QueryFirst(v=>v.LocationCode== stocks[0].LocationCode);
                    //查询相邻的货位
                    Dt_LocationInfo locaTow = new Dt_LocationInfo();
                    if (locaOne.Depth==1) 
                    {
                        locaTow = _LocationInfoRepository.QueryFirst(v => v.Row == locaOne.Row &&v.Column== locaOne.Column && v.Depth==2);
                    }
                    else
                    {
                        locaTow = _LocationInfoRepository.QueryFirst(v => v.Row == locaOne.Row && v.Column == locaOne.Column && v.Depth == 1);
                    }
                    locaOne.LocationStatus = (int)LocationStatusEnum.Lock;
                    locaTow.LocationStatus = (int)LocationStatusEnum.Lock;
                    //查询相邻的库存
                    var BBstock = _StockInfoRepository.QueryFirst(v=>v.LocationCode== locaTow.LocationCode);
                    BBstock.StockStatus = 1;
                    stocks[0].StockStatus = 1;
                    //查询终点站台信息
                    var station = _dt_stationInfoRepository.QueryFirst(v => v.msg == "整形机放料位");
                    //开始创建任务
                    Dt_Task outtask = new Dt_Task();
                    outtask.Roadway = "TC01";
                    outtask.TaskType = (int)TaskOutboundTypeEnum.Outbound;
                    outtask.TaskState = (int)TaskOutStatusEnum.OutNew;
                    outtask.SourceAddress = stocks[0].LocationCode;
                    outtask.TargetAddress = station.Row + "-" + station.Column + "-1";
                    outtask.NextAddress = locaTow.LocationCode;
                    outtask.Grade = 1;
                    outtask.WMSId = 3;
                    outtask.Remark = batch.materType;
                    outtask.PalletCode = "text";
                    outtask.CurrentAddress = "text";
 
                    _unitOfWorkManage.BeginTran();
                    _LocationInfoRepository.UpdateData(locaOne);
                    _LocationInfoRepository.UpdateData(locaTow);
                    _StockInfoRepository.UpdateData(BBstock);
                    _dt_taskRepositiry.AddData(outtask);
                    _StockInfoRepository.UpdateData(stocks[0]);
                    _unitOfWorkManage.CommitTran();
                    #endregion
                }
                catch (Exception ex)
                {
                    _unitOfWorkManage.RollbackTran();
                    _ErrormsginfoService.UpdateErrorMsg(ex.Message, 3);
                }
            }
            return Task.CompletedTask;
        }
    }
}