ningshuxia
2023-02-24 1e4b358de58e36bfbf692ab2538ff9e7d60fd025
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
using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace IStation.WinFrmUI.Scatl
{
    /// <summary>
    /// 设备导入导出辅助类
    /// </summary>
    public class MonitorPointImportExportHelper
    {
        /// <summary>
        /// 测点组扩展 测点列表
        /// </summary>
        public class JsonDataModel : Model.MonitorPointGroup
        {
            public JsonDataModel() { }
            public JsonDataModel(Model.MonitorPointGroup rhs, List<Model.MonitorPointExSignalList> monitorPointList) : base(rhs)
            {
                this.MonitorPointList = monitorPointList?.ToList();
            }
 
            /// <summary>
            /// 监测点列表
            /// </summary>
            public List<Model.MonitorPointExSignalList> MonitorPointList { get; set; }
        }
 
        //导入
        public static bool Import(long projectId)
        {
            var dlg = new OpenFileDialog();
            dlg.Title = "选择监测点文件";
            dlg.Filter = "Monitor文档|*.monitor";
            dlg.AutoUpgradeEnabled = true;
            if (dlg.ShowDialog() != DialogResult.OK)
            {
                return false;
            }
            var json = System.IO.File.ReadAllText(dlg.FileName);
            if (string.IsNullOrEmpty(json))
            {
                XtraMessageBox.Show("监测点文件解析失败!");
                return false;
            }
            var list = JsonHelper.Json2Object<List<JsonDataModel>>(json);
            return Cover(projectId,list);
        }
 
        public static bool Cover(long projectId,List<JsonDataModel> list)
        {
            if (list == null || list.Count < 1)
            {
                XtraMessageBox.Show("监测点文件解析失败!");
                return false;
            }
            var groups = list.Where(x => x.Id > 0).Select(x => new Model.MonitorPointGroup(x)).ToList();
            if (groups.Count > 0)
            {
                groups.RemoveAll(x => x.Id < 1);
                if (groups.Count > 0)
                {
                    var bol = new BLL.MonitorPointGroup().Covers(projectId, groups);
                    if (!bol)
                    {
                        XtraMessageBox.Show("监测点组覆盖失败!");
                        return false;
                    }
                }
            }
            var monitor_list = list.Where(x => x.MonitorPointList != null && x.MonitorPointList.Count > 0).SelectMany(x => x.MonitorPointList).ToList();
            if (monitor_list.Count < 1)
            {
                XtraMessageBox.Show("未检测到监测点信息");
                return false;
            }
            var result = new BLL.MonitorPoint().CoverEx(projectId, monitor_list);
            if (!result)
            {
                XtraMessageBox.Show("监测点覆盖失败!");
                return false;
            }
            return true;
        }
 
        //导出
        public static bool Export(long projectId)
        {
            var dlg = new SaveFileDialog();
            dlg.Title = "选择监测点文件保存路径";
            dlg.Filter = "Monitor文档|*.monitor";
            dlg.AutoUpgradeEnabled = true;
            if (dlg.ShowDialog() != DialogResult.OK)
            {
                return false;
            }
            var group_list = new BLL.MonitorPointGroup().GetAll(projectId);
            if (group_list == null)
                group_list = new List<Model.MonitorPointGroup>();
            var monitor_list = new BLL.MonitorPoint().GetExSignalList(projectId);
            var list = group_list.Select(x => new JsonDataModel(x, monitor_list?.Where(t => t.GroupId == x.Id).ToList())).ToList();
            if (list == null)
                list = new List<JsonDataModel>();
            var notGroupList = monitor_list?.Where(x => x.GroupId < 1).ToList();
            if (notGroupList != null && notGroupList.Count > 0)
            {
                var group = new JsonDataModel(); 
                group.MonitorPointList = notGroupList.ToList();
                list.Add(group);
            }
 
            var json = JsonHelper.Object2FormatJson(list);
            System.IO.File.WriteAllText(dlg.FileName, json);
            return true;
        }
 
 
         
 
    }
}