using AutoMapper;
using CapMachine.Core;
using CapMachine.Model;
using CapMachine.Model.CANLIN;
using CapMachine.Wpf.CanDrive;
using CapMachine.Wpf.Dtos;
using CapMachine.Wpf.PrismEvent;
using CapMachine.Wpf.Services;
using Ganss.Excel;
using Microsoft.Win32;
using NPOI.SS.UserModel.Charts;
using Prism.Commands;
using Prism.Events;
using Prism.Regions;
using Prism.Services.Dialogs;
using Syncfusion.Windows.Tools.Controls;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace CapMachine.Wpf.ViewModels
{
///
/// CAN配置ViewModel
///
public class CANConfigViewModel : NavigationViewModel
{
public CANConfigViewModel(IDialogService dialogService, IFreeSql freeSql,
IEventAggregator eventAggregator, IRegionManager regionManager, SysRunService sysRunService,
ConfigService configService, CanDriveService canDriveService,
IMapper mapper, MachineRtDataService machineRtDataService)
{
//LogService = logService;
FreeSql = freeSql;
EventAggregator = eventAggregator;
RegionManager = regionManager;
SysRunService = sysRunService;
ConfigService = configService;
CanDriveService = canDriveService;
Mapper = mapper;
this.MachineRtDataService = machineRtDataService;
//MachineDataService = machineDataService;
DialogService = dialogService;
WriteNameCbxItems = new ObservableCollection()
{
new CbxItems(){ Key="转速",Text="转速"},
new CbxItems(){ Key="阿斯顿发",Text="阿斯顿发"},
new CbxItems(){ Key="都尴尬的",Text="都尴尬的"},
new CbxItems(){ Key="1212",Text="1212"},
new CbxItems(){ Key="112",Text="ADSFADSF"},
};
InitLoadCanConfigPro();
}
///
/// FreeSQL 实例函数
///
public IFreeSql FreeSql { get; }
public IEventAggregator EventAggregator { get; }
public IRegionManager RegionManager { get; }
public SysRunService SysRunService { get; }
public ConfigService ConfigService { get; }
public CanDriveService CanDriveService { get; }
public IMapper Mapper { get; }
private MachineRtDataService MachineRtDataService { get; }
///
/// 弹窗服务
///
public IDialogService DialogService { get; }
#region CanConfigPro
///
/// 初始化j加载执行方法
///
private void InitLoadCanConfigPro()
{
//CAN配置集合数据
canLinConfigPros = FreeSql.Select().Where(a => a.CANLINInfo == CANLIN.CAN)
.Include(a => a.CANConfigExd)
.IncludeMany(a => a.CanLinConfigContents)
.ToList();
ListCanLinConfigPro = new ObservableCollection(canLinConfigPros);
if (SelectCanLinConfigPro != null)
{
SelectCanLinConfigPro = canLinConfigPros.Where(a => a.Id == SelectCanLinConfigPro.Id).FirstOrDefault()!;
SelectedCANConfigExdDto = Mapper.Map(SelectCanLinConfigPro!.CANConfigExd);
var WirteData = SelectCanLinConfigPro.CanLinConfigContents!.Where(a => a.RWInfo == RW.Write).ToList();
if (WirteData != null && WirteData.Count > 0)
{
ListWriteCanLinRWConfigDto = new ObservableCollection(Mapper.Map>(WirteData));
}
var ReadData = SelectCanLinConfigPro.CanLinConfigContents!.Where(a => a.RWInfo == RW.Read).ToList();
if (ReadData != null && ReadData.Count > 0)
{
ListReadCanLinRWConfigDto = new ObservableCollection(Mapper.Map>(ReadData));
}
}
}
private bool _IsCanConfigProActive = true;
///
/// CAN 配置是否被激活
///
public bool IsCanConfigProActive
{
get { return _IsCanConfigProActive; }
set { _IsCanConfigProActive = value; RaisePropertyChanged(); }
}
//CanLinConfigPromdCmd
private DelegateCommand _CanLinConfigPromdCmd;
///
/// CanConfigPro操作的指令
///
public DelegateCommand CanLinConfigPromdCmd
{
set
{
_CanLinConfigPromdCmd = value;
}
get
{
if (_CanLinConfigPromdCmd == null)
{
_CanLinConfigPromdCmd = new DelegateCommand((Par) => CanLinConfigPromdCmdMethod(Par));
}
return _CanLinConfigPromdCmd;
}
}
///
/// CanConfigPro操作的指令方法
///
///
private void CanLinConfigPromdCmdMethod(string Par)
{
switch (Par)
{
case "Add":
//弹窗
DialogService.ShowDialog("DialogCanLinConfigCreateView", new DialogParameters() { { "Name", "" } }, (par) =>
{
if (par.Result == ButtonResult.OK)
{
//程序名称
var ReturnValue = par.Parameters.GetValue("Name");
//加载默认的数据 CANConfigExd
var InsertCANConfigExd = FreeSql.Insert(new CANConfigExd()
{
BaudRate = 250,
DbcPath = "请配置DBC路径",
Cycle = 100,
}).ExecuteInserted();
//返回数据,刷新Chart
var InsertData = FreeSql.Insert(new CanLinConfigPro()
{
ConfigName = ReturnValue,
CANLINInfo = CANLIN.CAN,
CANConfigExdId = InsertCANConfigExd.FirstOrDefault()!.Id
}).ExecuteInserted();
if (InsertData != null)
{
//加载默认的数据 CanLinRWConfig
FreeSql.Insert(new CanLinRWConfig()
{
Content = "Speed",
DefautValue = "0",
Name = "转速",
RWInfo = RW.Write,
CanLinConfigProId = InsertData.FirstOrDefault().Id
}).ExecuteInserted();
FreeSql.Insert(new CanLinRWConfig()
{
Content = "Vol",
DefautValue = "0",
Name = "电压",
RWInfo = RW.Read,
CanLinConfigProId = InsertData.FirstOrDefault().Id
}).ExecuteInserted();
}
InitLoadCanConfigPro();
SelectCanLinConfigPro = canLinConfigPros!.Find(a => a.Id == InsertData.FirstOrDefault()!.Id);
}
else if (par.Result == ButtonResult.Cancel)
{
//取消
}
});
break;
case "Edit":
if (SelectCanLinConfigPro != null)
{
//弹窗
DialogService.ShowDialog("DialogCanLinConfigCreateView", new DialogParameters() { { "Name", SelectCanLinConfigPro.ConfigName } }, (par) =>
{
if (par.Result == ButtonResult.OK)
{
//程序名称
var ReturnValue = par.Parameters.GetValue("Name");
FreeSql.Update()
.Set(a => a.ConfigName, ReturnValue)
.Where(a => a.Id == SelectCanLinConfigPro.Id)
.ExecuteAffrows();
InitLoadCanConfigPro();
SelectCanLinConfigPro = null;
}
else if (par.Result == ButtonResult.Cancel)
{
//取消
}
});
}
else
{
MessageBox.Show("选中后再操作", "提示", MessageBoxButton.OK, MessageBoxImage.Hand);
}
break;
case "Save":
case "Delete":
if (SelectCanLinConfigPro != null)
{
var repo = FreeSql.GetRepository();
repo.DbContextOptions.EnableCascadeSave = true; //关键设置
var DeleteData = repo.Select
.Include(a => a.CANConfigExd)
.IncludeMany(a => a.CanLinConfigContents)
.Where(a => a.Id == SelectCanLinConfigPro.Id)
.ToList();
repo.Delete(DeleteData);
foreach (var item in DeleteData)
{
FreeSql.Delete(item.CANConfigExdId).ExecuteAffrows();
}
InitLoadCanConfigPro();
}
else
{
MessageBox.Show("选中后再操作", "提示", MessageBoxButton.OK, MessageBoxImage.Hand);
}
break;
case "Active":
if (SelectCanLinConfigPro != null)
{
IsCanConfigProActive = !IsCanConfigProActive;
}
else
{
MessageBox.Show("选中后再操作", "提示", MessageBoxButton.OK, MessageBoxImage.Hand);
}
break;
default:
break;
}
}
///
/// CAN List集合信息
///
private List canLinConfigPros = new List();
private ObservableCollection _ListCanLinConfigPro;
///
/// CAN 配置程序集合 ObservableCollection
///
public ObservableCollection ListCanLinConfigPro
{
get { return _ListCanLinConfigPro; }
set { _ListCanLinConfigPro = value; RaisePropertyChanged(); }
}
///
/// 选中的CanLinConfigPro
///
public CanLinConfigPro SelectCanLinConfigPro { get; set; }
private DelegateCommand