Admin
3 天以前 6184f79fcb92452dee17f5441aff77911336aa95
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
#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 * 命名空间:WIDESEA_TaskInfoService
 * 创建者:胡童庆
 * 创建时间:2024/8/2 16:13:36
 * 版本:V1.0.0
 * 描述:
 *
 * ----------------------------------------------------------------
 * 修改人:
 * 修改时间:
 * 版本:V1.0.1
 * 修改说明:
 * 
 *----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
 
using AutoMapper;
using SqlSugar;
using System.Reflection;
using System.Reflection.Emit;
using WIDESEA_Common.CommonEnum;
using WIDESEA_Common.LocationEnum;
using WIDESEA_Common.OtherEnum;
using WIDESEA_Common.StockEnum;
using WIDESEA_Common.TaskEnum;
using WIDESEA_Core;
using WIDESEA_Core.BaseRepository;
using WIDESEA_Core.BaseServices;
using WIDESEA_Core.Enums;
using WIDESEA_Core.Helper;
using WIDESEA_DTO.Task;
using WIDESEA_IBasicService;
using WIDESEA_IInboundService;
using WIDESEA_IOutboundService;
using WIDESEA_IRecordService;
using WIDESEA_IStockService;
using WIDESEA_ITaskInfoService;
using WIDESEA_Model.Models;
 
namespace WIDESEA_TaskInfoService
{
    public partial class TaskService : ServiceBase<Dt_Task, IRepository<Dt_Task>>, ITaskService
    {
        private readonly IMapper _mapper;
        private readonly IUnitOfWorkManage _unitOfWorkManage;
        private readonly IStockInfoService _stockInfoService;
        private readonly IStockInfoDetailService _stockDetailService;
        private readonly ILocationInfoService _locationInfoService;
 
 
        public string ReceiveWCSTask = WIDESEA_Core.Helper.AppSettings.Configuration["ReceiveWCSTask"];
        public IRepository<Dt_Task> Repository => BaseDal;
 
        private Dictionary<string, OrderByType> _taskOrderBy = new()
            {
                {nameof(Dt_Task.Grade),OrderByType.Desc },
                {nameof(Dt_Task.CreateDate),OrderByType.Asc},
            };
 
        public List<int> TaskTypes => typeof(TaskTypeEnum).GetEnumIndexList();
 
        public List<int> TaskOutboundTypes => typeof(TaskTypeEnum).GetEnumIndexList();
 
