新框架PC后端代码(祈禧6月初版本)
南骏 池
3 天以前 72449a1b8699b65712e57fba8abce5a8240e9465
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Dynamic;
using Gs.Toolbox;
using Gs.Toolbox.ApiCore.Abstract.Mvc;
using Gs.Toolbox.ApiCore.Common.Mvc;
using Gs.Toolbox.ApiCore.Group;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using static Gs.Toolbox.UtilityHelper;
 
 
namespace Gs.Report;
 
[ApiGroup(ApiGroupNames.Report)]
public class XlsInOutController : IRomteService
{
    private readonly IHttpContextAccessor _http;
    private readonly string _userCode, _userGuid, _orgFids;
 
    public XlsInOutController(IHttpContextAccessor httpContextAccessor)
    {
        _http = httpContextAccessor;
        (_userCode, _userGuid, _orgFids) =
            GetUserGuidAndOrgGuid(_http);
    }
 
    /// <summary>
    /// 数据导出,需要存储过程支撑,目前没用到
    /// </summary>
    /// <param name="mode"></param>
    /// <returns></returns>
    [RequestMethod(RequestMethods.POST)]
    public ReturnDto<ExpandoObject> XlsOutView([FromBody] dynamic mode)
    {
        string rptParameter = mode.outParameter;
        rptParameter = _rptGetParameterName(rptParameter);
        string outWhere = mode.outWhere;
        string outSortName = mode.outSortName;
        string outSortOrder = mode.outSortOrder;
        var _pdfFloder = AppSettingsHelper.getValueByKey("DownPath");
        var _pdfName = Guid.NewGuid() + ".xls";
        var _pdfSaveFolder = Path.Combine(AppContext.BaseDirectory, _pdfFloder);
        if (!Directory.Exists(_pdfSaveFolder))
            Directory.CreateDirectory(_pdfSaveFolder);
        var pdfSavePath = Path.Combine(_pdfSaveFolder, _pdfName);
        dynamic m = new ExpandoObject();
        //读数据
        var dset = new DataSet();
        try
        {
            using (var conn = new SqlConnection(DbHelperSQL.strConn))
            {
                conn.Open();
                using (var comm = new SqlCommand(rptParameter, conn))
                {
                    comm.CommandType = CommandType.StoredProcedure;
                    SqlParameter[] parameters =
                    {
                        new("@pi1", 1)
                    };
                    parameters[0].Value = 0;
                    foreach (var parameter in parameters)
                        comm.Parameters.Add(parameter);
                    using (var dt = new SqlDataAdapter(comm))
                    {
                        dt.Fill(dset, "0");
                    }
                }
 
                conn.Close();
            }
 
            if (dset == null || dset.Tables.Count <= 0)
                return ReturnDto<dynamic>.QuickReturn(m, ReturnCode.Exception,
                    "没有查询到任何数据");
            var ary = new ArrayList();
            ExcelHelper.ExportAryHead(dset.Tables[0], ary, pdfSavePath);
            m.fileUrl = "down/" + _pdfName;
            return ReturnDto<dynamic>.QuickReturn(m, ReturnCode.Success,
                "读取成功!");
        }
        catch (Exception ex)
        {
            LogHelper.Debug(ToString(), ex.Message);
            return ReturnDto<dynamic>.QuickReturn(m, ReturnCode.Exception,
                "读取失败," + ex.Message);
        }
    }
 
    /// <summary>
    ///     根据参数读取存储过程名称
    /// </summary>
    /// <returns></returns>
    private string _rptGetParameterName(string str)
    {
        var _ary = str.Split('{');
        if (_ary.Length > 0)
        {
            var rptParameter = _ary[0];
            return rptParameter;
        }
 
        return str;
    }
 
 
    #region 各种导出
 
