AgvTaskJobService.cs 3.42 KB
using Hh.Mes.Common.log;
using Hh.Mes.Pojo.System;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.EnumEntitys;
using Hh.Mes.Service.Logs;
using Hh.Mes.Service.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Hh.Mes.Service.QuartzJobService
{
    public class AgvTaskJobService : RepositorySqlSugar<bus_agv_task>
    {
        public void Execute(string cerPath, string cerPwd)
        {
            var init = (int)EnumAGVState.任务创建;
            var send = (int)EnumAGVState.任务已发送;
            var sendAgvTaskList = new List<bus_agv_task>();
            var agvTaskList = base.Context.Queryable<bus_agv_task>().Where(t => t.state == init).ToList();
            if (agvTaskList.Count > 0)
            {
                string errorMsg = string.Empty;
                var fitup = EnumLocationzoneCode.人工装料区;
                var locationList = base.Context.Queryable<base_location>().Where(x => x.zoneCode == (int)fitup).ToList();
                if (locationList.Count == 0)
                {
                    errorMsg = $"人工装料点(库位管理{fitup})不存在,请在【库位管理】基础数据中维护!";
                }
                //按照上料区域进行分组,A1、B1为一组,A2、B2为一组,满一组才能给AGV下发任务。
                foreach (var locationGroup in locationList.GroupBy(t => t.fitupArea))
                {
                    var fitupAreaLocation = locationGroup.Select(t => t.locationCode).ToList();
                    var agvTasTemp = agvTaskList.Where(t => t.agvTaskType == (int)EnumAgvTaskType.上料配送 && fitupAreaLocation.Contains(t.startPosition)).ToList();
                    if (agvTasTemp.Count == 2)
                    {
                        sendAgvTaskList.AddRange(agvTasTemp);
                    }
                    agvTasTemp = agvTaskList.Where(t => t.agvTaskType == (int)EnumAgvTaskType.料框回收 && fitupAreaLocation.Contains(t.endPosition)).ToList();
                    if (agvTasTemp.Count == 2)
                    {
                        var ids = agvTasTemp.Select(t => t.id).ToList();
                        sendAgvTaskList.AddRange(agvTasTemp);
                    }
                }
                if (sendAgvTaskList.Count > 0)
                {
                    //先拼接好SQL
                    var ids = sendAgvTaskList.Select(t => t.id).ToList();
                    base.Context.Updateable<bus_agv_task>()
                                .SetColumns(t => t.state == send)
                                .Where(t => ids.Contains(t.id))
                                .AddQueue();
                    //然后下发AGV任务
                    foreach (var item in sendAgvTaskList)
                    {
                        //补全给AGV下发任务部分
                    }
                    //最后更新数据库
                    if (ExecuteQueues(base.Context) <= 0)
                    {
                        errorMsg = SystemVariable.dataActionError;
                    }
                }

                if (string.IsNullOrEmpty(errorMsg))
                { 
                    QueueInterLog.GetInstance.EnqueueInterLog(null, null, EnumLog.中控全局错误.ToString(), errorMsg, 0, user: "AOP", sysTitle: EnumLog.中控全局错误.ToString());
                    Log4NetHelper.Instance.Error(errorMsg);
                }
            }
        }
    }
}