分支自 SuZhouGuanHong/TaiYuanTaiZhong

dengjunjie
2024-04-27 0b5ccdca6263cf7a2cee460f30c76ef1efea2811
´úÂë¹ÜÀí/WMS/WMS_Server/WIDESEA_WMS/Services/system/Partial/dt_inventoryService.cs
@@ -21,6 +21,21 @@
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
{
@@ -42,6 +57,170 @@
            //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();
@@ -49,17 +228,70 @@
            try
            {
                List<string> KeyList = new List<string>();
                foreach ( string key in keys )
                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())}";
                content = base.Del(keys, delList);
                WriteDBLog.Write($"手动删除库存 ", str, LogState.Sucess, "WMS", UserContext.Current.UserName);
                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);
            }