    /// <summary>
    /// 导出首检
    /// </summary>
    /// <param name="mode"></param>
    /// <returns></returns>
    [RequestMethod(RequestMethods.POST)]
    public ReturnDto<ExpandoObject> XlsOutShouJian([FromBody] dynamic mode)
    {
        string guid = mode.guid;
        var _pdfFloder = AppSettingsHelper.getValueByKey("DownPath");
        var _pdfName = Guid.NewGuid() + ".xls";
        var _pdfSaveFolder = Path.Combine(AppContext.BaseDirectory, _pdfFloder);
        if (!Directory.Exists(_pdfSaveFolder))
            Directory.CreateDirectory(_pdfSaveFolder);
        var pdfSavePath = Path.Combine(_pdfSaveFolder, _pdfName);
        dynamic m = new ExpandoObject();
        //读数据
        var dset = new DataSet();
        SqlParameter[] parameters ={
            new("@inMainGuid", guid),
            new("@inP1", ""),
            new("@inP2", ""),
            new("@inP3", ""),
            new("@inP4", "")};
        try
        {
            dset = DbHelperSQL.RunProcedure("[xlsOutShouJian]", parameters, "0");
            if (dset == null || dset.Tables.Count <= 0)
                return ReturnDto<dynamic>.QuickReturn(m, ReturnCode.Exception,
                    "没有查询到任何数据");
            var ary = new ArrayList();
            ExcelHelper.ExportShouJian(dset, pdfSavePath);
            m.fileUrl = "down/" + _pdfName;
            return ReturnDto<dynamic>.QuickReturn(m, ReturnCode.Success,
                "读取成功!");
        }
        catch (Exception ex)
        {
            LogHelper.Debug(ToString(), ex.Message);
            return ReturnDto<dynamic>.QuickReturn(m, ReturnCode.Exception,
                "读取失败," + ex.Message);
        }
    }
 
    /// <summary>
    /// 导出iqc检
    /// </summary>
    /// <param name="mode"></param>
    /// <returns></returns>
    [RequestMethod(RequestMethods.POST)]
    public ReturnDto<ExpandoObject> XlsOutIqc([FromBody] dynamic mode)
    {
        string guid = mode.guid;
        var _pdfFloder = AppSettingsHelper.getValueByKey("DownPath");
        var _pdfName = Guid.NewGuid() + ".xls";
        var _pdfSaveFolder = Path.Combine(AppContext.BaseDirectory, _pdfFloder);
        if (!Directory.Exists(_pdfSaveFolder))
            Directory.CreateDirectory(_pdfSaveFolder);
        var pdfSavePath = Path.Combine(_pdfSaveFolder, _pdfName);
        dynamic m = new ExpandoObject();
        //读数据
        var dset = new DataSet();
        SqlParameter[] parameters ={
            new("@inMainGuid", guid),
            new("@inP1", ""),
            new("@inP2", ""),
            new("@inP3", ""),
            new("@inP4", "")};
        try
        {
            dset = DbHelperSQL.RunProcedure("[xlsOutIqc]", parameters, "0");
            if (dset == null || dset.Tables.Count <= 0)
                return ReturnDto<dynamic>.QuickReturn(m, ReturnCode.Exception,
                    "没有查询到任何数据");
            var ary = new ArrayList();
            ExcelHelper.ExportIqc(dset, pdfSavePath);
            m.fileUrl = "down/" + _pdfName;
            return ReturnDto<dynamic>.QuickReturn(m, ReturnCode.Success,
                "读取成功!");
        }
        catch (Exception ex)
        {
            LogHelper.Debug(ToString(), ex.Message);
            return ReturnDto<dynamic>.QuickReturn(m, ReturnCode.Exception,
                "读取失败," + ex.Message);
        }
    }
 
 
    /// <summary>
    /// 导出巡检
    /// </summary>
    /// <param name="mode"></param>
    /// <returns></returns>
    [RequestMethod(RequestMethods.POST)]
    public ReturnDto<ExpandoObject> XlsOutXunJian([FromBody] dynamic mode)
    {
        string begDate = mode.begDate;
        var _pdfFloder = AppSettingsHelper.getValueByKey("DownPath");
        var _pdfName = Guid.NewGuid() + ".xls";
        var _pdfSaveFolder = Path.Combine(AppContext.BaseDirectory, _pdfFloder);
        if (!Directory.Exists(_pdfSaveFolder))
            Directory.CreateDirectory(_pdfSaveFolder);
        var pdfSavePath = Path.Combine(_pdfSaveFolder, _pdfName);
        dynamic m = new ExpandoObject();
        //读数据
        var dset = new DataSet();
        SqlParameter[] parameters ={
            new("@begDate", begDate),
            new("@inP1", ""),
            new("@inP2", ""),
            new("@inP3", ""),
            new("@inP4", "")};
        try
        {
            dset = DbHelperSQL.RunProcedure("[xlsOutXunJian]", parameters, "0");
            if (dset == null || dset.Tables.Count <= 0)
                return ReturnDto<dynamic>.QuickReturn(m, ReturnCode.Exception,
                    "没有查询到任何数据");
            var ary = new ArrayList();
            ExcelHelper.ExportXunJian(dset, pdfSavePath);
            m.fileUrl = "down/" + _pdfName;
            return ReturnDto<dynamic>.QuickReturn(m, ReturnCode.Success,
                "读取成功!");
        }
        catch (Exception ex)
        {
            LogHelper.Debug(ToString(), ex.Message);
            return ReturnDto<dynamic>.QuickReturn(m, ReturnCode.Exception,
                "读取失败," + ex.Message);
        }
    }
 
