刘磊
2025-11-11 6fed1f731b16c1820a43fc34130a70b21465e2bb
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
using log4net.Core;
using Mapster;
using Masuit.Tools;
using System.Collections.Generic;
using System.Threading.Tasks;
using WIDESEA_Comm.AGVTask;
using WIDESEA_Comm.WCSInterface.Requst;
using WIDESEA_Common;
using WIDESEA_Core.Const;
using WIDESEA_DTO.WMS;
using WIDESEA_IStorageBasicRepository;
using WIDESEA_StorageBasicRepository;
using WIDESEA_StorageTaskRepository;
using WIDESEA_StoragIntegrationServices;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob.Models;
 
namespace WIDESEA_StorageTaskServices;
 
public partial class Dt_TaskService : ServiceBase<Dt_Task, IDt_TaskRepository>, IDt_TaskService
{
    #region 请求任务入库
    /// <summary>
    /// 请求入库
    /// </summary>
    /// <param name="input">请求模型</param>
    /// <returns>包含任务信息的响应内容</returns>
    public async Task<WebResponseContent> RequestInTask(RequestTaskDto input)
    {
        // 创建一个WebResponseContent对象
        WebResponseContent content = new WebResponseContent();
        try
        {
            //string palletCode = saveModel.MainData["palletCode"].ToString();
            //string station = saveModel.MainData["station"].ToString();
 
            // 调用BaseDal.QueryFirstAsync方法,查询任务
            var task = await BaseDal.QueryFirstAsync(x => x.PalletCode == input.PalletCode);
            if (task != null)
            {
                throw new Exception($"托盘{input.PalletCode}已存在任务");
            }
 
            var stationInfo = _stationManagerRepository.QueryFirst(x => x.stationChildCode == input.Position);
            if (stationInfo == null)
            {
                throw new Exception($"站台{input.Position}不存在");
            }
           
            // 获取库位
            var location = RequestLocation(stationInfo.Roadway);
            if (location == null)
            {
                return content.Error("无法获取货位信息或库位已满");
            }
           
            var newtask = new Dt_Task
            {
                CurrentAddress = input.Position,
                Grade = 1,
                Roadway = stationInfo.Roadway,
                TargetAddress = location.LocationCode,
                Dispatchertime = DateTime.Now,
               
                NextAddress = stationInfo.Roadway,
                OrderNo = null,
                PalletCode = input.PalletCode,
                SourceAddress = stationInfo.stationLocation,
                TaskState = (int)TaskInStatusEnum.InNew,
                TaskType = (int)TaskInboundTypeEnum.Inbound,
                TaskNum = await BaseDal.GetTaskNo(),
                Creater = "Systeam",
                
            };
 
          
            _unitOfWorkManage.BeginTran();
 
            BaseDal.AddData(newtask);
            location.LocationStatus = (int)LocationEnum.InStockDisable;
            _locationRepository.UpdateData(location);
            _unitOfWorkManage.CommitTran();
 
            content.OK("申请入库成功",data: newtask);
        }
        catch (Exception er)
        {
            _unitOfWorkManage.RollbackTran();
            // 如果发生异常,则调用content.Error方法,记录错误信息,并输出错误信息
            content.Error($"入库申请失败:{er.Message}");
            Console.WriteLine(er.Message);
        }
        // 返回content
        return content;
    }
    #endregion 请求任务入库
 
    #region 库位分配
    #region 获取货位
    object objLOCK = new object();
    /// <summary>
    /// 双升库位分配
    /// </summary>
    /// <param name="requestTask"></param>
    /// <param name="locationInfos"></param>
    /// <returns></returns>
    public DtLocationInfo RequestLocation(string roadwayNo)
    {
        lock (objLOCK)
        {
            try
            {
                List<DtLocationInfo> locations = new List<DtLocationInfo>();
                locations = _locationRepository.QueryData(x => x.RoadwayNo == roadwayNo && x.LocationStatus == (int)LocationEnum.Free && x.EnalbeStatus == 1);   //&& x.LocationType == 1
 
                var location = GetEmptyLocation(locations);
                if (location == null)
                {
                    throw new Exception("库位已满");
                }
                return location;
            }
            catch (Exception err)
            {          
                Console.WriteLine(err.Message.ToString());
                return null;
            }
        }
    }
 
 
    private DtLocationInfo GetEmptyLocation(List<DtLocationInfo> dtLocationInfos)
    {
        var locationinfo = dtLocationInfos.Where(x => x.LocationStatus == (int)LocationEnum.Free && x.EnalbeStatus == 1).OrderBy(x => x.Column).ThenBy(x => x.Row).ThenBy(x => x.Layer).FirstOrDefault();   //.ThenByDescending(x => x.Depth)
 
        return locationinfo;
    }
 
    #endregion 获取货位
    #endregion 库位分配
 
    public WebResponseContent confirmTask(int taskNum)
    {
        WebResponseContent content = new WebResponseContent();
        try
        {
            var taskInfo = BaseDal.QueryFirst(x => x.TaskNum == taskNum);
            if (taskInfo == null) throw new Exception("未知任务");
 
            if (taskInfo.TaskState != (int)TaskOutStatusEnum.OutFinish) throw new Exception("当前任务未完成,禁止确认");
 
            var taskHty = taskInfo.Adapt<Dt_Task_Hty>();
            taskHty.FinishTime = DateTime.Now;
            taskHty.OperateType = (int)OperateTypeEnum.人工确认;
 
            BaseDal.DeleteData(taskInfo);
            _task_HtyRepository.AddData(taskHty);
 
            content.OK("确认完成");
        }
        catch (Exception ex)
        {
            content.Error($"确认异常:{ex.Message}");
        }
 
        return content;
    }
 
}