ConfigVM.cs 3.18 KB
using HandyControl.Controls;
using HHECS.Application.Enums;
using HHECS.BllModel;
using HHECS.Model.Entities;
using HHECS.WinCommon.ViewModel;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using MessageBox = HandyControl.Controls.MessageBox;

namespace HHECS.WinClient.View.SystemInfo
{
    public class ConfigVM : VMBase
    {
        public string Code { get; set; }

        public string Name { get; set; }

        public List<Config> Configs { get; set; }

        public PageInfo PageInfo { get; set; } = new PageInfo();

        public void Query()
        {
            Expression<Func<Config, bool>> filter = t => true;
            if (!string.IsNullOrWhiteSpace(Code))
            {
                filter = filter.And(t => t.Code.Contains(Code));
            }
            if (!string.IsNullOrWhiteSpace(Name))
            {
                filter = filter.And(t => t.Name.Contains(Name));
            }

            var result = App.SystemService.GetConfigs(filter, PageInfo.PageIndex, PageInfo.PageSize, out long totalCount);
            if (result.Success)
            {
                PageInfo.TotalCount = totalCount;
                Configs = result.Data;
            }
            else
            {
                MessageBox.Show($"查询失败:{result.Msg}");
            }

        }

        internal void Delete(List<Config> configs)
        {
            if (configs == null || configs.Count == 0)
            {
                MessageBox.Show("未选中数据");
                return;
            }
            if (MessageBox.Show("是否确认删除?", "警告", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
            {
                return;
            }
            BllResult bllResult = App.SystemService.DeleteConfigs(configs);
            if (bllResult.Success)
            {
                App.LogService.LogOperation(Title.ConfigDelete, ModuleConst.Config, $"配置删除成功.数据:{JsonConvert.SerializeObject(configs.Select(t => new { Id = t.Id, Code = t.Code, Name = t.Name, Value = t.Value }))}", bllResult.Code.ToString(), App.User.UserCode);
                MessageBox.Show("删除成功");
            }
            else
            {
                App.LogService.LogOperation(Title.ConfigDelete, ModuleConst.Config, $"配置删除失败.数据:{JsonConvert.SerializeObject(configs.Select(t => new { Id = t.Id, Code = t.Code, Name = t.Name, Value = t.Value }))},详情:{bllResult.Msg}", bllResult.Code.ToString(), App.User.UserCode);
                MessageBox.Error($"删除失败:{bllResult.Msg}");
            }
            Query();
        }

        internal void Edit(Config config)
        {
            if (config == null)
            {
                MessageBox.Show("请选中数据");
                return;
            }
            WinConfigAddOrEdit win = new WinConfigAddOrEdit(config.Id);
            win.ShowDialog();
            Query();
        }

        internal void New()
        {
            WinConfigAddOrEdit win = new WinConfigAddOrEdit(0);
            win.ShowDialog();
            Query();
        }
    }
}