Files
6098/Cowain.Bake.Main/ViewModels/AddBatteryViewModel.cs

166 lines
5.3 KiB
C#
Raw Permalink Normal View History

using Cowain.Bake.BLL;
using Cowain.Bake.Common;
using Cowain.Bake.Common.Core;
using Cowain.Bake.Common.Enums;
using Cowain.Bake.Model;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Unity;
using JSON = Newtonsoft.Json.JsonConvert;
namespace Cowain.Bake.Main.ViewModels
{
public class AddBatteryViewModel : BindableBase
{
private EBatteryStatus selectedEnum;
public EBatteryStatus SelectedEnum
{
get { return selectedEnum; }
set { SetProperty(ref selectedEnum, value); }
}
public List<object> Options { get; set; }
private object selectedOption;
public object SelectedOption
{
get { return selectedOption; }
set { SetProperty(ref selectedOption, value); }
}
public string batteryCode;
public string BatteryCode
{
get => batteryCode;
set => SetProperty(ref batteryCode, value);
}
int _palletID = 0;
int _palletVID = 0;
private IUnityContainer _unityContainer;
public AddBatteryViewModel(IUnityContainer unityContainer, int palletId, int VID)
{
_unityContainer = unityContainer;
_palletID = palletId;
_palletVID = VID;
SelectedEnum = EBatteryStatus.ToPallet;
SelectedPositionX = 1;
SelectedPositionY = 2;
Options = new List<object> { "否", "是" };
SelectedOption = "否";
}
public DelegateCommand<object> AddCommand => new DelegateCommand<object>((x) =>
{
if (string.IsNullOrEmpty(BatteryCode))
{
HandyControl.Controls.MessageBox.Warning("请输入电芯条码!");
return;
}
if (_unityContainer.Resolve<BatteryInfoService>().IsExistBattery(_palletVID, SelectedPositionX, SelectedPositionY))
{
HandyControl.Controls.MessageBox.Warning("夹具位置存在电芯,此位置不能再增加电芯!");
return;
}
if (0 == _palletVID)
{
HandyControl.Controls.MessageBox.Warning("此夹具的虚拟ID为0不能增加电芯!");
return;
}
TBatteryInfo batteryInfo = new TBatteryInfo()
{
PalletVirtualId = _palletVID,
BatteryCode = BatteryCode,
BatteryStatus = (sbyte)SelectedEnum,
PositionX = (sbyte)SelectedPositionX,
PositionY = (sbyte)SelectedPositionY,
ScanTime = DateTime.Now,
BindingTime = DateTime.Now,
DummyFlag = (SelectedOption?.ToString() == "是") ? true : false
};
if(0 == _unityContainer.Resolve<BatteryInfoService>().Insert(batteryInfo))
{
LogHelper.Instance.Debug($"手动增加电芯:【{BatteryCode}】失败!", true);
}
else
{
LogHelper.Instance.Info($"手动增加电芯:【{BatteryCode}】成功!", true);
_unityContainer.Resolve<LogService>().AddLog($"手动增加电芯成功,{ JSON.SerializeObject(batteryInfo)}", E_LogType.Operate.ToString());
_unityContainer.Resolve<PalletInfoService>().ModifyBatteryQty(_palletID); //数据减1
}
});
public List<KeyValuePair<EBatteryStatus, string>> EnumOptions
{
get
{
var enumType = typeof(EBatteryStatus);
var options = new List<KeyValuePair<EBatteryStatus, string>>();
foreach (var value in Enum.GetValues(enumType))
{
var field = enumType.GetField(value.ToString());
var attributes = field.GetCustomAttributes(typeof(DescriptionAttribute), false);
var description = ((DescriptionAttribute)attributes[0]).Description;
options.Add(new KeyValuePair<EBatteryStatus, string>((EBatteryStatus)value, description));
}
return options;
}
}
private int selectedPositionY;
public int SelectedPositionY
{
get { return selectedPositionY; }
set { SetProperty(ref selectedPositionY, value); }
}
private int selectedPositionX;
public int SelectedPositionX
{
get { return selectedPositionX; }
set { SetProperty(ref selectedPositionX, value); }
}
public List<int> PositionYRange
{
get
{
List<int> numbers = new List<int>();
for (int i = 1; i <= Global.PALLET_COLS; i++)
{
numbers.Add(i);
}
return numbers;
}
}
public List<int> PositionXRange
{
get
{
List<int> numbers = new List<int>();
for (int i = 1; i <= Global.PALLET_ROWS; i++)
{
numbers.Add(i);
}
return numbers;
}
}
}
}