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
using WIDESEA_Common.Constants;
using WIDESEA_Common.LocationEnum;
using WIDESEA_Common.StockEnum;
using WIDESEA_Common.TaskEnum;
using WIDESEA_Common.WareHouseEnum;
using WIDESEA_Core;
using WIDESEA_DTO.Task;
using WIDESEA_Model.Models;
 
namespace WIDESEA_TaskInfoService
{
    public partial class TaskService
    {
        #region 出库任务
 
        /// <summary>
        /// 根据指定的任务详情异步创建新的出库任务
        /// </summary>
        public async Task<WebResponseContent> CreateTaskOutboundAsync(CreateTaskDto taskDto)
        {
            try
            {
                var stockResult = await _stockInfoService.GetStockInfoAsync(taskDto.WarehouseId);
                if (stockResult == null || !stockResult.Any())
                    return WebResponseContent.Instance.Error("未找到库存信息");
 
                var taskList = stockResult.Select(item => new Dt_Task
                {
                    WarehouseId = item.WarehouseId,
                    PalletCode = item.PalletCode,
                    PalletType = item.PalletType,
                    SourceAddress = item.LocationCode,
                    TargetAddress = taskDto.TargetAddress,
                    Roadway = item.LocationDetails.RoadwayNo,
                    TaskType = TaskTypeEnum.Outbound.GetHashCode(),
                    TaskStatus = TaskStatusEnum.New.GetHashCode(),
                    Grade = 1,
                    TaskNum = 0,
                    CurrentAddress = item.LocationCode,
                    NextAddress = taskDto.TargetAddress,
                    Creater = "system",
                }).ToList();
 
                var result = await BaseDal.AddDataAsync(taskList) > 0;
                var wmstaskDto = result ? _mapper.Map<WMSTaskDTO>(taskList) : null;
                return WebResponseContent.Instance.OK(result ? "任务创建成功" : "任务创建失败", wmstaskDto ?? new object());
            }
            catch (Exception ex)
            {
                return WebResponseContent.Instance.Error($"任务创建失败: {ex.Message}");
            }
        }
 
        /// <summary>
        /// 出库任务完成 :修改库存,修改货位状态,删除任务数据,添加历史任务数据
        /// </summary>
        public async Task<WebResponseContent> OutboundFinishTaskAsync(CreateTaskDto taskDto)
        {
            try
            {
                var task = await BaseDal.QueryFirstAsync(s => s.PalletCode == taskDto.PalletCode);
                if (task == null) return WebResponseContent.Instance.Error("未找到对应的任务");
 
                var location = await _locationInfoService.GetLocationInfo(task.Roadway, task.SourceAddress);
                if (location == null) return WebResponseContent.Instance.Error("未找到对应的货位");
 
                var stockInfo = await _stockInfoService.GetStockInfoAsync(taskDto.PalletCode);
                if (stockInfo == null) return WebResponseContent.Instance.Error("未找到对应库存信息");
 
                // 判断是不是极卷库任务
                if (taskDto.WarehouseId == (int)WarehouseEnum.FJ1 || taskDto.WarehouseId == (int)WarehouseEnum.ZJ1)
                {
                    OutTaskCompleteDto outTaskCompleteDto = new OutTaskCompleteDto()
                    {
                        TaskId = task.OrderNo ?? string.Empty,
                        DevId = task.TargetAddress ?? string.Empty,
                        ReqTime = DateTime.Now.ToString()
                    };
                    return await OutTaskComplete(outTaskCompleteDto);
                }
 
                WebResponseContent content = new WebResponseContent();
                return await _unitOfWorkManage.BeginTranAsync(async () =>
                {
                    stockInfo.LocationId = 0;
                    stockInfo.LocationCode = string.Empty;
                    stockInfo.OutboundDate = DateTime.Now;
 
                    location.LocationStatus = LocationStatusEnum.Free.GetHashCode();
 
                    var updateLocationResult = await _locationInfoService.UpdateLocationInfoAsync(location);
                    var updateStockResult = await _stockInfoService.UpdateStockAsync(stockInfo);
                    if (!updateLocationResult || !updateStockResult)
                        return WebResponseContent.Instance.Error("任务完成失败");
 
                    // 高温2号出库到CWSC1时,自动创建入库任务到常温1号巷道
                    WMSTaskDTO? inboundTaskDto = null;
                    if (task.TargetAddress == TaskAddressConstants.GW2_ADDRESS)
                    {
                        var inboundTask = new Dt_Task
                        {
                            TaskNum = await BaseDal.GetTaskNo(),
                            PalletCode = task.PalletCode,
                            PalletType = task.PalletType,
                            Roadway = "CW1",
                            TaskType = TaskInboundTypeEnum.Inbound.GetHashCode(),
                            TaskStatus = TaskInStatusEnum.InNew.GetHashCode(),
                            SourceAddress = task.TargetAddress,
                            TargetAddress = task.TargetAddress,
                            CurrentAddress = task.TargetAddress,
                            NextAddress = task.TargetAddress,
                            WarehouseId = (int)WarehouseEnum.CW1,
                            Grade = 1,
                            Creater = "system_auto"
                        };
                        await Repository.AddDataAsync(inboundTask);
                        inboundTaskDto = _mapper.Map<WMSTaskDTO>(inboundTask);
                    }
 
                    var completeResult = await CompleteTaskAsync(task, "出库完成");
                    if (!completeResult.Status)
                        return completeResult;
 
                    // 返回入库任务信息(如果有)
                    if (inboundTaskDto != null)
                    {
                        return content.OK("出库完成,已创建入库任务", inboundTaskDto);
                    }
                    return content.OK("出库完成");
                });
            }
            catch (Exception ex)
            {
                return WebResponseContent.Instance.Error($"完成任务失败: {ex.Message}");
            }
        }
 
        #endregion 出库任务
    }
}