分支自 SuZhouGuanHong/TaiYuanTaiZhong

dengjunjie
2024-03-25 65151e7ac63e10113ecbeeabf302a4240bf08e97
´úÂë¹ÜÀí/PCS/WCS_Server/WIDESEA_WCS/JobsPart/Common/OutboundTask.cs
@@ -5,12 +5,15 @@
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Comm;
using WIDESEA_Comm.LogInfo;
using WIDESEA_Comm.TaskNo;
using WIDESEA_Core.EFDbContext;
using WIDESEA_Core.FreeDB;
using WIDESEA_Core.Extensions;
using WIDESEA_Entity.DomainModels;
using WIDESEA_WCS.IRepositories;
using WIDESEA_WCS.Repositories;
using WIDESEA_WMS.IRepositories;
using WIDESEA_WMS.Repositories;
using static FreeSql.Internal.GlobalFilter;
using static System.Collections.Specialized.BitVector32;
@@ -18,7 +21,6 @@
{
    public class OutboundTask
    {
        static FreeDB freeDB = new FreeDB();
        /// <summary>
        /// åˆ›å»ºå‡ºåº“任务
        /// </summary>
@@ -28,21 +30,39 @@
            {
                VOLContext context = new VOLContext();
                Idt_stationinfoRepository stationinfoRepository = new dt_stationinfoRepository(context);
                var mes_heads = freeDB.Select<dt_mes_head>().Where(x => x.processCode == "28").OrderBy(x => x.expectedStartTime).ToList();
                Idt_mes_headRepository mes_HeadRepository = new dt_mes_headRepository(context);
                Idt_mes_detailRepository mes_DetailRepository = new dt_mes_detailRepository(context);
                Idt_inventoryRepository inventoryRepository = new dt_inventoryRepository(context);
                Idt_agvtaskRepository agvtaskRepository = new dt_agvtaskRepository(context);
                var mes_heads = mes_HeadRepository.Find(x => x.processCode == "28").OrderBy(x => x.expectedStartTime).ToList();
                //var mes_heads = mes_HeadRepository.Find(x => x.processCode == "28").OrderByDescending(x => x.CreateTime).ToList();
                foreach (var mes_head in mes_heads)
                {
                    #region æ ¹æ®åº“存入库时间最晚查询
                    var inventory = freeDB.Select<dt_inventory>().Where(x => x.FigureNumber == mes_head.drawingNo).OrderByDescending(x => x.OnlineTime).First();
                    var inventory = inventoryRepository.Find(x => x.FigureNumber == mes_head.drawingNo).OrderByDescending(x => x.OnlineTime).FirstOrDefault();
                    if (inventory != null)
                    {
                        var Station = stationinfoRepository.Find(x => x.stationCode == inventory.stationCode).FirstOrDefault();
                        if (Station != null)
                        {
                            var stationinfo = stationinfoRepository.Find(x => x.line == Station.line && x.area == Station.area && x.enable).OrderByDescending(x => x.column).FirstOrDefault();
                            #region å­˜åœ¨å…¥åº“任务不能出库
                            var InTask = stationinfoRepository.Find(x => x.line == Station.line && x.area == Station.area && x.location_state == LocationStateEnum.InBusy.ToString()).Any();
                            if (InTask) continue;
                            #endregion
                            var stationinfo = stationinfoRepository.Find(x => x.line == Station.line && x.area == Station.area && x.enable && x.location_state == LocationStateEnum.Stroge.ToString()).OrderByDescending(x => x.column).FirstOrDefault();
                            if (stationinfo != null)
                            {
                                if (stationinfo.bindSN == null)
                                    throw new Exception(stationinfo.stationCode + "车轮SN号信息异常!");
                                if (string.IsNullOrEmpty(stationinfo.bindSN))
                                {
                                    stationinfo.remark = "车轮SN号信息异常";
                                    stationinfo.location_state = "Abnormal";
                                    stationinfoRepository.Update(stationinfo, true);
                                    continue;
                                    //throw new Exception(stationinfo.stationCode + "车轮SN号信息异常!");
                                }
                                if (agvtaskRepository.Find(x => x.agv_fromaddress == stationinfo.stationCode).Any())
                                    continue;
                                List<string> lists = new List<string>();
                                var SNS = stationinfo.bindSN.Split(",");
                                foreach (var SN in SNS)
@@ -52,7 +72,63 @@
                                }
                                foreach (var item in lists)
                                {
                                    if (freeDB.Select<dt_mes_detail>().Where(x => x.jobID == mes_head.jobID && x.SN == item).Any())
                                    //var a = mes_DetailRepository.Find(x => x.jobID == mes_head.jobID /*&& x.SN == item*/).ToList();
                                    //var b = a.Where(x => x.SN == item).FirstOrDefault();
                                    if (mes_DetailRepository.Find(x => x.jobID == mes_head.jobID && x.SN == item).Any())
                                    {
                                        dt_agvtask agvtask = new dt_agvtask()
                                        {
                                            agv_fromaddress = stationinfo.stationCode,
                                            agv_id = Guid.NewGuid(),
                                            agv_tasknum = IdenxManager.GetTaskNo("KH-", "WMS"),
                                            agv_grade = 2,
                                            agv_createtime = DateTime.Now,
                                            agv_taskstate = "Queue",
                                            agv_materielid = stationinfo.stationType,
                                            agv_qty = stationinfo.quantity,
                                            agv_tasktype = "TaskType_Outbound",
                                            agv_toaddress = "",
                                            agv_userid = "系统",
                                            bindSN = stationinfo.bindSN,
                                            jobID = mes_head.jobID,
                                            agv_worktype = Convert.ToInt32(mes_head.processCode),
                                            agv_materbarcode = mes_head.materialCode,
                                            agv_Traytype = stationinfo.tray_type,
                                            agv_TrayStatus = stationinfo.tray_status
                                        };
                                        stationinfo.location_state = LocationStateEnum.OutBusy.ToString();
                                        stationinfoRepository.Update(stationinfo, true);
                                        agvtaskRepository.Add(agvtask, true);
                                        WriteDBLog.Success("创建出库任务", $"任务编号:{agvtask.agv_tasknum}", "PCS");
                                        return;
                                    }
                                }
                            }
                            var stationinfos = stationinfoRepository.Find(x => x.stationType == mes_head.drawingNo && x.line != Station.line && x.area == Station.area && x.location_state == "Stroge").OrderBy(x => x.line).ThenByDescending(x => x.column).ToList();
                            foreach (var item in stationinfos)
                            {
                                if (item != null && stationinfo.enable)
                                {
                                    if (item.bindSN == null)
                                    {
                                        stationinfo.remark = "车轮SN号信息异常";
                                        stationinfo.location_state = "Abnormal";
                                        stationinfoRepository.Update(stationinfo, true);
                                        continue;
                                    }
                                    if (agvtaskRepository.Find(x => x.agv_fromaddress == stationinfo.stationCode).Any())
                                        continue;
                                    List<string> lists = new List<string>();
                                    var SNS = stationinfo.bindSN.Split(",");
                                    foreach (var SN in SNS)
                                    {
                                        if (!string.IsNullOrEmpty(SN))
                                            lists.Add(SN);
                                    }
                                    foreach (var list in lists)
                                    {
                                        if (mes_DetailRepository.Find(x => x.jobID == mes_head.jobID && x.SN == list).Any())
                                    {
                                        dt_agvtask agvtask = new dt_agvtask()
                                        {
@@ -64,6 +140,7 @@
                                            agv_taskstate = "Queue",
                                            agv_materielid = stationinfo.stationType,
                                            agv_qty = stationinfo.quantity,
                                                jobID = mes_head.jobID,
                                            agv_tasktype = "TaskType_Outbound",
                                            agv_toaddress = "",
                                            agv_userid = "系统",
@@ -73,67 +150,63 @@
                                            agv_Traytype = stationinfo.tray_type,
                                            agv_TrayStatus = stationinfo.tray_status
                                        };
                                        dt_stationinfo TargetLocation = GetEmptyLocation(stationinfoRepository);
                                        if (TargetLocation != null)
                                        {
                                            agvtask.agv_taskstate = "Create";
                                            agvtask.agv_toaddress = TargetLocation.stationCode;
                                            TargetLocation.location_state = LocationStateEnum.Busy.ToString();
                                            freeDB.Update(TargetLocation);
                                        }
                                        freeDB.Add(agvtask);
                                            WriteDBLog.Success("创建出库任务", $"任务编号:{agvtask.agv_tasknum}", "PCS");
                                            agvtaskRepository.Add(agvtask, true);
                                        return;
                                    }
                                }
                            }
                            stationinfo = stationinfoRepository.Find(x => x.stationType == mes_head.drawingNo && x.line != Station.line).OrderBy(x => x.line).ThenByDescending(x => x.column).FirstOrDefault();
                            if (stationinfo != null && stationinfo.enable)
                            {
                                if (stationinfo.bindSN == null)
                                    throw new Exception(stationinfo.stationCode + "车轮SN号信息异常!");
                                List<string> lists = new List<string>();
                                var SNS = stationinfo.bindSN.Split(",");
                                foreach (var SN in SNS)
                                {
                                    if (!string.IsNullOrEmpty(SN))
                                        lists.Add(SN);
                                }
                                foreach (var item in lists)
                                {
                                    if (freeDB.Select<dt_mes_detail>().Where(x => x.jobID == mes_head.jobID && x.SN == item).Any())
                                    {
                                        dt_agvtask agvtask = new dt_agvtask()
                                        {
                                            agv_fromaddress = stationinfo.stationCode,
                                            agv_id = Guid.NewGuid(),
                                            agv_tasknum = IdenxManager.GetTaskNo("KH-", "WMS"),
                                            agv_grade = 1,
                                            agv_createtime = DateTime.Now,
                                            agv_taskstate = "Queue",
                                            agv_materielid = stationinfo.stationType,
                                            agv_qty = stationinfo.quantity,
                                            agv_tasktype = "TaskType_Outbound",
                                            agv_toaddress = "",
                                            agv_userid = "系统",
                                            bindSN = stationinfo.bindSN,
                                            agv_worktype = Convert.ToInt32(mes_head.processCode),
                                            agv_materbarcode = mes_head.materialCode,
                                            agv_Traytype = stationinfo.tray_type,
                                            agv_TrayStatus = stationinfo.tray_status
                                        };
                                        dt_stationinfo TargetLocation = GetEmptyLocation(stationinfoRepository);
                                        if (TargetLocation != null)
                                        {
                                            agvtask.agv_taskstate = "Create";
                                            agvtask.agv_toaddress = TargetLocation.stationCode;
                                            TargetLocation.location_state = LocationStateEnum.Busy.ToString();
                                            freeDB.Update(TargetLocation);
                                        }
                                        freeDB.Add(agvtask);
                                        return;
                                    }
                                }
                            }
                            #region MyRegion
                            //if (stationinfo != null && stationinfo.enable)
                            //{
                            //    if (stationinfo.bindSN == null)
                            //        throw new Exception(stationinfo.stationCode + "车轮SN号信息异常!");
                            //    List<string> lists = new List<string>();
                            //    var SNS = stationinfo.bindSN.Split(",");
                            //    foreach (var SN in SNS)
                            //    {
                            //        if (!string.IsNullOrEmpty(SN))
                            //            lists.Add(SN);
                            //    }
                            //    foreach (var item in lists)
                            //    {
                            //        if (freeDB.Select<dt_mes_detail>().Where(x => x.jobID == mes_head.jobID && x.SN == item).Any())
                            //        {
                            //            dt_agvtask agvtask = new dt_agvtask()
                            //            {
                            //                agv_fromaddress = stationinfo.stationCode,
                            //                agv_id = Guid.NewGuid(),
                            //                agv_tasknum = IdenxManager.GetTaskNo("KH-", "WMS"),
                            //                agv_grade = 1,
                            //                agv_createtime = DateTime.Now,
                            //                agv_taskstate = "Queue",
                            //                agv_materielid = stationinfo.stationType,
                            //                agv_qty = stationinfo.quantity,
                            //                agv_tasktype = "TaskType_Outbound",
                            //                agv_toaddress = "",
                            //                agv_userid = "系统",
                            //                bindSN = stationinfo.bindSN,
                            //                agv_worktype = Convert.ToInt32(mes_head.processCode),
                            //                agv_materbarcode = mes_head.materialCode,
                            //                agv_Traytype = stationinfo.tray_type,
                            //                agv_TrayStatus = stationinfo.tray_status
                            //            };
                            //            dt_stationinfo TargetLocation = GetEmptyLocation(stationinfoRepository);
                            //            if (TargetLocation != null)
                            //            {
                            //                agvtask.agv_taskstate = "Create";
                            //                agvtask.agv_toaddress = TargetLocation.stationCode;
                            //                TargetLocation.location_state = LocationStateEnum.Busy.ToString();
                            //                freeDB.Update(TargetLocation);
                            //            }
                            //            freeDB.Add(agvtask);
                            //            return;
                            //        }
                            //    }
                            //}
                            #endregion
                        }
                    }
                    #endregion
@@ -189,27 +262,11 @@
                }
            }
            catch (Exception)
            catch (Exception ex)
            {
                throw;
                WriteDBLog.Error("创建出库任务", $"错误信息:{ex.Message}", "PCS");
            }
        }
        private static dt_stationinfo GetEmptyLocation(Idt_stationinfoRepository stationinfoRepository)
        {
            dt_stationinfo TargetLocation = null;
            var stations = stationinfoRepository.Find(x => x.stationCode.Contains("S01001"));
            foreach (var station in stations)
            {
                TargetLocation = stationinfoRepository.FindFirst(x => x.stationCode == station.stationCode && x.location_state == LocationStateEnum.Empty.ToString() && x.enable);
                if (TargetLocation != null) continue;
            }
            if (TargetLocation == null)
            {
                throw new Exception("检测线上料位已满");
            }
            return TargetLocation;
        }
    }
}