# 商家账单日/周/月查询增加 mdId 过滤 实现计划

> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.

**Goal:** 为 `getzddaylist`、`getzdmeeklist`、`getzdmatlist` 三个接口增加可选的 `mdId` 门店过滤参数。

**Architecture:** 在 Controller 层接收可选参数 `mdId`，传入 Mapper 层 SQL。利用 `#{mdId} IS NULL OR md_id = #{mdId}` 模式在 `@Select` 注解中实现动态过滤，无需改用 XML。同时更新对应的统计查询（`getdaysum`/`getmeeksum`/`getmatsum` 及 `getShDaysum`/`getShMeeksum`/`getshMonthsum`），保证列表和汇总数据一致。

**Tech Stack:** Java, Spring Boot, MyBatis Plus, MySQL

---

## 涉及文件

| 文件 | 操作 | 说明 |
|------|------|------|
| `ruoyi-admin/src/main/java/com/ruoyi/app/pay/UserBillingController.java` | 修改 | 3 个接口方法增加 mdId 参数 |
| `ruoyi-system/src/main/java/com/ruoyi/system/mapper/UserBillingMapper.java` | 修改 | 6 个 `@Select` SQL 增加 mdId 条件 |

---

### Task 1: Mapper 层 — 修改日账单相关 SQL

**Files:**
- Modify: `ruoyi-system/src/main/java/com/ruoyi/system/mapper/UserBillingMapper.java`

- [ ] **Step 1: 修改 `getdaylist` 方法 — 增加 mdId 参数和 SQL 条件**

将方法签名从：
```java
List<UserBilling> getdaylist(@Param("shid") Long shid, @Param("riqi") String riqi,@Param("page") Long page,@Param("size") Long size);
```
改为：
```java
List<UserBilling> getdaylist(@Param("shid") Long shid, @Param("riqi") String riqi, @Param("mdId") Long mdId, @Param("page") Long page, @Param("size") Long size);
```

SQL 从：
```sql
SELECT * FROM user_billing WHERE type=0 and user_id=#{shid} and date_format (cretim,'%Y-%m-%d %H:%i:%s') >= date_format (CONCAT(#{riqi}, ' 00:00:00'),'%Y-%m-%d %H:%i:%s') and date_format (cretim,'%Y-%m-%d %H:%i:%s') <= date_format (CONCAT(#{riqi}, ' 23:59:59'),'%Y-%m-%d %H:%i:%s') ORDER BY id desc limit #{page},#{size}
```
改为：
```sql
SELECT * FROM user_billing WHERE type=0 and user_id=#{shid} and (#{mdId} IS NULL OR md_id = #{mdId}) and date_format (cretim,'%Y-%m-%d %H:%i:%s') >= date_format (CONCAT(#{riqi}, ' 00:00:00'),'%Y-%m-%d %H:%i:%s') and date_format (cretim,'%Y-%m-%d %H:%i:%s') <= date_format (CONCAT(#{riqi}, ' 23:59:59'),'%Y-%m-%d %H:%i:%s') ORDER BY id desc limit #{page},#{size}
```

- [ ] **Step 2: 修改 `getdaysum` 方法 — 增加 mdId 参数和 SQL 条件**

方法签名增加 `@Param("mdId") Long mdId`，SQL 在 `user_id=#{shid}` 后增加 `and (#{mdId} IS NULL OR md_id = #{mdId})`。

