WinConfigAddOrEdit.xaml.cs 3.26 KB
using HHECS.Bll;
using HHECS.Model;
using HHECS.View.Win;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace HHECS.View.SystemInfo
{
    /// <summary>
    /// WinConfigAddOrEdit.xaml 的交互逻辑
    /// </summary>
    public partial class WinConfigAddOrEdit : BaseWindow
    {
        public int? Id { get; set; }
        public Config CurrentConfig { get; set; } = new Config();
        public WinConfigAddOrEdit(int? id)
        {
            this.Id = id;
            this.Title = id == null ? "新增" : "编辑";
            InitializeComponent();
            Init();
            this.GridMain.DataContext = CurrentConfig;
        }

        private void Init()
        {
            if (Id != null)
            {
                BllResult<List<Config>> result = AppSession.Bll.GetCommonModelByCondition<Config>($"where id = {Id}");
                if (result.Success)
                {
                    var temp = result.Data[0];
                    CurrentConfig.Id = temp.Id;
                    CurrentConfig.Code = temp.Code;
                    CurrentConfig.Name = temp.Name;
                    CurrentConfig.Value = temp.Value;
                    CurrentConfig.Remark = temp.Remark;
                    CurrentConfig.Created = temp.Created;
                    CurrentConfig.CreatedBy = temp.CreatedBy;
                    CurrentConfig.Updated = temp.Updated;
                    CurrentConfig.UpdatedBy = temp.UpdatedBy;
                }
                else
                {
                    MessageBox.Show($"未查询到Id为{Id}的参数");
                }
                //编辑模式下,设置code不能更改
                TxtConfigCode.IsReadOnly = true;
            }
        }

        private void BtnSave_Click(object sender, RoutedEventArgs e)
        {
            if (CurrentConfig.Id == null)
            {
                //新增
                CurrentConfig.Created = DateTime.Now;
                CurrentConfig.CreatedBy = AppSession.User.UserCode;
                BllResult<int?> result = AppSession.Bll.SaveCommonModel<Config>(CurrentConfig);
                if (result.Success)
                {
                    MessageBox.Show("新增成功");
                    TxtConfigCode.IsReadOnly = true;
                    CurrentConfig.Id = result.Data;
                }
                else
                {
                    MessageBox.Show($"新增失败:{result.Msg}");
                }
            }
            else
            {
                //更新
                BllResult result = AppSession.Bll.UpdateCommonModel<Config>(CurrentConfig);
                if (result.Success)
                {
                    MessageBox.Show("更新成功");
                }
                else
                {
                    MessageBox.Show($"更新失败:{result.Msg}");
                }
            }
        }

        private void BtnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}