huangxiaoqiang
11 小时以前 e483ac11616ffc9260d8f491fcc0d66f480b5443
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
using HslCommunication;
using Mapster;
using Masuit.Tools;
using Microsoft.AspNetCore.Components.Routing;
using Newtonsoft.Json;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Math;
using Quartz;
using WIDESEAWCS_BasicInfoService;
using WIDESEAWCS_Common;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Caches;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_DTO.TaskInfo;
using WIDESEAWCS_IBasicInfoRepository;
using WIDESEAWCS_IBasicInfoService;
using WIDESEAWCS_ISystemServices;
using WIDESEAWCS_ITaskInfoRepository;
using WIDESEAWCS_ITaskInfoService;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_QuartzJob.Service;
using WIDESEAWCS_SignalR;
using WIDESEAWCS_Tasks.OtherDeviceJob;
 
namespace WIDESEAWCS_Tasks
{
    [DisallowConcurrentExecution]
    public class CommonFrontOtherDevicesJob : JobBase, IJob
    {
        private readonly ITaskService _taskService;
        private readonly ITaskExecuteDetailService _taskExecuteDetailService;
        private readonly ITaskRepository _taskRepository;
        private readonly IRouterService _routerService;
        private readonly ICacheService _cacheService;
        private readonly INoticeService _noticeService;
        private readonly IDt_StationManagerRepository _stationManagerRepository;
        private readonly IDt_StationManagerService _stationManagerService;
        private readonly ITask_HtyRepository _task_htyRepository;
        private readonly ISys_ConfigService _sys_ConfigService;
        private static List<string>? userTokenIds;
        private static List<int>? userIds;
 
        public CommonFrontOtherDevicesJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IRouterService routerService, ICacheService cacheService, INoticeService noticeService, IDt_StationManagerRepository stationManagerRepository, IDt_StationManagerService stationManagerService, ITask_HtyRepository task_htyRepository, ISys_ConfigService sys_ConfigService)
        {
            _taskService = taskService;
            _taskExecuteDetailService = taskExecuteDetailService;
            _taskRepository = taskRepository;
            _routerService = routerService;
            _cacheService = cacheService;
            _noticeService = noticeService;
            _stationManagerService = stationManagerService;
            _stationManagerRepository = stationManagerRepository;
            _task_htyRepository = task_htyRepository;
            _sys_ConfigService = sys_ConfigService;
        }
 
        public async Task Execute(IJobExecutionContext context)
        {
            try
            {
                FrontOtherDevice frontOtherDevice = (FrontOtherDevice)context.JobDetail.JobDataMap.Get("JobParams");
                List<Dt_StationManager> stationManagers = _stationManagerService.GetAllStationByDeviceCode(frontOtherDevice.DeviceCode);
                foreach (var item in stationManagers.Where(x=>x.stationType == 1))
                {
                    //读取上料请求
                    var UpRequest = frontOtherDevice.GetValue<UpOtherDeviceDBName, short>(UpOtherDeviceDBName.UpRequest,item.stationChildCode);
                    if(UpRequest == 1)
                    {
                        await RequestUpMaterial(frontOtherDevice, item);
                    }
                }
                foreach (var item in stationManagers.Where(x => x.stationType == 2))
                {
                    //读取下料请求
                    var DownRequest = frontOtherDevice.GetValue<DownOtherDeviceDBName, short>(DownOtherDeviceDBName.DownRequest, item.stationChildCode);
                    if (DownRequest == 1)
                    {
                        await RequestDownMaterialAsync(frontOtherDevice, item);
                    }
                }
            }
            catch (Exception ex)
            {
                WriteError("CommonOtherDevicesJob", "test", ex);
            }
            return;
        }
 
