-
Notifications
You must be signed in to change notification settings - Fork 0
/
frmGenerateJson.cs
221 lines (176 loc) · 6.47 KB
/
frmGenerateJson.cs
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DBJ.Controls;
using DBJ.Divorced.Define;
using DBJ.Divorced.Helper;
namespace DBJ.Tool.Json
{
public partial class frmGenerateJson : ParentForm
{
public frmGenerateJson()
{
InitializeComponent();
base.InitLoadingPanel(this.Size);
}
public List<List<KeyValue>> InstaceList { get; set; }
public List<KeyValue> CommonList { get; set; }
public List<string> NeedGenerateList { get; set; }
private List<string> GetCheckedList()
{
var tmp = new List<string>();
for (int i = 0; i < this.checkedListBox1.CheckedItems.Count; i++)
{
tmp.Add(this.checkedListBox1.CheckedItems[i].ToString());
}
return tmp;
}
private string JsonTemplate { get; set; }
private string TemplatePath { get; set; }
private string OutputPath { get; set; }
private string DataPath { get; set; }
private void btnGenerate_Click(object sender, EventArgs e)
{
TemplatePath = this.txtTemplate.Text;
OutputPath = this.txtOutput.Text;
//DataPath = this.txtData.Text;
if (string.IsNullOrEmpty(TemplatePath) || string.IsNullOrEmpty(OutputPath) ||
string.IsNullOrEmpty(DataPath))
{
MessageBox.Show("Pelase enter");
return;
}
if (!Check())
{
MessageBox.Show("File not eixsted");
return;
}
ReadJsonTemplate();
NeedGenerateList = GetCheckedList();
ReplaceAll();
MessageBox.Show("完成");
}
private void ReplaceAll()
{
foreach (var instance in this.InstaceList)
{
ReplaceOne(instance);
}
}
private void ReplaceOne(List<KeyValue> instanceList)
{
var fileKey = "FileName";
var fileName = instanceList.FirstOrDefault(x => x.Key == fileKey).Value;
if (!this.NeedGenerateList.Contains(fileName))
return;
var path = $"{this.OutputPath}\\{fileName}.json";
var tempTemplate = this.JsonTemplate.Clone().ToString();
for (int i = 1; i < instanceList.Count; i++)
{
var item = instanceList[i];
var key = $"#{item.Key}#";
var value = item.Value;
tempTemplate = tempTemplate.Replace(key, value);
}
foreach (var item in this.CommonList)
{
var key = $"#{item.Key}#";
var value = item.Value;
tempTemplate = tempTemplate.Replace(key, value);
}
FileHelper.SaveFile(path, tempTemplate);
}
private void ReadJsonTemplate()
{
this.JsonTemplate = FileHelper.ReadFile(TemplatePath);
}
private void ReadDataDele()
{
var instanceSheetName = "Default";
var commonSheetName = "Common";
this.InstaceList = ExcelHelper.GetContentVertical(DataPath, instanceSheetName);
var temp = ExcelHelper.GetContent(DataPath, commonSheetName);
this.CommonList = new List<KeyValue>();
foreach (var item in temp)
{
var kv = new KeyValue();
kv.Key = item[0].Value;
kv.Value = item[1].Value;
CommonList.Add(kv);
}
ExcelHelper.KillProcess();
this.BeginInvoke(new DeleParaNone(ReadDataAction));
base.EndExecute(null);
}
private void ReadDataAction()
{
this.checkedListBox1.Items.Clear();;
foreach (var item in this.InstaceList)
{
this.checkedListBox1.Items.Add(item[0].Value);
}
}
private bool Check()
{
if (!FileHelper.ExistFile(TemplatePath) || !FileHelper.ExistFile(DataPath) ||
!FileHelper.ExistFolder(OutputPath))
{
return false;
}
return true;
}
private void btnSelectTemplate_Click(object sender, EventArgs e)
{
OpenFileDialog _dialog = new OpenFileDialog { Filter = "Json files|*.json|All files|*.*" };
if (_dialog.ShowDialog() != DialogResult.OK)
return;
this.txtTemplate.Text = _dialog.FileName;
}
private void btnSelectData_Click(object sender, EventArgs e)
{
OpenFileDialog _dialog = new OpenFileDialog { Filter = "Excel files|*.xls|All files|*.*" };
if (_dialog.ShowDialog() != DialogResult.OK)
return;
this.DataPath = _dialog.FileName;
base.PrepareExecute();
base.StartExecute(ReadDataDele);
}
private void btnSelectOutputFolder_Click(object sender, EventArgs e)
{
var fbd1 = new FolderBrowserDialog();
if (fbd1.ShowDialog() == DialogResult.OK)
{
var path = fbd1.SelectedPath;
this.txtOutput.Text = path;
}
}
private void frmGenerateJson_Load(object sender, EventArgs e)
{
}
private void btnSelectAll_Click(object sender, EventArgs e)
{
for (int i = 0; i < this.checkedListBox1.Items.Count; i++)
{
this.checkedListBox1.SetItemChecked(i, true);
}
}
private void btnRevert_Click(object sender, EventArgs e)
{
for (int i = 0; i < this.checkedListBox1.Items.Count; i++)
{
this.checkedListBox1.SetItemChecked(i, !this.checkedListBox1.GetItemChecked(i));
}
}
private void btnClose_Click(object sender, EventArgs e)
{
//ExcelHelper.GetContentVertical(@"D:\Json\JsonTemplate.xls", "Sheet1");
this.Close();
}
}
}