jt
2021-06-10 5d0d028456874576560552f5a5c4e8b801786f11
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using Newtonsoft.Json;
using System.Drawing.Printing;
using HH.WMS.Entitys.Basic;
using System.Linq;
using System.IO;
using HH.WMS.Client.Common;
using HH.WMS.Client.frm;
using HH.WMS.Common;
using DevComponents.DotNetBar.Controls;
using HH.WMS.Client.CustomControl;
 
namespace HH.WMS.Client
{
    public partial class frmTempletSet : DevComponents.DotNetBar.Office2007Form 
    {
        public frmTempletSet()
        {
            InitializeComponent();
        }
 
        private void frmTempletSet_Load(object sender, EventArgs e)
        {
            this.EnableGlass = false;
            LoadDS();
        }
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        
        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmAddTemplet from = new frmAddTemplet();
            if (from.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                LoadDS();
            }
        }
 
        private void LoadDS()
        {
            dgvlist.Rows.Clear();
            try
            {
                List<string> PrintDataSource = new List<string>();
                PrintDataSource.Add("");
                foreach (string sPrint in PrinterSettings.InstalledPrinters)//»ñÈ¡ËùÓдòÓ¡»úÃû³Æ
                {
                    PrintDataSource.Add(sPrint);
                }
 
                string str = WebApiManager.HttpWMS_Get("/api/PrintTemplet/GetPrintTemplet?mac=" + "0000");//StaticUtil.Mac);
                OperateResult results = JsonConvert.DeserializeObject<OperateResult>(str);
                if (!results.Success)
                {
                    MessageBox.Show(results.Msg);
                    return;
                }
                List<TN_WM_PRINT_TEMPLETEntity> v = JsonConvert.DeserializeObject<List<TN_WM_PRINT_TEMPLETEntity>>(results.Data.ToString());
 
                foreach (TN_WM_PRINT_TEMPLETEntity m in v)
                {
                    DataGridViewRow row = new DataGridViewRow();
                    row.Tag = m.CN_GUID;
 
                    DataGridViewTextBoxCell cell0 = new DataGridViewTextBoxCell();
                    cell0.Value = m.CN_GUID;
                    row.Cells.Add(cell0);
 
                    DataGridViewTextBoxCell cell1 = new DataGridViewTextBoxCell();
                    cell1.Value = m.CN_S_TEMPLET_TYPE;
                    row.Cells.Add(cell1);
 
                    DataGridViewTextBoxCell cell2 = new DataGridViewTextBoxCell();
                    cell2.Value = m.CN_S_TEMPLET_NAME;
                    cell2.Tag = m.CN_S_TEMPLET_NAME;
                    row.Cells.Add(cell2);
 
                    DataGridViewComboBoxCell cell3 = new DataGridViewComboBoxCell();
                    cell3.DataSource = PrintDataSource;
                    m.CN_S_PRINTER = m.CN_S_PRINTER.Replace('!', '\\');
                    cell3.Tag = m.CN_S_PRINTER;
                    if (PrintDataSource.IndexOf(m.CN_S_PRINTER) >= 0)
                        cell3.Value = m.CN_S_PRINTER;
                    else
                    {
                    //    if (!m.CN_S_MAC.Equals(StaticUtil.Mac))
                    //        cell3.Value = m.CN_S_PRINTER;
                    }
 
                    row.Cells.Add(cell3);
 
                    DataGridViewTextBoxCell cell5 = new DataGridViewTextBoxCell();
                    cell5.Value = string.IsNullOrEmpty(m.CN_S_MAC) ? StaticUtil.Mac : m.CN_S_MAC;
                    if (string.IsNullOrEmpty(m.CN_S_MAC))
                        cell5.Style.ForeColor = Color.Red;
                    row.Cells.Add(cell5);
 
 
                    DataGridViewTextBoxCell cell16 = new DataGridViewTextBoxCell();
                    cell16.Value = m.CN_T_UPLOAD_TIME;
                    row.Cells.Add(cell16);
 
                    DataGridViewTextBoxCell cell17 = new DataGridViewTextBoxCell();
                    cell17.Value = m.CN_S_UPLOADOR == null ? "" : m.CN_S_UPLOADOR.Trim();
                    row.Cells.Add(cell17);
                    
                    DataGridViewTextBoxCell cell171 = new DataGridViewTextBoxCell();
                    cell171.Value = m.CN_C_IS_FIRST;
                    row.Cells.Add(cell171);
                    
                    DataGridViewTextBoxCell cell172 = new DataGridViewTextBoxCell();
                    cell172.Value = m.CN_C_IS_TOOL_PRINT;
                    row.Cells.Add(cell172);
 
 
                    dgvlist.Rows.Add(row);
 
                    if (m.CN_C_IS_UPLOADED != "Y")
                    {
                        //δÉÏ´«
                        ((DataGridViewDisableButtonXCell)row.Cells["colDown"]).Enabled = false;
                    }
 
                    if (IsExist(m.CN_S_TEMPLET_NAME))
                    {
                        //±¾µØ´æÔÚÄ£°å
                        row.DefaultCellStyle.BackColor = Color.FromArgb(154, 255, 154);
                    }
                    else
                    {
                        row.DefaultCellStyle.BackColor = Color.FromArgb(255, 0, 0);
                        ((DataGridViewDisableButtonXCell)row.Cells["colUp"]).Enabled = false;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBoxEx.Show(ex.Message);
            }
        }
 
        private bool IsExist(string fileName)
        {
            
            string path = Application.StartupPath+@"\ActionReport\Template\" + fileName;
 
            if (!File.Exists(path))
            {
                return false;
            }
            return true;
        }
 
        public delegate void DesignDelegate(string path);
        public event DesignDelegate DesignEvent;
        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (1 != dgvlist.SelectedRows.Count)
            {
                MessageBoxEx.Show("ÇëÑ¡ÔñÒ»¸ö±à¼­µÄÄ£°å"); return;
            }
            string name = dgvlist.SelectedRows[0].Cells["CN_S_TEMPLET_NAME"].Value.ToString().Trim();
            string path =Application.StartupPath+"\\ActionReport\\Template\\" + name;
 
            if (!File.Exists(path))
            {
                MessageBoxEx.Show("¸ÃÄ£°å²»´æÔÚ£¡"); return;
            }
            EndUserDesigner designer = new EndUserDesigner();
            DesignEvent += designer.OnDesign;
            DesignEvent(path);
            designer.ShowDialog();
        }
        
        #region ÖØÃüÃû
        /// <summary>
        /// ÖØÃüÃû
        /// </summary>
        /// <param name="dirPath">Îļþ¼Ð</param>
        /// <param name="oldName">ÎļþÃû</param>
        /// <param name="newName">ÐÂÎļþÃû</param>
        /// <returns></returns>
        private OperateResult ReName(string dirPath, string oldName,string newName)
        {
            try
            {
                DirectoryInfo directoryinfo = new DirectoryInfo(dirPath);
                string newPath = dirPath + "\\" + newName;
                if (File.Exists(newPath))
                {
                    return OperateResult.Error("ÒÑ´æÔÚ¸ÃÎļþÃû£¡");
                }
                FileInfo[] files = directoryinfo.GetFiles();
                List<FileInfo> fi = files.Where(x => x.Name.Equals(oldName)).ToList();
                if (fi.Count>0)
                {
                    fi[0].MoveTo(newPath);
                    return OperateResult.Succeed();
                }
                else
                    return OperateResult.Error("Ô­Îļþ²»´æÔÚ£¡");
            }
            catch (Exception ex)
            {
                return OperateResult.Error(ex.Message);
            }
        }
        #endregion
 
        private void btnDeleteClick(object sender, EventArgs e)
        {
            if (1 != dgvlist.SelectedRows.Count)
            {
                MessageBoxEx.Show("ÇëÑ¡ÔñÒ»¸ö±à¼­µÄÄ£°å"); return;
            }
            if (dgvlist.SelectedRows[0].Cells["CN_S_MAC"].Style.ForeColor == Color.Red)
            {
                MessageBoxEx.Show("µ±Ç°Ä£°å²»ÊôÓÚ±¾»úÄ£°å£¬²»ÔÊÐíɾ³ý£¡"); return;
            }
 
            DialogResult dr = MessageBoxEx.Show("È·ÈÏɾ³ý¸ÃÄ£°åô£¡", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
            if (dr != System.Windows.Forms.DialogResult.Yes)
            {
                return;
            }
            string name = dgvlist.SelectedRows[0].Cells["CN_S_TEMPLET_NAME"].Value.ToString().Trim();
            string path =Application.StartupPath+"\\ActionReport\\Template\\" + name;
            if (File.Exists(path))
            {
                File.Delete(path);
            }
 
            string guid = dgvlist.SelectedRows[0].Tag.ToString();
 
            string str = WebApiManager.HttpWMS_Get("/api/PrintTemplet/DeleteTemplet?guid=" + guid);
            OperateResult or = JsonConvert.DeserializeObject<OperateResult>(str);
            if (or.Status != ResultStatus.Success)
                MessageBoxEx.Show(or.Msg);
            else
                dgvlist.Rows.Remove(dgvlist.SelectedRows[0]);
        }
 
        #region ÉÏ´«Ä£°å
        /// <summary>
        /// ÉÏ´«Ä£°å
        /// </summary>
        /// <param name="guid"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        private bool Upload(string guid,string fileName)
        {
            //if (1 != dgvlist.SelectedRows.Count)
            //{
            //    MessageBoxEx.Show("ÇëÑ¡ÔñÒ»¸öÐèÒªÉÏ´«µÄÄ£°å"); return;
            //}
            //if (dgvlist.SelectedRows[0].Cells["CN_S_MAC"].Style.ForeColor == Color.Red)
            //{
            //    MessageBoxEx.Show("µ±Ç°Ä£°å²»ÊôÓÚ±¾»úÄ£°å£¬ÎÞ·¨ÉÏ´«£¡"); return;
            //}
 
            //string name = dgvlist.SelectedRows[0].Cells["CN_S_TEMPLET_NAME"].Value.ToString().Trim();
            string path =Application.StartupPath+"\\ActionReport\\Template\\" + fileName;
 
            StringBuilder sb = new StringBuilder();
            FileStream fs = new FileStream(path, FileMode.Open); 
 
            byte[] buffer = new byte[fs.Length];
            fs.Read(buffer, 0, buffer.Length);
            var requstData = new
            {
                guid=guid,
                file = buffer,
                tokenId = StaticUtil.TokenId,
                //path = "resultPath",
                fileName = fileName
            };
 
            fs.Close();
            fs.Dispose();
            var result = HttpPostRequst(StaticUtil.ServiceUrl + "/api/PrintTemplet/UploadTemplet", JsonConvert.SerializeObject(requstData), "");
            OperateResult or = JsonConvert.DeserializeObject<OperateResult>(result);
            if (or.Status == ResultStatus.Success)
            {
                return true;
            }
            else
            {
                MessageBoxEx.Show(or.Msg);
                return false;
            }
        }
        #endregion
 
        public static string HttpPostRequst(string url, string postData, string ActionUrl)
        {
            try
            {
                byte[] bs = Encoding.UTF8.GetBytes(postData);
 
                System.Net.HttpWebRequest result = (System.Net.HttpWebRequest)System.Net.WebRequest.Create("http://" + url + ActionUrl);
                result.ContentType = "application/json";
                result.ContentLength = bs.Length;
                result.Method = "POST";
 
                using (Stream reqStream = result.GetRequestStream())
                {
                    reqStream.Write(bs, 0, bs.Length);
                }
                using (System.Net.WebResponse wr = result.GetResponse())
                {
                    string reader = new StreamReader(wr.GetResponseStream(),
                        Encoding.UTF8).ReadToEnd();
                    if (!string.IsNullOrEmpty(reader))
                    {
                        reader = reader.Replace("\\", "");
                        reader = reader.TrimEnd('\"');
                        reader = reader.TrimStart('\"');
                    }
                    return reader;
                }
            }
            catch (Exception ex)
            {
                return JsonConvert.SerializeObject(OperateResult.Error(ex.Message));
            }
        }
 
        #region ÏÂÔØÄ£°åµ½±¾µØ
        /// <summary>
        /// ÏÂÔØÄ£°åµ½±¾µØ
        /// </summary>
        /// <param name="fileName">Ä£°åÎļþÃû</param>
        /// <returns></returns>
        private bool Down(string fileName)
        {
            try
            {
                System.Windows.Forms.ProgressBar prog = new ProgressBar();
                System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create("http://" + StaticUtil.ServiceUrl + "/api/PrintTemplet/DownLoadTemplet?fileName=" + fileName);
                System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse();
                long totalBytes = myrp.ContentLength;
 
                if (prog != null)
                {
                    prog.Maximum = (int)totalBytes;
                }
 
                System.IO.Stream st = myrp.GetResponseStream();
                System.IO.Stream so = new System.IO.FileStream(Application.StartupPath+"\\ActionReport\\Template\\" + fileName, System.IO.FileMode.Create);
 
                byte[] by = new byte[20480];
                int osize = st.Read(by, 0, (int)by.Length);
                while (osize > 0)
                {
                    //System.Windows.Forms.Application.DoEvents();
                    so.Write(by, 0, osize);
                    osize = st.Read(by, 0, (int)by.Length);
                }
                so.Close();
                st.Close();
                return true;
            }
            catch (Exception ex)
            {
                MessageBoxEx.Show(ex.Message);
                return false;
            }
        }
 
        #endregion
        private void dgvlist_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
            {
                if (this.dgvlist.Columns[e.ColumnIndex].HeaderText == "ÉÏ´«")
                {
                    string fileName = dgvlist.Rows[e.RowIndex].Cells["CN_S_TEMPLET_NAME"].Value.ToString().Trim();
 
                    string guid = dgvlist.Rows[e.RowIndex].Cells["CN_GUID"].Value.ToString().Trim();
                    if (Upload(guid, fileName))
                    {
                        DataGridViewRow row = dgvlist.Rows[e.RowIndex];
                        ((DataGridViewDisableButtonXCell)row.Cells["colDown"]).Enabled = true;
                        MessageBoxEx.Show("ÉÏ´«³É¹¦£¡");
                    }
                }
                else if (this.dgvlist.Columns[e.ColumnIndex].HeaderText == "ÏÂÔØ")
                {
                    string fileName = dgvlist.Rows[e.RowIndex].Cells["CN_S_TEMPLET_NAME"].Value.ToString().Trim();
                    if (Down(fileName))
                    {
                        DataGridViewRow row = dgvlist.Rows[e.RowIndex];
                        row.DefaultCellStyle.BackColor = Color.FromArgb(154, 255, 154);
                        ((DataGridViewDisableButtonXCell)row.Cells["colUp"]).Enabled = true;
                        MessageBoxEx.Show("ÏÂÔØ³É¹¦£¡");
                    }
                }
            }
        }
 
        private void dgvlist_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (e.ColumnIndex == 2)
                {
                    #region ÐÞ¸ÄÄ£°åÃû³Æ
                    DataGridViewCell cell = dgvlist.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    string guid = dgvlist.Rows[e.RowIndex].Tag.ToString();
 
                    string oldValue = cell.Tag.ToString();
                    string value = cell.Value.ToString();
                    if (oldValue == value)
                        return;
                    if (dgvlist.Rows[e.RowIndex].Cells["CN_S_MAC"].Style.ForeColor == Color.Red)
                    {
                        MessageBoxEx.Show("µ±Ç°Ä£°å²»ÊôÓÚ±¾»úÄ£°å£¬ÎÞ·¨ÐÞ¸ÄÃû³Æ£¡");
                        dgvlist.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = dgvlist.Rows[e.RowIndex].Cells[e.ColumnIndex].Tag.ToString();
                        return;
                    }
 
                    if (!value.EndsWith(".rdlx"))
                    {
                        value += ".rdlx";
                    }
 
                    string path = Application.StartupPath + @"\ActionReport\Template\";
                    OperateResult or = ReName(path, oldValue, value);
                    if (or.Status == ResultStatus.Success)
                    {
                        string str = WebApiManager.HttpWMS_Get("/api/PrintTemplet/UpdateTempletName?guid=" + guid
                               + "&fileName=" + value);
                        or = JsonConvert.DeserializeObject<OperateResult>(str);
                        if (or.Status != ResultStatus.Success)
                            MessageBox.Show(or.Msg);
                        else
                            dgvlist.Rows[e.RowIndex].Cells[e.ColumnIndex].Tag = value;
                    }
                    else
                    {
                        MessageBoxEx.Show(or.Msg);
                    }
                    #endregion
                }
                else if (e.ColumnIndex == 3)
                {
                    #region ¸ü¸Ä´òÓ¡»ú
                    DataGridViewCell cell = dgvlist.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    string guid = dgvlist.Rows[e.RowIndex].Tag.ToString();
 
                    string oldValue = cell.Tag.ToString();
                    string value = cell.Value.ToString();
                    if (oldValue == value)
                        return;
                    //if (dgvlist.Rows[e.RowIndex].Cells["CN_S_MAC"].Style.ForeColor == Color.Red)
                    //{
                    //    MessageBox.Show("µ±Ç°Ä£°å²»ÊôÓÚ±¾»úÄ£°å£¬ÎÞ·¨Ð޸ĴòÓ¡»ú£¡");
                    //    dgvlist.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = dgvlist.Rows[e.RowIndex].Cells[e.ColumnIndex].Tag.ToString();
                    //    return;
                    //}
                    string isFirst = dgvlist.Rows[e.RowIndex].Cells["CN_C_IS_FIRST"].Value.ToString().Trim();
                    string toolPrint = dgvlist.Rows[e.RowIndex].Cells["CN_C_IS_TOOL_PRINT"].Value.ToString().Trim();
                    string mac = dgvlist.Rows[e.RowIndex].Cells["CN_S_MAC"].Value.ToString().Trim();
 
                    string str = "";
 
                    TN_WM_PRINT_TEMPLETEntity m = new TN_WM_PRINT_TEMPLETEntity() {TokenId =StaticUtil.TokenId,CN_GUID = guid, CN_S_PRINTER = value };
                    if (toolPrint.Equals("N") || StaticUtil.Mac == mac)
                    {
                        //²»Êǹ¤¾ß´òÓ¡µÄÖ»ÁôÒ»·ÝÄ£°å
                        str = WebApiManager.HttpWMS_Post("/api/PrintTemplet/UpdateTempletPrinter",JsonConvert.SerializeObject(m));
                    }
                    else
                    {
                        m.CN_S_MAC = StaticUtil.Mac;
                        str = WebApiManager.HttpWMS_Post("/api/PrintTemplet/CreateTempletPrinter", JsonConvert.SerializeObject(m));
                    }
                    OperateResult or = JsonConvert.DeserializeObject<OperateResult>(str);
                    if (or.Status != ResultStatus.Success)
                        MessageBox.Show(or.Msg);
                    else
                        dgvlist.Rows[e.RowIndex].Cells[e.ColumnIndex].Tag = value;
                    #endregion
                }
            }
            catch (Exception ex)
            {
                MessageBoxEx.Show(ex.Message);
            }
        }
 
        private void dgvlist_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
 
        }
 
    }
}