Quellcode durchsuchen

Merge branch 'master' of http://192.168.1.222:3000/zhengbingbing/ZLJ_API_V6.0

zhengbingbing vor 5 Jahren
Ursprung
Commit
619b13029c

+ 88 - 1
代码/TVShoppingCallCenter_ZLJ/Controllers/Customer/VIPInfoController.cs

@@ -11,6 +11,9 @@ using Microsoft.AspNetCore.Mvc;
11 11
 using TVShoppingCallCenter_ZLJ.Models.Inputs;
12 12
 using SqlSugar;
13 13
 using System.Common.Helpers;
14
+using Microsoft.Extensions.Configuration;
15
+using System.Data;
16
+using System.Reflection;
14 17
 
15 18
 namespace TVShoppingCallCenter_ZLJ.Controllers.Customer
16 19
 {
@@ -22,12 +25,14 @@ namespace TVShoppingCallCenter_ZLJ.Controllers.Customer
22 25
         private readonly ICus_VipInfoRepository _cus_vip_infoRepository;
23 26
         private readonly ISys_UserAccountRepository _sys_user_accountRepository;
24 27
         private readonly ICus_MsgRepository _cus_msgRepository;
28
+        private readonly IConfiguration _configuration;
25 29
 
26
-        public VipInfoController(ICus_VipInfoRepository cus_vip_infoRepository, ISys_UserAccountRepository sys_user_accountRepository, ICus_MsgRepository cus_msgRepository)
30
+        public VipInfoController(ICus_VipInfoRepository cus_vip_infoRepository, ISys_UserAccountRepository sys_user_accountRepository, ICus_MsgRepository cus_msgRepository,IConfiguration configuration)
27 31
         {
28 32
             _cus_vip_infoRepository = cus_vip_infoRepository;
29 33
             _sys_user_accountRepository = sys_user_accountRepository;
30 34
             _cus_msgRepository = cus_msgRepository;
35
+            _configuration = configuration;
31 36
         }
32 37
 
33 38
         [HttpGet]
@@ -512,6 +517,88 @@ namespace TVShoppingCallCenter_ZLJ.Controllers.Customer
512 517
             }
513 518
             //return Success("导出成功");
514 519
         }
520
+
521
+        /// <summary>
522
+        /// 上传文件并导入数据库
523
+        /// </summary>
524
+        /// <returns></returns>
525
+        [HttpPost("importexcel")]
526
+        public async Task<IActionResult> ImportExcel( int headrow = 0)
527
+        {
528
+           
529
+            //string usercode = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
530
+
531
+            Microsoft.AspNetCore.Http.IFormFile _upfile = Request.Form.Files[0];
532
+            if (!_upfile.ContentType.Equals("application/vnd.ms-excel") && !_upfile.ContentType.Equals("application/x-xls") && !_upfile.ContentType.Equals("application/x-xlsx") && !_upfile.ContentType.Equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") && !_upfile.ContentType.Equals("application/octet-stream"))
533
+                return Error($"请正确上传Excel文件:file.ContentType={_upfile.ContentType}");
534
+
535
+            NPOIHelper npoi = new NPOIHelper();
536
+            var dtExcel = npoi.ExcelToTable1(_upfile, headrow);
537
+            int num = dtExcel.Rows.Count;
538
+            var cols = dtExcel.Columns;
539
+            int colnum = cols.Count;
540
+
541
+            string dbkeys = _configuration["upload:acotdbkeys"].ToString();
542
+            string[] dbcols = dbkeys.Split(",");
543
+            string errmsg = string.Empty;
544
+
545
+            if (num > 0)
546
+            {
547
+                int index = 1;
548
+                foreach (DataRow dr in dtExcel.Rows)
549
+                {
550
+                    var model = new T_Cus_VipInfo();
551
+                    
552
+                    model.F_State = 1;
553
+                    model.F_CreateBy = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
554
+                    model.F_CreateOn = DateTime.Now;
555
+
556
+                    var dbcolslist = dbcols.ToList();
557
+                    
558
+                    Type t = model.GetType();
559
+                    PropertyInfo[] PropertyList = t.GetProperties();
560
+
561
+                    foreach (PropertyInfo item in PropertyList)
562
+                    {
563
+                        if (dbcolslist.Contains(item.Name))
564
+                        {
565
+                            object v = Convert.ChangeType(dr[dbcolslist.IndexOf(item.Name)].ToString(), item.PropertyType);
566
+                            item.SetValue(model, v, null);
567
+                            
568
+                        }
569
+                    }
570
+
571
+                    int b = await _cus_vip_infoRepository.Add(model);
572
+
573
+                    
574
+                    if (b>0)
575
+                    {
576
+                        if (!string.IsNullOrEmpty(errmsg))
577
+                        {
578
+                            errmsg = errmsg + "\r\n第" + index + "行导入失败!";
579
+                        }
580
+                        else
581
+                        {
582
+                            errmsg = "第" + index + "行导入失败!";
583
+                        }
584
+                    }
585
+                    index++;
586
+                    
587
+                }
588
+            }
589
+            else
590
+            {
591
+                return Error("文件中无数据");
592
+            }
593
+
594
+            if (!string.IsNullOrEmpty(errmsg))
595
+            {
596
+                //删除已导入的部分
597
+                return Error(errmsg);
598
+            }
599
+
600
+            return Success("导入成功");
601
+        }
515 602
         #endregion
516 603
     }
517 604
 }

+ 7 - 0
代码/TVShoppingCallCenter_ZLJ/appsettings.json

@@ -16,6 +16,13 @@
16 16
     "Defaultdatabase": "5",
17 17
     "InstanceName": "base"
18 18
   },
19
+  "upload": {
20
+    "url": "http://192.168.4.18:5008/api/upload",
21
+    "params": "uploadtype=excel",
22
+    "acotmbname": "会员信息导入模板.xlsx",
23
+    "acotmbkeys": "会员卡号,姓名,生日,年龄,性别,鞋码,手机号1,手机号2,固定电话,推荐人,标签,备注,昵称,注册时间,省份,城市,县区,乡镇,邮编,地址1,地址2,会员积分,累计消费,累计积分,销售负责人,最近联系时间,会员类型",
24
+    "acotdbkeys": "F_VIPCode,F_Name,F_Birthday,F_Age,F_Sex,F_ShoeSize,F_Mobile,F_Mobile1,F_Phone,F_Recommender,F_Label,F_Note,F_Nickname,F_RegTime,F_Province,F_City,F_Area,F_Town,F_Postcode,F_Address,F_Address1,F_Score,F_Money,F_TotalScore,F_Saleperson,F_Contact,F_Type"
25
+  },
19 26
   "Logging": {
20 27
     "LogLevel": {
21 28
       "Default": "Warning"