分支自 SuZhouGuanHong/TaiYuanTaiZhong

dengjunjie
2024-07-19 7a4c218909936721fe281737491d10efc7378e09
´úÂë¹ÜÀí/PCS/WCS_Server/WIDESEA_WCS/JobsPart/Common/StationTask.cs
@@ -6,18 +6,23 @@
using WIDESEA_WCS.IRepositories;
using WIDESEA_WCS.Repositories;
using WIDESEA_Core.EFDbContext;
using WIDESEA_Core.FreeDB;
using WIDESEA_Entity.DomainModels;
using WIDESEA_Comm;
using static System.Collections.Specialized.BitVector32;
using WIDESEA_Comm.TaskNo;
using WIDESEA_Entity.ToAGV;
using WIDESEA_WCS.WCSClient;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Math;
using WIDESEA_WMS.IRepositories;
using WIDESEA_WMS.Repositories;
using static FreeSql.Internal.GlobalFilter;
using WIDESEA_Comm.LogInfo;
namespace WIDESEA_WCS.JobsPart.Common
{
    public class StationTask
    {
        static FreeDB freeDB = new FreeDB();
        /// <summary>
        /// åˆ›å»ºå…¥åº“任务
        /// </summary>
@@ -27,96 +32,64 @@
            {
                VOLContext context = new VOLContext();
                Idt_stationinfoRepository stationinfoRepository = new dt_stationinfoRepository(context);
                Idt_WorkinfoRepository workinfoRepository = new dt_WorkinfoRepository(context);
                Idt_mes_headRepository mes_HeadRepository = new dt_mes_headRepository(context);
                Idt_agvtaskRepository agvtaskRepository = new dt_agvtaskRepository(context);
                IVV_Mes_WorkinfoRepository workinfoRepository = new VV_Mes_WorkinfoRepository(context);
                List<dt_stationinfo> stationinfos = stationinfoRepository.Find(x => x.stationCode.Contains('X'));
                var Pipeline_client = PLCClient.Clients.FirstOrDefault(t => t.PLCName == "链条机");
                if (Pipeline_client == null) throw new Exception("链条机调度服务未开启!");
                if (!Pipeline_client.IsConnected) throw new Exception("与链条机连接超时!");
                List<dt_stationinfo> stationinfos = stationinfoRepository.Find(x => x.stationCode.Contains("X") && x.enable && x.quantity > 0 && !string.IsNullOrEmpty(x.stationType) && !string.IsNullOrEmpty(x.Number)).ToList();
                foreach (var item in stationinfos)
                {
                    //根据下料口的绑定信息查询对应工单,检测此工单是否已完成  å®Œæˆå³å¯å°†å…¶é€å…¥åº“
                    dt_mes_head mesinfo = freeDB.Select<dt_mes_head>().Where(x => x.jobID == item.Number).First();
                    dt_geometry_data dt_Geometry_Data = freeDB.Select<dt_geometry_data>().Where(x => x.Description == mesinfo.drawingNo).First();
                    int CompeletedNum = Convert.ToInt32(mesinfo.quantity) - Convert.ToInt32(mesinfo.finishNum);
                    //判定任务是否已创建//如已存在  
                    if (freeDB.Select<dt_agvtask>().Where(x => x.agv_fromaddress == item.stationCode).Count() > 0)
                    if (agvtaskRepository.Find(x => x.agv_fromaddress == item.stationCode).Any())
                        continue;
                    if (item.quantity == 5 || CompeletedNum == 0)  //循环读取车轮数为5或者订单已完成数量的下料口    â†’工单人工关闭的工单
                    var PalletSignal = Pipeline_client.ReadByOrder<Int16>("R_PalletSignal", item.stationCode);//读取托盘信号:1:有,2无
                    var MaterialSignal = Pipeline_client.ReadByOrder<Int16>("R_MaterialSignal", item.stationCode);//读取货物信号:1:有,2无
                    if (PalletSignal != 1 || MaterialSignal != 1) continue;
                    //根据下料口的绑定信息查询对应工单,检测此工单是否已完成  å®Œæˆå³å¯å°†å…¶é€å…¥åº“
                    var work = workinfoRepository.Find(x => x.workOrder == item.Number && x.drawingNo == item.stationType && x.heatID == item.heatNumber && x.processCode == "17").OrderByDescending(x => x.CreateTime).FirstOrDefault();
                    if (work == null)
                    {
                        item.remark = "触发入库任务,但未找到工单信息!";
                        item.location_state = LocationStateEnum.Abnormal.ToString();
                        stationinfoRepository.Update(item, x => new { x.remark, x.location_state }, true);
                        continue;
                    }
                    var geometry = Pipeline.QueryMateriel(work.drawingNo);
                    if (geometry == null)
                    {
                        item.remark = "未找到车轮信息!";
                        item.location_state = LocationStateEnum.Abnormal.ToString();
                        stationinfoRepository.Update(item, x => new { x.remark, x.location_state }, true);
                        continue;
                    }
                    dt_stationinfo TargetLocation = null;
                    string tasktype = "";
                        if (mesinfo.quantity <= 50 || item.stationCode.Contains("3"))    //小于50件直接出库
                        {
                            dt_stationinfo TargetLocation = GetEmptyLocation(stationinfoRepository);
                            //todo å¯»æ‰¾å¯æ”¾è´§å¤–协放货台
                            if (TargetLocation != null)
                            {
                                dt_agvtask agvtask = new dt_agvtask()
                                {
                                    agv_fromaddress = item.stationCode,
                                    agv_id = Guid.NewGuid(),
                                    agv_tasknum = IdenxManager.GetTaskNo("KH-", "WMS"),
                                    agv_grade = 1,
                                    agv_createtime = DateTime.Now,
                                    agv_taskstate = "Create",
                                    agv_materielid = item.stationType,
                                    agv_qty = item.quantity,
                                    agv_tasktype = "TaskType_OutsourceInbound",
                                    agv_toaddress = TargetLocation.stationCode,
                                    agv_userid = "系统",
                                    bindSN = item.bindSN,
                                    agv_worktype = Convert.ToInt32(mesinfo.processCode),
                                    agv_materbarcode = mesinfo.materialCode,
                                    agv_barcode=item.tray_type
                                };
                                freeDB.Add(agvtask);
                                TargetLocation.location_state = LocationStateEnum.Busy.ToString();
                                freeDB.Update(TargetLocation);
                            }
                        }
                    //if (item.quantity == 5)
                    if (item.quantity == geometry.stackNoRange)
                    {
                        if (!string.IsNullOrEmpty(work.area) || item.stationCode.Contains("3"))
                            tasktype = "TaskType_OutsourceInbound";
                        else
                        {
                            dt_stationinfo TargetLocation = GetEmptyLocation(stationinfoRepository, mesinfo, item, CompeletedNum, dt_Geometry_Data.e);
                            tasktype = "TaskType_Inbound";
                            if (TargetLocation != null)
                            {
                                //todo:  è°ƒç”¨WMS接口创建任务
                                dt_agvtask agvtask = new dt_agvtask()
                                {
                                    agv_fromaddress = item.stationCode,
                                    agv_id = Guid.NewGuid(),
                                    agv_tasknum = IdenxManager.GetTaskNo("KH-", "WMS"),
                                    agv_grade = 1,
                                    agv_createtime = DateTime.Now,
                                    agv_taskstate = "Create",
                                    agv_materielid = item.stationType,
                                    agv_qty = item.quantity,
                                    agv_tasktype = "TaskType_Inbound",
                                    agv_toaddress = TargetLocation.stationCode,
                                    agv_userid = "系统",
                                    bindSN = item.bindSN,
                                    agv_worktype = Convert.ToInt32(mesinfo.processCode),
                                    agv_materbarcode = mesinfo.materialCode,
                                    agv_barcode=item.tray_type
                                };
                                freeDB.Add(agvtask);
                                TargetLocation.location_state = LocationStateEnum.Busy.ToString();
                                freeDB.Update(TargetLocation);
                            }
                        }
                        AGVTask.AddQueueTask(stationinfoRepository, agvtaskRepository, item, tasktype, work);
                    }
                }
            }
            catch (Exception ex)
            {
                WriteDBLog.Error("创建入库任务", $"错误信息: {ex.Message}", "PCS");
            }
        }
        /// <summary>
@@ -126,10 +99,53 @@
        /// <param name="workinfo">订单</param>
        /// <param name="stationinfo">下料口信息</param>
        /// <returns></returns>
        private static dt_stationinfo GetEmptyLocation(Idt_stationinfoRepository stationinfoRepository, dt_mes_head mesinfo, dt_stationinfo stationinfo, int CompeletedNum, float e)
        public static dt_stationinfo GetEmptyLocation(Idt_stationinfoRepository stationinfoRepository, dt_mes_head mesinfo, dt_stationinfo stationinfo, int CompeletedNum, float e)
        {
            //放货位
            dt_stationinfo TargetLocation = null;
            #region åŒç±»åž‹æ”¾ä¸€è¡Œ
            //if (area1 == "1")
            TargetLocation = stationinfoRepository.Find(x => x.stationType == mesinfo.drawingNo).FirstOrDefault();
            //TargetLocation = stationinfoRepository.Find(x => x.stationType == mesinfo.drawingNo && x.area == area1).OrderByDescending(x => x.line).OrderBy(x => x.column).FirstOrDefault();
            if (TargetLocation != null)
            {
                #region åŒä¸€è¡Œåªå…è®¸ä¸€ä¸ªä»»åŠ¡
                //var count = stationinfoRepository.Find(x => x.line == TargetLocation.line && x.enable && x.area == TargetLocation.area && x.location_state == LocationStateEnum.Busy.ToString()).ToList();
                //if (count.Count > 0)
                //    return null;
                #endregion
                #region å‡ºåº“任务优先,存在出库任务则不能入库
                bool OutTask = false;
                if (TargetLocation.area == "1")
                {
                    TargetLocation = stationinfoRepository.Find(x => x.column == TargetLocation.column && x.area == TargetLocation.area && x.enable && x.location_state == LocationStateEnum.Empty.ToString()).OrderByDescending(x => x.line).FirstOrDefault();
                    if (TargetLocation != null)
                        OutTask = stationinfoRepository.Find(x => x.column == TargetLocation.column && x.area == TargetLocation.area && x.enable && x.location_state == LocationStateEnum.OutBusy.ToString()).Any();
                    if (OutTask)//这列存在出库任务,寻找一列空货位
                        TargetLocation = stationinfoRepository.Find(x => x.line == 2 && x.area == TargetLocation.area && x.enable && x.location_state == LocationStateEnum.Empty.ToString()).OrderBy(x => x.column).FirstOrDefault();
                    if (TargetLocation != null) return TargetLocation;
                }
                else
                {
                    TargetLocation = stationinfoRepository.Find(x => x.line == TargetLocation.line && x.area == TargetLocation.area && x.enable && x.location_state == LocationStateEnum.Empty.ToString()).OrderBy(x => x.column).FirstOrDefault();
                    if (TargetLocation != null)
                        OutTask = stationinfoRepository.Find(x => x.line == TargetLocation.line && x.area == TargetLocation.area && x.location_state == LocationStateEnum.OutBusy.ToString()).Any();
                    //库内存在出库任务,重新开一行入库
                    if (OutTask)
                        TargetLocation = stationinfoRepository.Find(x => x.column == (TargetLocation.area == "2" ? 1 : 10) && x.area == TargetLocation.area && x.enable && x.location_state == LocationStateEnum.Empty.ToString()).OrderBy(x => x.line).FirstOrDefault();
                    if (TargetLocation != null) return TargetLocation;
                }
                #endregion
                //TargetLocation = stationinfoRepository.Find(x => x.line == TargetLocation.line && x.area == TargetLocation.area && x.location_state == LocationStateEnum.Empty.ToString() && x.enable).OrderBy(x => x.column).FirstOrDefault();
                //if (TargetLocation != null)
                //    return TargetLocation;
            }
            #endregion
            #region æ ¹æ®è®¢å•数量来寻找对应库区且同物料类型库位
            //if (workinfo.PlannedQuantity < 50)    //库区1 ç‰©æ–™ç±»åž‹å¤š
@@ -206,23 +222,38 @@
            //} 
            #endregion
            var detail = freeDB.Select<dt_mes_detail>().Where(x => x.jobID == mesinfo.jobID).First();
            VOLContext context = new VOLContext();
            Idt_mes_detailRepository mes_DetailRepository = new dt_mes_detailRepository(context);
            Idt_inventoryRepository inventoryRepository = new dt_inventoryRepository(context);
            var detail = mes_DetailRepository.Find(x => x.jobID == mesinfo.jobID).FirstOrDefault();
            if (detail == null)
            {
                stationinfo.remark = "触发入库任务,但未找到工单子表!";
                stationinfo.location_state = LocationStateEnum.Abnormal.ToString();
                stationinfoRepository.Update(stationinfo, true);
                return null;
            }
            #region æŸ¥æ‰¾åº“å­˜
            dt_inventory inventory = null;
            if (detail.heatID != null)
            {
                inventory = freeDB.Select<dt_inventory>().Where(x => x.HeatNumber == detail.heatID && x.FigureNumber == mesinfo.drawingNo && x.jobID == mesinfo.jobID).First();
                inventory = inventoryRepository.Find(x => /*x.HeatNumber == detail.heatID &&*/ x.FigureNumber == mesinfo.drawingNo /*&& x.jobID == mesinfo.jobID*/).OrderByDescending(x => x.OnlineTime).FirstOrDefault();
            }
            else
            {
                inventory = freeDB.Select<dt_inventory>().Where(x => x.FigureNumber == mesinfo.drawingNo && x.jobID == mesinfo.jobID).First();
                inventory = inventoryRepository.Find(x => x.FigureNumber == mesinfo.drawingNo /*&& x.jobID == mesinfo.jobID*/).OrderByDescending(x => x.OnlineTime).FirstOrDefault();
            }
            #endregion
            #region æ ¹æ®åº“存查找可入库空货位
            if (inventory != null)
            {
                var station = stationinfoRepository.FindFirst(x => x.stationCode == inventory.stationCode);
                if (CompeletedNum <= 15 || station.area == "1")
                if (/*CompeletedNum <= 15 ||*/ station.area == "1")
                {
                    TargetLocation = stationinfoRepository.Find(x => x.column == station.column && x.location_state == LocationStateEnum.Empty.ToString() && x.enable && x.area == inventory.area).OrderBy(x => x.line).FirstOrDefault();
                    TargetLocation = stationinfoRepository.Find(x => x.column == station.column && x.location_state == LocationStateEnum.Empty.ToString() && x.enable && x.area == inventory.area).OrderByDescending(x => x.line).FirstOrDefault();
                }
                else
                {
@@ -230,10 +261,15 @@
                }
                if (TargetLocation != null) return TargetLocation;
            }
            #endregion
            if (CompeletedNum <= 15)
            var area1 = string.IsNullOrEmpty(mesinfo.area) ? GetArea(mesinfo.quantity, e) : mesinfo.area;
            if (CompeletedNum <= 15 || area1 == "1")
            {
                TargetLocation = stationinfoRepository.Find(x => x.line == 1 && x.location_state == LocationStateEnum.Empty.ToString() && x.enable && x.area == "1").OrderBy(x => x.column).FirstOrDefault();
                TargetLocation = stationinfoRepository.Find(x => x.line == 2 && x.location_state == LocationStateEnum.Empty.ToString() && x.enable && x.area == "1").OrderBy(x => x.column).FirstOrDefault();
            }
            else
            {
@@ -242,10 +278,35 @@
                //else if (180 < mesinfo.quantity && mesinfo.quantity <= 225 && e < 1100)
                //    TargetLocation = stationinfoRepository.Find(x => x.column == 1 && x.area == "3" && x.location_state == LocationStateEnum.Empty.ToString() && x.enable).OrderBy(x => x.line).FirstOrDefault();
                //if (50 < mesinfo.quantity && e > 1100 || TargetLocation == null)
                var area = GetArea(mesinfo.quantity, e);
                TargetLocation = stationinfoRepository.Find(x => x.column == 1 && x.area == area && x.location_state == LocationStateEnum.Empty.ToString() && x.enable).OrderBy(x => x.line).FirstOrDefault();
                //var area = GetArea(mesinfo.quantity, e);
                TargetLocation = stationinfoRepository.Find(x => x.column == (area1 == "2" ? 1 : 10) && x.area == area1 && x.location_state == LocationStateEnum.Empty.ToString() && x.enable).OrderBy(x => x.line).OrderBy(x => x.column).FirstOrDefault();
                if (TargetLocation == null)
                    TargetLocation = stationinfoRepository.Find(x => x.column == 1 && x.area == "4" && x.location_state == LocationStateEnum.Empty.ToString() && x.enable).OrderBy(x => x.line).FirstOrDefault();
                {
                    if (area1 == "2")
                        area1 = "3";
                    else if (area1 == "3")
                        area1 = "2";
                    TargetLocation = stationinfoRepository.Find(x => x.column == (area1 == "2" ? 1 : 10) && x.area == area1 && x.location_state == LocationStateEnum.Empty.ToString() && x.enable).OrderBy(x => x.line).OrderBy(x => x.column).FirstOrDefault();
                    if (TargetLocation == null)
                    {
                        //TargetLocation = stationinfoRepository.Find(x => x.stationType == mesinfo.drawingNo && x.area == "1").OrderByDescending(x => x.column).FirstOrDefault();
                        //if (TargetLocation != null)
                        //    TargetLocation = stationinfoRepository.Find(x => x.column == TargetLocation.column && x.location_state == LocationStateEnum.Empty.ToString() && x.enable && x.area == "1").OrderByDescending(x => x.line).FirstOrDefault();
                        //if (TargetLocation == null)
                        //    TargetLocation = stationinfoRepository.Find(x => x.location_state == LocationStateEnum.Empty.ToString() && x.enable && x.area == "1").OrderBy(x => x.column).OrderByDescending(x => x.line).FirstOrDefault();
                        if (TargetLocation == null)
                            TargetLocation = stationinfoRepository.Find(x => x.line == 2 && x.location_state == LocationStateEnum.Empty.ToString() && x.enable && x.area == "1").OrderBy(x => x.column).FirstOrDefault();
                    }
                }
                //if (TargetLocation == null)
                //    TargetLocation = stationinfoRepository.Find(x => x.column == 1 && x.area == "4" && x.location_state == LocationStateEnum.Empty.ToString() && x.enable).OrderBy(x => x.line).FirstOrDefault();
            }
            #region
@@ -280,21 +341,41 @@
            }
            return TargetLocation;
        }
        private static dt_stationinfo GetEmptyLocation(Idt_stationinfoRepository stationinfoRepository)
        /// <summary>
        /// ä¸´æ—¶ä½¿ç”¨
        /// </summary>
        /// <param name="stationinfoRepository"></param>
        /// <returns></returns>
        public static dt_stationinfo GetEmptyLocation1(Idt_stationinfoRepository stationinfoRepository)
        {
            return stationinfoRepository.Find(x => x.line == 1 && x.location_state == LocationStateEnum.Empty.ToString() && x.enable && x.area == "3").OrderBy(x => x.column).FirstOrDefault();//行和区域需写死
        }
        /// <summary>
        /// åŽ»å¤–å
        /// </summary>
        /// <param name="stationinfoRepository"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public static dt_stationinfo GetEmptyLocation(Idt_stationinfoRepository stationinfoRepository)
        {
            dt_stationinfo TargetLocation = null;
            List<string> target = new List<string>() { "W01001001", "W01001002", "W01001003" };
            List<string> target = new List<string>() { "W01001003", "W01001002", "W01001001" };
            var Pipeline_client = PLCClient.Clients.FirstOrDefault(t => t.PLCName == "链条机");
            if (Pipeline_client == null) throw new Exception("链条机调度服务未开启!");
            if (!Pipeline_client.IsConnected) throw new Exception("与链条机连接超时!");
            foreach (var item in target)
            {
                TargetLocation = stationinfoRepository.FindFirst(x => x.stationCode == item && x.location_state == LocationStateEnum.Empty.ToString() && x.enable);
                if (TargetLocation != null) continue;
                TargetLocation = stationinfoRepository.Find(x => x.stationCode == item && x.location_state == LocationStateEnum.Empty.ToString() && x.enable).FirstOrDefault();
                var PalletSignal = Pipeline_client.ReadByOrder<Int16>("R_PalletSignal", item);//读取托盘信号:1:有,2无
                var MaterialSignal = Pipeline_client.ReadByOrder<Int16>("R_MaterialSignal", item);//读取货物信号:1:有,2无
                if (TargetLocation != null && PalletSignal == 2 && MaterialSignal == 2) return TargetLocation;
            }
            if (TargetLocation == null)
            {
                throw new Exception("外协入库口已满");
            }
            //if (TargetLocation == null)
            //{
            //    throw new Exception("外协入库口已满");
            //}
            return TargetLocation;
        }
        /// <summary>
@@ -308,7 +389,7 @@
            {
                if (quantity <= 180)
                    return "2";
                else if (quantity <= 225)
                else /*if (quantity <= 225)*/
                    return "3";
            }
            else
@@ -322,7 +403,7 @@
            //    return "3";
            //else if (quantity > 50 && e > 1100)
            //    return "4";
            return null;
            //return null;
        }
    }
}