PLCVM.cs
4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using AutoMapper;
using HHECS.Application.Enums;
using HHECS.Infrastructure.Excel;
using HHECS.Model.Entities;
using HHECS.Model.ExcelModels;
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.EquipmentInfo
{
public class PLCVM : VMBase
{
public string PLCCode { get; set; }
public string PLCName { get; set; }
public List<PLC> PLCs { get; set; }
public PageInfo PageInfo { get; set; } = new PageInfo();
public void Query()
{
Expression<Func<PLC, bool>> filter = t => true;
if (!string.IsNullOrWhiteSpace(PLCCode))
{
filter = filter.And(t => t.Code.Contains(PLCCode));
}
if (!string.IsNullOrWhiteSpace(PLCName))
{
filter = filter.And(t => t.Name.Contains(PLCName));
}
var result = App.EquipmentService.GetPLCs(filter, PageInfo.PageIndex, PageInfo.PageSize, out long totalCount);
if (result.Success)
{
PageInfo.TotalCount = totalCount;
PLCs = result.Data;
}
else
{
MessageBox.Show($"查询失败:{result.Msg}");
}
}
public void Edit(PLC plc)
{
if (plc == null)
{
MessageBox.Show("请选中数据");
return;
}
WinPLCAddOrEdit win = new WinPLCAddOrEdit(plc.Id);
win.ShowDialog();
Query();
}
public void New()
{
WinPLCAddOrEdit win = new WinPLCAddOrEdit(0);
win.ShowDialog();
Query();
}
public void Delete(List<PLC> plcs)
{
if (plcs == null || plcs.Count == 0)
{
MessageBox.Show("未选中数据");
return;
}
if (MessageBox.Show("是否确认删除?", "警告", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
{
return;
}
var bllResult = App.EquipmentService.DeletePLCs(plcs);
if (bllResult.Success)
{
App.LogService.LogOperation(Title.MaintainOperationDelete, ModuleConst.MaintainOperation, $"PLC数据删除成功.数据:{JsonConvert.SerializeObject(plcs.Select(t => new { Id = t.Id, Name = t.Name }))}", bllResult.Code.ToString(), App.User.UserCode);
MessageBox.Show("删除成功");
}
else
{
App.LogService.LogOperation(Title.MaintainOperationDelete, ModuleConst.MaintainOperation, $"PLC数据删除失败.数据:{JsonConvert.SerializeObject(plcs.Select(t => new { Id = t.Id, Name = t.Name }))}.详情:{bllResult.Msg}", bllResult.Code.ToString(), App.User.UserCode);
MessageBox.Error($"删除失败:{bllResult.Msg}");
}
Query();
}
public async void Export()
{
try
{
if (MessageBox.Show("是否确认导出?", "注意", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
{
return;
}
Expression<Func<PLC, bool>> filter = t => true;
var result = App.EquipmentService.GetPLCs(filter);
if (result.Success)
{
var operations = result.Data;
var config = new MapperConfiguration(cfg => cfg.CreateMap<PLC, PLCExcelModel>());
var mapper = config.CreateMapper();
List<PLCExcelModel> models = new List<PLCExcelModel>();
operations.ForEach(t =>
{
var temp = mapper.Map<PLCExcelModel>(t);
models.Add(temp);
});
string TimeTxt = DateTime.Now.ToString("yyyy年MM月dd日HH点mm分ss秒");
await NPIOHelper.ExportDTtoExcelAsync<PLCExcelModel>(models, "PLC数据", string.Format($"{App.ExportPath}PLC数据{TimeTxt}.xlsx"), App.ExportPath);
MessageBox.Show($"PLC数据导出成功");
}
else
{
MessageBox.Show($"导出失败:{result.Msg}");
}
}
catch (Exception ex)
{
MessageBox.Show($"导出异常:{ex.Message}");
}
}
}
}