EquipmentAddOrEditVM.cs 3.7 KB
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HHECS.DAQClient.Model;
using System.Windows;
using MessageBox = HandyControl.Controls.MessageBox;

namespace HHECS.DAQClient.ViewModel.EquipmentVM
{
    public partial class EquipmentAddOrEditVM : ObservableObject
    {
        [ObservableProperty]
        private EquipmentExtend equipment = new EquipmentExtend();

        [ObservableProperty]
        private Dictionary<string, int> equipmentTypes = new Dictionary<string, int>();


        [ObservableProperty]
        private bool cbxIsEnable = true;

        private bool IsEdit = false;

        public Window Owner { get; set; }

        private readonly IFreeSql _freeSql;

        public EquipmentAddOrEditVM(IFreeSql freeSql)
        {
            _freeSql = freeSql;
        }

        public void InitialData(int? equipmentId = null)
        {
            try
            {
                var result = _freeSql.Queryable<EquipmentTypeExtend>().ToList(x => new { x.Id, x.Name });
                EquipmentTypes = result.ToDictionary(x => x.Name, y => y.Id);
                if (equipmentId != null)
                {
                    Owner.Title = "修改设备数据";
                    IsEdit = true;
                    CbxIsEnable = false;
                    Equipment = _freeSql.Queryable<EquipmentExtend>().Where(x => x.Id.Equals(equipmentId)).First();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Error($"[{nameof(EquipmentAddOrEditVM)}]加载数据失败.{ex.Message}");
            }
        }

        [RelayCommand]
        public void Save()
        {
            try
            {
                if (Equipment.EquipmentTypeId == default)
                {
                    MessageBox.Warning($"请选择设备类型!");
                    return;
                }

                var equipmentPropTemps = _freeSql.Queryable<EquipmentTypePropTemplateExtend>().Where(x => x.EquipmentTypeId == Equipment.EquipmentTypeId).ToList();
                if (equipmentPropTemps.Count == 0)
                {
                    MessageBox.Warning($"当前选择的设备类型模板数据为空!");
                    return;
                }

                if (!IsEdit)
                {
                    Equipment.Created = DateTime.Now;
                    _freeSql.Insert(Equipment).ExecuteAffrows();
                }
                else
                {
                    _freeSql.Update<EquipmentExtend>().Set(x => x.Updated, DateTime.Now).Where(x => x.Id == Equipment.Id).ExecuteAffrows();
                }

                if (!IsEdit)
                {
                    var equipmentId = _freeSql.Queryable<EquipmentExtend>().Where(x => x.Code == Equipment.Code).First(x => x.Id);
                    var equipmentProps = equipmentPropTemps.Select(x => new EquipmentPropExtend
                    {
                        EquipmentId = equipmentId,
                        EquipmentTypePropTemplateId = x.Id,
                        EquipmentTypePropTemplateCode = x.Code,
                        Address = x.Address,
                        Value = string.Empty,
                        Remark = x.Name,
                        Created = DateTime.Now,
                    }).ToList();
                    _freeSql.Insert(equipmentProps).ExecuteAffrows();
                }
                Owner.DialogResult = true;
                MessageBox.Success($"操作成功");
            }
            catch (Exception ex)
            {
                MessageBox.Error($"操作失败.{ex.Message}");
            }
        }

        [RelayCommand]
        public void Cancel()
        {
            Owner.Close();
        }
    }
}