huangxiaoqiang
2025-05-19 051aa9a0e7f58af6665fb4526e42cf8060fbaa05
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
using AngleSharp.Io;
using Mapster;
using Masuit.Tools;
using SqlSugar;
using System.Net.NetworkInformation;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using WIDESEA_Cache;
using WIDESEA_Core.Const;
using WIDESEA_DTO.AGV;
using WIDESEA_DTO.WMS;
using WIDESEA_IServices;
using WIDESEA_IStorageSocketRepository;
using WIDESEA_IStorageSocketServices;
using WIDESEA_IStoragIntegrationServices;
using WIDESEA_Model.Models.AGV;
using WIDESEA_StorageSocketServices;
using WIDESEA_StorageTaskRepository;
using WIDESEAWCS_BasicInfoRepository;
using WIDESEAWCS_Model.Models;
 
namespace WIDESEA_StorageTaskServices;
 
public partial class Dt_TaskService : ServiceBase<Dt_Task, IDt_TaskRepository>, IDt_TaskService
{
    private readonly LogFactory LogFactory = new LogFactory();
    private readonly IUnitOfWorkManage _unitOfWorkManage;
    private readonly IStockInfoRepository _stockInfoRepository;
    private readonly IDt_Task_HtyRepository _task_HtyRepository;
    private readonly IMapper _mapper;
    private readonly ILocationInfoRepository _locationRepository;
   
    public Dt_TaskService(IDt_TaskRepository BaseDal,
                                IUnitOfWorkManage unitOfWorkManage,
                                IStockInfoRepository stockInfoRepository,
                                IDt_Task_HtyRepository task_HtyRepository,
                                IMapper mapper,
                                ILocationInfoRepository locationRepository,
                                IStockInfoDetailRepository stockInfoDetailRepository) : base(BaseDal)
    {
        _unitOfWorkManage = unitOfWorkManage;
        _stockInfoRepository = stockInfoRepository;
        _task_HtyRepository = task_HtyRepository;
        _mapper = mapper;
        _locationRepository = locationRepository;
    }
    
 
 
    #region 外部接口方法
 
    public WebResponseContent CompleteInboundTask(Dt_Task task)
    {
        WebResponseContent content = new WebResponseContent();
        try
        {
            _unitOfWorkManage.BeginTran();
            var locationInf = _locationRepository.QueryFirst(x => x.LocationCode == task.TargetAddress);
            locationInf.LocationStatus = (int)LocationEnum.InStock;
            var stock = new DtStockInfo()
            {
                PalletCode = task.PalletCode,
                LocationCode = task.TargetAddress,
                CreateDate = DateTime.Now,
                Creater = "system",
                LocationId = locationInf.Id,
            };
            _stockInfoRepository.AddData(stock);
            _locationRepository.UpdateData(locationInf);
            TaskMoveHty(task);
            _unitOfWorkManage.CommitTran();
            return content.OK();
        }
        catch (Exception ex)
        {
            _unitOfWorkManage.RollbackTran();
            return content.Error(ex.Message);
        }
    }
 
    public WebResponseContent CompleteOutboundTask(Dt_Task task)
    {
        WebResponseContent content = new WebResponseContent();
        try
        {
            _unitOfWorkManage.BeginTran();
            var locationInf = _locationRepository.QueryFirst(x => x.LocationCode == task.TargetAddress);
            locationInf.LocationStatus = (int)LocationEnum.Free;
            var stock = _stockInfoRepository.QueryFirst(x=>x.PalletCode == task.PalletCode);
 
            DtStockInfo_Hty stockInfo_Hty = stock.Adapt<DtStockInfo_Hty>();
            stockInfo_Hty.ModifyDate = DateTime.Now;
 
            AddStockInfoHty(stockInfo_Hty);
 
            _stockInfoRepository.DeleteData(stock);
            _locationRepository.UpdateData(locationInf);
 
            TaskMoveHty(task);
 
            _unitOfWorkManage.CommitTran();
            return content.OK();
        }
        catch (Exception ex)
        {
            _unitOfWorkManage.RollbackTran();
            return content.Error(ex.Message);
        }
    }
 
