Blame view

ViewModel/CommunicationVM/CommunicationAddOrEditVM.cs 2.4 KB
唐召明 authored
1
2
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
唐召明 authored
3
using HHECS.EquipmentModel;
唐召明 authored
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using HHECS.RobotTool.Model;
using System.Windows;
using MessageBox = HandyControl.Controls.MessageBox;

namespace HHECS.RobotTool.ViewModel.CommunicationVM
{
    public partial class CommunicationAddOrEditVM : ObservableObject
    {
        [ObservableProperty]
        private CommunicationConfig communicationConfig = new CommunicationConfig();

        [ObservableProperty]
        private Dictionary<string, CommunicationTypeConst> communicationTypes = new Dictionary<string, CommunicationTypeConst>();
唐召明 authored
18
        public Window Owner { get; set; } = null!;
唐召明 authored
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

        private bool IsEdit = false;

        private readonly IFreeSql _freeSql;

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

        public void InitialData(int? communicationConfigId = null)
        {
            try
            {
                CommunicationTypes = Enum.GetNames<CommunicationTypeConst>().ToDictionary(x => x, Enum.Parse<CommunicationTypeConst>);
                if (communicationConfigId != null)
                {
                    Owner.Title = "修改通讯配置";
                    IsEdit = true;
                    CommunicationConfig = _freeSql.Queryable<CommunicationConfig>().Where(x => x.Id.Equals(communicationConfigId)).First();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Error($"初始化数据失败.{ex.Message}");
            }
        }

        [RelayCommand]
        public void Save()
        {
            try
            {
                if (!IsEdit)
                {
唐召明 authored
54
                    CommunicationConfig.Created = DateTime.Now;
唐召明 authored
55
56
57
58
                    _freeSql.Insert(CommunicationConfig).ExecuteAffrows();
                }
                else
                {
唐召明 authored
59
60
                    CommunicationConfig.Updated = DateTime.Now;
                    _freeSql.Update<CommunicationConfig>().SetSource(CommunicationConfig).ExecuteAffrows();
唐召明 authored
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
                }
                Owner.DialogResult = true;
                MessageBox.Success("操作成功");
            }
            catch (Exception ex)
            {
                MessageBox.Error($"操作失败.{ex.Message}");
            }
        }

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