From 63526ab48d0ef093a9323beb1086c4dfcfb3eb59 Mon Sep 17 00:00:00 2001 From: “wangming” <“wangming@antissoft.com”> Date: Mon, 3 Aug 2026 00:04:32 +0800 Subject: [PATCH] fix: 门店目标归属统一存组织ID,并修复日报汇总与转卡品项统计 --- antis-ncc-admin/src/views/lqYcsdMdmbsd/components/edit-dialog.vue | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------- antis-ncc-admin/src/views/lqYcsdMdmbsd/index.vue | 13 ++++++++----- antis-ncc-admin/src/views/statisticsList/form1.vue | 5 +++++ antis-ncc-admin/src/views/statisticsList/form7.vue | 21 ++++++++++++++++++++- netcore/src/Modularity/Extend/NCC.Extend.Entitys/Dto/LqKdKdjlb/BillingRecordSummaryExportOutput.cs | 5 +++++ netcore/src/Modularity/Extend/NCC.Extend.Entitys/Dto/LqKdKdjlb/HealthCoachStatisticsOutput.cs | 5 +++++ netcore/src/Modularity/Extend/NCC.Extend/LqDailyReportService.cs | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------- netcore/src/Modularity/Extend/NCC.Extend/LqKdKdjlbService.cs | 136 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------------------------- netcore/src/Modularity/Extend/NCC.Extend/LqMdTargetService.cs | 200 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------- store-pc/src/components/StoreTargetDialog.vue | 47 +++++++++++++++++++++++++++++++++++++---------- 项目文档相关/sql/2026-08-02/lq_kd_pxmx_转卡品项回填yjsj.sql | 22 ++++++++++++++++++++++ 项目文档相关/sql/2026-08-02/lq_kd_pxmx_转卡品项补全yjsj.sql | 23 +++++++++++++++++++++++ 项目文档相关/sql/2026-08-02/lq_md_target_归属字段名称转组织ID.sql | 174 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 13 files changed, 696 insertions(+), 156 deletions(-) create mode 100644 项目文档相关/sql/2026-08-02/lq_kd_pxmx_转卡品项回填yjsj.sql create mode 100644 项目文档相关/sql/2026-08-02/lq_kd_pxmx_转卡品项补全yjsj.sql create mode 100644 项目文档相关/sql/2026-08-02/lq_md_target_归属字段名称转组织ID.sql diff --git a/antis-ncc-admin/src/views/lqYcsdMdmbsd/components/edit-dialog.vue b/antis-ncc-admin/src/views/lqYcsdMdmbsd/components/edit-dialog.vue index 6fcdc40..861aea4 100644 --- a/antis-ncc-admin/src/views/lqYcsdMdmbsd/components/edit-dialog.vue +++ b/antis-ncc-admin/src/views/lqYcsdMdmbsd/components/edit-dialog.vue @@ -443,16 +443,15 @@ export default { methods: { // 获取事业部选项 getBusinessUnitOptions() { - lqmdtargetApi.previewDataInterface('734724946332222725').then(res => { + return lqmdtargetApi.previewDataInterface('734724946332222725').then(res => { this.businessUnitOptions = res.data.list || [] }).catch(() => { this.businessUnitOptions = [] }) - }, // 获取科技部选项 getTechDepartmentOptions() { - lqmdtargetApi.previewDataInterface('734725044101448965').then(res => { + return lqmdtargetApi.previewDataInterface('734725044101448965').then(res => { this.techDepartmentOptions = res.data.list || [] }).catch(() => { this.techDepartmentOptions = [] @@ -460,7 +459,7 @@ export default { }, // 获取教育部选项 getEducationDepartmentOptions() { - lqmdtargetApi.previewDataInterface('734725010941281541').then(res => { + return lqmdtargetApi.previewDataInterface('734725010941281541').then(res => { this.educationDepartmentOptions = res.data.list || [] }).catch(() => { this.educationDepartmentOptions = [] @@ -468,7 +467,7 @@ export default { }, // 获取大项目部选项 getMajorProjectDepartmentOptions() { - lqmdtargetApi.previewDataInterface('734725079920805125').then(res => { + return lqmdtargetApi.previewDataInterface('734725079920805125').then(res => { this.majorProjectDepartmentOptions = res.data.list || [] }).catch(() => { this.majorProjectDepartmentOptions = [] @@ -484,36 +483,71 @@ export default { this.managerList = [] } }, + // 将历史名称或异常值归一化为组织ID(下拉选项 value 均为 id) + coerceOrganizeFields() { + const fieldMap = [ + ['businessUnit', this.businessUnitOptions], + ['techDepartment', this.techDepartmentOptions], + ['educationDepartment', this.educationDepartmentOptions], + ['majorProjectDepartment', this.majorProjectDepartmentOptions] + ] + fieldMap.forEach(([field, options]) => { + const val = this.localFormData[field] + if (!val || !options || options.length === 0) return + if (/^\d{15,}$/.test(String(val))) return + const option = options.find(item => + item.fullName === val || item.FullName === val || item.id === val || item.Id === val + ) + if (option) { + this.localFormData[field] = option.id || option.Id + } + }) + }, // 初始化表单数据 - initFormData() { - this.getBusinessUnitOptions() - this.getTechDepartmentOptions() - this.getEducationDepartmentOptions() - this.getMajorProjectDepartmentOptions() - this.getManagerList() + async initFormData() { + await Promise.all([ + this.getBusinessUnitOptions(), + this.getTechDepartmentOptions(), + this.getEducationDepartmentOptions(), + this.getMajorProjectDepartmentOptions(), + this.getManagerList() + ]) if (this.isEdit && this.formData) { - // 编辑模式,填充现有数据 + let row = this.formData + const id = this.formData.id || this.formData.Id + if (id) { + try { + const res = await lqmdtargetApi.getById(id) + if (res && res.data) { + row = res.data + } + } catch (error) { + console.error('获取门店目标详情失败:', error) + } + } + // 编辑模式,填充现有数据(详情接口返回组织ID,避免列表展示名称被误保存) this.localFormData = { - storeId: this.formData.storeId || this.formData.StoreId || '', - month: this.formData.month || this.formData.Month || '', - businessUnit: this.formData.businessUnit || this.formData.BusinessUnit || '', - techDepartment: this.formData.techDepartment || this.formData.TechDepartment || '', - educationDepartment: this.formData.educationDepartment || this.formData.EducationDepartment || '', - majorProjectDepartment: this.formData.majorProjectDepartment || this.formData.MajorProjectDepartment || '', - businessUnitTarget: this.formData.businessUnitTarget !== undefined ? Number(this.formData.businessUnitTarget) : 0, - techDepartmentTarget: this.formData.techDepartmentTarget !== undefined ? Number(this.formData.techDepartmentTarget) : 0, - educationDepartmentTarget: this.formData.educationDepartmentTarget !== undefined ? Number(this.formData.educationDepartmentTarget) : 0, - majorProjectDepartmentTarget: this.formData.majorProjectDepartmentTarget !== undefined ? Number(this.formData.majorProjectDepartmentTarget) : 0, - businessUnitGeneralManager: this.formData.businessUnitGeneralManager || this.formData.BusinessUnitGeneralManager || '', - businessUnitManager: this.formData.businessUnitManager || this.formData.BusinessUnitManager || '', - storeTarget: this.formData.storeTarget !== undefined ? Number(this.formData.storeTarget) : 0, - storeLifeline: this.formData.storeLifeline !== undefined ? Number(this.formData.storeLifeline) : 0, - storeConsumeTarget: this.formData.storeConsumeTarget !== undefined ? Number(this.formData.storeConsumeTarget) : 0, - storeProjectTarget: this.formData.storeProjectTarget !== undefined ? Number(this.formData.storeProjectTarget) : 0, - storeHeadcountTarget: this.formData.storeHeadcountTarget !== undefined ? Number(this.formData.storeHeadcountTarget) : 0, - assistantHeadcountTargetStage1: this.formData.assistantHeadcountTargetStage1 !== undefined ? Number(this.formData.assistantHeadcountTargetStage1) : 0, - assistantHeadcountTargetStage2: this.formData.assistantHeadcountTargetStage2 !== undefined ? Number(this.formData.assistantHeadcountTargetStage2) : 0 + storeId: row.storeId || row.StoreId || '', + month: row.month || row.Month || '', + businessUnit: row.businessUnit || row.BusinessUnit || '', + techDepartment: row.techDepartment || row.TechDepartment || '', + educationDepartment: row.educationDepartment || row.EducationDepartment || '', + majorProjectDepartment: row.majorProjectDepartment || row.MajorProjectDepartment || '', + businessUnitTarget: row.businessUnitTarget !== undefined ? Number(row.businessUnitTarget) : 0, + techDepartmentTarget: row.techDepartmentTarget !== undefined ? Number(row.techDepartmentTarget) : 0, + educationDepartmentTarget: row.educationDepartmentTarget !== undefined ? Number(row.educationDepartmentTarget) : 0, + majorProjectDepartmentTarget: row.majorProjectDepartmentTarget !== undefined ? Number(row.majorProjectDepartmentTarget) : 0, + businessUnitGeneralManager: row.businessUnitGeneralManager || row.BusinessUnitGeneralManager || '', + businessUnitManager: row.businessUnitManager || row.BusinessUnitManager || '', + storeTarget: row.storeTarget !== undefined ? Number(row.storeTarget) : 0, + storeLifeline: row.storeLifeline !== undefined ? Number(row.storeLifeline) : 0, + storeConsumeTarget: row.storeConsumeTarget !== undefined ? Number(row.storeConsumeTarget) : 0, + storeProjectTarget: row.storeProjectTarget !== undefined ? Number(row.storeProjectTarget) : 0, + storeHeadcountTarget: row.storeHeadcountTarget !== undefined ? Number(row.storeHeadcountTarget) : 0, + assistantHeadcountTargetStage1: row.assistantHeadcountTargetStage1 !== undefined ? Number(row.assistantHeadcountTargetStage1) : 0, + assistantHeadcountTargetStage2: row.assistantHeadcountTargetStage2 !== undefined ? Number(row.assistantHeadcountTargetStage2) : 0 } + this.coerceOrganizeFields() } else { // 新增模式,重置表单 this.localFormData = { diff --git a/antis-ncc-admin/src/views/lqYcsdMdmbsd/index.vue b/antis-ncc-admin/src/views/lqYcsdMdmbsd/index.vue index aec64cf..1378938 100644 --- a/antis-ncc-admin/src/views/lqYcsdMdmbsd/index.vue +++ b/antis-ncc-admin/src/views/lqYcsdMdmbsd/index.vue @@ -397,11 +397,14 @@ export default { }) }, - // 获取部门名称 - getDepartmentName(id, options) { - if (!id || !options || options.length === 0) return '' - const option = options.find(item => item.id === id || item.Id === id) - return option ? (option.fullName || option.FullName) : '' + // 获取部门名称:优先按组织ID匹配选项;后端已解析为名称或库内仅存名称时直接展示 + getDepartmentName(value, options) { + if (!value) return '' + if (options && options.length > 0) { + const option = options.find(item => item.id === value || item.Id === value) + if (option) return option.fullName || option.FullName || value + } + return value }, // 获取列表数据 diff --git a/antis-ncc-admin/src/views/statisticsList/form1.vue b/antis-ncc-admin/src/views/statisticsList/form1.vue index 49ec6f3..1ead48f 100644 --- a/antis-ncc-admin/src/views/statisticsList/form1.vue +++ b/antis-ncc-admin/src/views/statisticsList/form1.vue @@ -844,6 +844,7 @@ export default { // 构建表头 const headers = [ + '开单ID', '开单日期', '客户姓名', '客户电话', @@ -875,6 +876,7 @@ export default { this.records.forEach(row => { // 获取基础数据 const baseData = { + billingId: row.id || '无', date: this.formatDate(row.date) || '无', customerName: row.customerName || '无', customerPhone: row.customerPhone || '无', @@ -916,6 +918,7 @@ export default { // 如果没有健康师,创建一行空数据 if (healthTeachers.length === 0) { const rowData = [ + baseData.billingId, baseData.date, baseData.customerName, baseData.customerPhone, @@ -973,6 +976,7 @@ export default { // 构建行数据(所有行都显示完整数据,补全空白部分) const rowData = [ + baseData.billingId, // 开单ID baseData.date, // 开单日期 baseData.customerName, // 客户姓名 baseData.customerPhone, // 客户电话 @@ -1010,6 +1014,7 @@ export default { // 设置列宽 ws['!cols'] = [ + { wch: 22 }, // 开单ID { wch: 12 }, // 开单日期 { wch: 12 }, // 客户姓名 { wch: 15 }, // 客户电话 diff --git a/antis-ncc-admin/src/views/statisticsList/form7.vue b/antis-ncc-admin/src/views/statisticsList/form7.vue index 4be8baf..381e3e6 100644 --- a/antis-ncc-admin/src/views/statisticsList/form7.vue +++ b/antis-ncc-admin/src/views/statisticsList/form7.vue @@ -241,7 +241,21 @@ :data="list" has-c > - + + + @@ -965,6 +979,7 @@ export default { sheetName: '门店综合统计', columns: [ { label: '员工姓名', prop: 'employeeName' }, + { label: '在职状态', formatter: row => (row.isOnJob === 0 ? '已离职' : '在职') }, { label: '门店名称', prop: 'storeName' }, { label: '部门名称', prop: 'departmentName' }, { label: '金三角名称', prop: 'goldTriangleName' }, @@ -1137,6 +1152,10 @@ export default { } } +.employee-status-tag { + margin-left: 6px; +} + // 搜索框区域样式 .NCC-common-search-box { .el-form-item { diff --git a/netcore/src/Modularity/Extend/NCC.Extend.Entitys/Dto/LqKdKdjlb/BillingRecordSummaryExportOutput.cs b/netcore/src/Modularity/Extend/NCC.Extend.Entitys/Dto/LqKdKdjlb/BillingRecordSummaryExportOutput.cs index 313e386..683463f 100644 --- a/netcore/src/Modularity/Extend/NCC.Extend.Entitys/Dto/LqKdKdjlb/BillingRecordSummaryExportOutput.cs +++ b/netcore/src/Modularity/Extend/NCC.Extend.Entitys/Dto/LqKdKdjlb/BillingRecordSummaryExportOutput.cs @@ -8,6 +8,11 @@ namespace NCC.Extend.Entitys.Dto.LqKdKdjlb public class BillingRecordSummaryExportOutput { /// + /// 开单ID + /// + public string BillingId { get; set; } + + /// /// 开单日期 /// public string BillingDate { get; set; } diff --git a/netcore/src/Modularity/Extend/NCC.Extend.Entitys/Dto/LqKdKdjlb/HealthCoachStatisticsOutput.cs b/netcore/src/Modularity/Extend/NCC.Extend.Entitys/Dto/LqKdKdjlb/HealthCoachStatisticsOutput.cs index f85796b..a43237e 100644 --- a/netcore/src/Modularity/Extend/NCC.Extend.Entitys/Dto/LqKdKdjlb/HealthCoachStatisticsOutput.cs +++ b/netcore/src/Modularity/Extend/NCC.Extend.Entitys/Dto/LqKdKdjlb/HealthCoachStatisticsOutput.cs @@ -119,5 +119,10 @@ namespace NCC.Extend.Entitys.Dto.LqKdKdjlb /// 队伍业绩占比 - 该健康师所在金三角的业绩占门店总业绩的比例(百分比,0-100) /// public decimal teamPerformanceRatio { get; set; } + + /// + /// 是否在职(BASE_USER.F_IsOnJob,0=离职,null 视为在职) + /// + public int? isOnJob { get; set; } } } diff --git a/netcore/src/Modularity/Extend/NCC.Extend/LqDailyReportService.cs b/netcore/src/Modularity/Extend/NCC.Extend/LqDailyReportService.cs index a37a1f9..f034dd4 100644 --- a/netcore/src/Modularity/Extend/NCC.Extend/LqDailyReportService.cs +++ b/netcore/src/Modularity/Extend/NCC.Extend/LqDailyReportService.cs @@ -378,7 +378,7 @@ namespace NCC.Extend COALESCE(SUM(target.F_BusinessUnitTarget), 0) as TargetPerformance, COUNT(DISTINCT target.F_StoreId) as StoreCount FROM base_organize o - LEFT JOIN lq_md_target target ON target.F_BusinessUnit = o.F_Id AND target.F_Month = '{month}' + LEFT JOIN lq_md_target target ON (target.F_BusinessUnit = o.F_Id OR target.F_BusinessUnit = o.F_FullName) AND target.F_Month = '{month}' WHERE o.F_Category = 'department' AND (o.F_DeleteMark IS NULL OR o.F_DeleteMark != 1) AND o.F_FullName IN ('事业一部', '事业二部', '事业三部', '事业四部', '事业五部', '事业六部') @@ -418,20 +418,21 @@ namespace NCC.Extend var billingPerformanceSql = $@" SELECT - target.F_BusinessUnit as BusinessUnitId, + o.F_Id as BusinessUnitId, COALESCE(SUM(billing.sfyj), 0) as BillingPerformance FROM lq_kd_kdjlb billing INNER JOIN lq_md_target target ON billing.djmd = target.F_StoreId AND target.F_Month = '{month}' - INNER JOIN base_organize o ON target.F_BusinessUnit = o.F_Id + INNER JOIN base_organize o ON (target.F_BusinessUnit = o.F_Id OR target.F_BusinessUnit = o.F_FullName) WHERE billing.F_IsEffective = 1 AND target.F_BusinessUnit IS NOT NULL + AND target.F_BusinessUnit != '' AND o.F_Category = 'department' AND (o.F_DeleteMark IS NULL OR o.F_DeleteMark != 1) AND o.F_FullName IN ('事业一部', '事业二部', '事业三部', '事业四部', '事业五部', '事业六部') AND DATE(billing.kdrq) >= '{startDate:yyyy-MM-dd}' AND DATE(billing.kdrq) <= '{endDate:yyyy-MM-dd}' - AND target.F_BusinessUnit IN ('{unitIdsStr}') - GROUP BY target.F_BusinessUnit"; + AND o.F_Id IN ('{unitIdsStr}') + GROUP BY o.F_Id"; var billingPerformanceData = await _db.Ado.SqlQueryAsync(billingPerformanceSql); @@ -439,20 +440,21 @@ namespace NCC.Extend // 重要:门店归属事业部应该从门店目标表(lq_md_target)中获取,不同月份的归属可能不一样 var refundPerformanceSql = $@" SELECT - target.F_BusinessUnit as BusinessUnitId, + o.F_Id as BusinessUnitId, COALESCE(SUM(COALESCE(refund.F_ActualRefundAmount, refund.tkje, 0)), 0) as RefundPerformance FROM lq_hytk_hytk refund INNER JOIN lq_md_target target ON refund.md = target.F_StoreId AND target.F_Month = '{month}' - INNER JOIN base_organize o ON target.F_BusinessUnit = o.F_Id + INNER JOIN base_organize o ON (target.F_BusinessUnit = o.F_Id OR target.F_BusinessUnit = o.F_FullName) WHERE refund.F_IsEffective = 1 AND target.F_BusinessUnit IS NOT NULL + AND target.F_BusinessUnit != '' AND o.F_Category = 'department' AND (o.F_DeleteMark IS NULL OR o.F_DeleteMark != 1) AND o.F_FullName IN ('事业一部', '事业二部', '事业三部', '事业四部', '事业五部', '事业六部') AND DATE(refund.tksj) >= '{startDate:yyyy-MM-dd}' AND DATE(refund.tksj) <= '{endDate:yyyy-MM-dd}' - AND target.F_BusinessUnit IN ('{unitIdsStr}') - GROUP BY target.F_BusinessUnit"; + AND o.F_Id IN ('{unitIdsStr}') + GROUP BY o.F_Id"; var refundPerformanceData = await _db.Ado.SqlQueryAsync(refundPerformanceSql); @@ -588,7 +590,7 @@ namespace NCC.Extend COALESCE(SUM(target.{deptType.Value.targetFieldName}), 0) as TargetPerformance, COUNT(DISTINCT target.F_StoreId) as StoreCount FROM base_organize o - LEFT JOIN lq_md_target target ON target.{deptType.Value.fieldName} = o.F_Id AND target.F_Month = '{month}' + LEFT JOIN lq_md_target target ON (target.{deptType.Value.fieldName} = o.F_Id OR target.{deptType.Value.fieldName} = o.F_FullName) AND target.F_Month = '{month}' WHERE o.F_Category = 'department' AND (o.F_DeleteMark IS NULL OR o.F_DeleteMark != 1) AND o.F_FullName IN ('{deptNamesStr}') @@ -627,7 +629,8 @@ namespace NCC.Extend } // 第二步:优化策略 - 先获取门店列表,只查询这些门店的数据,大幅提升性能 - var deptIdsStr = string.Join("','", departmentDict.Keys); + var organizeAliasToIdMap = await BuildOrganizeAliasToIdMapAsync(departmentDict.Keys); + var deptAliasInClause = string.Join("','", organizeAliasToIdMap.Keys.Where(x => !string.IsNullOrEmpty(x))); // 2.1 先快速获取有目标记录的门店ID列表(使用索引,非常快,通常<100ms) var storeIdsSql = $@" @@ -635,9 +638,9 @@ namespace NCC.Extend FROM lq_md_target WHERE F_Month = '{month}' AND ( - F_EducationDepartment IN ('{deptIdsStr}') - OR F_TechDepartment IN ('{deptIdsStr}') - OR F_MajorProjectDepartment IN ('{deptIdsStr}') + F_EducationDepartment IN ('{deptAliasInClause}') + OR F_TechDepartment IN ('{deptAliasInClause}') + OR F_MajorProjectDepartment IN ('{deptAliasInClause}') )"; var storeIdsData = await _db.Ado.SqlQueryAsync(storeIdsSql); @@ -758,9 +761,13 @@ namespace NCC.Extend else if (deptField == "F_MajorProjectDepartment") targetDeptId = target?.F_MajorProjectDepartment?.ToString(); - if (!string.IsNullOrEmpty(targetDeptId) && departmentDict.ContainsKey(targetDeptId)) + if (!string.IsNullOrEmpty(targetDeptId)) { - departmentDict[targetDeptId].BillingPerformance += storeAmount; + var resolvedDeptId = ResolveOrganizeId(targetDeptId, organizeAliasToIdMap); + if (!string.IsNullOrEmpty(resolvedDeptId) && departmentDict.ContainsKey(resolvedDeptId)) + { + departmentDict[resolvedDeptId].BillingPerformance += storeAmount; + } } } } @@ -807,9 +814,13 @@ namespace NCC.Extend else if (deptField == "F_MajorProjectDepartment") targetDeptId = target?.F_MajorProjectDepartment?.ToString(); - if (!string.IsNullOrEmpty(targetDeptId) && departmentDict.ContainsKey(targetDeptId)) + if (!string.IsNullOrEmpty(targetDeptId)) { - departmentDict[targetDeptId].RefundPerformance += storeRefundAmount; + var resolvedDeptId = ResolveOrganizeId(targetDeptId, organizeAliasToIdMap); + if (!string.IsNullOrEmpty(resolvedDeptId) && departmentDict.ContainsKey(resolvedDeptId)) + { + departmentDict[resolvedDeptId].RefundPerformance += storeRefundAmount; + } } } } @@ -857,9 +868,13 @@ namespace NCC.Extend else if (deptField == "F_MajorProjectDepartment") targetDeptId = target?.F_MajorProjectDepartment?.ToString(); - if (!string.IsNullOrEmpty(targetDeptId) && departmentDict.ContainsKey(targetDeptId)) + if (!string.IsNullOrEmpty(targetDeptId)) { - departmentDict[targetDeptId].DeductAmount += storeDeductAmount; + var resolvedDeptId = ResolveOrganizeId(targetDeptId, organizeAliasToIdMap); + if (!string.IsNullOrEmpty(resolvedDeptId) && departmentDict.ContainsKey(resolvedDeptId)) + { + departmentDict[resolvedDeptId].DeductAmount += storeDeductAmount; + } } } } @@ -1605,7 +1620,7 @@ namespace NCC.Extend (billing, target, store, org) => billing.Djmd == target.StoreId && target.Month == month - && target.BusinessUnit == org.Id + && (target.BusinessUnit == org.Id || target.BusinessUnit == org.FullName) && billing.Djmd == store.Id) .Where((billing, target, store, org) => billing.IsEffective == StatusEnum.有效.GetHashCode()) .Where((billing, target, store, org) => billing.Sfyj > 0) // 只统计有金额的开单 @@ -1973,6 +1988,54 @@ namespace NCC.Extend } #endregion + /// + /// 构建组织别名(ID/名称)到组织ID的映射,兼容 lq_md_target 归属字段混存 ID 与名称。 + /// + private async Task> BuildOrganizeAliasToIdMapAsync(IEnumerable organizeIds) + { + var ids = organizeIds? + .Where(x => !string.IsNullOrWhiteSpace(x)) + .Distinct() + .ToList() ?? new List(); + + if (!ids.Any()) + { + return new Dictionary(StringComparer.Ordinal); + } + + var orgs = await _db.Queryable() + .Where(x => ids.Contains(x.Id) && x.DeleteMark == null) + .Select(x => new { x.Id, x.FullName }) + .ToListAsync(); + + var map = new Dictionary(StringComparer.Ordinal); + foreach (var org in orgs) + { + map[org.Id] = org.Id; + if (!string.IsNullOrWhiteSpace(org.FullName)) + { + map[org.FullName] = org.Id; + } + } + + return map; + } + + /// + /// 将 lq_md_target 中的组织归属值(ID 或名称)解析为标准组织ID。 + /// + private static string ResolveOrganizeId(string rawValue, Dictionary aliasToIdMap) + { + if (string.IsNullOrWhiteSpace(rawValue)) + { + return null; + } + + return aliasToIdMap != null && aliasToIdMap.TryGetValue(rawValue, out var id) + ? id + : rawValue; + } + } } diff --git a/netcore/src/Modularity/Extend/NCC.Extend/LqKdKdjlbService.cs b/netcore/src/Modularity/Extend/NCC.Extend/LqKdKdjlbService.cs index 234a1a2..a0a801f 100644 --- a/netcore/src/Modularity/Extend/NCC.Extend/LqKdKdjlbService.cs +++ b/netcore/src/Modularity/Extend/NCC.Extend/LqKdKdjlbService.cs @@ -381,6 +381,7 @@ namespace NCC.Extend.LqKdKdjlb { exportData.Add(new BillingRecordSummaryExportOutput { + BillingId = billing.id ?? "无", BillingDate = FormatDate(billing.kdrq), StoreName = billing.djmdName ?? "无", CustomerName = billing.kdhyc ?? "无", @@ -416,6 +417,7 @@ namespace NCC.Extend.LqKdKdjlb { exportData.Add(new BillingRecordSummaryExportOutput { + BillingId = billing.id ?? "无", BillingDate = FormatDate(billing.kdrq), StoreName = billing.djmdName ?? "无", CustomerName = billing.kdhyc ?? "无", @@ -461,7 +463,7 @@ namespace NCC.Extend.LqKdKdjlb // 配置Excel导出 List paramList = - "[{\"value\":\"开单日期\",\"field\":\"BillingDate\"},{\"value\":\"单据门店\",\"field\":\"StoreName\"},{\"value\":\"客户姓名\",\"field\":\"CustomerName\"},{\"value\":\"客户电话\",\"field\":\"CustomerPhone\"},{\"value\":\"客户档案号\",\"field\":\"CustomerArchiveNo\"},{\"value\":\"进店渠道\",\"field\":\"CustomerChannel\"},{\"value\":\"金三角\",\"field\":\"GoldTriangle\"},{\"value\":\"客户类型\",\"field\":\"CustomerType\"},{\"value\":\"健康师\",\"field\":\"HealthCoach\"},{\"value\":\"业绩金额\",\"field\":\"PerformanceAmount\"},{\"value\":\"相关品项\",\"field\":\"RelatedItem\"},{\"value\":\"分类\",\"field\":\"Category\"},{\"value\":\"类型\",\"field\":\"Type\"},{\"value\":\"单价\",\"field\":\"UnitPrice\"},{\"value\":\"次数\",\"field\":\"ProjectNumber\"},{\"value\":\"品项金额\",\"field\":\"ItemAmount\"},{\"value\":\"已付金额\",\"field\":\"PaidAmount\"},{\"value\":\"欠款金额\",\"field\":\"DebtAmount\"},{\"value\":\"总金额\",\"field\":\"TotalAmount\"},{\"value\":\"支付方式\",\"field\":\"PaymentMethod\"},{\"value\":\"合作机构\",\"field\":\"CooperationInstitution\"},{\"value\":\"结算机构\",\"field\":\"SettlementInstitution\"},{\"value\":\"活动名称\",\"field\":\"ActivityName\"},{\"value\":\"备注\",\"field\":\"Remark\"},{\"value\":\"开单时间\",\"field\":\"CreateTime\"}]".ToList(); + "[{\"value\":\"开单ID\",\"field\":\"BillingId\"},{\"value\":\"开单日期\",\"field\":\"BillingDate\"},{\"value\":\"单据门店\",\"field\":\"StoreName\"},{\"value\":\"客户姓名\",\"field\":\"CustomerName\"},{\"value\":\"客户电话\",\"field\":\"CustomerPhone\"},{\"value\":\"客户档案号\",\"field\":\"CustomerArchiveNo\"},{\"value\":\"进店渠道\",\"field\":\"CustomerChannel\"},{\"value\":\"金三角\",\"field\":\"GoldTriangle\"},{\"value\":\"客户类型\",\"field\":\"CustomerType\"},{\"value\":\"健康师\",\"field\":\"HealthCoach\"},{\"value\":\"业绩金额\",\"field\":\"PerformanceAmount\"},{\"value\":\"相关品项\",\"field\":\"RelatedItem\"},{\"value\":\"分类\",\"field\":\"Category\"},{\"value\":\"类型\",\"field\":\"Type\"},{\"value\":\"单价\",\"field\":\"UnitPrice\"},{\"value\":\"次数\",\"field\":\"ProjectNumber\"},{\"value\":\"品项金额\",\"field\":\"ItemAmount\"},{\"value\":\"已付金额\",\"field\":\"PaidAmount\"},{\"value\":\"欠款金额\",\"field\":\"DebtAmount\"},{\"value\":\"总金额\",\"field\":\"TotalAmount\"},{\"value\":\"支付方式\",\"field\":\"PaymentMethod\"},{\"value\":\"合作机构\",\"field\":\"CooperationInstitution\"},{\"value\":\"结算机构\",\"field\":\"SettlementInstitution\"},{\"value\":\"活动名称\",\"field\":\"ActivityName\"},{\"value\":\"备注\",\"field\":\"Remark\"},{\"value\":\"开单时间\",\"field\":\"CreateTime\"}]".ToList(); ExcelConfig excelconfig = new ExcelConfig(); excelconfig.FileName = "开单记录汇总_" + DateTime.Now.ToString("yyyy-MM-dd_HHmmss") + ".xls"; @@ -3282,6 +3284,7 @@ namespace NCC.Extend.LqKdKdjlb var detailedRecords = billingRecords.Select(billing => new { // 基本信息 + id = billing.id, date = billing.kdrq?.ToString("yyyy-MM-dd"), customerName = billing.kdhyc, customerPhone = billing.kdhysjh, @@ -3997,6 +4000,7 @@ namespace NCC.Extend.LqKdKdjlb TotalPrice = transferAmount, ActualPrice = transferAmount, IsEffective = StatusEnum.有效.GetHashCode(), + Yjsj = transferTime, Remark = $"从会员 {fromMember.Khmc} 转入", ItemCategory = await _db.Queryable().Where(x => x.Id == refundPxmxEntity.Px).Select(x => x.Qt2).FirstAsync(), PerformanceType = await _db.Queryable().Where(x => x.Id == refundPxmxEntity.Px).Select(x => x.Fl3).FirstAsync() ?? "", @@ -4259,6 +4263,10 @@ namespace NCC.Extend.LqKdKdjlb /// - employeeName: 健康师姓名(可选) /// - currentPage / pageSize: 分页(数据库层 LIMIT;pageSize 最大 500,避免一次拉全量健康师导致超时) /// + /// 人员范围(与 historical-on-job-inference 一致,按统计起止时间): + /// - 仅展示统计时段内曾在职的健康师:入职不晚于 endTime,且(无离职日或离职日≥startTime) + /// - 统计开始前已离职的员工不展示;时段内中途离职者仍展示其时段内业绩,isOnJob=0 时前端可标「已离职」 + /// /// 返回字段说明: /// - EmployeeId: 健康师ID /// - EmployeeName: 健康师姓名 @@ -4310,6 +4318,7 @@ namespace NCC.Extend.LqKdKdjlb SELECT u.F_Id as EmployeeId, u.F_REALNAME as EmployeeName, + u.F_IsOnJob as IsOnJob, u.F_MDID as StoreId, md.dm as StoreName, COALESCE(target.F_BusinessUnit, md.syb) as DepartmentId, @@ -4654,6 +4663,12 @@ namespace NCC.Extend.LqKdKdjlb var filterSql = conditions.Any() ? " AND " + string.Join(" AND ", conditions) : string.Empty; + // 统计时段内在职口径:不依赖当前 F_IsOnJob,避免历史月误杀;时段开始前已离职的不展示 + const string periodOnJobSql = @" + AND u.F_DeleteMark IS NULL + AND u.F_ENTRYDATE IS NOT NULL AND u.F_ENTRYDATE <= @endTime + AND (u.F_LeaveDate IS NULL OR u.F_LeaveDate >= @startTime)"; + var pageIndex = input.currentPage < 1 ? 1 : input.currentPage; var pageSize = input.pageSize < 1 ? 20 : Math.Min(input.pageSize, 500); var offset = (pageIndex - 1) * pageSize; @@ -4662,7 +4677,7 @@ namespace NCC.Extend.LqKdKdjlb SELECT COUNT(1) FROM BASE_USER u LEFT JOIN lq_mdxx md ON u.F_MDID = md.F_Id - WHERE u.F_GW = '健康师'" + filterSql; + WHERE u.F_GW = '健康师'" + periodOnJobSql + filterSql; var totalCount = await _db.Ado.GetIntAsync(countSql, baseParameters); @@ -4670,7 +4685,7 @@ namespace NCC.Extend.LqKdKdjlb SELECT u.F_Id FROM BASE_USER u LEFT JOIN lq_mdxx md ON u.F_MDID = md.F_Id - WHERE u.F_GW = '健康师'" + filterSql + @" + WHERE u.F_GW = '健康师'" + periodOnJobSql + filterSql + @" ORDER BY u.F_REALNAME LIMIT @hcOffset, @hcPageSize"; @@ -4800,6 +4815,68 @@ namespace NCC.Extend.LqKdKdjlb } /// + /// 品项开单时间:优先品项 yjsj,转卡等历史数据兜底开单主表 kdrq。 + /// + private static DateTime? ResolveBillingItemTime(LqKdPxmxEntity pxmx, Dictionary billingKdrqDict) + { + if (pxmx?.Yjsj != null) + { + return pxmx.Yjsj; + } + + if (!string.IsNullOrEmpty(pxmx?.Glkdbh) + && billingKdrqDict != null + && billingKdrqDict.TryGetValue(pxmx.Glkdbh, out var kdrq)) + { + return kdrq; + } + + return null; + } + + /// + /// 开单品项明细基础查询。时间筛选使用 COALESCE(pxmx.yjsj, 开单.kdrq),兼容转卡等未写 yjsj 的品项行。 + /// + private ISugarQueryable BuildBillingItemDetailPxmxQuery( + BillingItemDetailListQueryInput input, + DateTime? startBillingTime, + DateTime? endBillingTime, + List filterStoreIds) + { + DateTime? startDt = null; + DateTime? endDt = null; + if (startBillingTime.HasValue) + { + startDt = new DateTime(startBillingTime.Value.Year, startBillingTime.Value.Month, startBillingTime.Value.Day, 0, 0, 0); + } + + if (endBillingTime.HasValue) + { + endDt = new DateTime(endBillingTime.Value.Year, endBillingTime.Value.Month, endBillingTime.Value.Day, 23, 59, 59); + } + + return _db.Queryable((pxmx, kd) => new JoinQueryInfos( + JoinType.Inner, pxmx.Glkdbh == kd.Id)) + .Where((pxmx, kd) => pxmx.IsEffective == StatusEnum.有效.GetHashCode()) + .Where((pxmx, kd) => kd.IsEffective == StatusEnum.有效.GetHashCode()) + .WhereIF(!string.IsNullOrEmpty(input.Id), (pxmx, kd) => pxmx.Id == input.Id) + .WhereIF(!string.IsNullOrEmpty(input.BillingId), (pxmx, kd) => pxmx.Glkdbh == input.BillingId) + .WhereIF(startDt.HasValue, (pxmx, kd) => SqlFunc.IsNull(pxmx.Yjsj, kd.Kdrq) >= startDt) + .WhereIF(endDt.HasValue, (pxmx, kd) => SqlFunc.IsNull(pxmx.Yjsj, kd.Kdrq) <= endDt) + .WhereIF(!string.IsNullOrEmpty(input.ActivityId), (pxmx, kd) => pxmx.ActivityId == input.ActivityId) + .WhereIF(!string.IsNullOrEmpty(input.MemberId), (pxmx, kd) => pxmx.MemberId == input.MemberId) + .WhereIF(!string.IsNullOrEmpty(input.ItemId), (pxmx, kd) => pxmx.Px == input.ItemId) + .WhereIF(!string.IsNullOrEmpty(input.ItemName), (pxmx, kd) => (pxmx.Pxmc != null && pxmx.Pxmc.Contains(input.ItemName)) + || SqlFunc.Subqueryable().Where(z => z.Id == pxmx.Px && z.Xmmc != null && z.Xmmc.Contains(input.ItemName)).Any()) + .WhereIF(!string.IsNullOrEmpty(input.SourceType), (pxmx, kd) => pxmx.SourceType == input.SourceType) + .WhereIF(!string.IsNullOrEmpty(input.ItemType), (pxmx, kd) => pxmx.ItemCategory == input.ItemType) + .WhereIF(!string.IsNullOrEmpty(input.MemberName), (pxmx, kd) => SqlFunc.Subqueryable().Where(x => x.Id == pxmx.MemberId && x.Khmc != null && x.Khmc.Contains(input.MemberName)).Any()) + .WhereIF(!string.IsNullOrEmpty(input.MemberPhone), (pxmx, kd) => SqlFunc.Subqueryable().Where(x => x.Id == pxmx.MemberId && x.Sjh == input.MemberPhone).Any()) + .WhereIF(filterStoreIds != null && filterStoreIds.Any(), (pxmx, kd) => filterStoreIds.Contains(kd.Djmd)) + .Select((pxmx, kd) => pxmx); + } + + /// /// 获取开单品项明细记录列表(分页) /// /// @@ -4932,27 +5009,10 @@ namespace NCC.Extend.LqKdKdjlb } // 优化查询:先分页查询主表,再批量查询关联数据,避免子查询性能问题 - // 1. 先构建基础查询条件 - var baseQuery = _db.Queryable() - .Where(pxmx => pxmx.IsEffective == StatusEnum.有效.GetHashCode()) - .WhereIF(!string.IsNullOrEmpty(input.Id), pxmx => pxmx.Id == input.Id) - .WhereIF(!string.IsNullOrEmpty(input.BillingId), pxmx => pxmx.Glkdbh == input.BillingId) - .WhereIF(startBillingTime.HasValue, pxmx => pxmx.Yjsj >= new DateTime(startBillingTime.Value.Year, startBillingTime.Value.Month, startBillingTime.Value.Day, 0, 0, 0)) - .WhereIF(endBillingTime.HasValue, pxmx => pxmx.Yjsj <= new DateTime(endBillingTime.Value.Year, endBillingTime.Value.Month, endBillingTime.Value.Day, 23, 59, 59)) - .WhereIF(!string.IsNullOrEmpty(input.ActivityId), pxmx => pxmx.ActivityId == input.ActivityId) - .WhereIF(!string.IsNullOrEmpty(input.MemberId), pxmx => pxmx.MemberId == input.MemberId) - .WhereIF(!string.IsNullOrEmpty(input.ItemId), pxmx => pxmx.Px == input.ItemId) - .WhereIF(!string.IsNullOrEmpty(input.ItemName), pxmx => (pxmx.Pxmc != null && pxmx.Pxmc.Contains(input.ItemName)) - || SqlFunc.Subqueryable().Where(z => z.Id == pxmx.Px && z.Xmmc != null && z.Xmmc.Contains(input.ItemName)).Any()) - .WhereIF(!string.IsNullOrEmpty(input.SourceType), pxmx => pxmx.SourceType == input.SourceType) - .WhereIF(!string.IsNullOrEmpty(input.ItemType), pxmx => pxmx.ItemCategory == input.ItemType); - - // 2. 通过 EXISTS 子查询筛选关联字段(在分页前筛选,确保分页准确) - // 处理多门店筛选:优先使用StoreIds,如果没有则使用StoreId + // 处理多门店筛选:优先使用StoreIds,如果没有则使用StoreId;支持逗号串或多值参数 var filterStoreIds = new List(); if (!string.IsNullOrEmpty(input.StoreIds)) { - // 解析逗号分隔的门店ID字符串 filterStoreIds = input.StoreIds.Split(',') .Where(x => !string.IsNullOrWhiteSpace(x)) .Select(x => x.Trim()) @@ -4963,9 +5023,7 @@ namespace NCC.Extend.LqKdKdjlb filterStoreIds = new List { input.StoreId }; } - baseQuery = baseQuery.WhereIF(!string.IsNullOrEmpty(input.MemberName), pxmx => SqlFunc.Subqueryable().Where(x => x.Id == pxmx.MemberId && x.Khmc != null && x.Khmc.Contains(input.MemberName)).Any()) - .WhereIF(!string.IsNullOrEmpty(input.MemberPhone), pxmx => SqlFunc.Subqueryable().Where(x => x.Id == pxmx.MemberId && x.Sjh == input.MemberPhone).Any()) - .WhereIF(filterStoreIds.Any(), pxmx => SqlFunc.Subqueryable().Where(x => x.Id == pxmx.Glkdbh && filterStoreIds.Contains(x.Djmd)).Any()); + var baseQuery = BuildBillingItemDetailPxmxQuery(input, startBillingTime, endBillingTime, filterStoreIds); // 3. 先分页查询主表数据(查询实体类,提高性能) var pagedData = await baseQuery.OrderBy(sidx + " " + sort).ToPagedListAsync(input.currentPage, input.pageSize); @@ -5018,17 +5076,19 @@ namespace NCC.Extend.LqKdKdjlb var billingStoreDict = new Dictionary(); var billingExtraInfoDict = new Dictionary(); var billingDeductDict = new Dictionary(); + var billingKdrqDict = new Dictionary(); if (billingIds.Any()) { var billings = await _db.Queryable() .Where(x => billingIds.Contains(x.Id)) - .Select(x => new { x.Id, x.Djmd, x.Hgjg, x.Fkyy, x.Fkfs, x.DeductAmount }) + .Select(x => new { x.Id, x.Djmd, x.Hgjg, x.Fkyy, x.Fkfs, x.DeductAmount, x.Kdrq }) .ToListAsync(); billingStoreDict = billings.ToDictionary(x => x.Id, x => x.Djmd ?? ""); billingExtraInfoDict = billings.ToDictionary(x => x.Id, x => (dynamic)new { Hgjg = x.Hgjg, Fkyy = x.Fkyy, Fkfs = x.Fkfs }); billingDeductDict = billings.ToDictionary(x => x.Id, x => x.DeductAmount); + billingKdrqDict = billings.ToDictionary(x => x.Id, x => x.Kdrq); } var billingItemTotalDict = await GetBillingItemActualPriceTotalsAsync(billingIds); @@ -5064,7 +5124,7 @@ namespace NCC.Extend.LqKdKdjlb { id = pxmx.Id, billingId = pxmx.Glkdbh, - billingTime = pxmx.Yjsj, + billingTime = ResolveBillingItemTime(pxmx, billingKdrqDict), activityName = pxmx.ActivityId != null && activityDict.ContainsKey(pxmx.ActivityId) ? activityDict[pxmx.ActivityId] : "", memberName = pxmx.MemberId != null && memberDict.ContainsKey(pxmx.MemberId) ? memberDict[pxmx.MemberId].Name : "", memberPhone = pxmx.MemberId != null && memberDict.ContainsKey(pxmx.MemberId) ? memberDict[pxmx.MemberId].Phone : "", @@ -5139,20 +5199,6 @@ namespace NCC.Extend.LqKdKdjlb if (!startBillingTime.HasValue || !endBillingTime.HasValue) throw NCCException.Oh("请选择开始时间和结束时间"); - var baseQuery = _db.Queryable() - .Where(pxmx => pxmx.IsEffective == StatusEnum.有效.GetHashCode()) - .WhereIF(!string.IsNullOrEmpty(input.Id), pxmx => pxmx.Id == input.Id) - .WhereIF(!string.IsNullOrEmpty(input.BillingId), pxmx => pxmx.Glkdbh == input.BillingId) - .WhereIF(startBillingTime.HasValue, pxmx => pxmx.Yjsj >= new DateTime(startBillingTime.Value.Year, startBillingTime.Value.Month, startBillingTime.Value.Day, 0, 0, 0)) - .WhereIF(endBillingTime.HasValue, pxmx => pxmx.Yjsj <= new DateTime(endBillingTime.Value.Year, endBillingTime.Value.Month, endBillingTime.Value.Day, 23, 59, 59)) - .WhereIF(!string.IsNullOrEmpty(input.ActivityId), pxmx => pxmx.ActivityId == input.ActivityId) - .WhereIF(!string.IsNullOrEmpty(input.MemberId), pxmx => pxmx.MemberId == input.MemberId) - .WhereIF(!string.IsNullOrEmpty(input.ItemId), pxmx => pxmx.Px == input.ItemId) - .WhereIF(!string.IsNullOrEmpty(input.ItemName), pxmx => (pxmx.Pxmc != null && pxmx.Pxmc.Contains(input.ItemName)) - || SqlFunc.Subqueryable().Where(z => z.Id == pxmx.Px && z.Xmmc != null && z.Xmmc.Contains(input.ItemName)).Any()) - .WhereIF(!string.IsNullOrEmpty(input.SourceType), pxmx => pxmx.SourceType == input.SourceType) - .WhereIF(!string.IsNullOrEmpty(input.ItemType), pxmx => pxmx.ItemCategory == input.ItemType); - var filterStoreIds = new List(); if (!string.IsNullOrEmpty(input.StoreIds)) { @@ -5166,9 +5212,7 @@ namespace NCC.Extend.LqKdKdjlb filterStoreIds = new List { input.StoreId }; } - baseQuery = baseQuery.WhereIF(!string.IsNullOrEmpty(input.MemberName), pxmx => SqlFunc.Subqueryable().Where(x => x.Id == pxmx.MemberId && x.Khmc != null && x.Khmc.Contains(input.MemberName)).Any()) - .WhereIF(!string.IsNullOrEmpty(input.MemberPhone), pxmx => SqlFunc.Subqueryable().Where(x => x.Id == pxmx.MemberId && x.Sjh == input.MemberPhone).Any()) - .WhereIF(filterStoreIds.Any(), pxmx => SqlFunc.Subqueryable().Where(x => x.Id == pxmx.Glkdbh && filterStoreIds.Contains(x.Djmd)).Any()); + var baseQuery = BuildBillingItemDetailPxmxQuery(input, startBillingTime, endBillingTime, filterStoreIds); var allData = await baseQuery.OrderBy(sidx + " " + sort).ToListAsync(); if (allData == null || !allData.Any()) @@ -5188,15 +5232,17 @@ namespace NCC.Extend.LqKdKdjlb var billingStoreDict = new Dictionary(); var billingExtraInfoDict = new Dictionary(); var billingDeductDict = new Dictionary(); + var billingKdrqDict = new Dictionary(); if (billingIds.Any()) { var billings = await _db.Queryable() .Where(x => billingIds.Contains(x.Id)) - .Select(x => new { x.Id, x.Djmd, x.Hgjg, x.Fkyy, x.Fkfs, x.DeductAmount }) + .Select(x => new { x.Id, x.Djmd, x.Hgjg, x.Fkyy, x.Fkfs, x.DeductAmount, x.Kdrq }) .ToListAsync(); billingStoreDict = billings.ToDictionary(x => x.Id, x => x.Djmd ?? ""); billingExtraInfoDict = billings.ToDictionary(x => x.Id, x => (x.Hgjg ?? "", x.Fkyy ?? "", x.Fkfs ?? "")); billingDeductDict = billings.ToDictionary(x => x.Id, x => x.DeductAmount); + billingKdrqDict = billings.ToDictionary(x => x.Id, x => x.Kdrq); } var billingItemTotalDict = await GetBillingItemActualPriceTotalsAsync(billingIds); @@ -5234,7 +5280,7 @@ namespace NCC.Extend.LqKdKdjlb { id = pxmx.Id, billingId = pxmx.Glkdbh, - billingTime = pxmx.Yjsj, + billingTime = ResolveBillingItemTime(pxmx, billingKdrqDict), activityName = pxmx.ActivityId != null && activityDict.ContainsKey(pxmx.ActivityId) ? activityDict[pxmx.ActivityId] : "", memberName = pxmx.MemberId != null && memberDict.ContainsKey(pxmx.MemberId) ? memberDict[pxmx.MemberId].Name : "", memberPhone = pxmx.MemberId != null && memberDict.ContainsKey(pxmx.MemberId) ? memberDict[pxmx.MemberId].Phone : "", diff --git a/netcore/src/Modularity/Extend/NCC.Extend/LqMdTargetService.cs b/netcore/src/Modularity/Extend/NCC.Extend/LqMdTargetService.cs index 86e8ecd..db0ffd7 100644 --- a/netcore/src/Modularity/Extend/NCC.Extend/LqMdTargetService.cs +++ b/netcore/src/Modularity/Extend/NCC.Extend/LqMdTargetService.cs @@ -92,7 +92,7 @@ namespace NCC.Extend.LqMdTarget if (data.list != null) { var list = data.list.ToList(); - await EnrichExportNamesAsync(list); + await EnrichStoreDisplayAsync(list); data.list = list; } @@ -228,6 +228,7 @@ namespace NCC.Extend.LqMdTarget } var entity = input.Adapt(); entity.Id = YitIdHelper.NextId().ToString(); + await NormalizeOrganizeFieldsAsync(entity); entity.CreateTime = DateTime.Now; entity.CreateUser = userInfo.userId; var isOk = await _db.Insertable(entity).IgnoreColumns(ignoreNullColumn: true).ExecuteCommandAsync(); @@ -359,6 +360,7 @@ namespace NCC.Extend.LqMdTarget } var updateEntity = input.Adapt(); updateEntity.Id = id; + await NormalizeOrganizeFieldsAsync(updateEntity); updateEntity.UpdateTime = DateTime.Now; updateEntity.UpdateUser = userInfo.userId; var isOk = await _db.Updateable(updateEntity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync(); @@ -492,6 +494,12 @@ namespace NCC.Extend.LqMdTarget if (newEntities.Count > 0) { + var orgAliasMap = await BuildOrganizeAliasToIdMapAsync(); + foreach (var entity in newEntities) + { + NormalizeOrganizeFields(entity, orgAliasMap); + } + syncedCount = await _db.Insertable(newEntities).ExecuteCommandAsync(); } @@ -579,20 +587,27 @@ namespace NCC.Extend.LqMdTarget (t, s) => t.StoreId == s.Id) .Where((t, s) => t.Month == input.Month); - // 根据组织类型添加部门过滤条件 + var orgAliasMap = await BuildOrganizeAliasToIdMapAsync(); + var departmentId = ResolveOrganizeId(input.DepartmentId, orgAliasMap); + if (string.IsNullOrEmpty(departmentId)) + { + throw NCCException.Oh("部门ID无效"); + } + + // 根据组织类型添加部门过滤条件(兼容历史名称脏数据) switch (input.OrganizationType) { case "事业部": - query = query.Where((t, s) => t.BusinessUnit == input.DepartmentId); + query = query.Where((t, s) => t.BusinessUnit == departmentId || t.BusinessUnit == input.DepartmentId); break; case "科技部": - query = query.Where((t, s) => t.TechDepartment == input.DepartmentId); + query = query.Where((t, s) => t.TechDepartment == departmentId || t.TechDepartment == input.DepartmentId); break; case "教育部": - query = query.Where((t, s) => t.EducationDepartment == input.DepartmentId); + query = query.Where((t, s) => t.EducationDepartment == departmentId || t.EducationDepartment == input.DepartmentId); break; case "大项目部": - query = query.Where((t, s) => t.MajorProjectDepartment == input.DepartmentId); + query = query.Where((t, s) => t.MajorProjectDepartment == departmentId || t.MajorProjectDepartment == input.DepartmentId); break; default: throw NCCException.Oh($"不支持的组织类型:{input.OrganizationType},支持的类型:事业部、科技部、教育部、大项目部"); @@ -667,63 +682,155 @@ namespace NCC.Extend.LqMdTarget } /// - /// 解析门店与组织名称,供列表与导出展示(含已软删门店) + /// 列表展示:仅补门店名称,归属字段保持组织ID(避免编辑保存时把名称写回库) /// - private async Task EnrichExportNamesAsync(List exportData) + private async Task EnrichStoreDisplayAsync(List rows) { - if (exportData == null || exportData.Count == 0) + if (rows == null || rows.Count == 0) return; - var storeIds = exportData + var storeIds = rows .Where(x => !string.IsNullOrEmpty(x.storeId)) .Select(x => x.storeId) .Distinct() .ToList(); - var storeDict = storeIds.Count > 0 - ? (await _db.Queryable() + if (storeIds.Count == 0) + return; + + var storeDict = (await _db.Queryable() .Where(x => storeIds.Contains(x.Id)) .Select(x => new { x.Id, x.Dm, x.Status }) .ToListAsync()) - .ToDictionary( - x => x.Id, - x => new StoreDisplayInfo - { - Name = x.Dm ?? string.Empty, - Deleted = x.Status == InvalidStatus - }) - : new Dictionary(); - - var orgIds = exportData + .ToDictionary( + x => x.Id, + x => new StoreDisplayInfo + { + Name = x.Dm ?? string.Empty, + Deleted = x.Status == InvalidStatus + }); + + foreach (var item in rows) + { + if (!string.IsNullOrEmpty(item.storeId) && storeDict.TryGetValue(item.storeId, out var storeInfo)) + { + item.storeName = storeInfo.Name; + item.storeDeleted = storeInfo.Deleted; + } + } + } + + /// + /// 解析门店与组织名称,供导出展示(含已软删门店) + /// + private async Task EnrichExportNamesAsync(List exportData) + { + if (exportData == null || exportData.Count == 0) + return; + + await EnrichStoreDisplayAsync(exportData); + + var orgValues = exportData .SelectMany(x => new[] { x.businessUnit, x.techDepartment, x.educationDepartment, x.majorProjectDepartment }) .Where(x => !string.IsNullOrEmpty(x)) .Distinct() .ToList(); - var orgDict = orgIds.Count > 0 - ? (await _db.Queryable() - .Where(x => orgIds.Contains(x.Id) && x.DeleteMark == null) + var orgDict = new Dictionary(); + var orgNameDict = new Dictionary(StringComparer.Ordinal); + if (orgValues.Count > 0) + { + var orgs = await _db.Queryable() + .Where(x => (orgValues.Contains(x.Id) || orgValues.Contains(x.FullName)) + && x.DeleteMark == null + && x.Category == "department") .Select(x => new { x.Id, x.FullName }) - .ToListAsync()) - .ToDictionary(x => x.Id, x => x.FullName ?? string.Empty) - : new Dictionary(); + .ToListAsync(); - foreach (var item in exportData) - { - if (!string.IsNullOrEmpty(item.storeId) && storeDict.TryGetValue(item.storeId, out var storeInfo)) + orgDict = orgs.ToDictionary(x => x.Id, x => x.FullName ?? string.Empty); + foreach (var org in orgs.Where(x => !string.IsNullOrEmpty(x.FullName))) { - item.storeName = storeInfo.Name; - item.storeDeleted = storeInfo.Deleted; + orgNameDict[org.FullName] = org.FullName; } + } - item.businessUnit = ResolveOrganizeDisplayName(item.businessUnit, orgDict); - item.techDepartment = ResolveOrganizeDisplayName(item.techDepartment, orgDict); - item.educationDepartment = ResolveOrganizeDisplayName(item.educationDepartment, orgDict); - item.majorProjectDepartment = ResolveOrganizeDisplayName(item.majorProjectDepartment, orgDict); + foreach (var item in exportData) + { + item.businessUnit = ResolveOrganizeDisplayName(item.businessUnit, orgDict, orgNameDict); + item.techDepartment = ResolveOrganizeDisplayName(item.techDepartment, orgDict, orgNameDict); + item.educationDepartment = ResolveOrganizeDisplayName(item.educationDepartment, orgDict, orgNameDict); + item.majorProjectDepartment = ResolveOrganizeDisplayName(item.majorProjectDepartment, orgDict, orgNameDict); } } /// + /// 保存前将归属字段统一归一化为 base_organize.F_Id + /// + private async Task NormalizeOrganizeFieldsAsync(LqMdTargetEntity entity) + { + if (entity == null) + return; + + var orgAliasMap = await BuildOrganizeAliasToIdMapAsync(); + NormalizeOrganizeFields(entity, orgAliasMap); + } + + /// + /// 使用组织映射将归属字段归一化为组织ID + /// + private static void NormalizeOrganizeFields(LqMdTargetEntity entity, Dictionary orgAliasMap) + { + if (entity == null) + return; + + entity.BusinessUnit = ResolveOrganizeId(entity.BusinessUnit, orgAliasMap, "事业部"); + entity.TechDepartment = ResolveOrganizeId(entity.TechDepartment, orgAliasMap, "科技部"); + entity.EducationDepartment = ResolveOrganizeId(entity.EducationDepartment, orgAliasMap, "教育部"); + entity.MajorProjectDepartment = ResolveOrganizeId(entity.MajorProjectDepartment, orgAliasMap, "大项目部"); + } + + /// + /// 构建组织别名(ID/名称)到组织ID的映射 + /// + private async Task> BuildOrganizeAliasToIdMapAsync() + { + var orgs = await _db.Queryable() + .Where(x => x.Category == "department" && x.DeleteMark == null) + .Select(x => new { x.Id, x.FullName }) + .ToListAsync(); + + var map = new Dictionary(StringComparer.Ordinal); + foreach (var org in orgs) + { + if (!string.IsNullOrEmpty(org.Id)) + map[org.Id] = org.Id; + + if (!string.IsNullOrEmpty(org.FullName) && !map.ContainsKey(org.FullName)) + map[org.FullName] = org.Id; + } + + return map; + } + + /// + /// 组织归属值(ID 或名称)解析为标准组织ID;无法解析时抛出友好错误 + /// + private static string ResolveOrganizeId(string rawValue, Dictionary orgAliasMap, string fieldLabel = null) + { + if (string.IsNullOrWhiteSpace(rawValue)) + return rawValue; + + if (orgAliasMap != null && orgAliasMap.TryGetValue(rawValue, out var mappedId) && !string.IsNullOrEmpty(mappedId)) + return mappedId; + + if (Regex.IsMatch(rawValue, @"^\d{15,}$")) + return rawValue; + + var label = string.IsNullOrEmpty(fieldLabel) ? "组织归属" : fieldLabel; + throw NCCException.Oh($"{label}「{rawValue}」无效,请重新选择部门"); + } + + /// /// 门店展示信息(含软删标记) /// private sealed class StoreDisplayInfo @@ -735,14 +842,21 @@ namespace NCC.Extend.LqMdTarget /// /// 组织ID解析为名称,解析不到则保留原值 /// - private static string ResolveOrganizeDisplayName(string organizeId, Dictionary orgDict) + private static string ResolveOrganizeDisplayName( + string organizeValue, + Dictionary orgIdDict, + Dictionary orgNameDict) { - if (string.IsNullOrEmpty(organizeId)) - return organizeId; + if (string.IsNullOrEmpty(organizeValue)) + return organizeValue; + + if (orgIdDict.TryGetValue(organizeValue, out var name) && !string.IsNullOrEmpty(name)) + return name; + + if (orgNameDict.TryGetValue(organizeValue, out var existingName) && !string.IsNullOrEmpty(existingName)) + return existingName; - return orgDict.TryGetValue(organizeId, out var name) && !string.IsNullOrEmpty(name) - ? name - : organizeId; + return organizeValue; } private async Task TryWriteBusinessLogAsync(string moduleCode, string actionCode, string bizType, string bizId, string summary, string detailJson = null) diff --git a/store-pc/src/components/StoreTargetDialog.vue b/store-pc/src/components/StoreTargetDialog.vue index a4d6f1a..6aace04 100644 --- a/store-pc/src/components/StoreTargetDialog.vue +++ b/store-pc/src/components/StoreTargetDialog.vue @@ -199,6 +199,7 @@