WinTaskManager.xaml.cs 16.7 KB
using RCS.Dal;
using RCS.Model.Comm;
using RCS.Model.Entity;
using RCS.WinClient.Common;
using RCS.WinClient.Service;
using RCS.WinClient.WebApi.Client;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using static RCS.Model.Comm.EnumMsg;


namespace RCS.WinClient.Views.Pages
{
    /// <summary>
    /// WinTaskManager.xaml 的交互逻辑
    /// </summary>
    public partial class WinTaskManager : Window
    {
        public WinTaskManager()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 任务界面加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            DGrid_Tasks.ItemsSource = App.TaskList;
        }

        /// <summary>
        /// 关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        //重写Closing方法
        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            this.Hide();
            e.Cancel = true;
        }

        /// <summary>
        /// 拖动事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }


        private void DataGrid_MouseDown(object sender, MouseButtonEventArgs e)
        {
            (sender as DataGrid).SelectedItem = null;
        }

        /// <summary>
        /// 调整优先级到最大
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MItem_UpdateLevelMax_click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (DGrid_Tasks.SelectedItem == null) return;
                var task = DGrid_Tasks.SelectedItem as Base_Task;
                if (task == null)
                {
                    ComnMethod.Message($"调整优先级到最大失败,选中数据转化为Base_Task类型失败!");
                    return;
                }
                var oldTaskLevel = task.TaskLevel;
                var newTaskLevel = App.TaskList.Max(t => t.TaskLevel) + 1;
                //调整优先级的时候,相同起点站台的优先级,必须一样,不然会取错货
                App.TaskList.Where(x => x.Initial == task.Initial).ToList().ForEach(x => x.TaskLevel = newTaskLevel);
                BaseDal<Base_Task> _taskDb = new BaseDal<Base_Task>();
                var updateresult = _taskDb.Update(x => x.Initial == task.Initial, x => new Base_Task()
                {
                    TaskLevel = newTaskLevel,
                });
                if (!updateresult.IsSuccess)
                {
                    App.TaskList.Where(x => x.Initial == task.Initial).ToList().ForEach(x => x.TaskLevel = oldTaskLevel);
                    ComnMethod.Message($"调整优先级到最大失败,更新数据库失败,原因:{updateresult.Message}!");
                    return;
                }
            }
            catch (Exception ex)
            {
                ComnMethod.Message($"调整优先级到最大出现异常,原因:{ex.Message},堆栈:{ex.StackTrace}");
            }
        }

        /// <summary>
        /// 调整优先级到最大
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MItem_UpdateLevelMax_click_Old(object sender, RoutedEventArgs e)
        {
            try
            {
                if (DGrid_Tasks.SelectedItem == null) return;
                var task = DGrid_Tasks.SelectedItem as Base_Task;
                if (task == null)
                {
                    ComnMethod.Message($"调整优先级到最大失败,选中数据转化为Base_Task类型失败!");
                    return;
                }

                BaseDal<Base_Task> _taskDb = new BaseDal<Base_Task>();
                BaseDal<Base_Task> _taskDb1 = new BaseDal<Base_Task>();

                ///由于随意修改优先级,会造成中转任务的混乱,拉错物料
                ///新增优先级修改的判断
                //首先判断当前任务是否是拆分任务
                if (task.Initial == "缓存台502-1" || task.Target == "缓存台502-1")
                {
                    //如果是拆分任务就获取它对应的另一条任务
                    var taskList = App.TaskList.Where(t => (t.Initial == "缓存台502-1" || t.Target == "缓存台502-1") && t.TaskPallet == task.TaskPallet).ToList(); //t.TaskPallet == task.TaskPallet && );
                    if (taskList.Exists(t => t.IsSubmit))
                    {
                        ComnMethod.Message($"调整优先级到最大失败,有一条中转任务并没有完成,请稍后再试!");
                        return;
                    }
                    //获取所有的拆分任务是否可以被2整除
                    //else if (taskList.GroupBy(t => t.TaskPallet).Any(t => t.Count() != 2))
                    //{
                    //    ComnMethod.Message($"调整优先级到最大失败,有一条中转任务并没有完成,请稍后再试!");
                    //    return;
                    //}
                    else
                    {
                        var maxTaskLevel = App.TaskList.Max(t => t.TaskLevel);
                        task.TaskLevel = maxTaskLevel + 1;

                        var exTask = App.TaskList.FirstOrDefault(t => t.TaskPallet == task.TaskPallet);

                        var updateresult = _taskDb.Update(it => it.TaskNo == task.TaskNo, it => new Base_Task()
                        {
                            TaskLevel = task.TaskLevel,
                        });
                        if (!updateresult.IsSuccess)
                        {
                            ComnMethod.Message($"调整优先级到最大失败,更新数据库失败,原因:{updateresult.Message}!");
                            return;
                        }
                        var updateresult1 = _taskDb1.Update(it => it.TaskNo == exTask.TaskNo, it => new Base_Task()
                        {
                            TaskLevel = task.TaskLevel,
                        });
                        if (!updateresult1.IsSuccess)
                        {
                            ComnMethod.Message($"调整优先级到最大失败,更新数据库失败,原因:{updateresult1.Message}!");
                            return;
                        }
                    }
                }
                else if (task.Initial == "缓存台502-2" || task.Target == "缓存台502-2")
                {
                    //如果是拆分任务就获取它对应的另一条任务
                    var taskList = App.TaskList.Where(t => (t.Initial == "缓存台502-2" || t.Target == "缓存台502-2") && t.TaskPallet == task.TaskPallet).ToList(); //t.TaskPallet == task.TaskPallet && );
                    if (taskList.Exists(t => t.IsSubmit))
                    {
                        ComnMethod.Message($"调整优先级到最大失败,有一条中转任务并没有完成,请稍后再试!");
                        return;
                    }
                    //获取所有的拆分任务是否可以被2整除
                    //else if (taskList.GroupBy(t => t.TaskPallet).Any(t => t.Count() != 2))
                    //{
                    //    ComnMethod.Message($"调整优先级到最大失败,有一条中转任务并没有完成,请稍后再试!");
                    //    return;
                    //}
                    else
                    {
                        var maxTaskLevel = App.TaskList.Max(t => t.TaskLevel);
                        task.TaskLevel = maxTaskLevel + 1;

                        var exTask = App.TaskList.FirstOrDefault(t => t.TaskPallet == task.TaskPallet);

                        var updateresult = _taskDb.Update(it => it.TaskNo == task.TaskNo, it => new Base_Task()
                        {
                            TaskLevel = task.TaskLevel,
                        });
                        if (!updateresult.IsSuccess)
                        {
                            ComnMethod.Message($"调整优先级到最大失败,更新数据库失败,原因:{updateresult.Message}!");
                            return;
                        }
                        var updateresult1 = _taskDb1.Update(it => it.TaskNo == exTask.TaskNo, it => new Base_Task()
                        {
                            TaskLevel = task.TaskLevel,
                        });
                        if (!updateresult1.IsSuccess)
                        {
                            ComnMethod.Message($"调整优先级到最大失败,更新数据库失败,原因:{updateresult1.Message}!");
                            return;
                        }
                    }
                }
                else
                {
                    var maxTaskLevel = App.TaskList.Max(t => t.TaskLevel);
                    task.TaskLevel = maxTaskLevel + 1;

                    var updateresultlast = _taskDb.Update(it => it.TaskNo == task.TaskNo, it => new Base_Task()
                    {
                        TaskLevel = task.TaskLevel,
                    });
                    if (!updateresultlast.IsSuccess)
                    {
                        ComnMethod.Message($"调整优先级到最大失败,更新数据库失败,原因:{updateresultlast.Message}!");
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                ComnMethod.Message($"调整优先级到最大出现异常,原因:{ex.Message},堆栈:{ex.StackTrace}");
            }
        }

        /// <summary>
        /// 手动完成第一条子任务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void MItem_TaskFinishSon_click(object sender, RoutedEventArgs e)
        {
            try
            {
                var msg = "请确认是否完成第一条子任务?\n 完成第一个子任务后,可能导致任务出错!";
                if (MessageBox.Show(msg, "警告", MessageBoxButton.YesNo) == MessageBoxResult.No) return;
                if (DGrid_Tasks.SelectedItem == null) return;
                var task = DGrid_Tasks.SelectedItem as Base_Task;
                if (task == null)
                {
                    ComnMethod.Message($"完成第一条子任务失败,选中数据转化为Base_Task类型失败");
                    return;
                }
                Base_Agv? agv = App.AgvList.FirstOrDefault(a => a.AgvNo == task.TaskAgvNo);
                if (agv == null)
                {
                    ComnMethod.Message($"完成第一条子任务【{task.TaskNo}】失败,找不到任务对应的AGV【{task.TaskAgvNo}】");
                    return;
                }
                if (agv.AgvTask == null)
                {
                    ComnMethod.Message($"编号【{agv.AgvNo}】的AGV主任务不存在,不能完成第一条子任务");
                    return;
                }
                if (agv.AgvTask.SubTaskList == null || agv.AgvTask.SubTaskList.Count == 0)
                {
                    ComnMethod.Message($"编号【{agv.AgvNo}】的AGV主任务【{agv.AgvTask.TaskNo}】不存在子任务,不能完成第一条子任务");
                    return;
                }

                if (agv.Barcode != agv.AgvTask.SubTaskList[0].EndBarcode)
                {
                    ComnMethod.Message($"编号【{agv.AgvNo}】的AGV当前点{agv.Barcode}不在子任务终点【{agv.AgvTask.SubTaskList[0].EndBarcode}】,不能完成第一条子任务");
                    return;
                }
                DGrid_Tasks.ContextMenu.Visibility = Visibility.Hidden;

                await Task.Run(() =>
                {
                    agv.AgvTask.SubTaskList[0].SubTaskState = SubTaskState.手动完成;
                    var strResult = TaskManage.SonTaskFinish(agv);
                    ComnMethod.Message(strResult.Message);
                });
            }
            catch (Exception ex)
            {
                ComnMethod.Message($"完成第一条子任务出现异常,原因:{ex.Message},堆栈:{ex.StackTrace}");
            }
            DGrid_Tasks.ContextMenu.Visibility = Visibility.Visible;
        }

        /// <summary>
        /// 强制完成主任务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void MItem_TaskComplete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (DGrid_Tasks.SelectedItem == null) return;
                var task = DGrid_Tasks.SelectedItem as Base_Task;
                if (task == null) return;

                var msg = $"请确认是否强制完成【{task.TaskType}】的【{task.TaskNo}】任务,并且反馈上游系统任务完成?";
                if (MessageBox.Show(msg, "警告", MessageBoxButton.YesNo) == MessageBoxResult.No) return;
                if (string.IsNullOrEmpty(task.TaskAgvNo))
                {
                    task.TaskAgvNo = "手动强制完成";
                }

                //告知WMS任务完成
                // var result = WebApiClient.ToAcsWebApiTaskState(task, 100);
                //if (!result.IsSuccess)
                //{
                //    msg = $"【{task.TaskType}】的【{task.TaskNo}】任务回传上游系统失败,是否确认继续强制完成?\n 如果确认强制完成,需要去上游系统手动完成任务!";
                //    if (MessageBox.Show(msg, ",", MessageBoxButton.YesNo) == MessageBoxResult.No) return;
                //}
                DGrid_Tasks.ContextMenu.Visibility = Visibility.Hidden;

                await Task.Run(() =>
                {
                    task.TaskState = EnumMsg.TaskState.手动完成;
                    var strResult = TaskManage.TaskDelete(task, 100);
                    if (!strResult.IsSuccess)
                    {
                        if (MessageBox.Show("任务完成失败,是否强制删除?", "警告", MessageBoxButton.YesNo) == MessageBoxResult.No)
                            return;
                        strResult = TaskManage.TaskDelete(task, -1);
                    }
                    ComnMethod.Message(strResult.Message);
                });
            }
            catch (Exception ex)
            {
                MessageBox.Show($"强制完成任务出现异常,原因:{ex.Message},堆栈:{ex.StackTrace} \r");
            }
            DGrid_Tasks.ContextMenu.Visibility = Visibility.Visible;
        }

        /// <summary>
        /// 任务清除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void MItem_TaskClear_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (DGrid_Tasks.SelectedItem == null) return;
                var task = DGrid_Tasks.SelectedItem as Base_Task;
                if (task == null) return;

                var msg = $"请确认是否删除AGV【{task.TaskAgvNo}】任务号【{task.TaskNo}】的【{task.TaskType}】任务";
                if (MessageBox.Show(msg, "警告", MessageBoxButton.YesNo) == MessageBoxResult.No) return;

                if (task.TaskGroup == EnumMsg.TaskGroup.WMS任务.ToString())
                {
                    msg = $"AGV【{task.TaskAgvNo}】任务号【{task.TaskNo}】的【{task.TaskType}】任务是【WMS任务】,\n 是否已经在WMS中删除?";
                    if (MessageBox.Show(msg, "是否在WMS中已经删除任务", MessageBoxButton.YesNo) == MessageBoxResult.No) return;
                }
                DGrid_Tasks.ContextMenu.Visibility = Visibility.Hidden;

                await Task.Run(() =>
                {
                    task.TaskState = EnumMsg.TaskState.手动删除;
                    var strResult = TaskManage.TaskDelete(task);
                    ComnMethod.Message(strResult.Message);
                });
            }
            catch (Exception ex)
            {
                ComnMethod.Message($"删除任务出现异常,原因:{ex.Message},堆栈:{ex.StackTrace}");
            }
            DGrid_Tasks.ContextMenu.Visibility = Visibility.Visible;
        }
    }
}