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

namespace HHECS.WinClient.View.EquipmentInfo
{
    public class PLCAddOrEditVM : VMBase
    {
        public PLC PLC { get; set; }

        public string Title { get; set; } = "test";

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

        public Visibility Visibility { get; set; } = Visibility.Hidden;

        public PLCAddOrEditVM(int id)
        {
            if (id == 0)
            {
                this.Title = "新增";
                PLC = new PLC();
                Visibility = Visibility.Visible;
            }
            else
            {
                this.Title = "更新";
                var bllResult = App.EquipmentService.GetPLCs(t => t.Id == id);
                if (bllResult.Success && bllResult.Data.Count > 0)
                {
                    PLC = bllResult.Data[0];
                }
                else
                {
                    MessageBox.Show("未获取到指定设备维护操作数据");
                    Enable = false;
                }
            }
        }

        public void Save()
        {
            if (PLC.Created == null)
            {
                PLC.Created = DateTime.Now;
                PLC.CreatedBy = App.User.UserCode;
            }
            PLC.Updated = DateTime.Now;
            PLC.UpdatedBy = App.User.UserCode;
            var title = PLC.Id == 0 ? Application.Enums.Title.PLCAdd : Application.Enums.Title.PLCEdit;
            var a = new
            {
                Code = PLC.Code,
                Name = PLC.Name,
            };
            var result = App.EquipmentService.SavePLC(PLC);
            if (result.Success)
            {
                App.LogService.LogOperation(title, ModuleConst.PLC, $"维护操作保存成功.数据:{JsonConvert.SerializeObject(a)}", result.Code.ToString(), App.User.UserCode);
                MessageBox.Show("保存成功");
            }
            else
            {
                App.LogService.LogOperation(title, ModuleConst.PLC, $"维护操作保存失败.数据:{JsonConvert.SerializeObject(a)},详情:{result.Msg}", result.Code.ToString(), App.User.UserCode);
                MessageBox.Error($"保存失败:{result.Msg}");
            }
        }

        public void Clear()
        {
            PLC = new PLC();
        }
    }
}