1
z8018
2025-04-05 f94288b728f81b20c29e626526c584c11a7894a3
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
#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 * 命名空间:WIDESEAWCS_TaskInfoService
 * 创建者:胡童庆
 * 创建时间:2024/8/2 16:13:36
 * 版本:V1.0.0
 * 描述:
 *
 * ----------------------------------------------------------------
 * 修改人:
 * 修改时间:
 * 版本:V1.0.1
 * 修改说明:
 * 
 *----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
 
using AutoMapper;
using Magicodes.ExporterAndImporter.Core;
using Microsoft.AspNetCore.Mvc.RazorPages;
using NetTaste;
using Newtonsoft.Json;
using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Math;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Reflection.Metadata;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using WIDESEA_Common.Log;
using WIDESEAWCS_BasicInfoService;
using WIDESEAWCS_Common;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.BaseServices;
using WIDESEAWCS_Core.Enums;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_Core.Utilities;
using WIDESEAWCS_DTO.Enum;
using WIDESEAWCS_DTO.TaskInfo;
using WIDESEAWCS_IBasicInfoRepository;
using WIDESEAWCS_ITaskInfoRepository;
using WIDESEAWCS_ITaskInfoService;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob.Models;
using WIDESEAWCS_QuartzJob.Repository;
using WIDESEAWCS_QuartzJob.Service;
 
namespace WIDESEAWCS_TaskInfoService
{
    public class TaskService : ServiceBase<Dt_Task, ITaskRepository>, ITaskService
    {
        private readonly IMapper _mapper;
        private readonly IContainerRepository _containerRepository;
        private readonly IContainerItemRepository _containerItemRepository;
        private readonly IDeviceInfoRepository _deviceInfoRepository;
 
        public TaskService(ITaskRepository BaseDal, IMapper mapper, IContainerRepository containerRepository, IContainerItemRepository containerItemRepository, IDeviceInfoRepository deviceInfoRepository) : base(BaseDal)
        {
            _mapper = mapper;
            _containerRepository = containerRepository;
            _containerItemRepository = containerItemRepository;
            _deviceInfoRepository = deviceInfoRepository;
        }
 
        public Dt_Task? QueryAGantryUnExecuteTask(string gantryDeviceNo)
        {
            return BaseDal.QueryFirst(x => x.TaskState == (int)TaskStatusEnum.Gantry_New && x.DeviceCode == gantryDeviceNo);
        }
 
