WinTaskManager.xaml.cs 8.34 KB
using XingYe_ACS.BaseStruct;
using XingYe_ACS.Business;
using XingYe_ACS.Common;
using XingYe_ACS.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace XingYe_ACS.UI
{
    /// <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();
        }

        /// <summary>
        /// 任务清除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MItem_TaskClear_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (MessageBox.Show("请确认是否删除任务?\n删除任务后,需记录信息,到WMS处理订单!", "警告", MessageBoxButton.YesNo) == MessageBoxResult.No) return;
                if (DGrid_Tasks.SelectedItem == null) return;
                FTask task = DGrid_Tasks.SelectedItem as FTask;
                if (task == null) return;
                Agv agv = App.AgvList.FirstOrDefault(a => a.strAgvNo == task.strTaskAgv);

                string sqlDelete = string.Format("DELETE FROM  T_SonTask WHERE strTaskNo = '{0}';", task.strTaskNo);
                foreach (SonTask sonTask in task.sonTaskList)
                {
                    sqlDelete += string.Format("DELETE FROM  T_Base_PathPoint WHERE intSonTaskNo = '{0}';", sonTask.intSonTaskNo);
                }
                bool sqlResult = DbHelperSQL.SqlTransOperator(sqlDelete);
                if (!sqlResult)
                {
                    MessageBox.Show("子任务数据库清除失败");
                    return;
                }
                task.strEndTime = DateTime.Now.ToString();
                task.taskState = EnumMsg.ErrMsg.手动删除任务.ToString();
                bool isUpdateFTaskResult = UpdateManage.UpdateFTask(task);
                if (task.strTaskAgv != "") UpdateManage.InsertAgvMilage(task);
                if (!isUpdateFTaskResult)
                {
                    MessageBox.Show("主任务数据库清除失败");
                    return;
                }
                CServiceImpl.ToWebTaskRefresh(task, 99);
                App.AppDispatcher.Invoke(new Action(() =>
                {
                    App.TaskList.Remove(task);
                }));
                if (agv != null && agv.agvTask == task) agv.agvTask = null;
                if (agv != null && agv.strChooseStation != "")
                {
                    UpdateManage.UpdateAgvMsg(agv);
                    Station station = App.StationList.Find(a => a.strStationNo == agv.strChooseStation);
                    station.isLocked = false;
                    UpdateManage.UpdateStation(station);
                    agv.strChooseStation = "";
                }
            }
            catch (Exception Ex)
            {
                MessageBox.Show("任务清除:" + Ex.Message.Replace(Environment.NewLine, ""));
            }
        }

        private void MItem_ErrHandle_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (DGrid_Tasks.SelectedItem == null) return;
                FTask task = DGrid_Tasks.SelectedItem as FTask;
                if (task == null || !task.isTaskSubmit)
                {
                    MessageBox.Show("任务尚未提交/任务为空,无法进行此操作!");
                    return;
                }
                if (MessageBox.Show("确定执行此操作!", "提示", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                {
                    bool isChangeAgv = false;
                    if (MessageBox.Show("是否需要更改Agv执行此任务", "提示", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                    {
                        isChangeAgv = true;
                    }
                    //清除当前主任务下的所有子任务
                    string sqlDelete = string.Format("DELETE FROM T_SonTask WHERE strTaskNo = '{0}';", task.strTaskNo);
                    foreach (SonTask sonTask in task.sonTaskList)
                    {
                        sqlDelete += string.Format("DELETE FROM T_Base_PathPoint WHERE intSonTaskNo = '{0}';", sonTask.intSonTaskNo);
                    }
                    bool sqlResult = DbHelperSQL.SqlTransOperator(sqlDelete);
                    if (!sqlResult)
                    {
                        MessageBox.Show("子任务数据库清除失败");
                        return;
                    }

                    App.AppDispatcher.Invoke(new Action(() =>
                    {
                        task.sonTaskList.Clear();
                    }));

                    //更新主任务信息
                    task.isTaskSubmit = false;
                    task.strBeginTime = "";
                    if (isChangeAgv)
                    {
                        Agv agv = App.AgvList.First(a => a.strAgvNo == task.strTaskAgv);
                        agv.agvTask = null;
                    }
                    task.strTaskAgv = isChangeAgv ? "" : task.strTaskAgv;

                    bool isUpdateFTaskResult = UpdateManage.UpdateFTask(task);
                    if (isUpdateFTaskResult)
                    {
                        MessageBox.Show("异常处理成功!");
                    }
                }
            }
            catch (Exception Ex)
            {
                MessageBox.Show("异常处理:" + Ex.Message.Replace(Environment.NewLine, ""));
            }
        }

        private void MItem_TaskUpdate_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //if (DGrid_Tasks.SelectedItem == null) return;
                //FTask task = DGrid_Tasks.SelectedItem as FTask;
                //if (task == null || task.isTaskSubmit)
                //{
                //    MessageBox.Show("任务已提交/任务为空,无法进行此操作!");
                //    return;
                //}
                //if (task.taskType != EnumMsg.TaskType.配送出库)
                //{
                //    MessageBox.Show("只能更新出库任务站台!");
                //    return;
                //}
                //int index = new Random().Next(0,App.StationList.FindAll(a => a.isEnable && a.stationType == EnumMsg.StationType.出库站台).Count);
                //Station station = App.StationList.FindAll(a => a.isEnable && a.stationType == EnumMsg.StationType.出库站台)[index];
                //task.strStationNo = station.strStationNo;
                //task.endPoint = App.PointList.Find(a => a.strBarcode == station.strBarcode);
                //bool isUpdateResult = UpdateManage.UpdateFTask(task);
                //if (!isUpdateResult)
                //{
                //    MessageBox.Show("更新任务站台失败");
                //}
            }
            catch (Exception Ex)
            {
                MessageBox.Show("更新任务站台出现异常:" + Ex.Message.Replace(Environment.NewLine, ""));
            }
        }
    }
}