wtt
2024-09-11 ad22b8a5db5cbacf3751102163689f2b53a9952b
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
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Pub_Class;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web.Http;
using WebAPI.Models;
using System.IO;
using System.Web;
using WebAPI.Controllers.SCGL.日计划管理;
 
namespace WebAPI.Controllers
{
    //期初余额报表Controller
    public class YS_ContactBalController : ApiController
    {
        //获取系统参数
        Pub_Class.ClsXt_SystemParameter oSystemParameter = new Pub_Class.ClsXt_SystemParameter();
        public DBUtility.ClsPub.Enum_BillStatus BillStatus;
 
        private json objJsonResult = new json();
        SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
        DataSet ds;
 
        #region 期初余额报表 查询
        /// <summary>
        ///参数:string sql。
        ///返回值:object。
        /// </summary>
        [Route("YS_ContactBal/list")]
        [HttpGet]
        public object getYS_ContactBal(string sWhere, string user)
        {
            try
            {
                List<object> columnNameList = new List<object>();
                ////查看权限
                if (!DBUtility.ClsPub.Security_Log("YS_ContactBalReport_Query", 1, false, user))
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "无查看权限!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
 
                if (sWhere == null || sWhere.Equals(""))
                {
                    ds = oCN.RunProcReturn("select * from h_v_YSYF_ContactBal_Query order by 客户代码", "h_v_YSYF_ContactBal_Query");
                }
                else
                {
                    string sql1 = "select * from h_v_YSYF_ContactBal_Query where 1 = 1 ";
                    string sql = sql1 + sWhere + " order by 客户代码 ";
                    ds = oCN.RunProcReturn(sql, "h_v_YSYF_ContactBal_Query");
                }
 
                //添加列名
                foreach (DataColumn col in ds.Tables[0].Columns)
                {
                    Type dataType = col.DataType;
                    string ColmString = "{\"ColmCols\":\"" + col.ColumnName + "\",\"ColmType\":\"" + dataType.Name + "\"}";
                    columnNameList.Add(JsonConvert.DeserializeObject(ColmString));//获取到DataColumn列对象的列名
                }
 
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "Sucess!";
                objJsonResult.data = ds.Tables[0];
                objJsonResult.list = columnNameList;
                return objJsonResult;
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "Exception!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
 
        #region 期初余额报表 文件上传
        [Route("YS_ContactBal/YS_ContactBalReport_ImportByExcel")]
        [HttpPost]
        public object YS_ContactBalReport_ImportByExcel()
        {
            try
            {
                //var WorkBookName = HttpContext.Current.Request["WorkBookName"];
                //DBUtility.ClsPub.HOrgID = long.Parse(HttpContext.Current.Request["HOrgID"]);
                //获取文件名称
                var file = HttpContext.Current.Request.Files[0];
                //获取文件物理路径
                string ExcelPath = HttpContext.Current.Server.MapPath("~/" + file.FileName);
                //保存文件
                file.SaveAs(ExcelPath);
 
                NpoiHelper np = new NpoiHelper();
                DataSet ExcelDs = np.ReadExcel(ExcelPath, 1, 1, "0");
 
                //删除文件
                File.Delete(ExcelPath);
 
                //创建临时表
                DataTable tb2 = new DataTable("dt2");
 
                //添加列名
                for (int i = 0; i < ExcelDs.Tables[0].Columns.Count; i++)
                {
                    tb2.Columns.Add(ExcelDs.Tables[0].Rows[0][i].ToString());
                }
 
                //模板缺少列 但需要从数据库中查询出来显示在页面的字段
                tb2.Columns.Add("客户内码", typeof(string));//客户内码
                tb2.Columns.Add("货币内码", typeof(string));//货币内码
                tb2.Columns.Add("货币名称", typeof(string));//货币名称
                tb2.Columns.Add("判定结果", typeof(string));//判定结果
 
                //添加数据
                for (int i = 1; i < ExcelDs.Tables[0].Rows.Count; i++)
                {
                    DataRow row = tb2.NewRow();
                    for (int j = 0; j < ExcelDs.Tables[0].Columns.Count; j++)
                    {
                        row[j] = ExcelDs.Tables[0].Rows[i][j].ToString();
                    }
                    tb2.Rows.Add(row);
                }
 
                //查询Excel文件中没有的列
                var error = "";
                if (!tb2.Columns.Contains("类型"))
                {
                    error += "没有找到【类型】的标题,";
                }
 
 
                if (!tb2.Columns.Contains("客户代码"))
                {
                    error += "没有找到【客户代码】的标题,";
                }
                if (!tb2.Columns.Contains("客户名称"))
                {
                    error += "没有找到【客户名称】的标题,";
                }
                if (!tb2.Columns.Contains("期初余额"))
                {
                    error += "没有找到【期初余额】的标题,";
                }
                if (!tb2.Columns.Contains("组织代码"))
                {
                    error += "没有找到【组织代码】的标题,";
                }
                if (!tb2.Columns.Contains("组织名称"))
                {
                    error += "没有找到【组织名称】的标题,";
                }
 
                if (error.Length > 0)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = $"Excel模板存在错误,{error}\r\n";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
 
                //查询数据赋给数据表
                for (int i = 0; i <= tb2.Rows.Count - 1; i++)
                {
                    string HCusNumber = DBUtility.ClsPub.isStrNull(tb2.Rows[i]["客户代码"].ToString());
                    string HCusName = DBUtility.ClsPub.isStrNull(tb2.Rows[i]["客户名称"].ToString());
                    string HBeginBalance = DBUtility.ClsPub.isStrNull(tb2.Rows[i]["期初余额"].ToString());
 
                    int index = i + 1;
 
                    tb2.Rows[i]["判定结果"] = "";
                }
 
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = error;
                objJsonResult.data = tb2;
                return objJsonResult;
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "Exception!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
        #region 期初余额报表 导入(数据验证)
        [Route("YS_ContactBal/YS_ContactBalReport_ImportByExcel_Confirm")]
        [HttpPost]
        public object YS_ContactBalReport_ImportByExcel_Confirm([FromBody] JObject sMainSub)
        {
            var _value = sMainSub["sMainSub"].ToString();
            string msg1 = _value.ToString();
            string[] sArray = msg1.Split(new string[] { "&和" }, StringSplitOptions.RemoveEmptyEntries);
            string msg2 = sArray[0].ToString();
            string user = sArray[1].ToString();
            string[] importFieldList = sArray[2].ToString().Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
            try
            {
                List<object> Excel = Newtonsoft.Json.JsonConvert.DeserializeObject<List<object>>(msg2);
                List<Dictionary<string, string>> list = new List<Dictionary<string, string>>();
 
                foreach (JObject item in Excel)
                {
                    Dictionary<string, string> dic = new Dictionary<string, string>();
                    foreach (var itm in item.Properties())
                    {
                        dic.Add(itm.Name, itm.Value.ToString());
                    }
                    list.Add(dic);
                }
 
                //创建临时表并设置列名
                DataTable tb2 = new DataTable("dt2");
 
                tb2.Columns.Add("类型", typeof(string));
 
                tb2.Columns.Add("客户内码", typeof(string));
                tb2.Columns.Add("客户代码", typeof(string));
                tb2.Columns.Add("客户名称", typeof(string));
                tb2.Columns.Add("货币内码", typeof(string));
                tb2.Columns.Add("货币名称", typeof(string));
                tb2.Columns.Add("期初余额", typeof(string));
                tb2.Columns.Add("组织代码", typeof(string));
                tb2.Columns.Add("组织名称", typeof(string));
                tb2.Columns.Add("判定结果", typeof(string));
 
                string error = "";
                int i = 1;
                string sql = "";
                foreach (Dictionary<string, string> item in list)
                {
                    //记录判定结果
                    string err = "";
 
                    DataRow row = tb2.NewRow();
                    tb2.Rows.Add(row);
                    
                    string HFRP = item["类型"].ToString();
 
                    string HCusID = item["客户内码"].ToString();
                    string HCusNumber = item["客户代码"].ToString();
                    string HCusName = item["客户名称"].ToString();
                    string HCurID = item["货币内码"].ToString();
                    string HCurName = item["货币名称"].ToString();
                    string HBeginBalance = item["期初余额"].ToString();
                    string HOrgNumber = item["组织代码"].ToString();
                    string HOrgName = item["组织名称"].ToString();
 
                    tb2.Rows[i - 1]["类型"] = HFRP;
 
                    tb2.Rows[i - 1]["客户内码"] = HCusID;
                    tb2.Rows[i - 1]["客户代码"] = HCusNumber;
                    tb2.Rows[i - 1]["客户名称"] = HCusName;
                    tb2.Rows[i - 1]["货币内码"] = HCurID;
                    tb2.Rows[i - 1]["货币名称"] = HCurName;
                    tb2.Rows[i - 1]["期初余额"] = HBeginBalance;
                    tb2.Rows[i - 1]["组织代码"] = HOrgNumber;
                    tb2.Rows[i - 1]["组织名称"] = HOrgName;
                    tb2.Rows[i - 1]["判定结果"] = "";
 
 
                    //校验类型是否符合规定的类型
                    if(HFRP != "0" && HFRP != "1") 
                    {
                        err += "类型不正确,请输入(0:应收款 或 1:应付款);";
                    }
 
                    //根据导入字段获取客户相关数据,并进行校验
                    sql = "";
                    if (importFieldList[0] == "HOrgNumber")
                    {
                        sql = "select * from Xt_ORGANIZATIONS where HNumber = '" + HOrgNumber + "'";
                    }
                    else if (importFieldList[0] == "HOrgName")
                    {
                        sql = "select * from Xt_ORGANIZATIONS where HName = '" + HOrgName + "'";
                    }
                    ds = oCN.RunProcReturn(sql, "Xt_ORGANIZATIONS");
                    if (ds.Tables[0].Rows.Count == 0)
                    {
                        err += "组织不存在;";
 
                        HCusID = "0";
                        tb2.Rows[i - 1]["客户内码"] = "0";
                        tb2.Rows[i - 1]["货币内码"] = "0";
                    }
                    else
                    {
                        tb2.Rows[i - 1]["组织代码"] = ds.Tables[0].Rows[0]["HNumber"];
                        tb2.Rows[i - 1]["组织名称"] = ds.Tables[0].Rows[0]["HName"];
 
                        sql = "select c.HItemID,c.HNumber,c.HName,ISNULL(c.HCurID,0) HCurID,ISNULL(cur.HName,'') HCurName from " +
                        "Gy_Customer as c " +
                        "left join Xt_ORGANIZATIONS o on c.HUSEORGID = o.HItemID " +
                        "left join Gy_Currency cur on c.HCurID = cur.HItemID " + 
                        "where c.HNumber = '" + HCusNumber + "' " +
                        "and o.HNumber = '" + ds.Tables[0].Rows[0]["HNumber"] + "' " +
                        "and o.HName = '" + ds.Tables[0].Rows[0]["HName"] + "'";
                        ds = oCN.RunProcReturn(sql, "Gy_Customer");
                        if (ds.Tables[0].Rows.Count == 0)
                        {
                            err += "该组织下客户不存在;";
                            HCusID = "0";
                            tb2.Rows[i - 1]["客户内码"] = "0";
                            tb2.Rows[i - 1]["货币内码"] = "0";
                        }
                        else
                        {
                            tb2.Rows[i - 1]["客户内码"] = ds.Tables[0].Rows[0]["HItemID"];
                            tb2.Rows[i - 1]["客户代码"] = ds.Tables[0].Rows[0]["HNumber"];
                            tb2.Rows[i - 1]["客户名称"] = ds.Tables[0].Rows[0]["HName"];
                            tb2.Rows[i - 1]["货币内码"] = ds.Tables[0].Rows[0]["HCurID"];
                            tb2.Rows[i - 1]["货币名称"] = ds.Tables[0].Rows[0]["HCurName"];
 
                            HCusID = ds.Tables[0].Rows[0]["HItemID"].ToString();
                        }
                    }
 
                    //判定该客户是否已经存在期初余额
                    sql = "select * from YSYF_ContactBal where HCusID = " + HCusID;
                    ds = oCN.RunProcReturn(sql, "YSYF_ContactBal");
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        err += "该客户已经存在期初余额;";
                    }
 
                    tb2.Rows[i - 1]["判定结果"] = err;
                    error += err;
 
                    i++;
                }
 
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = (error.Length > 0 ? "0" : "1");
                objJsonResult.data = tb2;
                return objJsonResult;
            }
            catch (Exception e)
            {
                LogService.Write(e);
                oCN.RollBack();
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "Exception!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
        #region 期初余额报表 导入(保存)
        [Route("YS_ContactBal/YS_ContactBalReport_ImportByExcel_Save")]
        [HttpPost]
        public object YS_ContactBalReport_ImportByExcel_Save([FromBody] JObject sMainSub)
        {
            var _value = sMainSub["sMainSub"].ToString();
            string msg1 = _value.ToString();
            string[] sArray = msg1.Split(new string[] { "&和" }, StringSplitOptions.RemoveEmptyEntries);
            string msg2 = sArray[0].ToString();
            string user = sArray[1].ToString();
            string saveType = sArray[2].ToString();
            try
            {
                if (!DBUtility.ClsPub.Security_Log("YS_ContactBalReport_ExcelImport", 1, false, user))
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "无导入权限!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
 
                List<object> Excel = Newtonsoft.Json.JsonConvert.DeserializeObject<List<object>>(msg2);
                List<Dictionary<string, string>> list = new List<Dictionary<string, string>>();
 
                foreach (JObject item in Excel)
                {
                    Dictionary<string, string> dic = new Dictionary<string, string>();
                    foreach (var itm in item.Properties())
                    {
                        dic.Add(itm.Name, itm.Value.ToString());
                    }
                    list.Add(dic);
                }
 
                if (saveType == "1")
                {
                    oCN.BeginTran();
                    string err = "";
                    int i = 1;
                    string sql = "";
                    foreach (Dictionary<string, string> item in list)
                    {
                        string HFRP = item["类型"].ToString();
 
                        string HCusID = item["客户内码"].ToString();
                        string HCusNumber = item["客户代码"].ToString();
                        string HCusName = item["客户名称"].ToString();
                        string HCurID = item["货币内码"].ToString();
                        string HCurName = item["货币名称"].ToString();
                        string HBeginBalance = item["期初余额"].ToString();
                        string HOrgNumber = item["组织代码"].ToString();
                        string HOrgName = item["组织名称"].ToString();
 
                        //校验类型是否符合规定的类型
                        if (HFRP != "0" && HFRP != "1")
                        {
                            err += "第" + i + "行:类型不正确,请输入(0:应收款 或 1:应付款);";
                        }
 
                        sql = "select * from YSYF_ContactBal where HCusID = " + HCusID;
                        ds = oCN.RunProcReturn(sql, "YSYF_ContactBal");
                        if (ds.Tables[0].Rows.Count == 0)
                        {
                            sql = "insert into YSYF_ContactBal(HYear,HPeriod,HRP,HCusID,HCurID,HBeginBalance) " +
                                "values(" +
                                "YEAR(getdate())" +
                                ",Month(getdate())" +
                                "," + HFRP + "" +
                                "," + HCusID + "" +
                                "," + HCurID + "" +
                                "," + HBeginBalance + "" +
                                ")";
                            oCN.RunProc(sql);
                        }
                        else
                        {
                            sql = "update YSYF_ContactBal set " +
                                "HBeginBalance = " + HBeginBalance + " " +
                                "where HCusID = " + HCusID;
                            oCN.RunProc(sql);
                        }
                        i++;
                    }
                }
                else if (saveType == "2")
                {
                    oCN.BeginTran();
                    string err = "";
                    int i = 1;
                    string sql = "";
                    foreach (Dictionary<string, string> item in list)
                    {
                        string HFRP = item["类型"].ToString();
 
                        string HCusID = item["客户内码"].ToString();
                        string HCusNumber = item["客户代码"].ToString();
                        string HCusName = item["客户名称"].ToString();
                        string HCurID = item["货币内码"].ToString();
                        string HCurName = item["货币名称"].ToString();
                        string HBeginBalance = item["期初余额"].ToString();
                        string HOrgNumber = item["组织代码"].ToString();
                        string HOrgName = item["组织名称"].ToString();
 
                        //校验类型是否符合规定的类型
                        if (HFRP != "0" && HFRP != "1")
                        {
                            err += "第" + i + "行:类型不正确,请输入(0:应收款 或 1:应付款);";
                        }
 
                        sql = "select * from YSYF_ContactBal where HCusID = " + HCusID;
                        ds = oCN.RunProcReturn(sql, "YSYF_ContactBal");
                        if (ds.Tables[0].Rows.Count == 0)
                        {
                            sql = "insert into YSYF_ContactBal(HYear,HPeriod,HRP,HCusID,HCurID,HBeginBalance) " +
                                "values(" +
                                "YEAR(getdate())" +
                                ",Month(getdate())" +
                                "," + HFRP + "" +
                                "," + HCusID + "" +
                                "," + HCurID + "" +
                                "," + HBeginBalance + "" +
                                ")";
                            oCN.RunProc(sql);
                        }
                        else
                        {
                            err += "第" + i + "行:该客户已经存在期初余额;!";
                        }
                        i++;
                    }
                    //判断是否存在错误
                    if (err.Length > 0)
                    {
                        oCN.RollBack();
                        objJsonResult.code = "0";
                        objJsonResult.count = 0;
                        objJsonResult.Message = "导入错误:" + err;
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                }
 
                oCN.Commit();
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "导入成功!";
                objJsonResult.data = null;
                return objJsonResult;
            }
            catch (Exception e)
            {
                LogService.Write(e);
                oCN.RollBack();
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "Exception!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
    }
}