    #endregion
 
 
    #region  各种导入
    /// <summary>
    /// 检验工具导入
    /// </summary>
    /// <param name="tmpGuid"></param>
    /// <returns></returns>
    [RequestMethod(RequestMethods.POST)]
    public ReturnDto<int?> XlsInJygj([FromBody] dynamic mode)
    {
        Dictionary<string, string> dic = new Dictionary<string, string>
        {
            { "名称", "t1" },
        };
        string tmpGuid = mode.tmpGuid;
        int? rtnInt = (int)ReturnCode.Default;
        int? _it = 0;//返回的行数
        string _msg = "";//返回的信息
        (_it, _msg) = getTable(tmpGuid, dic);
        if (_it <= 0)
            return ReturnDto<int>.QuickReturn(rtnInt, ReturnCode.Exception, _msg);
        System.Text.StringBuilder sbMsg = new System.Text.StringBuilder();
        using (SqlConnection conn = new SqlConnection(DbHelperSQL.strConn))
        {
            using (SqlCommand cmd = new SqlCommand("[xlsInJygj]", conn))
            {
                try
                {
                    conn.Open();
                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlParameter[] parameters = new SqlParameter[] {
                                new SqlParameter("@outGuid",SqlDbType.NVarChar,100),
                                new SqlParameter("@outMsg",SqlDbType.NVarChar,300),
                                new SqlParameter("@inEdtUserGuid",_userGuid),
                                new SqlParameter("@tmpGuid",tmpGuid),
                            };
                    parameters[0].Direction = ParameterDirection.Output;
                    parameters[1].Direction = ParameterDirection.Output;
                    foreach (SqlParameter parameter in parameters)
                    {
                        cmd.Parameters.Add(parameter);
                    }
                    rtnInt = cmd.ExecuteNonQuery();
                    sbMsg.Append(parameters[1].Value.ToString());
                }
                catch (Exception ex)
                {
                    rtnInt = -1;
                    sbMsg.Append("操作失败:" + ex.Message);
                }
                finally
                {
                    conn.Close();
                }
            }
        }
        if (rtnInt <= 0)
            return ReturnDto<int>.QuickReturn(rtnInt, ReturnCode.Exception, sbMsg.ToString());
        return ReturnDto<int>.QuickReturn(rtnInt, ReturnCode.Success, sbMsg.ToString());
    }
 
 
    /// <summary>
    /// 检验项目导入
    /// </summary>
    /// <param name="tmpGuid"></param>
    /// <returns></returns>
    [RequestMethod(RequestMethods.POST)]
    public ReturnDto<int?> XlsInJyxm([FromBody] dynamic mode)
    {
        Dictionary<string, string> dic = new Dictionary<string, string>
        {
            { "名称", "t1" },
        };
        string tmpGuid = mode.tmpGuid;
        int? rtnInt = (int)ReturnCode.Default;
        int? _it = 0;//返回的行数
        string _msg = "";//返回的信息
        (_it, _msg) = getTable(tmpGuid, dic);
        if (_it <= 0)
            return ReturnDto<int>.QuickReturn(rtnInt, ReturnCode.Exception, _msg);
        System.Text.StringBuilder sbMsg = new System.Text.StringBuilder();
        using (SqlConnection conn = new SqlConnection(DbHelperSQL.strConn))
        {
            using (SqlCommand cmd = new SqlCommand("[xlsInJyxm]", conn))
            {
                try
                {
                    conn.Open();
                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlParameter[] parameters = new SqlParameter[] {
                                new SqlParameter("@outGuid",SqlDbType.NVarChar,100),
                                new SqlParameter("@outMsg",SqlDbType.NVarChar,300),
                                new SqlParameter("@inEdtUserGuid",_userGuid),
                                new SqlParameter("@tmpGuid",tmpGuid),
                            };
                    parameters[0].Direction = ParameterDirection.Output;
                    parameters[1].Direction = ParameterDirection.Output;
                    foreach (SqlParameter parameter in parameters)
                    {
                        cmd.Parameters.Add(parameter);
                    }
                    rtnInt = cmd.ExecuteNonQuery();
                    sbMsg.Append(parameters[1].Value.ToString());
                }
                catch (Exception ex)
                {
                    rtnInt = -1;
                    sbMsg.Append("操作失败:" + ex.Message);
                }
                finally
                {
                    conn.Close();
                }
            }
        }
        if (rtnInt <= 0)
            return ReturnDto<int>.QuickReturn(rtnInt, ReturnCode.Exception, sbMsg.ToString());
        return ReturnDto<int>.QuickReturn(rtnInt, ReturnCode.Success, sbMsg.ToString());
    }
 
