Blame view

项目文档相关/docs/事业部开单统计播报问题修复方案.md 9.29 KB
257347ad   “wangming”   feat: enhance fil...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
  # 事业部开单统计播报问题修复方案
  
  **问题**:绿纤西站店没有在事业部开单统计数据播报中显示  
  **修复日期**:2025年1月  
  **修复状态**:✅ **已修复**
  
  ---
  
  ## 一、问题分析总结
  
  ### 1.1 问题根源
  
  **核心问题**`GetBusinessUnitBillingStatistics` 方法使用了错误的门店归属获取方式
  
  **错误实现**(修复前):
  ```csharp
  // ❌ 错误:从 lq_mdxx.syb 字段读取(已弃用的历史字段)
  var billingQuery = _db.Queryable<LqKdKdjlbEntity, LqMdxxEntity, OrganizeEntity>(
          (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<LqKdKdjlbEntity, LqMdTargetEntity, LqMdxxEntity, OrganizeEntity>(
          (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
8daf47d0   “wangming”   修改访问地址
184
     curl -X POST "http://localhost:2015/api/Extend/LqDailyReport/get-business-unit-billing-statistics" \
257347ad   “wangming”   feat: enhance fil...
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
       -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` - 已修复
  - ⚠️ 其他统计方法需要检查
  
  ---
  
  **修复完成**
  
  **修复状态**:✅ **代码已修复,等待测试验证**