DictVM.cs 7.34 KB
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 DictVM : VMBase
    {
        #region 属性

        #region Dict

        public string Code { get; set; }

        public string Name { get; set; }

        public List<Dict> Dicts { get; set; }

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

        public bool IsTabMainSelected { get; set; } = true;

        #endregion

        #region DictDetail

        public string DetailCode { get; set; }

        public string DetailName { get; set; }

        public Dict CurrentDict { get; set; }

        public List<DictDetail> DictDetails { get; set; }

        public bool IsTabDetailSelected { get; set; } = false;

        #endregion

        #endregion

        #region 方法

        #region Dict

        public void Query()
        {
            Expression<Func<Dict, 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.GetDicts(filter, PageInfo.PageIndex, PageInfo.PageSize, out long totalCount);
            if (result.Success)
            {
                PageInfo.TotalCount = totalCount;
                Dicts = result.Data;
            }
            else
            {
                MessageBox.Show($"查询失败:{result.Msg}");
            }
        }

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

        public void Edit(Dict dict)
        {
            if (dict == null)
            {
                MessageBox.Show("请选中一条数据");
                return;
            }
            WinDictAddOrEdit win = new WinDictAddOrEdit(dict.Id);
            win.ShowDialog();
            Query();
        }

        public void New()
        {
            WinDictAddOrEdit win = new WinDictAddOrEdit(0);
            win.ShowDialog();
            Query();
        }

        #endregion

        #region DictDetail

        public void QueryDetail(Dict dict)
        {
            if (dict == null)
            {
                MessageBox.Show("未选中主数据!");
                IsTabMainSelected = true;
                return;
            }
            //重新查询一遍主数据,防止同步删除
            var result = App.SystemService.GetDicts(t => t.Id == dict.Id);
            if (result.Success && result.Data.Count > 0)
            {
                CurrentDict = result.Data[0];

                Expression<Func<DictDetail, bool>> filter = t => t.DictId == dict.Id;
                if (!String.IsNullOrWhiteSpace(DetailCode))
                {
                    filter = filter.And(t => t.Code.Contains(DetailCode));
                }
                if (!String.IsNullOrWhiteSpace(DetailName))
                {
                    filter = filter.And(t => t.Name.Contains(DetailName));
                }

                BllResult<List<DictDetail>> detailResult = App.SystemService.GetDictDetails(filter);
                if (detailResult.Success)
                {
                    DictDetails = detailResult.Data;
                }
                else
                {
                    DictDetails = null;
                    MessageBox.Show($"查询当前字典的明细失败:{detailResult.Msg}");
                }
                IsTabDetailSelected = true;
            }
            else
            {
                MessageBox.Show($"未能查询到主数据,请刷新");
                IsTabMainSelected = true;
            }
        }

        public void DeleteDetail(List<DictDetail> dictDetails)
        {
            if (dictDetails == null || dictDetails.Count == 0)
            {
                MessageBox.Show("未选中数据");
                return;
            }
            if (MessageBox.Show("是否确认删除?", "警告", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
            {
                return;
            }
            BllResult bllResult = App.SystemService.DeleteDictDetails(dictDetails);
            if (bllResult.Success)
            {
                App.LogService.LogOperation(Title.DictDetailDelete, ModuleConst.Dict, $"字典明细删除成功.数据:{JsonConvert.SerializeObject(dictDetails.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.DictDetailDelete, ModuleConst.Dict, $"字典明细删除失败.数据:{JsonConvert.SerializeObject(dictDetails.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}");
            }
            QueryDetail(CurrentDict);
        }

        public void EditDetail(DictDetail dictDetail)
        {
            if (dictDetail == null)
            {
                MessageBox.Show("请选中一条数据");
                return;
            }
            WinDictDetailAddOrEdit win = new WinDictDetailAddOrEdit(dictDetail.Id, dictDetail.DictId);
            win.ShowDialog();
            QueryDetail(CurrentDict);
        }

        public void NewDetail()
        {
            if (CurrentDict == null)
            {
                MessageBox.Show("未选中主数据!请通过选中主数据,双击进入详情界面.");
                IsTabMainSelected = true;
                return;
            }
            WinDictDetailAddOrEdit win = new WinDictDetailAddOrEdit(0, CurrentDict.Id);
            win.ShowDialog();
            QueryDetail(CurrentDict);
        }

        #endregion

        #endregion
    }
}