原 SQL：
```sql
SELECT IFNULL(COUNT(1),0) AS sul,IFNULL(SUM(amount),0) AS shouru,IFNULL(SUM(divvy),0) AS commission,IFNULL(SUM(tax),0) AS tax,IFNULL(SUM(behalf_amount),0) AS behalfAmount FROM user_billing WHERE type=0 and user_id=#{shid} and date_format (cretim,'%Y-%m-%d %H:%i:%s') >= date_format (CONCAT(#{riqi}, ' 00:00:00'),'%Y-%m-%d %H:%i:%s') and date_format (cretim,'%Y-%m-%d %H:%i:%s') <= date_format (CONCAT(#{riqi}, ' 23:59:59'),'%Y-%m-%d %H:%i:%s') and state=0
```
改为：
```sql
SELECT IFNULL(COUNT(1),0) AS sul,IFNULL(SUM(amount),0) AS shouru,IFNULL(SUM(divvy),0) AS commission,IFNULL(SUM(tax),0) AS tax,IFNULL(SUM(behalf_amount),0) AS behalfAmount FROM user_billing WHERE type=0 and user_id=#{shid} and (#{mdId} IS NULL OR md_id = #{mdId}) and date_format (cretim,'%Y-%m-%d %H:%i:%s') >= date_format (CONCAT(#{riqi}, ' 00:00:00'),'%Y-%m-%d %H:%i:%s') and date_format (cretim,'%Y-%m-%d %H:%i:%s') <= date_format (CONCAT(#{riqi}, ' 23:59:59'),'%Y-%m-%d %H:%i:%s') and state=0
```

- [ ] **Step 3: 修改 `getShDaysum` 方法 — 增加 mdId 参数和 SQL 条件**

方法签名增加 `@Param("mdId") Long mdId`，SQL 在 `b.user_id=#{shid}` 后增加 `and (#{mdId} IS NULL OR b.md_id = #{mdId})`。

原 SQL：
```sql
SELECT IFNULL(SUM(o.food_amount),0) AS foodAmount,IFNULL(SUM(o.md_discount_amount),0)+IFNULL(SUM(o.md_sales_reduction),0) as shYh  FROM user_billing b left join pos_order o on b.dd_id=o.dd_id WHERE b.type=0 and b.user_id=#{shid} and date_format (b.cretim,'%Y-%m-%d %H:%i:%s') >= date_format (CONCAT(#{riqi}, ' 00:00:00'),'%Y-%m-%d %H:%i:%s') and date_format (b.cretim,'%Y-%m-%d %H:%i:%s') <= date_format (CONCAT(#{riqi}, ' 23:59:59'),'%Y-%m-%d %H:%i:%s')
```
改为：
```sql
SELECT IFNULL(SUM(o.food_amount),0) AS foodAmount,IFNULL(SUM(o.md_discount_amount),0)+IFNULL(SUM(o.md_sales_reduction),0) as shYh  FROM user_billing b left join pos_order o on b.dd_id=o.dd_id WHERE b.type=0 and b.user_id=#{shid} and (#{mdId} IS NULL OR b.md_id = #{mdId}) and date_format (b.cretim,'%Y-%m-%d %H:%i:%s') >= date_format (CONCAT(#{riqi}, ' 00:00:00'),'%Y-%m-%d %H:%i:%s') and date_format (b.cretim,'%Y-%m-%d %H:%i:%s') <= date_format (CONCAT(#{riqi}, ' 23:59:59'),'%Y-%m-%d %H:%i:%s')
```

- [ ] **Step 4: Commit**

```bash
git add ruoyi-system/src/main/java/com/ruoyi/system/mapper/UserBillingMapper.java
git commit -m "feat(billing): 日账单 mapper 增加 mdId 可选过滤"
```

---

### Task 2: Mapper 层 — 修改周账单相关 SQL

**Files:**
- Modify: `ruoyi-system/src/main/java/com/ruoyi/system/mapper/UserBillingMapper.java`

- [ ] **Step 1: 修改 `getmeeklist` 方法 — 增加 mdId 参数和 SQL 条件**

方法签名增加 `@Param("mdId") Long mdId`，SQL 在 `user_id=#{shid}` 后增加 `and (#{mdId} IS NULL OR md_id = #{mdId})`。