        public WebResponseContent CreateTask(string takePosition, string putPosition, string deviceCode, int length, int width, int height)
        {
            try
            {
                Dt_Container takeContainer = _containerRepository.QueryFirst(x => x.ContainerCode == takePosition && x.ContainerType == ContainerTypeEnum.TakeContainer.ObjToInt());
                if (takeContainer == null)
                {
                    return WebResponseContent.Instance.Error("取货位置不存在");
                }
 
                Dt_Container putContainer = _containerRepository.QueryFirst(x => x.ContainerCode == putPosition && x.ContainerType == ContainerTypeEnum.PutContainer.ObjToInt());
                if (putContainer == null)
                {
                    return WebResponseContent.Instance.Error("放货位置不存在");
                }
 
                ContainerSize containerSize = new ContainerSize(putContainer.ContainerLength, putContainer.ContainerWidth, putContainer.ContainerHeight);
                List<Dt_ContainerItem> containerItems = _containerItemRepository.QueryData(x => x.ContainerId == putContainer.Id);
 
                List<PlacedBlock> placedBlocks = containerItems.Select(x => new PlacedBlock(new Point3D(x.ItemPositionX, x.ItemPositionY, x.ItemPositionZ), x.ItemLength, x.ItemWidth, x.ItemHeight)).ToList();
 
                PlaceBlockService placeBlockService = new PlaceBlockService(containerSize, placedBlocks);
 
                Point3D? point3D = placeBlockService.PlaceBlock(length, width, height);
                if (point3D == null)
                {
                    return WebResponseContent.Instance.Error("放货位置已满");
                }
                string code = DateTime.Now.ToString("yyyyMMddHHmmss");
 
                Dt_ContainerItem dt_ContainerItem = new Dt_ContainerItem()
                {
                    ContainerId = putContainer.Id,
                    ItemCode = code,
                    ItemLength = length,
                    ItemWidth = width,
                    ItemHeight = height,
                    ItemPositionX = point3D.Value.X,
                    ItemPositionY = point3D.Value.Y,
                    ItemPositionZ = point3D.Value.Z,
                    ItemStatus = (int)ItemStatusEnum.Assigned,
                    ItemName = code
                };
 
                int positionR = 1;
 
                int takePositionX = 0;
                int takePositionY = 0;
 
                int putPositionX = 0;
                int putPositionY = 0;
 
                int takeContainerPositionX = 0;
                int takeContainerPositionY = 0;
                int takeContainerPositionZ = 0;
 
                int putContainerPositionX = 0;
                int putContainerPositionY = 0;
                int putContainerPositionZ = 0;
 
                int deviceHalfLength = 0;
                int deviceHalfWidth = 0;
 
                if (length > 920 && width >= 300)
                {
                    positionR = 1;
                    if (OPositions.ZPositions.TryGetValue(takeContainer.ContainerCode, out Position? takeOPosition))
                    {
                        takeContainerPositionX = takeOPosition.PositionX;
                        takeContainerPositionY = takeOPosition.PositionY;
                        takeContainerPositionZ = takeOPosition.PositionZ;
                    }
 
                    if (OPositions.ZPositions.TryGetValue(putContainer.ContainerCode, out Position? putOPosition))
                    {
                        putContainerPositionX = putOPosition.PositionX;
                        putContainerPositionY = putOPosition.PositionY;
                        putContainerPositionZ = putOPosition.PositionZ;
                    }
 
                    deviceHalfLength = 460;
                    deviceHalfWidth = 265;
                }
                else
                {
                    positionR = 0;
 
                    if (OPositions.HPositions.TryGetValue(takeContainer.ContainerCode, out Position? takeOPosition))
                    {
                        takeContainerPositionX = takeOPosition.PositionX;
                        takeContainerPositionY = takeOPosition.PositionY;
                        takeContainerPositionZ = takeOPosition.PositionZ;
                    }
 
                    if (OPositions.HPositions.TryGetValue(putContainer.ContainerCode, out Position? putOPosition))
                    {
                        putContainerPositionX = putOPosition.PositionX;
                        putContainerPositionY = putOPosition.PositionY;
                        putContainerPositionZ = putOPosition.PositionZ;
                    }
 
                    deviceHalfLength = 265;
                    deviceHalfWidth = 460;
                }
 
                //吸盘长530 300 间隔660  最大920 吸盘宽130
 
                int sourceTakePositionX = 0;
                int sourceTakePositionY = 0;
 
                int sourcePutPositionX = 0;
                int sourceTPutPositionY = 0;
 
                if (length / 2 + point3D.Value.X > 1000)
                {
                    takePositionY = 1000;
                    putPositionY = putContainerPositionY + 1000;
 
                    sourceTakePositionY = 1000;
                    sourceTPutPositionY = 1000;
                }
                else
                {
                    takePositionY = takeContainerPositionY - length / 2 + deviceHalfLength;
                    putPositionY = putContainerPositionY + length / 2 + point3D.Value.X - deviceHalfLength;
 
                    sourceTakePositionY = length / 2 + point3D.Value.X + deviceHalfLength;
                    sourceTPutPositionY = length / 2 + point3D.Value.X - deviceHalfLength;
                }
 
 
                if (width >= 530)
                {
                    takePositionX = takeContainerPositionX + Math.Abs( width / 2 - deviceHalfWidth);
                    putPositionX = point3D.Value.Y + putContainerPositionX + Math.Abs(width / 2 - deviceHalfWidth);
 
                    sourceTakePositionX = Math.Abs(width / 2 - deviceHalfWidth);
                    sourcePutPositionX = point3D.Value.Y + Math.Abs(width / 2 - deviceHalfWidth);
                }
                else
                {
                    takePositionX = takeContainerPositionX;
                    putPositionX = point3D.Value.Y + takeContainerPositionX;
 
                    sourceTakePositionX = 0;
                    sourcePutPositionX = point3D.Value.Y;
                }
 
                int takePositionZ = takeContainerPositionZ - (height - (height - 10));
                int putPositionZ = (putContainerPositionZ - (height - (height - 10))) - point3D.Value.Z;
 
                int sourceTakePositionZ = height - (height - 10);
                int sourcePutPositionZ = height - (height - 10) + point3D.Value.Z;
 
                Dt_Task dt_Task = new Dt_Task()
                {
                    TaskNum = 1,
                    PalletCode = code,
                    DeviceCode = deviceCode,
                    TaskState = (int)TaskStatusEnum.Gantry_New,
                    TaskType = 0,
                    SourceAddress = $"{takePosition}-{sourceTakePositionX}-{sourceTakePositionY}-{sourceTakePositionZ}-{positionR}",
                    TargetAddress = $"{putPosition}-{sourcePutPositionX}-{sourceTPutPositionY}-{sourcePutPositionZ}-{positionR}",
                    CurrentAddress = $"{takePosition}-{takePositionX}-{takePositionY}-{takePositionZ}-{positionR}",
                    NextAddress = $"{putPosition}-{putPositionX}-{putPositionY}-{putPositionZ}-{positionR}",
                    Grade = 0,
                };
 
                _containerItemRepository.AddData(dt_ContainerItem);
                base.AddData(dt_Task);
 
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
    }
}