        public TaskService(IRepository<Dt_Task> BaseDal, IMapper mapper, IUnitOfWorkManage unitOfWorkManage, IStockInfoService stockInfoService, IStockInfoDetailService stockDetailService, ILocationInfoService locationInfoService) : base(BaseDal)
        {
            _mapper = mapper;
            _unitOfWorkManage = unitOfWorkManage;
            _stockInfoService = stockInfoService;
            _stockDetailService = stockDetailService;
            _locationInfoService = locationInfoService;
        }
        public int GetTaskNum(string sequenceName)
        {
            return Db.Ado.GetScalar($"SELECT NEXT VALUE FOR {sequenceName}").ObjToInt();
        }
 
 
        //判断移库任务
        public WebResponseContent IsRelocations(int TaskNum)
        {
            WebResponseContent content = new WebResponseContent();
            List<Dt_LocationInfo> loca = new List<Dt_LocationInfo>();
            try
            {
                Dt_Task task = BaseDal.QueryData().Where(x => x.TaskNum == TaskNum).FirstOrDefault();
                if (task == null) return content.Error($"WMS未找到该任务信息,任务号:{TaskNum}");
 
                string Locaddres = task.TaskType switch
                {
                    (int)TaskTypeEnum.Outbound => task.SourceAddress,
                    (int)TaskTypeEnum.Inbound => task.TargetAddress,
                    _ => throw new Exception($"任务号:{TaskNum},当前申请的任务类型不对")
                };
 
                if (string.IsNullOrEmpty(Locaddres))
                    return content.Error($"任务号:{TaskNum},需要判断的货位编号为空");
 
 
                Dt_LocationInfo locationInfos = _locationInfoService.GetLocationInfo(task.WarehouseId,Locaddres);
                if (locationInfos == null) return content.Error($"未找到该货位信息,任务号:{TaskNum},:{Locaddres},巷道号:{task.Roadway} ,仓库编号:{task.WarehouseId}");
 
                if (locationInfos.Depth != (int)LocationDepth.deep) return content.Error($"任务号:{TaskNum},当前货位到的不是深库位,不需要判断是否需要移库,货位编号:{Locaddres},巷道号:{task.Roadway} ,仓库编号:{task.WarehouseId}");
 
                //查找当前浅货位
                Dt_LocationInfo Shallowlocation = _locationInfoService.ShallowGetLocation(locationInfos);   //传入深库位,获取浅库位
 
                if (Shallowlocation == null) return content.Error($"需要判断移库的任务:{task.TaskNum},未查找到货位编号:{locationInfos.LocationCode}的浅货位");
 
                if (Shallowlocation.EnableStatus == (int)LocationEnableEnum.Disable) return content.Error($"需要判断移库的任务:{task.TaskNum},当前任务的浅货位被禁用,不可进行移库,货位编号:{Shallowlocation.LocationCode}");
 
                //判断浅货位是否有任务
 
                Dt_Task Shalltask = BaseDal.QueryData(x => x.Roadway == Shallowlocation.LocationCode && x.WarehouseId == Shallowlocation.WarehouseId).Where(x => (x.SourceAddress == Shallowlocation.LocationCode || x.TargetAddress == Shallowlocation.LocationCode)).FirstOrDefault();
                if(Shalltask!=null) return content.OK(data: Shalltask); //返回浅货位在做的任务
 
                if (Shallowlocation.LocationStatus == (int)LocationStatusEnum.Free) return content.OK(data:task);   //返回需要判断的任务,可正常出入库
 
                if (Shallowlocation.LocationStatus != (int)LocationStatusEnum.InStock) return content.Error($"查找到浅库位,货位编号:{Shallowlocation.LocationCode}不是有货状态,不可生成移库任务");
 
                Dt_StockInfo dt_StockInfo = _stockInfoService.Repository.QueryData(x => x.LocationCode == Shallowlocation.LocationCode).Where(x => x.StockStatus == (int)StockStatusEmun.已入库).FirstOrDefault();
 
                if (dt_StockInfo == null) return content.Error($"未找到浅库位的库存信息,货位编号:{Shallowlocation.LocationCode}");
 
                //查找新库位
                Dt_LocationInfo NextLocation=_locationInfoService.GetLocation(Shallowlocation.WarehouseId, Shallowlocation.RoadwayNo, Shallowlocation.LocationType);
                if (NextLocation == null) return content.Error($"仓库id:{Shallowlocation.WarehouseId},巷道号:{Shallowlocation.RoadwayNo},货位类型:{Shallowlocation.LocationType},未查找到新货位,无法进行巷道内移库");
 
                //创建任务
                Dt_Task dt_Task = new()
                {
                    PalletCode = dt_StockInfo.PalletCode,
                    Roadway = locationInfos.RoadwayNo,
                    TaskType = TaskTypeEnum.Relocation.ObjToInt(),
                    TaskStatus = TaskRelocationStatusEnum.RelocationNew.ObjToInt(),
                    SourceAddress = locationInfos.LocationCode,
                    TargetAddress = NextLocation.LocationCode,
                    CurrentAddress = locationInfos.LocationCode,
                    NextAddress = NextLocation.LocationCode,
                    Grade = 2,
                    Creater = "WMS",
                    CreateDate = DateTime.Now,
                    TaskNum = GetTaskNum(nameof(SequenceEnum.SeqTaskNum)),
                    Depth = 0,
                    WarehouseId=Shallowlocation.WarehouseId,
                };
                locationInfos.LocationStatus = LocationStatusEnum.Lock.ObjToInt();
                NextLocation.LocationStatus = LocationStatusEnum.Lock.ObjToInt();
                loca.Add(NextLocation);
                loca.Add(locationInfos);
 
                _unitOfWorkManage.BeginTran();
 
                _locationInfoService.UpdateData(loca);
                BaseDal.AddData(dt_Task);
                _unitOfWorkManage.CommitTran();
                return content.OK(data: dt_Task);
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return content.Error($"任务编号:{TaskNum}调取移库接口出错,错误信息:{ex.Message}");
            }
        }
 
 
        //任务阶段性完成
        public WebResponseContent InOutTaskReporting(TaskReportRequest reportRequest)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                Dt_Task task = BaseDal.QueryData().Where(x => x.TaskNum == reportRequest.TaskNum).FirstOrDefault();
                if (task == null) return content.Error($"WMS未找到该任务信息,任务号:{reportRequest.TaskNum}");
 