    public WebResponseContent CompleteRelocationboundTask(Dt_Task task)
    {
        WebResponseContent content = new WebResponseContent();
        try
        {
            _unitOfWorkManage.BeginTran();
            var locationInf = _locationRepository.QueryFirst(x => x.LocationCode == task.TargetAddress);
            var location = _locationRepository.QueryFirst(x => x.LocationCode == task.SourceAddress);
            locationInf.LocationStatus = (int)LocationEnum.InStock;
            location.LocationStatus = (int)LocationEnum.Free;
 
            var stock = _stockInfoRepository.QueryFirst(x => x.PalletCode == task.PalletCode);
 
            stock.LocationCode = locationInf.LocationCode;
            stock.LocationId = locationInf.Id;
 
 
            _stockInfoRepository.UpdateData(stock);
            _locationRepository.UpdateData(locationInf);
            _locationRepository.UpdateData(location);
            TaskMoveHty(task);
            _unitOfWorkManage.CommitTran();
            return content.OK();
        }
        catch (Exception ex)
        {
            _unitOfWorkManage.RollbackTran();
            return content.Error(ex.Message);
        }
    }
    public WebResponseContent TaskMoveHty(Dt_Task task)
    {
        WebResponseContent content = new WebResponseContent();
        try
        {
            _unitOfWorkManage.BeginTran();
            var taskHtyNG = CreateHistoricalTask(task);
            var isTaskHtyAdd = _task_HtyRepository.AddData(taskHtyNG) > 0;
            var isTaskDelete = Delete(task.TaskId);
            var AgvTask = SqlSugarHelper.DbAGV.Queryable<task_call>().Where(x => x.d_involed5 == task.TaskNum).First();
            if(AgvTask != null)
            {
                SqlSugarHelper.DbAGV.Deleteable(AgvTask).ExecuteCommand();
            }
            _unitOfWorkManage.CommitTran();
            return content.OK();
        }
        catch (Exception ex)
        {
            _unitOfWorkManage.RollbackTran();
            return content.Error(ex.Message);
        }
    }
 
    #endregion 外部接口方法
 
    #region private 内部方法
 
    /// <summary>
    /// 创建历史任务记录
    /// </summary>
    /// <param name="task"></param>
    /// <returns></returns>
    public Dt_Task_Hty CreateHistoricalTask(Dt_Task task, bool isHand = false)
    {
        task.CurrentAddress = task.NextAddress;
 
        // 创建历史任务
        var taskHty = _mapper.Map<Dt_Task_Hty>(task);
        taskHty.FinishTime = DateTime.Now;
        taskHty.TaskId = 0;
        taskHty.OperateType = isHand ? (int)OperateTypeEnum.人工删除 : App.User.UserName != null ? (int)OperateTypeEnum.人工完成 : (int)OperateTypeEnum.自动完成;
        taskHty.SourceId = task.TaskId;
        if (isHand)
        {
            taskHty.Creater = App.User.UserName != null ? App.User.UserName : "System";
        }
        return taskHty;
    }
    /// <summary>
    /// 删除一个任务
    /// </summary>
    /// <param name="id">任务ID</param>
    /// <returns>是否删除成功</returns>
    public bool Delete(int id)
    {
        return  BaseDal.Delete(id);
    }
    private void  AddStockInfoHty(DtStockInfo_Hty dtStock)
    {
        var isStockAdd = SqlSugarHelper.DbWMS.InsertNav(dtStock).IncludesAllFirstLayer().ExecuteCommand();
        if (!isStockAdd)
        {
            throw new Exception("库存历史信息添加失败");
        }
    }
 
    #endregion private 内部方法
}