原 SQL：
```sql
SELECT * FROM user_billing WHERE type=0 and user_id=#{shid} and WEEK(cretim) = WEEK(#{riqi}) and  YEAR(cretim) = YEAR(#{riqi}) ORDER BY id desc limit #{page},#{size}
```
改为：
```sql
SELECT * FROM user_billing WHERE type=0 and user_id=#{shid} and (#{mdId} IS NULL OR md_id = #{mdId}) and WEEK(cretim) = WEEK(#{riqi}) and  YEAR(cretim) = YEAR(#{riqi}) ORDER BY id desc limit #{page},#{size}
```

- [ ] **Step 2: 修改 `getmeeksum` 方法 — 增加 mdId 参数和 SQL 条件**

方法签名增加 `@Param("mdId") Long mdId`，SQL 在 `user_id=#{shid}` 后增加 `and (#{mdId} IS NULL OR md_id = #{mdId})`。

原 SQL：
```sql
SELECT IFNULL(COUNT(1),0) AS sul,IFNULL(SUM(amount),0) AS shouru ,IFNULL(SUM(divvy),0) AS commission,IFNULL(SUM(tax),0) AS tax,IFNULL(SUM(behalf_amount),0) AS behalfAmount FROM user_billing WHERE type=0 and user_id=#{shid} and  WEEK(cretim) = WEEK(#{riqi}) and  YEAR(cretim) = YEAR(#{riqi}) and state=0
```
改为：
```sql
SELECT IFNULL(COUNT(1),0) AS sul,IFNULL(SUM(amount),0) AS shouru ,IFNULL(SUM(divvy),0) AS commission,IFNULL(SUM(tax),0) AS tax,IFNULL(SUM(behalf_amount),0) AS behalfAmount FROM user_billing WHERE type=0 and user_id=#{shid} and (#{mdId} IS NULL OR md_id = #{mdId}) and  WEEK(cretim) = WEEK(#{riqi}) and  YEAR(cretim) = YEAR(#{riqi}) and state=0
```

- [ ] **Step 3: 修改 `getShMeeksum` 方法 — 增加 mdId 参数和 SQL 条件**

方法签名增加 `@Param("mdId") Long mdId`，SQL 在 `b.user_id=#{shid}` 后增加 `and (#{mdId} IS NULL OR b.md_id = #{mdId})`。

原 SQL：
```sql
SELECT IFNULL(SUM(o.food_amount),0) AS foodAmount,IFNULL(SUM(o.md_discount_amount),0)+IFNULL(SUM(o.md_sales_reduction),0) as shYh  FROM user_billing b left join pos_order o on b.dd_id=o.dd_id WHERE b.type=0 and b.user_id=#{shid} and  WEEK(b.cretim) = WEEK(#{riqi}) and  YEAR(b.cretim) = YEAR(#{riqi}) and b.state=0
```
改为：
```sql
SELECT IFNULL(SUM(o.food_amount),0) AS foodAmount,IFNULL(SUM(o.md_discount_amount),0)+IFNULL(SUM(o.md_sales_reduction),0) as shYh  FROM user_billing b left join pos_order o on b.dd_id=o.dd_id WHERE b.type=0 and b.user_id=#{shid} and (#{mdId} IS NULL OR b.md_id = #{mdId}) and  WEEK(b.cretim) = WEEK(#{riqi}) and  YEAR(b.cretim) = YEAR(#{riqi}) and b.state=0
```

- [ ] **Step 4: Commit**

```bash
git add ruoyi-system/src/main/java/com/ruoyi/system/mapper/UserBillingMapper.java
git commit -m "feat(billing): 周账单 mapper 增加 mdId 可选过滤"
```

---

### Task 3: Mapper 层 — 修改月账单相关 SQL

**Files:**
- Modify: `ruoyi-system/src/main/java/com/ruoyi/system/mapper/UserBillingMapper.java`

- [ ] **Step 1: 修改 `getmatlist` 方法 — 增加 mdId 参数和 SQL 条件**

方法签名增加 `@Param("mdId") Long mdId`，SQL 在 `user_id=#{shid}` 后增加 `and (#{mdId} IS NULL OR md_id = #{mdId})`。