                task.TaskStatus = reportRequest.TaskStatus;
                task.CurrentAddress = reportRequest.CurrentAddress;
                task.NextAddress = reportRequest.NextAddress;
 
 
                if (task.TaskType == (int)TaskTypeEnum.Outbound && task.TaskStatus==(int)TaskOutStatusEnum.SC_OutFinish)
                {
                    //堆垛机出库完成后
 
                    //查找货位,库存
                    Dt_StockInfo stockInfo = _stockInfoService.GetStockInfo(task.PalletCode);
                    Dt_StockInfoDetail dt_StockInfodetail = _stockDetailService.Repository.QueryData(x => x.Status == (int)StockStatusEmun.出库锁定).Where(x => x.StockId == stockInfo.Id).FirstOrDefault();
                    Dt_LocationInfo locationpoint = _locationInfoService.GetLocationInfo(task.WarehouseId, task.SourceAddress);
 
                    
                    locationpoint.LocationStatus = (int)LocationStatusEnum.Free;
 
                    _unitOfWorkManage.BeginTran();
                    BaseDal.UpdateData(task);
                    _locationInfoService.Repository.UpdateData(locationpoint);
                    _stockInfoService.DeleteData(stockInfo);
                    _stockDetailService.DeleteData(dt_StockInfodetail);
                    _stockInfoService.Repository.DeleteAndMoveIntoHty(stockInfo, App.User.UserId == 0 ? OperateTypeEnum.自动完成 : OperateTypeEnum.人工完成);
                    _stockDetailService.Repository.DeleteAndMoveIntoHty(stockInfo.Details, App.User.UserId == 0 ? OperateTypeEnum.自动完成 : OperateTypeEnum.人工完成);
                    _unitOfWorkManage.CommitTran();
                    //删除库存数据
                    return content.OK("库存出库成功");
                }
                else if(task.TaskType == (int)TaskTypeEnum.Inbound && task.TaskStatus == (int)TaskInStatusEnum.Line_InFinish)
                {
                    //获取库位信息
                    Dt_LocationInfo NextLocation = _locationInfoService.GetLocation(task.WarehouseId, task.Roadway, 10);
                    if (NextLocation == null) return content.Error($"仓库id:{task.WarehouseId},巷道号:1,未查找到新货位,无法进行入库");
                    task.TargetAddress = NextLocation.LocationCode;
                    task.NextAddress = NextLocation.LocationCode;
                    NextLocation.LocationStatus= (int)LocationStatusEnum.Lock;
 
                    _unitOfWorkManage.BeginTran();
                    _locationInfoService.Repository.UpdateData(NextLocation);
                    BaseDal.UpdateData(task);
                    _unitOfWorkManage.CommitTran();
                    return content.OK(data: NextLocation.LocationCode);
                }
 
                _unitOfWorkManage.BeginTran();
                BaseDal.UpdateData(task);
                _unitOfWorkManage.CommitTran();
                return content.OK();
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return content.Error("任务阶段性完成失败,系统接口原因:" + ex.Message);
            }
        }
 
 
    }
}