Commit 297c68d79c5acf204259d549cf51bd18d61a94da
1 parent
2c7d6853
feat: 新增客户信息导出优化接口
- 新增ExportOptimized接口,使用批量查询优化性能 - 创建LqKhxxExportOutput导出专用DTO类 - 支持23个固定字段导出,包括客户目前归属名称等 - 文件保存到ExportFiles目录 - 新增Python脚本控制台工具script_runner.py - 新增测试脚本test_customer_export_optimized.sh
Showing
4 changed files
with
691 additions
and
0 deletions
netcore/src/Modularity/Extend/NCC.Extend.Entitys/Dto/LqKhxx/LqKhxxExportOutput.cs
0 → 100644
| 1 | +using System; | ||
| 2 | +using NCC.Code; | ||
| 3 | +using NCC.Extend.Entitys.Enum; | ||
| 4 | + | ||
| 5 | +namespace NCC.Extend.Entitys.Dto.LqKhxx | ||
| 6 | +{ | ||
| 7 | + /// <summary> | ||
| 8 | + /// 客户资料导出输出参数(扩展版) | ||
| 9 | + /// </summary> | ||
| 10 | + public class LqKhxxExportOutput | ||
| 11 | + { | ||
| 12 | + /// <summary> | ||
| 13 | + /// 客户编码 | ||
| 14 | + /// </summary> | ||
| 15 | + public string id { get; set; } | ||
| 16 | + | ||
| 17 | + /// <summary> | ||
| 18 | + /// 客户名称 | ||
| 19 | + /// </summary> | ||
| 20 | + public string khmc { get; set; } | ||
| 21 | + | ||
| 22 | + /// <summary> | ||
| 23 | + /// 手机号 | ||
| 24 | + /// </summary> | ||
| 25 | + public string sjh { get; set; } | ||
| 26 | + | ||
| 27 | + /// <summary> | ||
| 28 | + /// 档案号 | ||
| 29 | + /// </summary> | ||
| 30 | + public string dah { get; set; } | ||
| 31 | + | ||
| 32 | + /// <summary> | ||
| 33 | + /// 性别 | ||
| 34 | + /// </summary> | ||
| 35 | + public string xb { get; set; } | ||
| 36 | + | ||
| 37 | + /// <summary> | ||
| 38 | + /// 客户目前归属名称 | ||
| 39 | + /// </summary> | ||
| 40 | + public string khmqgsName { get; set; } | ||
| 41 | + | ||
| 42 | + /// <summary> | ||
| 43 | + /// 归属门店名称 | ||
| 44 | + /// </summary> | ||
| 45 | + public string gsmdName { get; set; } | ||
| 46 | + | ||
| 47 | + /// <summary> | ||
| 48 | + /// 注册时间 | ||
| 49 | + /// </summary> | ||
| 50 | + public DateTime? zcsj { get; set; } | ||
| 51 | + | ||
| 52 | + /// <summary> | ||
| 53 | + /// 客户类型名称 | ||
| 54 | + /// </summary> | ||
| 55 | + public string khlxName { get; set; } | ||
| 56 | + | ||
| 57 | + /// <summary> | ||
| 58 | + /// 客户阶段 | ||
| 59 | + /// </summary> | ||
| 60 | + public string khjd { get; set; } | ||
| 61 | + | ||
| 62 | + /// <summary> | ||
| 63 | + /// 客户消费 | ||
| 64 | + /// </summary> | ||
| 65 | + public string khxf { get; set; } | ||
| 66 | + | ||
| 67 | + /// <summary> | ||
| 68 | + /// 消费频次 | ||
| 69 | + /// </summary> | ||
| 70 | + public string xfpc { get; set; } | ||
| 71 | + | ||
| 72 | + /// <summary> | ||
| 73 | + /// 推荐人 | ||
| 74 | + /// </summary> | ||
| 75 | + public string tjrName { get; set; } | ||
| 76 | + | ||
| 77 | + /// <summary> | ||
| 78 | + /// 进店渠道 | ||
| 79 | + /// </summary> | ||
| 80 | + public string jdqd { get; set; } | ||
| 81 | + | ||
| 82 | + /// <summary> | ||
| 83 | + /// 联系地址 | ||
| 84 | + /// </summary> | ||
| 85 | + public string lxdz { get; set; } | ||
| 86 | + | ||
| 87 | + /// <summary> | ||
| 88 | + /// 备注 | ||
| 89 | + /// </summary> | ||
| 90 | + public string bz { get; set; } | ||
| 91 | + | ||
| 92 | + /// <summary> | ||
| 93 | + /// 阳历生日 | ||
| 94 | + /// </summary> | ||
| 95 | + public DateTime? yanglsr { get; set; } | ||
| 96 | + | ||
| 97 | + /// <summary> | ||
| 98 | + /// 首次到店时间 | ||
| 99 | + /// </summary> | ||
| 100 | + public DateTime? firstVisitTime { get; set; } | ||
| 101 | + | ||
| 102 | + /// <summary> | ||
| 103 | + /// 最后到店时间 | ||
| 104 | + /// </summary> | ||
| 105 | + public DateTime? lastVisitTime { get; set; } | ||
| 106 | + | ||
| 107 | + /// <summary> | ||
| 108 | + /// 到店天数 | ||
| 109 | + /// </summary> | ||
| 110 | + public int visitDays { get; set; } | ||
| 111 | + | ||
| 112 | + /// <summary> | ||
| 113 | + /// 沉睡天数 | ||
| 114 | + /// </summary> | ||
| 115 | + public int sleepDays { get; set; } | ||
| 116 | + | ||
| 117 | + /// <summary> | ||
| 118 | + /// 开单总金额 | ||
| 119 | + /// </summary> | ||
| 120 | + public decimal totalBillingAmount { get; set; } | ||
| 121 | + | ||
| 122 | + /// <summary> | ||
| 123 | + /// 剩余权益总金额 | ||
| 124 | + /// </summary> | ||
| 125 | + public decimal remainingRightsAmount { get; set; } | ||
| 126 | + } | ||
| 127 | +} | ||
| 128 | + |
netcore/src/Modularity/Extend/NCC.Extend/LqKhxxService.cs
| 1 | using System; | 1 | using System; |
| 2 | using System.Collections.Generic; | 2 | using System.Collections.Generic; |
| 3 | +using System.IO; | ||
| 3 | using System.Linq; | 4 | using System.Linq; |
| 4 | using System.Threading.Tasks; | 5 | using System.Threading.Tasks; |
| 5 | using Mapster; | 6 | using Mapster; |
| 6 | using Microsoft.AspNetCore.Mvc; | 7 | using Microsoft.AspNetCore.Mvc; |
| 7 | using Microsoft.Extensions.Logging; | 8 | using Microsoft.Extensions.Logging; |
| 8 | using NCC.ClayObject; | 9 | using NCC.ClayObject; |
| 10 | +using NCC.Code; | ||
| 9 | using NCC.Common.Configuration; | 11 | using NCC.Common.Configuration; |
| 10 | using NCC.Common.Core.Manager; | 12 | using NCC.Common.Core.Manager; |
| 11 | using NCC.Common.Enum; | 13 | using NCC.Common.Enum; |
| @@ -586,6 +588,336 @@ namespace NCC.Extend.LqKhxx | @@ -586,6 +588,336 @@ namespace NCC.Extend.LqKhxx | ||
| 586 | } | 588 | } |
| 587 | #endregion | 589 | #endregion |
| 588 | 590 | ||
| 591 | + #region 导出客户资料(优化版) | ||
| 592 | + /// <summary> | ||
| 593 | + /// 导出客户资料(优化版) | ||
| 594 | + /// 使用LEFT JOIN优化查询效率,固定导出指定字段 | ||
| 595 | + /// </summary> | ||
| 596 | + /// <param name="input">请求参数</param> | ||
| 597 | + /// <returns></returns> | ||
| 598 | + [HttpGet("Actions/ExportOptimized")] | ||
| 599 | + public async Task<dynamic> ExportOptimized([FromQuery] LqKhxxListQueryInput input) | ||
| 600 | + { | ||
| 601 | + var userInfo = await _userManager.GetUserInfo(); | ||
| 602 | + | ||
| 603 | + // 构建查询条件 | ||
| 604 | + var sidx = input.sidx == null ? "F_Id" : input.sidx; | ||
| 605 | + // 将前端字段名映射到数据库字段名 | ||
| 606 | + if (sidx == "id") sidx = "F_Id"; | ||
| 607 | + if (sidx == "khmc") sidx = "khmc"; | ||
| 608 | + if (sidx == "zcsj") sidx = "zcsj"; | ||
| 609 | + List<string> queryZcsj = input.zcsj != null ? input.zcsj.Split(',').ToObeject<List<string>>() : null; | ||
| 610 | + DateTime? startZcsj = queryZcsj != null ? Ext.GetDateTime(queryZcsj.First()) : null; | ||
| 611 | + DateTime? endZcsj = queryZcsj != null ? Ext.GetDateTime(queryZcsj.Last()) : null; | ||
| 612 | + List<string> queryYanglsr = input.yanglsr != null ? input.yanglsr.Split(',').ToObeject<List<string>>() : null; | ||
| 613 | + DateTime? startYanglsr = queryYanglsr != null ? Ext.GetDateTime(queryYanglsr.First()) : null; | ||
| 614 | + DateTime? endYanglsr = queryYanglsr != null ? Ext.GetDateTime(queryYanglsr.Last()) : null; | ||
| 615 | + | ||
| 616 | + // 先查询客户基础数据(使用子查询方式,类似GetNoPagingList,但优化为批量查询关联数据) | ||
| 617 | + var customerList = await _db.Queryable<LqKhxxEntity>() | ||
| 618 | + .WhereIF(!string.IsNullOrEmpty(input.id), p => p.Id.Contains(input.id)) | ||
| 619 | + .WhereIF(!string.IsNullOrEmpty(input.khmc), p => p.Khmc.Contains(input.khmc)) | ||
| 620 | + .WhereIF(!string.IsNullOrEmpty(input.sjh), p => p.Sjh.Contains(input.sjh)) | ||
| 621 | + .WhereIF(!string.IsNullOrEmpty(input.dah), p => p.Dah.Contains(input.dah)) | ||
| 622 | + .WhereIF(!string.IsNullOrEmpty(input.xb), p => p.Xb.Equals(input.xb)) | ||
| 623 | + .WhereIF(!string.IsNullOrEmpty(input.gsmd), p => p.Gsmd.Contains(input.gsmd)) | ||
| 624 | + .WhereIF(queryZcsj != null, p => p.Zcsj >= new DateTime(startZcsj.ToDate().Year, startZcsj.ToDate().Month, startZcsj.ToDate().Day, 0, 0, 0)) | ||
| 625 | + .WhereIF(queryZcsj != null, p => p.Zcsj <= new DateTime(endZcsj.ToDate().Year, endZcsj.ToDate().Month, endZcsj.ToDate().Day, 23, 59, 59)) | ||
| 626 | + .WhereIF(!string.IsNullOrEmpty(input.khlx), p => p.Khlx.Equals(input.khlx)) | ||
| 627 | + .WhereIF(!string.IsNullOrEmpty(input.khjd), p => p.Khjd.Equals(input.khjd)) | ||
| 628 | + .WhereIF(!string.IsNullOrEmpty(input.khxf), p => p.Khxf.Contains(input.khxf)) | ||
| 629 | + .WhereIF(!string.IsNullOrEmpty(input.xfpc), p => p.Xfpc.Equals(input.xfpc)) | ||
| 630 | + .WhereIF(!string.IsNullOrEmpty(input.tjr), p => p.Tjr.Contains(input.tjr)) | ||
| 631 | + .WhereIF(!string.IsNullOrEmpty(input.jdqd), p => p.Jdqd.Contains(input.jdqd)) | ||
| 632 | + .WhereIF(!string.IsNullOrEmpty(input.lxdz), p => p.Lxdz.Contains(input.lxdz)) | ||
| 633 | + .WhereIF(!string.IsNullOrEmpty(input.bz), p => p.Bz.Contains(input.bz)) | ||
| 634 | + .WhereIF(queryYanglsr != null, p => p.Yanglsr >= new DateTime(startYanglsr.ToDate().Year, startYanglsr.ToDate().Month, startYanglsr.ToDate().Day, 0, 0, 0)) | ||
| 635 | + .WhereIF(queryYanglsr != null, p => p.Yanglsr <= new DateTime(endYanglsr.ToDate().Year, endYanglsr.ToDate().Month, endYanglsr.ToDate().Day, 23, 59, 59)) | ||
| 636 | + .Where(p => p.IsEffective == StatusEnum.有效.GetHashCode()) | ||
| 637 | + .OrderBy(sidx + " " + input.sort) | ||
| 638 | + .ToListAsync(); | ||
| 639 | + | ||
| 640 | + if (customerList == null || !customerList.Any()) | ||
| 641 | + { | ||
| 642 | + // 如果没有数据,返回空结果 | ||
| 643 | + var emptyExcelConfig = new ExcelConfig(); | ||
| 644 | + emptyExcelConfig.FileName = "客户资料导出_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls"; | ||
| 645 | + emptyExcelConfig.HeadFont = "微软雅黑"; | ||
| 646 | + emptyExcelConfig.HeadPoint = 10; | ||
| 647 | + emptyExcelConfig.IsAllSizeColumn = true; | ||
| 648 | + emptyExcelConfig.ColumnModel = new List<ExcelColumnModel>(); | ||
| 649 | + | ||
| 650 | + // 查找项目根目录并创建ExportFiles文件夹 | ||
| 651 | + var emptyBaseDir = AppContext.BaseDirectory; | ||
| 652 | + var emptyProjectRoot = emptyBaseDir; | ||
| 653 | + var emptyDir = new DirectoryInfo(emptyBaseDir); | ||
| 654 | + while (emptyDir != null && emptyDir.Parent != null) | ||
| 655 | + { | ||
| 656 | + try | ||
| 657 | + { | ||
| 658 | + if (emptyDir.GetDirectories(".git").Any() || emptyDir.GetFiles("*.sln").Any()) | ||
| 659 | + { | ||
| 660 | + emptyProjectRoot = emptyDir.FullName; | ||
| 661 | + break; | ||
| 662 | + } | ||
| 663 | + } | ||
| 664 | + catch { } | ||
| 665 | + emptyDir = emptyDir.Parent; | ||
| 666 | + } | ||
| 667 | + var emptyExportFilesPath = Path.Combine(emptyProjectRoot, "ExportFiles"); | ||
| 668 | + if (!Directory.Exists(emptyExportFilesPath)) | ||
| 669 | + { | ||
| 670 | + Directory.CreateDirectory(emptyExportFilesPath); | ||
| 671 | + } | ||
| 672 | + var emptyAddPath = Path.Combine(emptyExportFilesPath, emptyExcelConfig.FileName); | ||
| 673 | + ExcelExportHelper<LqKhxxExportOutput>.Export(new List<LqKhxxExportOutput>(), emptyExcelConfig, emptyAddPath); | ||
| 674 | + var emptyFileName = _userManager.UserId + "|" + emptyAddPath + "|xls"; | ||
| 675 | + return new { name = emptyExcelConfig.FileName, url = "/api/File/Download?encryption=" + DESCEncryption.Encrypt(emptyFileName, "NCC") }; | ||
| 676 | + } | ||
| 677 | + | ||
| 678 | + // 批量查询门店信息(优化:一次性查询所有需要的门店) | ||
| 679 | + var storeIds = customerList | ||
| 680 | + .Where(c => !string.IsNullOrEmpty(c.Gsmd)) | ||
| 681 | + .Select(c => c.Gsmd) | ||
| 682 | + .Distinct() | ||
| 683 | + .Where(id => !string.IsNullOrEmpty(id)) | ||
| 684 | + .ToList(); | ||
| 685 | + var storeDict = new Dictionary<string, LqMdxxEntity>(); | ||
| 686 | + if (storeIds != null && storeIds.Any()) | ||
| 687 | + { | ||
| 688 | + var stores = await _db.Queryable<LqMdxxEntity>() | ||
| 689 | + .Where(s => storeIds.Contains(s.Id) && !string.IsNullOrEmpty(s.Id)) | ||
| 690 | + .ToListAsync(); | ||
| 691 | + if (stores != null && stores.Any()) | ||
| 692 | + { | ||
| 693 | + foreach (var store in stores) | ||
| 694 | + { | ||
| 695 | + if (!string.IsNullOrEmpty(store.Id) && !storeDict.ContainsKey(store.Id)) | ||
| 696 | + { | ||
| 697 | + storeDict[store.Id] = store; | ||
| 698 | + } | ||
| 699 | + } | ||
| 700 | + } | ||
| 701 | + } | ||
| 702 | + | ||
| 703 | + // 批量查询推荐人信息 | ||
| 704 | + var tjrIds = customerList | ||
| 705 | + .Where(c => !string.IsNullOrEmpty(c.Tjr)) | ||
| 706 | + .Select(c => c.Tjr) | ||
| 707 | + .Distinct() | ||
| 708 | + .Where(id => !string.IsNullOrEmpty(id)) | ||
| 709 | + .ToList(); | ||
| 710 | + var tjrDict = new Dictionary<string, string>(); | ||
| 711 | + if (tjrIds != null && tjrIds.Any()) | ||
| 712 | + { | ||
| 713 | + var tjrs = await _db.Queryable<LqKhxxEntity>() | ||
| 714 | + .Where(t => tjrIds.Contains(t.Id) && !string.IsNullOrEmpty(t.Id)) | ||
| 715 | + .Select(t => new { t.Id, t.Khmc }) | ||
| 716 | + .ToListAsync(); | ||
| 717 | + if (tjrs != null && tjrs.Any()) | ||
| 718 | + { | ||
| 719 | + foreach (var tjr in tjrs) | ||
| 720 | + { | ||
| 721 | + if (!string.IsNullOrEmpty(tjr.Id) && !tjrDict.ContainsKey(tjr.Id)) | ||
| 722 | + { | ||
| 723 | + tjrDict[tjr.Id] = tjr.Khmc ?? ""; | ||
| 724 | + } | ||
| 725 | + } | ||
| 726 | + } | ||
| 727 | + } | ||
| 728 | + | ||
| 729 | + // 转换为导出DTO | ||
| 730 | + var queryResult = customerList.Select(kh => new | ||
| 731 | + { | ||
| 732 | + id = kh.Id, | ||
| 733 | + khmc = kh.Khmc, | ||
| 734 | + sjh = kh.Sjh, | ||
| 735 | + dah = kh.Dah, | ||
| 736 | + xb = kh.Xb, | ||
| 737 | + gsmdName = (!string.IsNullOrEmpty(kh.Gsmd) && storeDict.ContainsKey(kh.Gsmd)) ? (storeDict[kh.Gsmd].Dm ?? "") : "", | ||
| 738 | + zcsj = kh.Zcsj, | ||
| 739 | + khlx = kh.Khlx, | ||
| 740 | + khjd = kh.Khjd, | ||
| 741 | + khxf = kh.Khxf, | ||
| 742 | + xfpc = kh.Xfpc, | ||
| 743 | + tjrName = (!string.IsNullOrEmpty(kh.Tjr) && tjrDict.ContainsKey(kh.Tjr)) ? tjrDict[kh.Tjr] : "", | ||
| 744 | + jdqd = kh.Jdqd, | ||
| 745 | + lxdz = kh.Lxdz, | ||
| 746 | + bz = kh.Bz, | ||
| 747 | + yanglsr = kh.Yanglsr, | ||
| 748 | + firstVisitTime = kh.FirstVisitTime, | ||
| 749 | + lastVisitTime = kh.LastVisitTime, | ||
| 750 | + visitDays = kh.VisitDays, | ||
| 751 | + sleepDays = kh.SleepDays, | ||
| 752 | + totalBillingAmount = kh.TotalBillingAmount, | ||
| 753 | + remainingRightsAmount = kh.RemainingRightsAmount, | ||
| 754 | + // 门店归属信息,用于计算客户目前归属名称 | ||
| 755 | + syb = (!string.IsNullOrEmpty(kh.Gsmd) && storeDict.ContainsKey(kh.Gsmd)) ? (storeDict[kh.Gsmd].Syb ?? "") : "", | ||
| 756 | + jyb = (!string.IsNullOrEmpty(kh.Gsmd) && storeDict.ContainsKey(kh.Gsmd)) ? (storeDict[kh.Gsmd].Jyb ?? "") : "", | ||
| 757 | + kjb = (!string.IsNullOrEmpty(kh.Gsmd) && storeDict.ContainsKey(kh.Gsmd)) ? (storeDict[kh.Gsmd].Kjb ?? "") : "", | ||
| 758 | + dxmb = (!string.IsNullOrEmpty(kh.Gsmd) && storeDict.ContainsKey(kh.Gsmd)) ? (storeDict[kh.Gsmd].Dxmb ?? "") : "" | ||
| 759 | + }).ToList(); | ||
| 760 | + | ||
| 761 | + // 转换为导出DTO,并计算客户类型名称和客户目前归属名称 | ||
| 762 | + var exportData = queryResult.Select(item => new LqKhxxExportOutput | ||
| 763 | + { | ||
| 764 | + id = item.id, | ||
| 765 | + khmc = item.khmc, | ||
| 766 | + sjh = item.sjh, | ||
| 767 | + dah = item.dah, | ||
| 768 | + xb = item.xb, | ||
| 769 | + gsmdName = item.gsmdName, | ||
| 770 | + zcsj = item.zcsj, | ||
| 771 | + khlxName = GetMemberTypeName(item.khlx), | ||
| 772 | + khjd = item.khjd, | ||
| 773 | + khxf = item.khxf, | ||
| 774 | + xfpc = item.xfpc, | ||
| 775 | + tjrName = item.tjrName, | ||
| 776 | + jdqd = item.jdqd, | ||
| 777 | + lxdz = item.lxdz, | ||
| 778 | + bz = item.bz, | ||
| 779 | + yanglsr = item.yanglsr, | ||
| 780 | + firstVisitTime = item.firstVisitTime, | ||
| 781 | + lastVisitTime = item.lastVisitTime, | ||
| 782 | + visitDays = item.visitDays, | ||
| 783 | + sleepDays = item.sleepDays, | ||
| 784 | + totalBillingAmount = item.totalBillingAmount, | ||
| 785 | + remainingRightsAmount = item.remainingRightsAmount, | ||
| 786 | + khmqgsName = GetCustomerBelongName(item.syb, item.jyb, item.kjb, item.dxmb) | ||
| 787 | + }).ToList(); | ||
| 788 | + | ||
| 789 | + // 获取客户类型名称的辅助方法 | ||
| 790 | + string GetMemberTypeName(string khlx) | ||
| 791 | + { | ||
| 792 | + if (string.IsNullOrEmpty(khlx)) return "无"; | ||
| 793 | + if (int.TryParse(khlx, out var type)) | ||
| 794 | + { | ||
| 795 | + return NCC.Code.EnumHelper.GetEnumDesc<MemberTypeEnum>(type); | ||
| 796 | + } | ||
| 797 | + return "无"; | ||
| 798 | + } | ||
| 799 | + | ||
| 800 | + // 获取客户目前归属名称的辅助方法 | ||
| 801 | + string GetCustomerBelongName(string syb, string jyb, string kjb, string dxmb) | ||
| 802 | + { | ||
| 803 | + var belongList = new List<string>(); | ||
| 804 | + if (!string.IsNullOrEmpty(syb)) belongList.Add("事业部"); | ||
| 805 | + if (!string.IsNullOrEmpty(jyb)) belongList.Add("教育部"); | ||
| 806 | + if (!string.IsNullOrEmpty(kjb)) belongList.Add("科技部"); | ||
| 807 | + if (!string.IsNullOrEmpty(dxmb)) belongList.Add("大项目部"); | ||
| 808 | + return belongList.Count > 0 ? string.Join("、", belongList) : "无"; | ||
| 809 | + } | ||
| 810 | + | ||
| 811 | + // 定义导出字段映射 | ||
| 812 | + List<ParamsModel> paramList = new List<ParamsModel> | ||
| 813 | + { | ||
| 814 | + new ParamsModel { value = "客户编码", field = "id" }, | ||
| 815 | + new ParamsModel { value = "客户名称", field = "khmc" }, | ||
| 816 | + new ParamsModel { value = "手机号", field = "sjh" }, | ||
| 817 | + new ParamsModel { value = "档案号", field = "dah" }, | ||
| 818 | + new ParamsModel { value = "性别", field = "xb" }, | ||
| 819 | + new ParamsModel { value = "客户目前归属名称", field = "khmqgsName" }, | ||
| 820 | + new ParamsModel { value = "归属门店名称", field = "gsmdName" }, | ||
| 821 | + new ParamsModel { value = "注册时间", field = "zcsj" }, | ||
| 822 | + new ParamsModel { value = "客户类型名称", field = "khlxName" }, | ||
| 823 | + new ParamsModel { value = "客户阶段", field = "khjd" }, | ||
| 824 | + new ParamsModel { value = "客户消费", field = "khxf" }, | ||
| 825 | + new ParamsModel { value = "消费频次", field = "xfpc" }, | ||
| 826 | + new ParamsModel { value = "推荐人", field = "tjrName" }, | ||
| 827 | + new ParamsModel { value = "进店渠道", field = "jdqd" }, | ||
| 828 | + new ParamsModel { value = "联系地址", field = "lxdz" }, | ||
| 829 | + new ParamsModel { value = "备注", field = "bz" }, | ||
| 830 | + new ParamsModel { value = "阳历生日", field = "yanglsr" }, | ||
| 831 | + new ParamsModel { value = "首次到店时间", field = "firstVisitTime" }, | ||
| 832 | + new ParamsModel { value = "最后到店时间", field = "lastVisitTime" }, | ||
| 833 | + new ParamsModel { value = "到店天数", field = "visitDays" }, | ||
| 834 | + new ParamsModel { value = "沉睡天数", field = "sleepDays" }, | ||
| 835 | + new ParamsModel { value = "开单总金额", field = "totalBillingAmount" }, | ||
| 836 | + new ParamsModel { value = "剩余权益总金额", field = "remainingRightsAmount" } | ||
| 837 | + }; | ||
| 838 | + | ||
| 839 | + // 配置Excel导出 | ||
| 840 | + ExcelConfig excelconfig = new ExcelConfig(); | ||
| 841 | + excelconfig.FileName = "客户资料导出_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls"; | ||
| 842 | + excelconfig.HeadFont = "微软雅黑"; | ||
| 843 | + excelconfig.HeadPoint = 10; | ||
| 844 | + excelconfig.IsAllSizeColumn = true; | ||
| 845 | + excelconfig.ColumnModel = new List<ExcelColumnModel>(); | ||
| 846 | + | ||
| 847 | + // 添加所有字段到导出配置 | ||
| 848 | + foreach (var param in paramList) | ||
| 849 | + { | ||
| 850 | + excelconfig.ColumnModel.Add(new ExcelColumnModel() { Column = param.field, ExcelColumn = param.value }); | ||
| 851 | + } | ||
| 852 | + | ||
| 853 | + // 查找项目根目录 | ||
| 854 | + var baseDir = AppContext.BaseDirectory; | ||
| 855 | + var projectRoot = baseDir; | ||
| 856 | + var dir = new DirectoryInfo(baseDir); | ||
| 857 | + | ||
| 858 | + // 优先查找包含 .git 目录的目录(真正的项目根目录) | ||
| 859 | + while (dir != null && dir.Parent != null) | ||
| 860 | + { | ||
| 861 | + try | ||
| 862 | + { | ||
| 863 | + if (dir.GetDirectories(".git").Any()) | ||
| 864 | + { | ||
| 865 | + projectRoot = dir.FullName; | ||
| 866 | + break; | ||
| 867 | + } | ||
| 868 | + } | ||
| 869 | + catch | ||
| 870 | + { | ||
| 871 | + // 忽略访问错误,继续向上查找 | ||
| 872 | + } | ||
| 873 | + dir = dir.Parent; | ||
| 874 | + } | ||
| 875 | + | ||
| 876 | + // 如果没找到 .git 目录,再查找包含 .sln 文件的目录 | ||
| 877 | + if (projectRoot == baseDir) | ||
| 878 | + { | ||
| 879 | + dir = new DirectoryInfo(baseDir); | ||
| 880 | + while (dir != null && dir.Parent != null) | ||
| 881 | + { | ||
| 882 | + try | ||
| 883 | + { | ||
| 884 | + if (dir.GetFiles("*.sln").Any()) | ||
| 885 | + { | ||
| 886 | + projectRoot = dir.FullName; | ||
| 887 | + break; | ||
| 888 | + } | ||
| 889 | + } | ||
| 890 | + catch | ||
| 891 | + { | ||
| 892 | + // 忽略访问错误,继续向上查找 | ||
| 893 | + } | ||
| 894 | + dir = dir.Parent; | ||
| 895 | + } | ||
| 896 | + } | ||
| 897 | + | ||
| 898 | + // 在项目根目录下创建 ExportFiles 文件夹 | ||
| 899 | + var exportFilesPath = Path.Combine(projectRoot, "ExportFiles"); | ||
| 900 | + if (!Directory.Exists(exportFilesPath)) | ||
| 901 | + { | ||
| 902 | + Directory.CreateDirectory(exportFilesPath); | ||
| 903 | + } | ||
| 904 | + | ||
| 905 | + var addPath = Path.Combine(exportFilesPath, excelconfig.FileName); | ||
| 906 | + | ||
| 907 | + // 导出Excel文件 | ||
| 908 | + ExcelExportHelper<LqKhxxExportOutput>.Export(exportData, excelconfig, addPath); | ||
| 909 | + | ||
| 910 | + var fileName = _userManager.UserId + "|" + addPath + "|xls"; | ||
| 911 | + var output = new | ||
| 912 | + { | ||
| 913 | + name = excelconfig.FileName, | ||
| 914 | + url = "/api/File/Download?encryption=" + DESCEncryption.Encrypt(fileName, "NCC") | ||
| 915 | + }; | ||
| 916 | + | ||
| 917 | + return output; | ||
| 918 | + } | ||
| 919 | + #endregion | ||
| 920 | + | ||
| 589 | #region 批量删除客户资料 | 921 | #region 批量删除客户资料 |
| 590 | /// <summary> | 922 | /// <summary> |
| 591 | /// 批量删除客户资料 | 923 | /// 批量删除客户资料 |
script_runner.py
0 → 100755
| 1 | +#!/usr/bin/env python3 | ||
| 2 | +# -*- coding: utf-8 -*- | ||
| 3 | +""" | ||
| 4 | +Python脚本执行控制台工具 | ||
| 5 | +提供菜单界面,方便选择和执行项目中的Python脚本 | ||
| 6 | +""" | ||
| 7 | + | ||
| 8 | +import os | ||
| 9 | +import sys | ||
| 10 | +import subprocess | ||
| 11 | +from pathlib import Path | ||
| 12 | + | ||
| 13 | +# 脚本配置 | ||
| 14 | +SCRIPTS = [ | ||
| 15 | + { | ||
| 16 | + 'id': '1', | ||
| 17 | + 'name': '生成2025年12月客户开单数据Excel', | ||
| 18 | + 'file': 'generate_november_customer_excel.py', | ||
| 19 | + 'description': '生成2025年12月客户开单数据Excel文件,包含客户档案号、姓名、注册时间、来源、开单业绩、时间、归属门店等字段' | ||
| 20 | + }, | ||
| 21 | + { | ||
| 22 | + 'id': '2', | ||
| 23 | + 'name': '导出所有用户剩余权益数据', | ||
| 24 | + 'file': 'export_all_member_remaining_rights.py', | ||
| 25 | + 'description': '导出所有用户的剩余权益数据Excel文件,包含客户档案号、姓名、手机号、注册时间、归属门店、来源、品项名称、单价、总购买次数、已消费次数、已退卡次数、已扣除次数、剩余次数、剩余价值等字段' | ||
| 26 | + }, | ||
| 27 | + { | ||
| 28 | + 'id': '3', | ||
| 29 | + 'name': '测试天王组业绩完成度接口', | ||
| 30 | + 'file': 'test_tianwang_api.py', | ||
| 31 | + 'description': '测试天王组业绩完成度接口,查询2025年1月和当前月份的数据' | ||
| 32 | + } | ||
| 33 | +] | ||
| 34 | + | ||
| 35 | +def clear_screen(): | ||
| 36 | + """清屏""" | ||
| 37 | + os.system('clear' if os.name != 'nt' else 'cls') | ||
| 38 | + | ||
| 39 | +def print_header(): | ||
| 40 | + """打印标题""" | ||
| 41 | + print("=" * 70) | ||
| 42 | + print(" " * 20 + "Python脚本执行工具") | ||
| 43 | + print("=" * 70) | ||
| 44 | + print() | ||
| 45 | + | ||
| 46 | +def print_menu(): | ||
| 47 | + """打印菜单""" | ||
| 48 | + print_header() | ||
| 49 | + print("可用脚本列表:") | ||
| 50 | + print("-" * 70) | ||
| 51 | + for script in SCRIPTS: | ||
| 52 | + print(f" [{script['id']}] {script['name']}") | ||
| 53 | + print(f" {script['description']}") | ||
| 54 | + print() | ||
| 55 | + print("-" * 70) | ||
| 56 | + print(" [0] 退出") | ||
| 57 | + print("=" * 70) | ||
| 58 | + print() | ||
| 59 | + | ||
| 60 | +def get_script_by_id(script_id): | ||
| 61 | + """根据ID获取脚本配置""" | ||
| 62 | + for script in SCRIPTS: | ||
| 63 | + if script['id'] == script_id: | ||
| 64 | + return script | ||
| 65 | + return None | ||
| 66 | + | ||
| 67 | +def execute_script(script): | ||
| 68 | + """执行脚本""" | ||
| 69 | + script_path = Path(__file__).parent / script['file'] | ||
| 70 | + | ||
| 71 | + if not script_path.exists(): | ||
| 72 | + print(f"\n❌ 错误: 脚本文件不存在: {script_path}") | ||
| 73 | + input("\n按回车键继续...") | ||
| 74 | + return | ||
| 75 | + | ||
| 76 | + print(f"\n{'=' * 70}") | ||
| 77 | + print(f"正在执行: {script['name']}") | ||
| 78 | + print(f"脚本文件: {script['file']}") | ||
| 79 | + print(f"{'=' * 70}\n") | ||
| 80 | + | ||
| 81 | + try: | ||
| 82 | + # 使用subprocess执行脚本,保持输出实时显示 | ||
| 83 | + process = subprocess.Popen( | ||
| 84 | + [sys.executable, str(script_path)], | ||
| 85 | + stdout=subprocess.PIPE, | ||
| 86 | + stderr=subprocess.STDOUT, | ||
| 87 | + universal_newlines=True, | ||
| 88 | + bufsize=1 | ||
| 89 | + ) | ||
| 90 | + | ||
| 91 | + # 实时输出 | ||
| 92 | + for line in process.stdout: | ||
| 93 | + print(line, end='') | ||
| 94 | + | ||
| 95 | + process.wait() | ||
| 96 | + | ||
| 97 | + if process.returncode == 0: | ||
| 98 | + print(f"\n{'=' * 70}") | ||
| 99 | + print(f"✅ 脚本执行成功: {script['name']}") | ||
| 100 | + print(f"{'=' * 70}\n") | ||
| 101 | + else: | ||
| 102 | + print(f"\n{'=' * 70}") | ||
| 103 | + print(f"❌ 脚本执行失败: {script['name']} (退出码: {process.returncode})") | ||
| 104 | + print(f"{'=' * 70}\n") | ||
| 105 | + | ||
| 106 | + except Exception as e: | ||
| 107 | + print(f"\n❌ 执行脚本时发生错误: {e}\n") | ||
| 108 | + | ||
| 109 | + input("按回车键继续...") | ||
| 110 | + | ||
| 111 | +def main(): | ||
| 112 | + """主函数""" | ||
| 113 | + while True: | ||
| 114 | + clear_screen() | ||
| 115 | + print_menu() | ||
| 116 | + | ||
| 117 | + try: | ||
| 118 | + choice = input("请选择要执行的脚本 (输入数字): ").strip() | ||
| 119 | + | ||
| 120 | + if choice == '0': | ||
| 121 | + print("\n感谢使用,再见!\n") | ||
| 122 | + break | ||
| 123 | + | ||
| 124 | + script = get_script_by_id(choice) | ||
| 125 | + | ||
| 126 | + if script: | ||
| 127 | + clear_screen() | ||
| 128 | + execute_script(script) | ||
| 129 | + else: | ||
| 130 | + print(f"\n❌ 无效的选择: {choice}") | ||
| 131 | + input("按回车键继续...") | ||
| 132 | + | ||
| 133 | + except KeyboardInterrupt: | ||
| 134 | + print("\n\n程序已中断,再见!\n") | ||
| 135 | + break | ||
| 136 | + except Exception as e: | ||
| 137 | + print(f"\n❌ 发生错误: {e}") | ||
| 138 | + input("按回车键继续...") | ||
| 139 | + | ||
| 140 | +if __name__ == '__main__': | ||
| 141 | + main() | ||
| 142 | + |
test_customer_export_optimized.sh
0 → 100755
| 1 | +#!/bin/bash | ||
| 2 | + | ||
| 3 | +# 测试客户信息导出接口(优化版) | ||
| 4 | + | ||
| 5 | +echo "=== 测试客户信息导出接口(优化版) ===" | ||
| 6 | +echo "" | ||
| 7 | + | ||
| 8 | +# 1. 获取token | ||
| 9 | +echo "步骤1: 获取Token..." | ||
| 10 | +TOKEN_RESPONSE=$(curl -s -X POST "http://localhost:2011/api/oauth/Login" \ | ||
| 11 | + -H "Content-Type: application/x-www-form-urlencoded" \ | ||
| 12 | + -d "account=admin&password=e10adc3949ba59abbe56e057f20f883e") | ||
| 13 | + | ||
| 14 | +TOKEN=$(echo "$TOKEN_RESPONSE" | python3 -c "import sys, json; data=json.load(sys.stdin); print(data['data']['token'])" 2>/dev/null) | ||
| 15 | + | ||
| 16 | +if [ -z "$TOKEN" ]; then | ||
| 17 | + echo "❌ Token获取失败" | ||
| 18 | + echo "响应: $TOKEN_RESPONSE" | ||
| 19 | + exit 1 | ||
| 20 | +fi | ||
| 21 | + | ||
| 22 | +echo "✅ Token获取成功" | ||
| 23 | +echo "" | ||
| 24 | + | ||
| 25 | +# 2. 测试导出接口(不带参数,导出所有数据) | ||
| 26 | +echo "步骤2: 调用导出接口..." | ||
| 27 | +echo "接口: GET /api/Extend/LqKhxx/Actions/ExportOptimized" | ||
| 28 | +echo "" | ||
| 29 | + | ||
| 30 | +EXPORT_RESPONSE=$(curl -s -X GET "http://localhost:2011/api/Extend/LqKhxx/Actions/ExportOptimized?currentPage=1&pageSize=10" \ | ||
| 31 | + -H "Authorization: $TOKEN") | ||
| 32 | + | ||
| 33 | +echo "响应结果:" | ||
| 34 | +echo "$EXPORT_RESPONSE" | python3 -c " | ||
| 35 | +import sys, json | ||
| 36 | +try: | ||
| 37 | + data = json.load(sys.stdin) | ||
| 38 | + print(f\"状态码: {data.get('code', 'N/A')}\") | ||
| 39 | + print(f\"消息: {data.get('msg', 'N/A')}\") | ||
| 40 | + if 'data' in data: | ||
| 41 | + print(f\"文件名: {data['data'].get('name', 'N/A')}\") | ||
| 42 | + print(f\"下载URL: {data['data'].get('url', 'N/A')}\") | ||
| 43 | + if data['data'].get('url'): | ||
| 44 | + print(\"\\n✅ 导出成功!文件已生成\") | ||
| 45 | + else: | ||
| 46 | + print(\"\\n⚠️ 导出响应成功,但未返回文件URL\") | ||
| 47 | + else: | ||
| 48 | + print(\"\\n❌ 响应中未包含data字段\") | ||
| 49 | +except Exception as e: | ||
| 50 | + print(f'解析错误: {e}') | ||
| 51 | + print('原始响应:') | ||
| 52 | + sys.stdin.seek(0) | ||
| 53 | + print(sys.stdin.read()) | ||
| 54 | +" 2>/dev/null | ||
| 55 | + | ||
| 56 | +echo "" | ||
| 57 | + | ||
| 58 | +# 3. 测试带筛选条件的导出 | ||
| 59 | +echo "步骤3: 测试带筛选条件的导出(客户名称包含'测试')..." | ||
| 60 | +echo "" | ||
| 61 | + | ||
| 62 | +EXPORT_FILTERED_RESPONSE=$(curl -s -X GET "http://localhost:2011/api/Extend/LqKhxx/Actions/ExportOptimized?khmc=测试¤tPage=1&pageSize=10" \ | ||
| 63 | + -H "Authorization: $TOKEN") | ||
| 64 | + | ||
| 65 | +echo "响应结果:" | ||
| 66 | +echo "$EXPORT_FILTERED_RESPONSE" | python3 -c " | ||
| 67 | +import sys, json | ||
| 68 | +try: | ||
| 69 | + data = json.load(sys.stdin) | ||
| 70 | + print(f\"状态码: {data.get('code', 'N/A')}\") | ||
| 71 | + print(f\"消息: {data.get('msg', 'N/A')}\") | ||
| 72 | + if 'data' in data: | ||
| 73 | + print(f\"文件名: {data['data'].get('name', 'N/A')}\") | ||
| 74 | + print(f\"下载URL: {data['data'].get('url', 'N/A')}\") | ||
| 75 | + if data['data'].get('url'): | ||
| 76 | + print(\"\\n✅ 筛选导出成功!文件已生成\") | ||
| 77 | + else: | ||
| 78 | + print(\"\\n⚠️ 导出响应成功,但未返回文件URL\") | ||
| 79 | + else: | ||
| 80 | + print(\"\\n❌ 响应中未包含data字段\") | ||
| 81 | +except Exception as e: | ||
| 82 | + print(f'解析错误: {e}') | ||
| 83 | +" 2>/dev/null | ||
| 84 | + | ||
| 85 | +echo "" | ||
| 86 | +echo "=== 测试完成 ===" | ||
| 87 | +echo "" | ||
| 88 | +echo "提示: 如果导出成功,文件会保存在项目根目录的 ExportFiles 文件夹中" | ||
| 89 | + |