原 SQL：
```sql
SELECT * FROM user_billing WHERE type=0 and user_id=#{shid} and DATE_FORMAT(cretim, '%Y-%m') = #{riqi}  ORDER BY id desc limit #{page},#{size}
```
改为：
```sql
SELECT * FROM user_billing WHERE type=0 and user_id=#{shid} and (#{mdId} IS NULL OR md_id = #{mdId}) and DATE_FORMAT(cretim, '%Y-%m') = #{riqi}  ORDER BY id desc limit #{page},#{size}
```

- [ ] **Step 2: 修改 `getmatsum` 方法 — 增加 mdId 参数和 SQL 条件**

方法签名增加 `@Param("mdId") Long mdId`，SQL 在 `user_id=#{shid}` 后增加 `and (#{mdId} IS NULL OR md_id = #{mdId})`。

原 SQL：
```sql
SELECT IFNULL(COUNT(1),0) AS sul,IFNULL(SUM(amount),0) AS shouru ,IFNULL(SUM(divvy),0) AS commission,IFNULL(SUM(tax),0) AS tax,IFNULL(SUM(behalf_amount),0) AS behalfAmount FROM user_billing WHERE type=0 and user_id=#{shid} and DATE_FORMAT(cretim, '%Y-%m') = #{riqi} and state=0
```
改为：
```sql
SELECT IFNULL(COUNT(1),0) AS sul,IFNULL(SUM(amount),0) AS shouru ,IFNULL(SUM(divvy),0) AS commission,IFNULL(SUM(tax),0) AS tax,IFNULL(SUM(behalf_amount),0) AS behalfAmount FROM user_billing WHERE type=0 and user_id=#{shid} and (#{mdId} IS NULL OR md_id = #{mdId}) and DATE_FORMAT(cretim, '%Y-%m') = #{riqi} and state=0
```

- [ ] **Step 3: 修改 `getshMonthsum` 方法 — 增加 mdId 参数和 SQL 条件**

方法签名增加 `@Param("mdId") Long mdId`，SQL 在 `b.user_id=#{shid}` 后增加 `and (#{mdId} IS NULL OR b.md_id = #{mdId})`。

原 SQL：
```sql
SELECT IFNULL(SUM(o.food_amount),0) AS foodAmount,IFNULL(SUM(o.md_discount_amount),0)+IFNULL(SUM(o.md_sales_reduction),0) as shYh  FROM user_billing b left join pos_order o on b.dd_id=o.dd_id WHERE b.type=0 and b.user_id=#{shid} and DATE_FORMAT(b.cretim, '%Y-%m') = #{riqi} and b.state=0
```
改为：
```sql
SELECT IFNULL(SUM(o.food_amount),0) AS foodAmount,IFNULL(SUM(o.md_discount_amount),0)+IFNULL(SUM(o.md_sales_reduction),0) as shYh  FROM user_billing b left join pos_order o on b.dd_id=o.dd_id WHERE b.type=0 and b.user_id=#{shid} and (#{mdId} IS NULL OR b.md_id = #{mdId}) and DATE_FORMAT(b.cretim, '%Y-%m') = #{riqi} and b.state=0
```

- [ ] **Step 4: Commit**

```bash
git add ruoyi-system/src/main/java/com/ruoyi/system/mapper/UserBillingMapper.java
git commit -m "feat(billing): 月账单 mapper 增加 mdId 可选过滤"
```

---

### Task 4: Controller 层 — 三个接口方法增加 mdId 参数

**Files:**
- Modify: `ruoyi-admin/src/main/java/com/ruoyi/app/pay/UserBillingController.java`

- [ ] **Step 1: 修改 `getzddaylist` 方法**

在方法签名中增加 `@RequestParam(required = false) Long mdId` 参数，并在调用 mapper 时传入 `mdId`。