    /// <summary>
    /// 工单后盖码导入
    /// </summary>
    /// <param name="tmpGuid"></param>
    /// <returns></returns>
    [RequestMethod(RequestMethods.POST)]
    public ReturnDto<int?> XlsInDaaHgm([FromBody] dynamic mode)
    {
        Dictionary<string, string> dic = new Dictionary<string, string>
        {
           { "工单编号", "t1" },
            { "条码", "t2" },
        };
        string tmpGuid = mode.tmpGuid;
        int? rtnInt = (int)ReturnCode.Default;
        int? _it = 0;//返回的行数
        string _msg = "";//返回的信息
        (_it, _msg) = getTable(tmpGuid, dic);
        if (_it <= 0)
            return ReturnDto<int>.QuickReturn(rtnInt, ReturnCode.Exception, _msg);
        System.Text.StringBuilder sbMsg = new System.Text.StringBuilder();
        using (SqlConnection conn = new SqlConnection(DbHelperSQL.strConn))
        {
            using (SqlCommand cmd = new SqlCommand("[xlsInDaaHgm]", conn))
            {
                try
                {
                    conn.Open();
                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlParameter[] parameters = new SqlParameter[] {
                                new SqlParameter("@outGuid",SqlDbType.NVarChar,100),
                                new SqlParameter("@outMsg",SqlDbType.NVarChar,300),
                                new SqlParameter("@inEdtUserGuid",_userGuid),
                                new SqlParameter("@tmpGuid",tmpGuid),
                            };
                    parameters[0].Direction = ParameterDirection.Output;
                    parameters[1].Direction = ParameterDirection.Output;
                    foreach (SqlParameter parameter in parameters)
                    {
                        cmd.Parameters.Add(parameter);
                    }
                    rtnInt = cmd.ExecuteNonQuery();
                    sbMsg.Append(parameters[1].Value.ToString());
                }
                catch (Exception ex)
                {
                    rtnInt = -1;
                    sbMsg.Append("操作失败:" + ex.Message);
                }
                finally
                {
                    conn.Close();
                }
            }
        }
        if (rtnInt <= 0)
            return ReturnDto<int>.QuickReturn(rtnInt, ReturnCode.Exception, sbMsg.ToString());
        return ReturnDto<int>.QuickReturn(rtnInt, ReturnCode.Success, sbMsg.ToString());
    }
 
 
    /// <summary>
    /// 物料检验项目导入
    /// </summary>
    /// <param name="tmpGuid"></param>
    /// <returns></returns>
    [RequestMethod(RequestMethods.POST)]
    public ReturnDto<int?> XlsInItemJyxm([FromBody] dynamic mode)
    {
        Dictionary<string, string> dic = new Dictionary<string, string>
        {
            { "使用组织编号", "t1" },
            { "物料编号", "t2" },
            { "检验项目", "t3" },
            { "检验工具", "t4" },
            { "上限", "t5" },
            { "下限", "t6" },
            { "标准值", "t7" },
            { "标准编码", "t8" },
            { "检验水平", "t9" },
            { "接受水平", "t10" },
            { "检验要求", "t11" },
        };
        string tmpGuid = mode.tmpGuid;
        string strType = mode.strType;//用于判断iqc,ipqc首检,ipqc巡检,fqc
        int? rtnInt = (int)ReturnCode.Default;
        int? _it = 0;//返回的行数
        string _msg = "";//返回的信息
        (_it, _msg) = getTable(tmpGuid, dic);
        if (_it <= 0)
            return ReturnDto<int>.QuickReturn(rtnInt, ReturnCode.Exception, _msg);
        System.Text.StringBuilder sbMsg = new System.Text.StringBuilder();
        using (SqlConnection conn = new SqlConnection(DbHelperSQL.strConn))
        {
            using (SqlCommand cmd = new SqlCommand("[xlsInItemJyxm]", conn))
            {
                try
                {
                    conn.Open();
                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlParameter[] parameters = new SqlParameter[] {
                                new SqlParameter("@outGuid",SqlDbType.NVarChar,100),
                                new SqlParameter("@outMsg",SqlDbType.NVarChar,300),
                                new SqlParameter("@inEdtUserGuid",_userGuid),
                                new SqlParameter("@tmpGuid",tmpGuid),
                                new SqlParameter("@strType",strType),
                            };
                    parameters[0].Direction = ParameterDirection.Output;
                    parameters[1].Direction = ParameterDirection.Output;
                    foreach (SqlParameter parameter in parameters)
                    {
                        cmd.Parameters.Add(parameter);
                    }
                    rtnInt = cmd.ExecuteNonQuery();
                    sbMsg.Append(parameters[1].Value.ToString());
                }
                catch (Exception ex)
                {
                    rtnInt = -1;
                    sbMsg.Append("操作失败:" + ex.Message);
                }
                finally
                {
                    conn.Close();
                }
            }
        }
        if (rtnInt <= 0)
            return ReturnDto<int>.QuickReturn(rtnInt, ReturnCode.Exception, sbMsg.ToString());
        return ReturnDto<int>.QuickReturn(rtnInt, ReturnCode.Success, sbMsg.ToString());
    }
 
 
    /// <summary>
    /// 返回dset
    /// </summary>
    /// <param name="tmpGuid"></param>
    /// <returns></returns>
    private static (int?, string?) getTable(string tmpGuid, Dictionary<string, string> dic)
    {
        var _it = 0;
        var _msg = "";
        System.Text.StringBuilder sbSql = new System.Text.StringBuilder();
        sbSql.Append("select top 1 url_Path from [dbo].[MES_FILE] where parent_Guid='" + tmpGuid + "'");
        string files = "";
        try
        {
            files = DbHelperSQL.GetSingle(sbSql.ToString()).ToString();
        }
        catch (Exception ex)
        {
            _it = 0;
            _msg = "操作失败:读取文件时发生错误:" + ex.Message;
        }
 
        if (string.IsNullOrEmpty(files))
        {
            _it = 0;
            _msg = "操作失败:请先上传文件";
        }
        var savePath = AppContext.BaseDirectory +
                       AppSettingsHelper.getValueByKey("UploadPath") + "/" + files;
        System.Data.DataTable dt = new DataTable();
        try
        {
            dt = ExcelHelper.ExcelToTable(savePath);
        }
        catch (Exception ex)
        {
            _it = 0;
            _msg = "操作失败,ExcelToTable读取上传文件发生错误:" + ex.Message;
        }
        dt.Columns.Add("tmpGuid", Type.GetType("System.Guid"));
        dt.Columns.Add("tmpIdx", Type.GetType("System.String"));
        for (int r = 0; r < dt.Rows.Count; r++)
        {
            dt.Rows[r]["tmpGuid"] = Guid.Parse(tmpGuid);
            dt.Rows[r]["tmpIdx"] = r + 3;
        }
        bool flag = false;
        using (SqlConnection destinationConnection = new SqlConnection(DbHelperSQL.strConn))
        {
            destinationConnection.Open();
            using (SqlBulkCopy bulkCopy = new SqlBulkCopy(destinationConnection))
            {
                try
                {
                    bulkCopy.DestinationTableName = "TMP_XLS";//要插入的表的表名
                    bulkCopy.BatchSize = dt.Rows.Count;
                    bulkCopy.ColumnMappings.Add("tmpGuid", "tmpGuid");//映射字段名 DataTable列名 ,数据库对应的列名
                    bulkCopy.ColumnMappings.Add("tmpIdx", "tmpIdx");
                    foreach (KeyValuePair<string, string> kvp in dic)
                    {
                        // bulkCopy.ColumnMappings.Add("名称", "t1");
                        bulkCopy.ColumnMappings.Add(kvp.Key, kvp.Value);
                    }
                    bulkCopy.WriteToServer(dt);
                    flag = true;
                }
                catch (Exception ex)
                {
                    _it = 0;
                    _msg = "操作失败,bulkCopy读取上传文件发生错误:" + ex.Message;
                }
            }
        }
        if (flag == false)
        {
            _it = 0;
            _msg = "操作失败,bulkCopy读取上传文件发生错误:";
        }
        _it = 1;
        return (_it, _msg);
    }
 
    #endregion
}