Quellcode durchsuchen

修改客户档案管理接口

machenyang vor 8 Jahren
Ursprung
Commit
a9cf033e83

+ 192 - 79
codegit/CallCenterApi/CallCenterApi.Interface/CallCenterApi.Interface/Controllers/customer/CustomerController.cs

@@ -13,6 +13,7 @@ using System.IO;
13 13
 using NPOI.XSSF.UserModel;
14 14
 using NPOI.HSSF.UserModel;
15 15
 using NPOI.SS.UserModel;
16
+using Newtonsoft.Json;
16 17
 
17 18
 namespace CallCenterApi.Interface.Controllers.customer
18 19
 {
@@ -30,9 +31,15 @@ namespace CallCenterApi.Interface.Controllers.customer
30 31
             {
31 32
                 string sql = " and F_DeleteFlag=0";
32 33
                 DataTable dt = new DataTable();
34
+                //联系人
35
+                string strname = HttpUtility.UrlDecode(RequestString.GetFormString("name"));
36
+                //电话
37
+                string strtel = HttpUtility.UrlDecode(RequestString.GetFormString("tel"));
38
+                //客户名称
39
+                string strCus = HttpUtility.UrlDecode(RequestString.GetFormString("Cusname"));
40
+                //省份
41
+                string strprov = HttpUtility.UrlDecode(RequestString.GetFormString("province"));
33 42
 
34
-                string strname = HttpUtility.UrlDecode(RequestString.GetQueryString("name"));
35
-                string strtel = HttpUtility.UrlDecode(RequestString.GetQueryString("tel"));
36 43
                 string strstarttime = HttpUtility.UrlDecode(RequestString.GetQueryString("starttime"));
37 44
                 string strendtime = HttpUtility.UrlDecode(RequestString.GetQueryString("endtime"));
38 45
 
@@ -49,6 +56,15 @@ namespace CallCenterApi.Interface.Controllers.customer
49 56
                 {
50 57
                     sql += " and (F_Telephone like '%" + strtel + "%' or F_Mobile like '%" + strtel + "%' or F_ChargeTelephone like '%" + strtel + "%') ";
51 58
                 }
59
+                if (strCus.Trim() != "" && strCus != "undefined")
60
+                {
61
+                    sql += " and F_CustomerIndustry like '%" + strCus.Trim() + "%' ";
62
+                }
63
+                if (strprov.Trim() != "" && strprov.Trim() != "undefined")
64
+                {
65
+                    sql += " and F_Province like '%" + strprov.Trim() + "%') ";
66
+                }
67
+
52 68
                 if (strstarttime.Trim() != "" && strstarttime != "undefined")
53 69
                 {
54 70
                     sql += " and datediff(day,F_CreatedOn,'" + strstarttime + "')<=0 ";
@@ -169,9 +185,11 @@ namespace CallCenterApi.Interface.Controllers.customer
169 185
                 {
170 186
                     //联系人
171 187
                     string name = RequestString.GetFormString("name");
188
+                    //省份
172 189
                     string province = RequestString.GetFormString("province");
173 190
                     //客户名称
174 191
                     string customerindustry = RequestString.GetFormString("customerindustry");
192
+                    //电话
175 193
                     string mobile = RequestString.GetFormString("mobile");
176 194
                     //获取当前表格选择的客户
177 195
                     int cid = Utils.StrToInt(RequestString.GetFormString("cid"), 0);
@@ -192,7 +210,7 @@ namespace CallCenterApi.Interface.Controllers.customer
192 210
                         int n = bll.Add(model);
193 211
                         if (n > 0)
194 212
                         {
195
-                            res = Success("新增成功!", n);
213
+                            res = Success("新增成功!", model);
196 214
                         }
197 215
                         else
198 216
                         {
@@ -210,7 +228,7 @@ namespace CallCenterApi.Interface.Controllers.customer
210 228
                             model.F_Mobile = mobile;
211 229
                             if (bll.Update(model))
212 230
                             {
213
-                                res = Success("修改成功!");
231
+                                res = Success("修改成功!",model);
214 232
                             }
215 233
                             else
216 234
                             {
@@ -333,97 +351,192 @@ namespace CallCenterApi.Interface.Controllers.customer
333 351
                 int userId = CurrentUser.UserData.F_UserId;
334 352
                 if (userId != 0)
335 353
                 {
336
-                    //需要先将文件保存到服务器项目下,再读取
337
-                    //传入的应该是整个文件,file和上传按钮
338
-                    string file = Request.Files[0].FileName;
339
-                    string fileextension = Request.Files[0].ContentType;
354
+                    ////需要先将文件保存到服务器项目下,再读取
355
+                    ////传入的应该是整个文件,file和上传按钮
356
+                    //string file = Request.Files[0].FileName;
357
+                    //string fileextension = Request.Files[0].ContentType;
358
+
359
+                    //if (!string.IsNullOrEmpty(file) && file != "undefined")
360
+                    //{
361
+                    //    //string extensionstr = Path.GetExtension(filestr);
362
+                    //    //if (extensionstr == ".xls" || extensionstr == ".xlsx")
363
+                    //    if (fileextension == "application/vnd.ms-excel" || fileextension == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
364
+                    //    {
365
+                    //        //将上传的文件保存在服务器下
366
+                    //        Request.SaveAs(Server.MapPath("~/App_Data/" + file), false);
367
+
368
+                    //        if (System.IO.File.Exists(Server.MapPath("~/App_Data/" + file)))
369
+                    //        {
370
+                    //            HSSFWorkbook hssfworkbook;
371
+                    //            XSSFWorkbook xssfworkbook;
372
+                    //            ISheet sheet;
373
+                    //            DataTable dt = new DataTable();
374
+                    //            using (FileStream filestream = new FileStream(Path.GetFullPath(file), FileMode.Open, FileAccess.Read))
375
+                    //            {
376
+                    //                if (fileextension == "application/vnd.ms-excel")
377
+                    //                {
378
+                    //                    hssfworkbook = new HSSFWorkbook(filestream);
379
+                    //                    sheet = hssfworkbook.GetSheetAt(0);
380
+                    //                }
381
+                    //                else
382
+                    //                {
383
+                    //                    xssfworkbook = new XSSFWorkbook(filestream);
384
+                    //                    sheet = xssfworkbook.GetSheetAt(0);
385
+                    //                }
386
+                    //            }
387
+                    //            System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
388
+
389
+                    //            IRow headerRow = sheet.GetRow(0);
390
+                    //            int cellCount = headerRow.LastCellNum;
391
+
392
+                    //            for (int j = 0; j < cellCount; j++)
393
+                    //            {
394
+                    //                ICell cell = headerRow.GetCell(j);
395
+                    //                dt.Columns.Add(cell.ToString());
396
+                    //            }
340 397
 
341
-                    if (!string.IsNullOrEmpty(file) && file != "undefined")
398
+                    //            Model.T_Cus_CustomerBase model = new Model.T_Cus_CustomerBase();
399
+                    //            BLL.T_Cus_CustomerBase bll = new BLL.T_Cus_CustomerBase();
400
+                    //            int count = 0;
401
+                    //            for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++)
402
+                    //            {
403
+                    //                IRow row = sheet.GetRow(i);
404
+                    //                DataRow dataRow = dt.NewRow();
405
+
406
+                    //                for (int j = row.FirstCellNum; j < cellCount; j++)
407
+                    //                {
408
+                    //                    if (row.GetCell(j) != null)
409
+                    //                        dataRow[j] = row.GetCell(j).ToString();
410
+                    //                }
411
+                    //                dt.Rows.Add(dataRow);
412
+
413
+                    //                //导入后要插入数据库
414
+                    //                model.F_CustomerName = dataRow[2].ToString();
415
+                    //                model.F_Province = dataRow[0].ToString();
416
+                    //                model.F_CustomerIndustry = dataRow[1].ToString();
417
+                    //                model.F_Mobile = dataRow[3].ToString();
418
+
419
+                    //                model.F_CreateBy = userId;
420
+                    //                model.F_CreatedOn = DateTime.Now;
421
+                    //                model.F_DeleteFlag = 0;
422
+                    //                int n = bll.Add(model);
423
+                    //                if (n > 0)
424
+                    //                { count++; }
425
+                    //            }
426
+                    //            res = Success("共导入" + dt.Rows.Count + "条,成功" + count + "条", dt);
427
+                    //        }
428
+                    //        else
429
+                    //        {
430
+                    //            res = Error("文件未上传成功");
431
+                    //        }
432
+                    //    }
433
+                    //    else
434
+                    //    {
435
+                    //        res = Error("文件类型错误");
436
+                    //    }
437
+                    //}
438
+                    //else
439
+                    //{
440
+                    //    res = Error("参数传输失败");
441
+                    //}
442
+                    HttpPostedFile _upFile = RequestString.GetFile("upFile");
443
+                    if (_upFile != null)
342 444
                     {
343
-                        //string extensionstr = Path.GetExtension(filestr);
344
-                        //if (extensionstr == ".xls" || extensionstr == ".xlsx")
345
-                        if (fileextension == "application/vnd.ms-excel" || fileextension == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
445
+                        string filepath = "";
446
+                        string datepath = DateTime.Now.ToString("yyyyMMddHHMMss");
447
+                        string aLastName = _upFile.FileName.Substring(_upFile.FileName.LastIndexOf(".") + 1, (_upFile.FileName.Length - _upFile.FileName.LastIndexOf(".") - 1)); //扩展名
448
+                        if (aLastName != "xls" && aLastName != "xlsx")
449
+                        {
450
+                            res = Error("文件类型错误,请选择Excel文件");
451
+                        }
452
+                        string newpath = datepath + "_" + _upFile.FileName;
453
+                        if (!Directory.Exists(Server.MapPath(this.Request.ApplicationPath + "\\ExcelData")))
346 454
                         {
347
-                            //将上传的文件保存在服务器下
348
-                            Request.SaveAs(Server.MapPath("~/App_Data/" + file), false);
455
+                            Directory.CreateDirectory(Server.MapPath(this.Request.ApplicationPath + "\\ExcelData"));
456
+                        }
457
+                        filepath = this.Request.ApplicationPath + "/ExcelData/" + newpath;
458
+                        string PhysicalPath = Server.MapPath(filepath);
459
+                        _upFile.SaveAs(PhysicalPath);
460
+
461
+                        DataTable dt = new DataTable();
349 462
 
350
-                            if (System.IO.File.Exists(Server.MapPath("~/App_Data/" + file)))
463
+                        IWorkbook workbook = null;
464
+
465
+                        using (FileStream file = new FileStream(PhysicalPath, FileMode.Open, FileAccess.Read))
466
+                        {
467
+                            if (aLastName == "xlsx") // 2007版本
351 468
                             {
352
-                                HSSFWorkbook hssfworkbook;
353
-                                XSSFWorkbook xssfworkbook;
354
-                                ISheet sheet;
355
-                                DataTable dt = new DataTable();
356
-                                using (FileStream filestream = new FileStream(Path.GetFullPath(file), FileMode.Open, FileAccess.Read))
357
-                                {
358
-                                    if (fileextension == "application/vnd.ms-excel")
359
-                                    {
360
-                                        hssfworkbook = new HSSFWorkbook(filestream);
361
-                                        sheet = hssfworkbook.GetSheetAt(0);
362
-                                    }
363
-                                    else
364
-                                    {
365
-                                        xssfworkbook = new XSSFWorkbook(filestream);
366
-                                        sheet = xssfworkbook.GetSheetAt(0);
367
-                                    }
368
-                                }
369
-                                System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
370
-
371
-                                IRow headerRow = sheet.GetRow(0);
372
-                                int cellCount = headerRow.LastCellNum;
373
-
374
-                                for (int j = 0; j < cellCount; j++)
375
-                                {
376
-                                    ICell cell = headerRow.GetCell(j);
377
-                                    dt.Columns.Add(cell.ToString());
378
-                                }
379
-
380
-                                Model.T_Cus_CustomerBase model = new Model.T_Cus_CustomerBase();
381
-                                BLL.T_Cus_CustomerBase bll = new BLL.T_Cus_CustomerBase();
382
-                                int count = 0;
383
-                                for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++)
384
-                                {
385
-                                    IRow row = sheet.GetRow(i);
386
-                                    DataRow dataRow = dt.NewRow();
387
-
388
-                                    for (int j = row.FirstCellNum; j < cellCount; j++)
389
-                                    {
390
-                                        if (row.GetCell(j) != null)
391
-                                            dataRow[j] = row.GetCell(j).ToString();
392
-                                    }
393
-                                    dt.Rows.Add(dataRow);
394
-
395
-                                    //导入后要插入数据库
396
-                                    model.F_CustomerName = dataRow[2].ToString();
397
-                                    model.F_Province = dataRow[0].ToString();
398
-                                    model.F_CustomerIndustry = dataRow[1].ToString();
399
-                                    model.F_Mobile = dataRow[3].ToString();
400
-
401
-                                    model.F_CreateBy = userId;
402
-                                    model.F_CreatedOn = DateTime.Now;
403
-                                    model.F_DeleteFlag = 0;
404
-                                    int n = bll.Add(model);
405
-                                    if (n > 0)
406
-                                    { count++; }
407
-                                }
408
-                                res = Success("共导入" + dt.Rows.Count + "条,成功" + count + "条", dt);
469
+                                workbook = new XSSFWorkbook(file);
409 470
                             }
410
-                            else
471
+                            else if (aLastName == "xls") // 2003版本
411 472
                             {
412
-                                res = Error("文件未上传成功");
473
+                                workbook = new HSSFWorkbook(file);
413 474
                             }
475
+                            //hssfworkbook = new HSSFWorkbook(file);
414 476
                         }
477
+                        ISheet sheet = workbook.GetSheetAt(0);
478
+                        System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
479
+
480
+                        IRow headerRow = sheet.GetRow(0);
481
+                        int cellCount = headerRow.LastCellNum;
482
+                        if (cellCount < 1)
483
+                            res = Error("文件标题没有数据");
484
+                        for (int j = 0; j < cellCount; j++)
485
+                        {
486
+                            ICell cell = headerRow.GetCell(j);
487
+                            dt.Columns.Add(cell.ToString());
488
+                        }
489
+                        Model.T_Cus_CustomerBase model = new Model.T_Cus_CustomerBase();
490
+                        BLL.T_Cus_CustomerBase bll = new BLL.T_Cus_CustomerBase();
491
+                        int count = 0;
492
+                        for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++)
493
+                        {
494
+                            IRow row = sheet.GetRow(i);
495
+                            DataRow dataRow = dt.NewRow();
496
+
497
+                            for (int j = row.FirstCellNum; j < cellCount; j++)
498
+                            {
499
+                                if (row.GetCell(j) != null)
500
+                                    dataRow[j] = row.GetCell(j).ToString();
501
+                            }
502
+                            //dt.Rows.Add(dataRow);
503
+                            dt.Rows.Add(dataRow);
504
+                            if (dt.Rows.Count < 1)
505
+                                res = Error("文件内容没有数据");
506
+                            //导入后要插入数据库
507
+                            model.F_CustomerName = dataRow[2].ToString();
508
+                            model.F_Province = dataRow[0].ToString();
509
+                            model.F_CustomerIndustry = dataRow[1].ToString();
510
+                            model.F_Mobile = dataRow[3].ToString();
511
+
512
+                            model.F_CreateBy = userId;
513
+                            model.F_CreatedOn = DateTime.Now;
514
+                            model.F_DeleteFlag = 0;
515
+                            int n = bll.Add(model);
516
+                            if (n > 0)
517
+                            { count++; }
518
+                        }
519
+                        //if (dt == null || dt.Rows.Count == 0)
520
+                        if (dt == null || dt.Rows.Count == 0)
521
+                            res = Error("文件没有数据");
522
+                        //var jstr = DataTableToJson(dt);
415 523
                         else
416 524
                         {
417
-                            res = Error("文件类型错误");
525
+                            var jstr = DataTableToJson(dt);
526
+                            res = Success("导入成功", jstr);
418 527
                         }
419
-                    }
420
-                    else
421
-                    {
422
-                        res = Error("参数传输失败");
528
+
423 529
                     }
424 530
                 }
425 531
             }
426 532
             return res;
427 533
         }
534
+
535
+        public string DataTableToJson(DataTable dt)
536
+        {
537
+            string JsonString = string.Empty;
538
+            JsonString = JsonConvert.SerializeObject(dt);
539
+            return JsonString;
540
+        }
428 541
     }
429 542
 }