Blame view

HHECS.DAQClient/ViewModel/EquipmentVM/EquipmentAddOrEditVM.cs 3.74 KB
1
2
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
唐召明 authored
3
using HHECS.DAQClient.Model;
4
5
6
7
8
9
10
11
using System.Windows;
using MessageBox = HandyControl.Controls.MessageBox;

namespace HHECS.DAQClient.ViewModel.EquipmentVM
{
    public partial class EquipmentAddOrEditVM : ObservableObject
    {
        [ObservableProperty]
唐召明 authored
12
        private EquipmentExtend equipment = new EquipmentExtend();
13
14
15
16
17
18
19
20
21
22
23
24

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


        [ObservableProperty]
        private bool cbxIsEnable = true;

        private bool IsEdit = false;

        public Window Owner { get; set; }
唐召明 authored
25
        private readonly IFreeSql _freeSql;
26
唐召明 authored
27
        public EquipmentAddOrEditVM(IFreeSql freeSql)
28
        {
唐召明 authored
29
            _freeSql = freeSql;
30
31
32
33
34
35
        }

        public void InitialData(int? equipmentId = null)
        {
            try
            {
唐召明 authored
36
                var result = _freeSql.Queryable<EquipmentTypeExtend>().ToList(x => new { x.Id, x.Name });
37
38
39
40
41
42
                EquipmentTypes = result.ToDictionary(x => x.Name, y => y.Id);
                if (equipmentId != null)
                {
                    Owner.Title = "修改设备数据";
                    IsEdit = true;
                    CbxIsEnable = false;
唐召明 authored
43
                    Equipment = _freeSql.Queryable<EquipmentExtend>().Where(x => x.Id.Equals(equipmentId)).First();
44
45
46
47
                }
            }
            catch (Exception ex)
            {
唐召明 authored
48
                MessageBox.Error($"[{nameof(EquipmentAddOrEditVM)}]加载数据失败.{ex.Message}");
49
50
51
52
53
54
55
56
57
58
59
60
61
62
            }
        }

        [RelayCommand]
        public void Save()
        {
            try
            {
                if (Equipment.EquipmentTypeId == default)
                {
                    MessageBox.Warning($"请选择设备类型!");
                    return;
                }
唐召明 authored
63
                var equipmentPropTemps = _freeSql.Queryable<EquipmentTypePropTemplateExtend>().Where(x => x.EquipmentTypeId == Equipment.EquipmentTypeId).ToList();
64
65
66
67
68
69
70
71
72
                if (equipmentPropTemps.Count == 0)
                {
                    MessageBox.Warning($"当前选择的设备类型模板数据为空!");
                    return;
                }

                if (!IsEdit)
                {
                    Equipment.Created = DateTime.Now;
唐召明 authored
73
                    _freeSql.Insert(Equipment).ExecuteAffrows();
74
75
76
                }
                else
                {
唐召明 authored
77
                    _freeSql.Update<EquipmentExtend>().Set(x => x.Updated, DateTime.Now).Where(x => x.Id == Equipment.Id).ExecuteAffrows();
78
79
80
81
                }

                if (!IsEdit)
                {
唐召明 authored
82
83
                    var equipmentId = _freeSql.Queryable<EquipmentExtend>().Where(x => x.Code == Equipment.Code).First(x => x.Id);
                    var equipmentProps = equipmentPropTemps.Select(x => new EquipmentPropExtend
84
85
86
87
88
89
90
91
92
93
                    {
                        EquipmentId = equipmentId,
                        EquipmentTypePropTemplateId = x.Id,
                        EquipmentTypePropTemplateCode = x.Code,
                        ServerHandle = 0,
                        Address = x.Address,
                        Value = string.Empty,
                        Remark = x.Name,
                        Created = DateTime.Now,
                    }).ToList();
唐召明 authored
94
                    _freeSql.Insert(equipmentProps).ExecuteAffrows();
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
                }
                Owner.DialogResult = true;
                MessageBox.Success($"操作成功");
            }
            catch (Exception ex)
            {
                MessageBox.Error($"操作失败.{ex.Message}");
            }
        }

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