分支自 SuZhouGuanHong/TaiYuanTaiZhong

dengjunjie
2024-04-27 0b5ccdca6263cf7a2cee460f30c76ef1efea2811
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
 *所有关于dt_inventory类的业务代码应在此处编写
*可使用repository.调用常用方法,获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*dt_inventoryService对增、删、改查、导入、导出、审核业务代码扩展参照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 System.Configuration;
using WIDESEA_Comm.LogInfo;
using WIDESEA_Core.ManageUser;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using WIDESEA_Core.EFDbContext;
using WIDESEA_WMS.Repositories;
using System.Collections.Generic;
using System.Threading.Tasks;
using static WIDESEA_Comm.MES_Info.BasicSN;
using System.Collections;
using static FreeSql.Internal.GlobalFilter;
using WIDESEA_Comm.MES_Info.Request;
using System.Xml.Linq;
using Newtonsoft.Json;
using Confluent.Kafka;
using WIDESEA_WMS.Common;
using WIDESEA_Comm.MES_Info;
using System.Reflection;
using static System.Collections.Specialized.BitVector32;
 
namespace WIDESEA_WMS.Services
{
    public partial class dt_inventoryService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly Idt_inventoryRepository _repository;//访问数据库
 
        [ActivatorUtilitiesConstructor]
        public dt_inventoryService(
            Idt_inventoryRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码,其他情况可以不用
            //base.Init(dbRepository);
        }
 
 
        /// <summary>
        /// 添加库存
        /// </summary>
        /// <param name="saveDataModel"></param>
        /// <returns></returns>
        public override WebResponseContent Add(SaveModel saveDataModel)
        {
            WebResponseContent content = new WebResponseContent();
            VOLContext context = new VOLContext();
            IVV_Mes_WorkinfoRepository workinfoRepository = new VV_Mes_WorkinfoRepository(context);
            Idt_stationinfoRepository stationinfoRepository = new dt_stationinfoRepository(context);
            var stationCode = saveDataModel.MainData["stationCode"].ToString();
            try
            {
                if (string.IsNullOrEmpty(stationCode)) throw new Exception($"缓存架编号不能为空!");
                if (!stationCode.Contains("B") && !stationCode.Contains("C") && !stationCode.Contains("D")) throw new Exception($"缓存架类型有误!");
                var station = stationinfoRepository.Find(x => x.stationCode == stationCode).FirstOrDefault();
                if (station == null) throw new Exception($"未找到货位{stationCode}");
                var bindSNs = saveDataModel.MainData["SN"].ToString().Split(",");
                var SNS = bindSNs.GroupBy(x => x).Where(x => x.Count() > 1).Select(x => x.Key);
                foreach (var SN in SNS)
                {
                    throw new Exception($"重复的SN号{SN}");
                }
                List<detail> list = new List<detail>();
                List<VV_Mes_Workinfo> workinfos = new List<VV_Mes_Workinfo>();
                foreach (var item in bindSNs)
                {
                    var work = workinfoRepository.Find(x => x.SN == item && x.processCode == "17").FirstOrDefault();
                    if (work == null) throw new Exception($"未找到车轮SN号:{item}的工单信息!");
                    workinfos.Add(work);
                    detail detail = new detail();
                    detail.sn = item;
                    list.Add(detail);
                }
                agvInWarehousePara outWarehousePara = new agvInWarehousePara()
                {
                    //WorkOrder = task.jobID,
                    zoneID = stationCode,
                    //processCode = task.agv_worktype.ToString(),
                    details = list,
                    layerNo = 1,
                    stackID = "1",
                    warehouseName = "Agv库",
                    Operator = UserContext.Current.UserName,
                };
                var postJson = JsonConvert.SerializeObject(outWarehousePara);
                var mesData = Request.RequestData(postJson, MESAPIAddress.IPAddress_MES + "agvInWarehouse");
                if (mesData.Contains("连接尝试失败")) throw new Exception(mesData);
                MES_Response requestMes = JsonConvert.DeserializeObject<MES_Response>(mesData);
 
                if (requestMes.code == "200" && requestMes.Type == "success")
                {
                    //日志记录上传数据成功
                    WriteWMSLog.LogAdd("", "成功", "MES", "WMS", postJson, mesData, "人工同步MES车轮入库", "agvInWarehouse", requestMes.message);
                    foreach (var workinfo in workinfos)
                    {
                        dt_inventory inventory = new dt_inventory()
                        {
                            SN = workinfo.SN,
                            Name = workinfo.productName,
                            FigureNumber = workinfo.drawingNo,
                            materialCode = workinfo.materialCode,
                            HeatNumber = workinfo.heatID,
                            drawingNoVer = workinfo.drawingNoVer,
                            BilletNumber = workinfo.billetID,
                            OnlineTime = DateTime.Now,
                            Operator = "admin",
                            stationCode = stationCode,
                            area = station.area,
                            jobID = workinfo.jobID,
                            workOrder = workinfo.workOrder,
                            ID = Guid.NewGuid()
                        };
                        _repository.Add(inventory, true);
                    }
                    WriteDBLog.Write($"手动添加库存 ", $"货位编号:{stationCode};SN号:{saveDataModel.MainData["SN"]}", LogState.Sucess, "WMS", UserContext.Current.UserName);
                }
                else
                {
                    WriteWMSLog.LogAdd("", "失败", "MES", "WMS", postJson, mesData, "人工同步MES车轮入库", "agvInWarehouse", requestMes.message);
                    throw new Exception("人工同步MES车轮入库失败!" + requestMes.message);
                }
            }
            catch (Exception ex)
            {
                content.Message = ex.Message;
                WriteDBLog.Write($"手动添加库存 ", new { 错误信息 = ex.Message, 数据 = $"货位编号:{stationCode};SN号:{saveDataModel.MainData["SN"]}" }, LogState.Error, "WMS", UserContext.Current.UserName);
            }
            return content;
            //return base.Add(saveDataModel);
        }
        /// <summary>
        /// 修改库存
        /// </summary>
        /// <param name="saveModel"></param>
        /// <returns></returns>
        public override WebResponseContent Update(SaveModel saveModel)
        {
            WebResponseContent content = new WebResponseContent();
            string str = "";
            try
            {
                VOLContext context = new VOLContext();
                Idt_stationinfoRepository stationinfoRepository = new dt_stationinfoRepository(context);
                var stationCode = saveModel.MainData["stationCode"].ToString();
                if (string.IsNullOrEmpty(stationCode)) throw new Exception($"缓存架编号不能为空!");
                if (!stationCode.Contains("B") && !stationCode.Contains("C") && !stationCode.Contains("D")) throw new Exception($"缓存架类型有误!");
                if (!stationinfoRepository.Find(x => x.stationCode == stationCode).Any()) throw new Exception($"未找到货位{stationCode}");
                var inventory = _repository.Find(x => x.ID.ToString() == saveModel.MainData["ID"].ToString()).FirstOrDefault();
                str = $"原货位编号:{inventory.stationCode};现货位编号:{stationCode};SN号:{inventory.SN}";
                List<detail1> list1 = new List<detail1>();
                detail1 detail = new detail1();
                detail.sn = inventory.SN;
                list1.Add(detail);
                agvMoveWarehousePara moveWarehousePara = new agvMoveWarehousePara()
                {
                    fromZoneID = inventory.stationCode,
                    toZoneID = stationCode,
                    details = list1,
                    fromLayerNo = "1",
                    fromStackID = "1",
                    fromWarehouseName = "Agv库",
                    toLayerNo = "1",
                    toStackID = "1",
                    toWarehouseName = "Agv库",
                    Operator = UserContext.Current.UserName,
                };
                var postJson = JsonConvert.SerializeObject(moveWarehousePara);
                var mesData = Request.RequestData(postJson, MESAPIAddress.IPAddress_MES + "agvMoveWarehouse");
                if (mesData.Contains("连接尝试失败"))
                    throw new Exception(mesData);
                MES_Response requestMes = JsonConvert.DeserializeObject<MES_Response>(mesData);
                if (requestMes.code == "200" && requestMes.Type == "success")
                {
                    //日志记录上传数据成功
                    WriteWMSLog.LogAdd("", "成功", "MES", "WMS", postJson, mesData, "人工同步MES车轮移库", "agvMoveWarehouse", requestMes.message);
                    inventory.stationCode = stationCode;
                    _repository.Update(inventory, true);
                    WriteDBLog.Write($"手动移库库存 ", str, LogState.Sucess, "WMS", UserContext.Current.UserName);
                }
                else
                {
                    WriteWMSLog.LogAdd("", "失败", "MES", "WMS", postJson, mesData, "人工同步MES车轮移库", "agvMoveWarehouse", requestMes.message);
                    throw new Exception("人工同步MES车轮移库失败!" + requestMes.message);
                }
 
                content.OK();
            }
            catch (Exception ex)
            {
                content.Message = ex.Message;
                WriteDBLog.Write($"手动移库库存 ", new { 错误信息 = ex.Message, 数据 = str }, LogState.Error, "WMS", UserContext.Current.UserName);
            }
            return content;
            //return base.Update(saveModel);
        }
        /// <summary>
        /// 删除库存
        /// </summary>
        /// <param name="keys"></param>
        /// <param name="delList"></param>
        /// <returns></returns>
        public override WebResponseContent Del(object[] keys, bool delList = true)
        {
            WebResponseContent content = new WebResponseContent();
            string str = "";
            try
            {
                List<string> KeyList = new List<string>();
                foreach (string key in keys)
                {
                    KeyList.Add(key);
                }
                var inventoryList = _repository.Find(x => KeyList.Contains(x.ID.ToString())).ToList();
                //str = $"{string.Join('、', inventoryList.Select(t => t.SN).ToArray())}";
                foreach (var inventorys in inventoryList.GroupBy(x => x.stationCode))
                {
                    try
                    {
                        List<detail> lists = new List<detail>();
                        foreach (var inventory in inventorys)
                        {
                            detail detail = new detail();
                            detail.sn = inventory.SN;
                            lists.Add(detail);
                            str = str == "" ? inventory.SN : "," + inventory.SN;
                        }
                        agvInWarehousePara outWarehousePara = new agvInWarehousePara()
                        {
                            //WorkOrder = task.jobID,
                            zoneID = inventorys.Key,
                            //processCode = task.agv_worktype.ToString(),
                            details = lists,
                            layerNo = 1,
                            stackID = "1",
                            warehouseName = "Agv库",
                            Operator = UserContext.Current.UserName,
                        };
                        var postJson = JsonConvert.SerializeObject(outWarehousePara);
                        var mesData = Request.RequestData(postJson, MESAPIAddress.IPAddress_MES + "agvOutWarehouse");
                        if (mesData.Contains("连接尝试失败")) throw new Exception(mesData);
                        MES_Response requestMes = JsonConvert.DeserializeObject<MES_Response>(mesData);
                        if (requestMes.code == "200" && requestMes.Type == "success")
                        {
                            //日志记录上传数据成功
                            WriteWMSLog.LogAdd("", "成功", "MES", "WMS", postJson, mesData, "人工同步MES车轮出库", "agvOutWarehouse", requestMes.message);
 
                            foreach (var inventory in inventorys)
                            {
                                _repository.Delete(inventory, true);
                            }
                            WriteDBLog.Write($"手动删除库存 ", $"货位编号:{inventorys.Key};SN号:{str}", LogState.Sucess, "WMS", UserContext.Current.UserName);
                        }
                        else
                        {
                            WriteWMSLog.LogAdd("", "失败", "MES", "WMS", postJson, mesData, "人工同步MES车轮出库", "agvOutWarehouse", requestMes.message);
                            throw new Exception("人工同步MES车轮出库失败!" + requestMes.message);
                        }
                    }
                    catch (Exception ex)
                    {
                        content.Message = ex.Message;
                        WriteDBLog.Write($"手动删除库存 ", new { 错误信息 = ex.Message, 数据 = str }, LogState.Error, "WMS", UserContext.Current.UserName);
                    }
 
                }
                content.OK();
                //content = base.Del(keys, delList);
                //WriteDBLog.Write($"手动删除库存 ", str, LogState.Sucess, "WMS", UserContext.Current.UserName);
            }
            catch (Exception ex)
            {
                content.Message = ex.Message;
                WriteDBLog.Write($"手动删除库存 ", new { 错误信息 = ex.Message, 数据 = str }, LogState.Error, "WMS", UserContext.Current.UserName);
            }
 
            return content;
        }
    }
}