/* *所有关于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); } /// /// 添加库存 /// /// /// 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 list = new List(); List workinfos = new List(); 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(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); } /// /// 修改库存 /// /// /// 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 list1 = new List(); 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(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); } /// /// 删除库存 /// /// /// /// public override WebResponseContent Del(object[] keys, bool delList = true) { WebResponseContent content = new WebResponseContent(); string str = ""; try { List KeyList = new List(); 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 lists = new List(); 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(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; } } }