分支自 SuZhouGuanHong/TaiYuanTaiZhong

dengjunjie
2024-05-14 25be6fbad7853391a52d3a56752cd1c308c2dfd0
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
/*
 *所有关于dt_agvtask类的业务代码应在此处编写
*可使用repository.调用常用方法,获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*dt_agvtaskService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using WIDESEA_Core.BaseProvider;
using WIDESEA_Core.Extensions.AutofacManager;
using WIDESEA_Entity.DomainModels;
using System.Linq;
using WIDESEA_Core.Utilities;
using System.Linq.Expressions;
using WIDESEA_Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using WIDESEA_WMS.IRepositories;
using WIDESEA_WMS.Repositories;
using WIDESEA_Core.EFDbContext;
using static System.Collections.Specialized.BitVector32;
using WIDESEA_Comm.TaskNo;
using WIDESEA_Common;
using WIDESEA_Core.ManageUser;
using WIDESEA_Comm.LogInfo;
using StackExchange.Redis;
using WIDESEA_Comm;
using WIDESEA_Entity.DomainModels.Mes;
 
namespace WIDESEA_WMS.Services
{
    public partial class dt_agvtaskService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly Idt_agvtaskRepository _repository;//访问数据库
 
        [ActivatorUtilitiesConstructor]
        public dt_agvtaskService(
            Idt_agvtaskRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码,其他情况可以不用
            //base.Init(dbRepository);
        }
 
        public WebResponseContent addNgTask(MesRequestTemp requestTemp)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                var fromaddress = requestTemp.from_address;
                VOLContext context = new VOLContext();
                Idt_stationinfoRepository stationinfoRepository = new dt_stationinfoRepository(context);
                IVV_Mes_WorkinfoRepository workinfoRepository = new VV_Mes_WorkinfoRepository(context);
                var station = stationinfoRepository.Find(x => x.stationCode == fromaddress).FirstOrDefault();
                if (_repository.Find(x => x.agv_fromaddress == fromaddress || x.agv_toaddress == fromaddress).Any())
                    throw new Exception($"货位{fromaddress}已存在任务!");
                if (station == null) throw new Exception($"未找到货位{fromaddress}!");
                if (!station.enable) throw new Exception($"货位{fromaddress}未启用!");
                if (station.quantity < 1) throw new Exception($"货位{fromaddress}无车轮!");
                var Work = workinfoRepository.Find(x => x.workOrder == station.Number && x.processCode == "17").FirstOrDefault();
                if (Work == null) throw new Exception($"未找到货位{fromaddress}的机加工工单信息");
                dt_agvtask agvtask = new dt_agvtask()
                {
                    agv_fromaddress = station.stationCode,
                    agv_id = Guid.NewGuid(),
                    agv_tasknum = IdenxManager.GetTaskNo("KH-", "WMS"),
                    agv_grade = 2,
                    agv_createtime = DateTime.Now,
                    agv_taskstate = "Queue",
                    agv_materielid = station.stationType,
                    agv_qty = station.quantity,
                    agv_tasktype = "TaskType_OutsourceInbound",
                    agv_toaddress = "",
                    agv_userid = UserContext.Current.UserName,
                    bindSN = station.bindSN,
                    agv_worktype = Convert.ToInt32(Work.processCode),
                    agv_materbarcode = Work.materialCode,
                    agv_Traytype = station.tray_type,
                    jobID = station.Number,
                    agv_TrayStatus = station.tray_status
                };
                _repository.Add(agvtask, true);
                station.location_state = LocationStateEnum.InBusy.ToString();
                stationinfoRepository.Update(station, true);
                content.OK();
            }
            catch (Exception ex)
            {
                content.Message = ex.Message;
            }
            return content;
        }
        public override WebResponseContent Update(SaveModel saveModel)
        {
            WebResponseContent content = new WebResponseContent();
            var agv_id = saveModel.MainData["agv_id"].ToString();
            var task = _repository.Find(x => x.agv_id.ToString() == agv_id).FirstOrDefault();
            if (task.agv_taskstate != AGVTaskStateEnum.Queue.ToString()) return content.Error("当前任务不可更改!");
            return base.Update(saveModel);
        }
        #region 添加NG任务
        public override WebResponseContent Add(SaveModel saveDataModel)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                var fromaddress = saveDataModel.MainData["agv_fromaddress"].ToString();
                VOLContext context = new VOLContext();
                Idt_stationinfoRepository stationinfoRepository = new dt_stationinfoRepository(context);
                IVV_Mes_WorkinfoRepository workinfoRepository = new VV_Mes_WorkinfoRepository(context);
                var station = stationinfoRepository.Find(x => x.stationCode == fromaddress).FirstOrDefault();
                if (_repository.Find(x => x.agv_fromaddress == fromaddress || x.agv_toaddress == fromaddress).Any())
                    throw new Exception($"货位{fromaddress}已存在任务!");
                if (station == null) throw new Exception($"未找到货位{fromaddress}!");
                if (!station.enable) throw new Exception($"货位{fromaddress}未启用!");
                if (station.quantity < 1) throw new Exception($"货位{fromaddress}无车轮!");
                var Work = workinfoRepository.Find(x => x.workOrder == station.Number && x.processCode == "17").FirstOrDefault();
                if (Work == null) throw new Exception($"未找到货位{fromaddress}的机加工工单信息");
                dt_agvtask agvtask = new dt_agvtask()
                {
                    agv_fromaddress = station.stationCode,
                    agv_id = Guid.NewGuid(),
                    agv_tasknum = IdenxManager.GetTaskNo("KH-", "WMS"),
                    agv_grade = 2,
                    agv_createtime = DateTime.Now,
                    agv_taskstate = "Queue",
                    agv_materielid = station.stationType,
                    agv_qty = station.quantity,
                    agv_tasktype = "TaskType_OutsourceInbound",
                    agv_toaddress = "",
                    agv_userid = UserContext.Current.UserName,
                    bindSN = station.bindSN,
                    agv_worktype = Convert.ToInt32(Work.processCode),
                    agv_materbarcode = Work.materialCode,
                    agv_Traytype = station.tray_type,
                    jobID = station.Number,
                    agv_TrayStatus = station.tray_status
                };
                _repository.Add(agvtask, true);
                station.location_state = LocationStateEnum.InBusy.ToString();
                stationinfoRepository.Update(station, true);
                content.OK();
            }
            catch (Exception ex)
            {
                content.Message = ex.Message;
            }
            return content;
        }
        #endregion
 
        #region 添加任务
        /// <summary>
        /// 添加任务
        /// </summary>
        /// <param name="saveDataModel"></param>
        /// <returns></returns>
        //public override WebResponseContent Add(SaveModel saveDataModel)
        //{
        //    WebResponseContent content = new WebResponseContent();
        //    try
        //    {
        //        VOLContext context = new VOLContext();
        //        Idt_stationinfoRepository stationinfoRepository = new dt_stationinfoRepository(context);
        //        var fromaddress = saveDataModel.MainData["agv_fromaddress"].ToString();
        //        var toaddress = saveDataModel.MainData["agv_toaddress"].ToString();
        //        var tasktype = saveDataModel.MainData["agv_tasktype"].ToString();
        //        var grade = saveDataModel.MainData["agv_grade"].ToInt();
        //        var worktype = saveDataModel.MainData["agv_worktype"].ToInt();
        //        var station1 = stationinfoRepository.FindFirst(x => x.stationCode == fromaddress);
        //        var station2 = stationinfoRepository.FindFirst(x => x.stationCode == toaddress);
        //        if (station1 == null || station2 == null)
        //            throw new Exception($"起点地址{fromaddress}或终点地址{toaddress}不存在!");
        //        if (string.IsNullOrEmpty(station1.stationType))
        //            throw new Exception($"起点{fromaddress}未绑定物料类型!");
        //        var task = _repository.Find(t => t.agv_fromaddress == fromaddress
        //               || t.agv_fromaddress == toaddress
        //               || t.agv_toaddress == fromaddress
        //               || t.agv_toaddress == toaddress
        //               ).Any();
        //        if (task)
        //            throw new Exception("起始或目的地址,已存在任务中!");
        //        dt_agvtask agvtask = new dt_agvtask();
        //        agvtask.agv_id = Guid.NewGuid();
        //        agvtask.agv_fromaddress = fromaddress;
        //        agvtask.agv_toaddress = toaddress;
        //        agvtask.agv_tasknum = IdenxManager.GetTaskNo("KH-");
        //        agvtask.agv_TrayStatus = station1.tray_status;
        //        agvtask.agv_Traytype = station1.tray_type;
        //        agvtask.agv_taskstate = AGVTaskStateEnum.Create.ToString();
        //        agvtask.agv_tasktype = tasktype;
        //        agvtask.agv_worktype = worktype;//工作类型
        //        agvtask.bindSN = station1.bindSN;
        //        agvtask.agv_materielid = station1.stationType;//物料类型
        //        agvtask.agv_qty = station1.quantity;
        //        agvtask.agv_createtime = DateTime.Now;
        //        agvtask.agv_grade = grade;//任务优先级
        //        agvtask.agv_userid = UserContext.Current.UserName;
        //        _repository.Add(agvtask, true);
        //        content.OK();
        //        WriteDBLog.Success($"手动添加任务", new { 数据 = saveDataModel }, "WMS", UserContext.Current.UserName);
        //    }
        //    catch (Exception ex)
        //    {
        //        WriteDBLog.Error($"手动添加任务", new { 数据 = saveDataModel, 异常信息 = ex.Message }, "WMS", UserContext.Current.UserName);
        //        content.Error(ex.Message);
        //    }
 
        //    return content;
        //}
        #endregion
 
    }
}