分支自 SuZhouGuanHong/TaiYuanTaiZhong

dengjunjie
2024-03-14 73a926018601d9a5a5a3d3f4c051537f45a8eff4
´úÂë¹ÜÀí/PCS/WCS_Server/WIDESEA_WCS/JobsPart/Common/StationTask.cs
@@ -9,57 +9,116 @@
using WIDESEA_Core.FreeDB;
using WIDESEA_Entity.DomainModels;
using WIDESEA_Comm;
using static System.Collections.Specialized.BitVector32;
using WIDESEA_Comm.TaskNo;
namespace WIDESEA_WCS.JobsPart.Common
{
    public class StationTask
    {
        FreeDB freeDB = new FreeDB();
        public void CreateTask()
        static FreeDB freeDB = new FreeDB();
        /// <summary>
        /// åˆ›å»ºå…¥åº“任务
        /// </summary>
        public static void CreateTask()
        {
            VOLContext context = new VOLContext();
            Idt_stationinfoRepository stationinfoRepository = new dt_stationinfoRepository(context);
            Idt_WorkinfoRepository workinfoRepository = new dt_WorkinfoRepository(context);
            List<dt_stationinfo> stationinfos = stationinfoRepository.Find(x => x.stationCode.Contains('X'));
            foreach (var item in stationinfos)
            try
            {
                //根据下料口的绑定信息查询对应工单,检测此工单是否已完成  å®Œæˆå³å¯å°†å…¶é€å…¥åº“
                dt_Workinfo workinfo = workinfoRepository.FindFirst(x => x.WorkNumber == item.Number);
                int CompeletedNum = Convert.ToInt32(workinfo.PlannedQuantity) - Convert.ToInt32(workinfo.QuantityCompletion);
                VOLContext context = new VOLContext();
                Idt_stationinfoRepository stationinfoRepository = new dt_stationinfoRepository(context);
                Idt_WorkinfoRepository workinfoRepository = new dt_WorkinfoRepository(context);
                //判定任务是否已创建//如已存在
                if (freeDB.Select<dt_agvtask>().Where(x => x.agv_fromaddress == item.stationCode).Count() > 0)
                    continue;
                List<dt_stationinfo> stationinfos = freeDB.Select<dt_stationinfo>().Where(x => x.stationCode.Contains("X") && x.quantity > 0 && !string.IsNullOrEmpty(x.stationType)).ToList();
                if (item.quantity == 5 || CompeletedNum == 0)  //循环读取车轮数为5或者订单已完成数量的下料口    â†’工单人工关闭的工单
                foreach (var item in stationinfos)
                {
                    dt_stationinfo TargetLocation = GetEmptyLocation(stationinfoRepository, workinfo, item);
                    //根据下料口的绑定信息查询对应工单,检测此工单是否已完成  å®Œæˆå³å¯å°†å…¶é€å…¥åº“
                    if (TargetLocation != null)
                    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)
                        continue;
                    if (item.quantity == 5 || CompeletedNum == 0)  //循环读取车轮数为5或者订单已完成数量的下料口    â†’工单人工关闭的工单
                    {
                        //todo:  è°ƒç”¨WMS接口创建任务
                        dt_agvtask agvtask = new dt_agvtask()
                        if (mesinfo.quantity <= 50 || item.stationCode.Contains("3"))    //小于50件直接出库
                        {
                            agv_fromaddress = item.stationCode,
                            agv_id = Guid.NewGuid(),
                            agv_grade = 0,
                            agv_barcode = "",
                            agv_createtime = DateTime.Now,
                            agv_taskstate = TaskStatus.Created.ToString(),
                            agv_toaddress = TargetLocation.stationCode,
                            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_Traytype = item.tray_type,
                                    agv_TrayStatus = item.tray_status
                                };
                        };
                                freeDB.Add(agvtask);
                                TargetLocation.location_state = LocationStateEnum.Busy.ToString();
                                freeDB.Update(TargetLocation);
                            }
                        }
                        else
                        {
                            dt_stationinfo TargetLocation = GetEmptyLocation(stationinfoRepository, mesinfo, item, CompeletedNum, dt_Geometry_Data.e);
                        freeDB.Add(agvtask);
                        TargetLocation.location_state = LocationStateEnum.Busy.ToString();
                        freeDB.Update(TargetLocation);
                            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_Traytype = item.tray_type,
                                    agv_TrayStatus = item.tray_status
                                };
                                freeDB.Add(agvtask);
                                TargetLocation.location_state = LocationStateEnum.Busy.ToString();
                                freeDB.Update(TargetLocation);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
        /// <summary>
@@ -69,68 +128,66 @@
        /// <param name="workinfo">订单</param>
        /// <param name="stationinfo">下料口信息</param>
        /// <returns></returns>
        private dt_stationinfo GetEmptyLocation(Idt_stationinfoRepository stationinfoRepository, dt_Workinfo workinfo, dt_stationinfo stationinfo)
        private static dt_stationinfo GetEmptyLocation(Idt_stationinfoRepository stationinfoRepository, dt_mes_head mesinfo, dt_stationinfo stationinfo, int CompeletedNum, float e)
        {
            //放货位
            dt_stationinfo TargetLocation = null;
            //根据订单数量来寻找对应库区且同物料类型库位
            #region æ ¹æ®è®¢å•数量来寻找对应库区且同物料类型库位
            //if (workinfo.PlannedQuantity < 50)    //库区1 ç‰©æ–™ç±»åž‹å¤š
            //{
            //    dt_stationinfo station = stationinfoRepository.Find(x => x.stationType == stationinfo.stationType && x.area == "1").OrderByDescending(x => x.lastUpdateTime).FirstOrDefault();
            //    if (station != null)
            //    {
            //        //如存在同物料类型且在1行的库存则新入库的物料放至同列 ä¸å­˜åœ¨åˆ™å¯»æ‰¾æ–°è¡Œåº“位
            //        if (station.line == 1)
            //        {
            //            TargetLocation = stationinfoRepository.Find(x => x.area == station.area && x.location_state == LocationStateEnum.Empty.ToString() && x.column == station.column && x.enable).OrderBy(x => x.line).ThenBy(x => x.column).FirstOrDefault();
            //        }
            //        TargetLocation = stationinfoRepository.Find(x => x.area == station.area && x.location_state == LocationStateEnum.Empty.ToString() && x.line == 1 && x.enable).OrderBy(x => x.line).ThenBy(x => x.column).FirstOrDefault();
            //    }
            //    else
            //    {
            //        TargetLocation = stationinfoRepository.Find(x => x.area == "1" && x.location_state == LocationStateEnum.Empty.ToString() && x.line == 1 && x.enable).OrderBy(x => x.line).ThenBy(x => x.column).FirstOrDefault();
            //    }
            //}
            //else
            //{
            //int quantity = Convert.ToInt32(workinfo.PlannedQuantity);
            //string area = GetArea(quantity);
            if (workinfo.PlannedQuantity < 50)    //库区1 ç‰©æ–™ç±»åž‹å¤š
            {
                dt_stationinfo station = stationinfoRepository.Find(x => x.stationType == stationinfo.stationType && x.area == "1").OrderByDescending(x => x.lastUpdateTime).FirstOrDefault();
                if (station != null)
                {
                    //如存在同物料类型且在1行的库存则新入库的物料放至同列 ä¸å­˜åœ¨åˆ™å¯»æ‰¾æ–°è¡Œåº“位
                    if (station.line == 1)
                    {
                        TargetLocation = stationinfoRepository.Find(x => x.area == station.area && x.location_state == LocationStateEnum.Empty.ToString() && x.column == station.column && x.enable).OrderBy(x => x.line).ThenBy(x => x.column).FirstOrDefault();
                    }
                    TargetLocation = stationinfoRepository.Find(x => x.area == station.area && x.location_state == LocationStateEnum.Empty.ToString() && x.line == 1 && x.enable).OrderBy(x => x.line).ThenBy(x => x.column).FirstOrDefault();
                }
                else
                {
                    TargetLocation = stationinfoRepository.Find(x => x.area == "1" && x.location_state == LocationStateEnum.Empty.ToString() && x.line == 1 && x.enable).OrderBy(x => x.line).ThenBy(x => x.column).FirstOrDefault();
                }
            }
            else
            {
                //int quantity = Convert.ToInt32(workinfo.PlannedQuantity);
                //string area = GetArea(quantity);
            //dt_materielinfo materielinfo = freeDB.Select<dt_materielinfo>().Where(x => x.materiel_id == stationinfo.stationType).First();
                dt_materielinfo materielinfo = freeDB.Select<dt_materielinfo>().Where(x => x.materiel_id == stationinfo.stationType).First();
            //if (materielinfo == null)
            //    throw new Exception($"无此物料类型{stationinfo.stationType}库区");
                if (materielinfo == null)
                    throw new Exception($"无此物料类型{stationinfo.stationType}库区");
            //string area = materielinfo.materiel_areaid.ToString();
                string area = materielinfo.materiel_areaid.ToString();
            //如存在同类型物料则放置同行库位 ä¸å­˜åœ¨åˆ™æ”¾ç½®æ–°è¡Œçš„第一列中
            //dt_stationinfo station = stationinfoRepository.Find(x => x.stationType == stationinfo.stationType && x.area == area).OrderBy(x => x.lastUpdateTime).FirstOrDefault();
            //if (station != null)
            //{
            //    TargetLocation = stationinfoRepository.Find(x => x.area == station.area && x.location_state == LocationStateEnum.Empty.ToString() && x.enable && x.line == station.line).OrderBy(x => x.column).FirstOrDefault();
            //    if (TargetLocation == null)
            //    {
            //        TargetLocation = stationinfoRepository.Find(x => x.area == area && x.location_state == LocationStateEnum.Empty.ToString() && x.enable && x.column == '1').OrderBy(x => x.line).FirstOrDefault();
            //    }
            //}
            //else
            //{
            //    TargetLocation = stationinfoRepository.Find(x => x.area == area && x.location_state == LocationStateEnum.Empty.ToString() && x.enable && x.column == '1').OrderBy(x => x.line).FirstOrDefault();
            //}
                //如存在同类型物料则放置同行库位 ä¸å­˜åœ¨åˆ™æ”¾ç½®æ–°è¡Œçš„第一列中
                dt_stationinfo station = stationinfoRepository.Find(x => x.stationType == stationinfo.stationType && x.area == area).OrderBy(x => x.lastUpdateTime).FirstOrDefault();
                if (station != null)
                {
                    TargetLocation = stationinfoRepository.Find(x => x.area == station.area && x.location_state == LocationStateEnum.Empty.ToString() && x.enable && x.line == station.line).OrderBy(x => x.column).FirstOrDefault();
                    if (TargetLocation == null)
                    {
                        TargetLocation = stationinfoRepository.Find(x => x.area == area && x.location_state == LocationStateEnum.Empty.ToString() && x.enable && x.column == '1').OrderBy(x => x.line).FirstOrDefault();
                    }
                }
                else
                {
                    TargetLocation = stationinfoRepository.Find(x => x.area == area && x.location_state == LocationStateEnum.Empty.ToString() && x.enable && x.column == '1').OrderBy(x => x.line).FirstOrDefault();
                }
                //todo  å¦‚物料已满则放置其他库区
                //if (TargetLocation == null)
                //{
                //    stationinfoRepository.Find(x => x.area == area && x.location_state == LocationStateEnum.Empty.ToString() && x.enable && x.column == '1').OrderBy(x => x.line).FirstOrDefault();
                //}
            }
            if (TargetLocation == null)
            {
                throw new Exception("库位已满");
            }
            //todo  å¦‚物料已满则放置其他库区
            //if (TargetLocation == null)
            //{
            //    stationinfoRepository.Find(x => x.area == area && x.location_state == LocationStateEnum.Empty.ToString() && x.enable && x.column == '1').OrderBy(x => x.line).FirstOrDefault();
            //}
            //}
            //if (TargetLocation == null)
            //{
            //    throw new Exception("库位已满");
            //}
            //else if (workinfo.PlannedQuantity >= 50 && workinfo.PlannedQuantity < 110)  //库区2 
            //{
@@ -148,23 +205,125 @@
            //else if(workinfo.PlannedQuantity >= 110 && workinfo.PlannedQuantity < 180)
            //{
            //}
            #endregion
            var detail = freeDB.Select<dt_mes_detail>().Where(x => x.jobID == mesinfo.jobID).First();
            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();
            }
            else
            {
                inventory = freeDB.Select<dt_inventory>().Where(x => x.FigureNumber == mesinfo.drawingNo && x.jobID == mesinfo.jobID).First();
            }
            if (inventory != null)
            {
                var station = stationinfoRepository.FindFirst(x => x.stationCode == inventory.stationCode);
                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();
                }
                else
                {
                    TargetLocation = stationinfoRepository.Find(x => x.line == station.line && x.location_state == LocationStateEnum.Empty.ToString() && x.enable && x.area == inventory.area).OrderBy(x => x.column).FirstOrDefault();
                }
                if (TargetLocation != null) return TargetLocation;
            }
            if (CompeletedNum <= 15)
            {
                TargetLocation = stationinfoRepository.Find(x => x.line == 1 && x.location_state == LocationStateEnum.Empty.ToString() && x.enable && x.area == "1").OrderBy(x => x.column).FirstOrDefault();
            }
            else
            {
                //if (50 < mesinfo.quantity && mesinfo.quantity <= 180 && e < 1100)
                //    TargetLocation = stationinfoRepository.Find(x => x.column == 1 && x.area == "2" && x.location_state == LocationStateEnum.Empty.ToString() && x.enable).OrderBy(x => x.line).FirstOrDefault();
                //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();
                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
            //根据订单炉号/工单号/类型
            //if (mesinfo.quantity >= 180)
            //{
            //    //查询库存记录中是否存在同类型/工单/炉号的记录  å¦‚有则放置同一行,无则寻找新库位
            //    var detail = freeDB.Select<dt_mes_detail>().Where(x => x.jobID == mesinfo.jobID).First();
            //    var inventory = freeDB.Select<dt_inventory>().Where(x => x.HeatNumber == detail.heatID && x.FigureNumber == mesinfo.drawingNo && x.jobID == mesinfo.jobID).First();
            //    if (inventory != null)
            //    {
            //        //int line = Convert.ToInt16(inventory.stationCode.Split("-")[0]);
            //        var line = stationinfoRepository.FindFirst(x => x.stationCode == inventory.stationCode).line;
            //        TargetLocation = stationinfoRepository.Find(x => x.line == line && x.location_state == LocationStateEnum.Empty.ToString() && x.enable && x.area == inventory.area).OrderBy(x => x.column).FirstOrDefault();
            //        if (TargetLocation == null && CompeletedNum <= 15)
            //        {
            //            TargetLocation = stationinfoRepository.Find(x => x.area == "1" && x.location_state == LocationStateEnum.Empty.ToString() && x.enable).OrderBy(x => x.line).ThenBy(x => x.column).FirstOrDefault();
            //        }
            //    }
            //    else
            //    {
            //        TargetLocation = stationinfoRepository.Find(x => x.area == "2" && x.location_state == LocationStateEnum.Empty.ToString() && x.enable).OrderBy(x => x.line).ThenBy(x => x.column).FirstOrDefault();
            //    }
            //}
            #endregion
            if (TargetLocation == null)
            {
                throw new Exception("库位已满");
            }
            return TargetLocation;
        }
        private static dt_stationinfo GetEmptyLocation(Idt_stationinfoRepository stationinfoRepository)
        {
            dt_stationinfo TargetLocation = null;
            List<string> target = new List<string>() { "W01001001", "W01001002", "W01001003" };
            foreach (var item in target)
            {
                TargetLocation = stationinfoRepository.FindFirst(x => x.stationCode == item && x.location_state == LocationStateEnum.Empty.ToString() && x.enable);
                if (TargetLocation != null) continue;
            }
            if (TargetLocation == null)
            {
                throw new Exception("外协入库口已满");
            }
            return TargetLocation;
        }
        /// <summary>
        /// æ ¹æ®è®¢å•数量返回库区(暂时弃用)   //更换为库位类型匹配
        /// </summary>
        /// <param name="quantity"></param>
        /// <returns></returns>
        private string GetArea(int quantity)
        private static string GetArea(int quantity, float e)
        {
            if (quantity >= 50 && quantity < 180)
                return "2";
            else if (quantity >= 180 && quantity < 180)
                return "3";
            else if (quantity >= 180 && quantity < 220)
            if (e < 1100)
            {
                if (quantity <= 180)
                    return "2";
                else if (quantity <= 225)
                    return "3";
            }
            else
            {
                return "4";
            }
            //if (quantity > 50 && quantity <= 180 && e < 1100)
            //    return "2";
            //else if (quantity > 180 && quantity <= 225 && e < 1100)
            //    return "3";
            //else if (quantity > 50 && e > 1100)
            //    return "4";
            return null;
        }
    }