修改前：
```java
public AjaxResult getzddaylist(@RequestHeader String token,
                               @RequestParam Long page,
                               @RequestParam Long size,
                               @RequestParam String riqi,
                               @RequestParam(required = false) Boolean isCancel
) {
```
修改后：
```java
public AjaxResult getzddaylist(@RequestHeader String token,
                               @RequestParam Long page,
                               @RequestParam Long size,
                               @RequestParam String riqi,
                               @RequestParam(required = false) Boolean isCancel,
                               @RequestParam(required = false) Long mdId
) {
```

调用 mapper 处，将 `null` 改为 `mdId`：

```java
// 原：List<UserBilling> list = userBillingMapper.getdaylist(id, riqi, (page - 1) * size, size);
List<UserBilling> list = userBillingMapper.getdaylist(id, riqi, mdId, (page - 1) * size, size);
// 原：BillDTO sum = userBillingMapper.getdaysum(id, riqi);
BillDTO sum = userBillingMapper.getdaysum(id, riqi, mdId);
```

以及商户统计处：
```java
// 原：ShBillingDto shBillingDto = userBillingMapper.getShDaysum(id, riqi);
ShBillingDto shBillingDto = userBillingMapper.getShDaysum(id, riqi, mdId);
```

- [ ] **Step 2: 修改 `getzdmeeklist` 方法**

在方法签名中增加 `@RequestParam(required = false) Long mdId` 参数：

```java
public AjaxResult getzdmeeklist(@RequestHeader String token,
                                @RequestParam Long page,
                                @RequestParam Long size,
                                @RequestParam String riqi,
                                @RequestParam(required = false) Boolean isCancel,
                                @RequestParam(required = false) Long mdId) {
```

调用 mapper 处传入 `mdId`：
```java
List<UserBilling> list = userBillingMapper.getmeeklist(id, riqi, mdId, (page - 1) * size, size);
BillDTO sum = userBillingMapper.getmeeksum(id, riqi, mdId);
```

以及商户统计处：
```java
ShBillingDto shBillingDto = userBillingMapper.getShMeeksum(id, riqi, mdId);
```

- [ ] **Step 3: 修改 `getzdmatlist` 方法**

在方法签名中增加 `@RequestParam(required = false) Long mdId` 参数：

```java
public AjaxResult getzdmatlist(@RequestHeader String token,
                               @RequestParam Long page,
                               @RequestParam Long size,
                               @RequestParam String riqi,
                               @RequestParam(required = false) Boolean isCancel,
                               @RequestParam(required = false) Long mdId) {
```

调用 mapper 处传入 `mdId`：
```java
List<UserBilling> list = userBillingMapper.getmatlist(id, riqi, mdId, (page - 1) * size, size);
BillDTO sum = userBillingMapper.getmatsum(id, riqi, mdId);
```

以及商户统计处：
```java
ShBillingDto shBillingDto = userBillingMapper.getshMonthsum(id, riqi, mdId);
```

- [ ] **Step 4: Commit**

```bash
git add ruoyi-admin/src/main/java/com/ruoyi/app/pay/UserBillingController.java
git commit -m "feat(billing): 日/周/月账单接口增加 mdId 可选过滤参数"
```

---

## Self-Review

**Spec coverage:**
- spec 要求 `getdaylist`、`getmeeklist`、`getmatlist` 增加 mdId 过滤 -> Task 1/2/3 (mapper) + Task 4 (controller) 覆盖
- mdId 为非必需参数 -> 所有 SQL 使用 `(#{mdId} IS NULL OR md_id = #{mdId})` 模式，mdId 为 null 时不影响原逻辑

**Placeholder scan:** 无 TBD/TODO，所有 SQL 和代码均完整提供。

**Type consistency:** 所有 mapper 方法 mdId 参数类型为 `Long`，与 controller `@RequestParam` 和 `UserBilling.mdId` 字段类型一致。

**Note:** 取消订单列表（`getDayCancleOrderList`/`getWeekCancleOrderList`/`getMonthCancleOrderList`）查询的是 `pos_order` 表而非 `user_billing` 表，spec 未要求对其增加 mdId 过滤，因此不在本次范围内。