        /// <summary>
        /// 请求上料
        /// </summary>
        /// <param name="frontOtherDevice"></param>
        /// <param name="stationManager"></param>
        /// <returns></returns>
        public async Task RequestUpMaterial(FrontOtherDevice frontOtherDevice, Dt_StationManager stationManager)
        {
            WebResponseContent content = new WebResponseContent();
            var wmsIpAddrss = _taskService.GetIpAddress(SysConfigKeyConst.RequestOutboundTask);
            var result = await HttpHelper.PostAsync(wmsIpAddrss, JsonConvert.SerializeObject(new { Position = stationManager.stationChildCode }));
            content = JsonConvert.DeserializeObject<WebResponseContent>(result);
            if (!content.Status)
            {
                ConsoleHelper.WriteErrorLine($"请求上料失败,站台:{stationManager.stationChildCode},错误信息:{content.Message}");
                return;
            }
            var task = JsonConvert.DeserializeObject<WMSTaskDTO>(content.Data.ToString());
            if (task == null)
            {
                ConsoleHelper.WriteErrorLine($"请求上料失败,站台:{stationManager.stationChildCode},错误信息:返回数据解析失败");
                return;
            }
            _taskService.ReceiveWMSTask(task);
        }
 
        /// <summary>
        /// 请求下料
        /// </summary>
        /// <param name="frontOtherDevice"></param>
        /// <param name="stationManager"></param>
        public async Task RequestDownMaterialAsync(FrontOtherDevice frontOtherDevice, Dt_StationManager stationManager)
        {
            WebResponseContent content = new WebResponseContent();
            if (stationManager.stationProcessCodeType == 1)
            {
                await RequestWMSInBoundTaskAsync(stationManager);
            }
            else
            {
                int sum = 0;
                List<string> str = stationManager.stationNextProcessCode.IsNullOrEmpty() ? new List<string>() : stationManager.stationNextProcessCode.Split(',').ToList();
                if (str.Count > 0)
                {
                    foreach (var item in str)
                    {
                        var nextStation = _stationManagerRepository.QueryFirst(x => x.stationProcessCode == item);
                        var UpRequest = frontOtherDevice.GetValue<UpOtherDeviceDBName, short>(UpOtherDeviceDBName.UpRequest, nextStation.stationChildCode);
                        if (UpRequest == 1)
                        {
                            sum++;
                            var wmsIpAddrss = _taskService.GetIpAddress(SysConfigKeyConst.RequestInToOutTask);
                            var result = await HttpHelper.PostAsync(wmsIpAddrss, JsonConvert.SerializeObject(new { Position = stationManager.stationChildCode , TargetAddress = nextStation.stationChildCode }));
                            content = JsonConvert.DeserializeObject<WebResponseContent>(result);
                            if (!content.Status)
                            {
                                ConsoleHelper.WriteErrorLine($"请求上料失败,站台:{stationManager.stationChildCode},错误信息:{content.Message}");
                                return;
                            }
                            var task = JsonConvert.DeserializeObject<WMSTaskDTO>(content.Data.ToString());
                            if (task == null)
                            {
                                ConsoleHelper.WriteErrorLine($"请求上料失败,站台:{stationManager.stationChildCode},错误信息:返回数据解析失败");
                                return;
                            }
                            _taskService.ReceiveWMSTask(task);
                            return;
                        }
                    }
                }
                if (sum == 0)
                {
                    //请求下料
                    await RequestWMSInBoundTaskAsync(stationManager);
                }
            }
        }
 
        public async Task RequestWMSInBoundTaskAsync(Dt_StationManager stationManager)
        {
            WebResponseContent content = new WebResponseContent();
            var wmsIpAddrss = _taskService.GetIpAddress(SysConfigKeyConst.RequestInboundTask);
            var result = await HttpHelper.PostAsync(wmsIpAddrss, JsonConvert.SerializeObject(new { Position = stationManager.stationChildCode }));
            content = JsonConvert.DeserializeObject<WebResponseContent>(result);
            if (!content.Status)
            {
                ConsoleHelper.WriteErrorLine($"请求上料失败,站台:{stationManager.stationChildCode},错误信息:{content.Message}");
                return;
            }
            var task = JsonConvert.DeserializeObject<WMSTaskDTO>(content.Data.ToString());
            if (task == null)
            {
                ConsoleHelper.WriteErrorLine($"请求上料失败,站台:{stationManager.stationChildCode},错误信息:返回数据解析失败");
                return;
            }
            _taskService.ReceiveWMSTask(task);
            return;
        }
    }
}