# 事业部开单统计播报问题修复方案 **问题**:绿纤西站店没有在事业部开单统计数据播报中显示 **修复日期**:2025年1月 **修复状态**:✅ **已修复** --- ## 一、问题分析总结 ### 1.1 问题根源 **核心问题**:`GetBusinessUnitBillingStatistics` 方法使用了错误的门店归属获取方式 **错误实现**(修复前): ```csharp // ❌ 错误:从 lq_mdxx.syb 字段读取(已弃用的历史字段) var billingQuery = _db.Queryable( (billing, store, org) => billing.Djmd == store.Id && store.Syb == org.Id) ``` **问题点**: 1. ❌ 使用 `lq_mdxx.syb` 字段(已弃用的历史字段) 2. ❌ 没有考虑月份维度,门店归属可能在不同月份发生变化 3. ❌ 违反了项目规范:**门店归属一律从 `lq_md_target` 按月份维度管理** --- ### 1.2 为什么绿纤西站店没有被统计 **可能的原因**: 1. **`lq_mdxx.syb` 字段为空或错误** - 如果绿纤西站店在 `lq_mdxx` 表的 `syb` 字段中没有正确设置 - 或者 `syb` 字段指向的组织不是"事业X部"(不包含"事业"关键字) - 则不会被统计进去 2. **`lq_md_target` 表中有正确的归属,但查询没有使用** - 如果绿纤西站店在 `lq_md_target` 表中有正确的月份归属记录 - 但查询使用的是 `lq_mdxx.syb`,导致数据不一致 3. **月份维度问题** - 如果查询时使用的月份与 `lq_md_target` 表中的月份不匹配 - 或者该门店在查询月份没有 `lq_md_target` 记录 --- ## 二、修复方案 ### 2.1 修复内容 **文件**:`netcore/src/Modularity/Extend/NCC.Extend/LqDailyReportService.cs` **方法**:`GetBusinessUnitBillingStatistics`(第1513-1604行) **修复要点**: 1. ✅ **添加月份计算**:根据开单日期确定月份(YYYYMM格式) 2. ✅ **使用 lq_md_target 表**:替代 `lq_mdxx.syb` 字段 3. ✅ **添加门店表关联**:获取门店名称 4. ✅ **更新注释**:说明修复原因和逻辑 --- ### 2.2 修复后的代码 ```csharp // 2. 根据开单日期确定月份(YYYYMM格式) // 重要:门店归属必须从门店目标表(lq_md_target)按月份维度获取 var month = targetDate.ToString("yyyyMM"); // 3. 查询指定日期的有效开单记录(有金额的) // 使用 lq_md_target 表获取门店归属,替代已弃用的 lq_mdxx.syb 字段 var billingQuery = _db.Queryable( (billing, target, store, org) => billing.Djmd == target.StoreId && target.Month == month && target.BusinessUnit == org.Id && billing.Djmd == store.Id) .Where((billing, target, store, org) => billing.IsEffective == StatusEnum.有效.GetHashCode()) .Where((billing, target, store, org) => billing.Sfyj > 0) .Where((billing, target, store, org) => billing.Kdrq.HasValue && billing.Kdrq.Value.Date == targetDate.Date) .Where((billing, target, store, org) => target.BusinessUnit != null && target.BusinessUnit != "") .Where((billing, target, store, org) => org.Category == "department") .Where((billing, target, store, org) => org.FullName.Contains("事业")) .Select((billing, target, store, org) => new { OrderId = billing.Id, StoreName = store.Dm, BusinessUnitId = org.Id, BusinessUnitName = org.FullName, Amount = billing.Sfyj, OrderTime = billing.Kdrq, }) .ToListAsync(); ``` **关键改进**: 1. ✅ **添加月份维度**:`var month = targetDate.ToString("yyyyMM");` 2. ✅ **使用 lq_md_target 表**:`billing.Djmd == target.StoreId && target.Month == month` 3. ✅ **关联门店表**:`billing.Djmd == store.Id`(获取门店名称) 4. ✅ **过滤条件**:`target.BusinessUnit != null && target.BusinessUnit != ""` --- ### 2.3 代码变更清单 | 变更项 | 变更前 | 变更后 | |-------|--------|--------| | **using引用** | 无 `lq_md_target` | ✅ 添加 `using NCC.Extend.Entitys.lq_md_target;` | | **月份计算** | 无 | ✅ 添加 `var month = targetDate.ToString("yyyyMM");` | | **查询关联** | `LqKdKdjlbEntity, LqMdxxEntity, OrganizeEntity` | ✅ 改为 `LqKdKdjlbEntity, LqMdTargetEntity, LqMdxxEntity, OrganizeEntity` | | **关联条件** | `store.Syb == org.Id` | ✅ 改为 `target.StoreId == billing.Djmd && target.Month == month && target.BusinessUnit == org.Id` | | **过滤条件** | 无 | ✅ 添加 `target.BusinessUnit != null && target.BusinessUnit != ""` | | **方法注释** | 无月份维度说明 | ✅ 添加重要说明 | --- ## 三、修复效果 ### 3.1 修复前 - ❌ 使用 `lq_mdxx.syb` 字段(已弃用) - ❌ 不考虑月份维度 - ❌ 绿纤西站店可能因为 `syb` 字段为空或错误而不被统计 ### 3.2 修复后 - ✅ 使用 `lq_md_target` 表(符合项目规范) - ✅ 按月份维度获取门店归属 - ✅ 绿纤西站店如果在该月份有正确的 `lq_md_target` 记录,会被正确统计 --- ## 四、验证方案 ### 4.1 数据验证SQL **检查绿纤西站店的数据**: ```sql -- 1. 查找绿纤西站店 SELECT F_Id, dm, syb FROM lq_mdxx WHERE dm LIKE '%西站%'; -- 2. 检查门店目标表中的归属信息(假设门店ID为 'xxx') SELECT F_StoreId, F_Month, F_BusinessUnit, (SELECT F_FullName FROM base_organize WHERE F_Id = F_BusinessUnit) as BusinessUnitName FROM lq_md_target WHERE F_StoreId = 'xxx' -- 替换为实际门店ID ORDER BY F_Month DESC; -- 3. 检查该门店在指定日期的开单记录 SELECT billing.F_Id, billing.djmd, billing.kdrq, billing.sfyj, billing.F_IsEffective, store.dm as StoreName, target.F_BusinessUnit, org.F_FullName as BusinessUnitName FROM lq_kd_kdjlb billing LEFT JOIN lq_mdxx store ON billing.djmd = store.F_Id LEFT JOIN lq_md_target target ON billing.djmd = target.F_StoreId AND target.F_Month = DATE_FORMAT(billing.kdrq, '%Y%m') LEFT JOIN base_organize org ON target.F_BusinessUnit = org.F_Id WHERE billing.djmd = 'xxx' -- 替换为实际门店ID AND DATE(billing.kdrq) = '2025-01-23' -- 替换为实际日期 AND billing.sfyj > 0 AND billing.F_IsEffective = 1; ``` --- ### 4.2 接口测试 **测试步骤**: 1. **获取登录token** 2. **调用接口获取统计数据**: ```bash curl -X POST "http://localhost:2011/api/Extend/LqDailyReport/get-business-unit-billing-statistics" \ -H "Authorization: $TOKEN" \ -H "Content-Type: application/json" \ -d '{"date": "2025-01-23"}' ``` 3. **验证结果**: - 检查返回数据中是否包含绿纤西站店 - 检查该门店是否归属到正确的事业部 --- ### 4.3 播报测试 **测试步骤**: 1. **创建一个绿纤西站店的开单记录**(确保 `sfyj > 0`) 2. **观察企业微信群**,检查播报内容中是否包含该门店 3. **验证播报格式**是否正确 --- ## 五、注意事项 ### 5.1 数据完整性 ⚠️ **重要**:如果某些门店在 `lq_md_target` 表中没有对应月份的记录,可能不会被统计 **建议**: - 确保所有门店在 `lq_md_target` 表中有对应月份的归属记录 - 如果缺失,需要先补充数据 ### 5.2 容错处理 **当前实现**: - 如果 `lq_md_target` 表中没有记录,该门店不会被统计 - 不会抛出异常,只是不包含在结果中 **建议**(可选): - 可以考虑添加日志记录,当发现门店没有 `lq_md_target` 记录时记录警告 - 或者添加容错逻辑:如果 `lq_md_target` 中没有记录,回退到 `lq_mdxx.syb`(但需要记录日志) --- ### 5.3 性能考虑 **索引优化**: - ✅ `lq_md_target` 表已有唯一索引:`idx_store_month (F_StoreId, F_Month)` - ✅ 查询性能应该良好 --- ## 六、修复总结 ### 6.1 修复内容 ✅ **已修复**: 1. 添加 `lq_md_target` 表的 using 引用 2. 修改查询逻辑,使用 `lq_md_target` 表替代 `lq_mdxx.syb` 3. 添加月份维度计算 4. 更新方法注释,说明修复原因 ### 6.2 符合规范 ✅ **符合项目规范**: - 使用 `lq_md_target` 表按月份维度获取门店归属 - 不再使用已弃用的 `lq_mdxx.syb` 字段 - 与其他统计方法(如 `GetBusinessUnitPerformanceCompletion`)保持一致 ### 6.3 预期效果 修复后: - ✅ 绿纤西站店(以及其他所有门店)的开单数据将根据 `lq_md_target` 表中的月份归属正确统计 - ✅ 播报内容应该包含该门店(如果该门店在该月份有正确的归属记录) - ✅ 统计数据应该准确反映各事业部的开单情况 --- ## 七、后续建议 ### 7.1 数据检查 建议检查以下数据: 1. **检查绿纤西站店在 `lq_md_target` 表中的记录**: - 确认该门店在查询月份是否有归属记录 - 确认 `F_BusinessUnit` 字段是否正确设置 2. **检查其他门店**: - 确保所有门店在 `lq_md_target` 表中有对应月份的归属记录 - 如果缺失,需要补充数据 ### 7.2 代码审查 建议检查其他类似的统计方法,确保都使用了正确的门店归属获取方式: - ✅ `GetBusinessUnitPerformanceCompletion` - 已使用 `lq_md_target` 表 - ✅ `GetBusinessUnitBillingStatistics` - 已修复 - ⚠️ 其他统计方法需要检查 --- **修复完成** **修复状态**:✅ **代码已修复,等待测试验证**