216 lines
8.0 KiB
C#
216 lines
8.0 KiB
C#
using Cowain.Bake.BLL;
|
||
using Cowain.Bake.Common;
|
||
using Cowain.Bake.Common.Core;
|
||
using Cowain.Bake.Common.Enums;
|
||
using Cowain.Bake.Common.Interface;
|
||
using Cowain.Bake.Model;
|
||
using HandyControl.Controls;
|
||
using Prism.Commands;
|
||
using Prism.Regions;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Collections.ObjectModel;
|
||
using Unity;
|
||
|
||
namespace Cowain.Bake.UI.ProductManagement.ViewModels
|
||
{
|
||
public class TaskViewModel: ViewModelBase, INavigationAware
|
||
{
|
||
//
|
||
private DateTime _startTime = DateTime.Now.AddHours(-4);
|
||
public new DateTime StartTime
|
||
{
|
||
get { return _startTime; }
|
||
set { SetProperty(ref _startTime, value); }
|
||
}
|
||
|
||
private DateTime _endTime = DateTime.Now.AddHours(10);
|
||
public new DateTime EndTime
|
||
{
|
||
get { return _endTime; }
|
||
set { SetProperty(ref _endTime, value); }
|
||
}
|
||
|
||
private ObservableCollection<TTaskRecord> _taskList;
|
||
public ObservableCollection<TTaskRecord> TaskList
|
||
{
|
||
get => _taskList ?? (_taskList = new ObservableCollection<TTaskRecord>());
|
||
set { SetProperty(ref _taskList, value); }
|
||
}
|
||
|
||
private ObservableCollection<TTaskStep> _taskStepList;
|
||
public ObservableCollection<TTaskStep> TaskStepList
|
||
{
|
||
get => _taskStepList ?? (_taskStepList = new ObservableCollection<TTaskStep>());
|
||
set { SetProperty(ref _taskStepList, value); }
|
||
}
|
||
private TTaskStep _selectTaskStep;
|
||
public TTaskStep SelectTaskStep
|
||
{
|
||
get => _selectTaskStep ?? (_selectTaskStep = new TTaskStep());
|
||
set
|
||
{
|
||
SetProperty(ref _selectTaskStep, value);
|
||
}
|
||
}
|
||
|
||
private TTaskRecord _selectTaskRecord;
|
||
public TTaskRecord SelectTaskRecord
|
||
{
|
||
get => _selectTaskRecord ?? (_selectTaskRecord = new TTaskRecord());
|
||
set
|
||
{
|
||
SetProperty(ref _selectTaskRecord, value);
|
||
}
|
||
}
|
||
//public Dictionary<int, string> SexDict { get; } = new Dictionary<int, string>();
|
||
public TaskViewModel(IUnityContainer unityContainer, IRegionManager regionManager) : base(unityContainer, regionManager)
|
||
{
|
||
this.PageTitle = "调度记录";
|
||
|
||
//var models =_unityContainer.Resolve<RgvActionService>().GetAll();
|
||
//foreach(var item in models)
|
||
//{
|
||
// SexDict.Add(item.Cmd, item.Desc);
|
||
//}
|
||
}
|
||
public DelegateCommand SelectCommand => new DelegateCommand(() =>
|
||
{
|
||
if (SelectTaskRecord != null && SelectTaskRecord.Id > 0)
|
||
{
|
||
TaskStepList.Clear();
|
||
var models = _unityContainer.Resolve<TaskStepService>().GetTaskAllStep(SelectTaskRecord.Id);
|
||
models.ForEach(item => TaskStepList.Add(item));
|
||
}
|
||
});
|
||
|
||
public DelegateCommand SendCommand => new DelegateCommand(() =>
|
||
{
|
||
if (null == SelectTaskRecord
|
||
|| 0 == SelectTaskRecord.Id
|
||
|| 0 == SelectTaskRecord.Target)
|
||
{
|
||
HandyControl.Controls.MessageBox.Info("请选择任务!", "提示");
|
||
return;
|
||
}
|
||
|
||
if ((int)ETaskStatus.ExecutionCompleted == SelectTaskRecord.Status)
|
||
{
|
||
HandyControl.Controls.MessageBox.Warning("已经完成的任务不能再发送!", "警告");
|
||
return;
|
||
}
|
||
|
||
if (SelectTaskStep != null)
|
||
{
|
||
if ((ETaskStep)SelectTaskStep.StepId == ETaskStep.None
|
||
|| (ETaskStep)SelectTaskStep.StepId == ETaskStep.Unexecuted
|
||
|| (ETaskStep)SelectTaskStep.StepId == ETaskStep.Finish)
|
||
{
|
||
HandyControl.Controls.MessageBox.Warning("请选择任务步骤!");
|
||
return;
|
||
}
|
||
|
||
if (System.Windows.MessageBoxResult.OK ==
|
||
HandyControl.Controls.MessageBox.Ask($"你确定重新发送【{((ETaskStep)SelectTaskStep.StepId).GetDescription()}】指令到PLC?", "发送指令"))
|
||
{
|
||
if(_unityContainer.Resolve<ICommonFun>().ManualTaskCmd(SelectTaskRecord, (short)SelectTaskStep.StepId))
|
||
{
|
||
HandyControl.Controls.MessageBox.Info("发送成功");
|
||
}
|
||
else
|
||
{
|
||
HandyControl.Controls.MessageBox.Fatal("发送失败,请重新发送!","警示");
|
||
}
|
||
}
|
||
}
|
||
});
|
||
public async override void Refresh()
|
||
{
|
||
TaskList.Clear();
|
||
|
||
var listTask = await System.Threading.Tasks.Task.Run(() =>
|
||
{
|
||
//从数据库查询任务
|
||
return _unityContainer.Resolve<TaskRecordService>().GetAllTaskRun();
|
||
});
|
||
listTask = UpdetaListStatus(listTask);
|
||
listTask.ForEach(x => TaskList.Add(x));
|
||
}
|
||
|
||
public DelegateCommand<object> QueryCommand => new DelegateCommand<object>((x) =>
|
||
{
|
||
TaskList.Clear();
|
||
var queryTask = _unityContainer.Resolve<TaskRecordService>().Query(StartTime, EndTime);
|
||
|
||
if (0 != queryTask.Count)
|
||
{
|
||
queryTask = UpdetaListStatus(queryTask);
|
||
queryTask.ForEach(item => TaskList.Add(item));
|
||
Growl.Success("查询完成!");
|
||
}
|
||
else
|
||
{
|
||
Growl.Success("没有数据!");
|
||
}
|
||
});
|
||
public List<TTaskRecord> UpdetaListStatus(List<TTaskRecord> taskRunList)
|
||
{
|
||
foreach (var item in taskRunList)
|
||
{
|
||
if (item.Status != (int)ETaskStatus.ExecutionCompleted)
|
||
{
|
||
item.Status = (int)ETaskStatus.None;
|
||
}
|
||
}
|
||
return taskRunList;
|
||
}
|
||
public DelegateCommand<object> DeleteCommand => new DelegateCommand<object>((x) =>
|
||
{
|
||
var result = HandyControl.Controls.MessageBox.Ask($@"是否确定要删除任务?", "操作提示");
|
||
if (result == System.Windows.MessageBoxResult.Cancel)
|
||
{
|
||
return;
|
||
}
|
||
var taskService = _unityContainer.Resolve<TaskRecordService>();
|
||
var task = x as TTaskRecord;
|
||
if (task != null)
|
||
{
|
||
int delCount = taskService.DeleteTask(task.Id);
|
||
if (delCount > 0)
|
||
{
|
||
//如果是未执行完成的任务,要清除主界面
|
||
if (task.Status != (int)ETaskStatus.ExecutionCompleted)
|
||
{
|
||
task.StepId = (int)ETaskStep.None;
|
||
CommonCoreHelper.Instance.BlockTask.Add(task);
|
||
}
|
||
|
||
LogHelper.Instance.Debug($"删除了一条任务,取盘位置{task.Source},放盘位置{task.Target},托盘ID{task.PalletId}");
|
||
Growl.Success("任务删除完成!");
|
||
this.Refresh();
|
||
}
|
||
else
|
||
{
|
||
Growl.Success("任务删除失败!");
|
||
}
|
||
}
|
||
});
|
||
|
||
void INavigationAware.OnNavigatedTo(NavigationContext navigationContext)
|
||
{
|
||
// 导航到当前页面时的处理逻辑,先执行构造方法,再执行本方法
|
||
}
|
||
|
||
bool INavigationAware.IsNavigationTarget(NavigationContext navigationContext)
|
||
{
|
||
return false; //false:表示每次导航都创建新实例,不重用旧实例; true:用旧实例,不会执行构造方法
|
||
}
|
||
|
||
void INavigationAware.OnNavigatedFrom(NavigationContext navigationContext)
|
||
{
|
||
//相当于析构方法,退出时(切换时)再执行。
|
||
// 在这里添加释放旧视图实例资源的代码
|
||
}
|
||
}
|
||
}
|