# Tra Loi Danh Gia Hang Loat (Auto Reply Rating)

## Tong quan

Chuc nang **Tra Loi Danh Gia Hang Loat** cho phep nguoi ban Shopee tu dong tra loi cac danh gia (rating/review) cua khach hang chua duoc phan hoi. He thong se quet danh sach cac danh gia chua tra loi theo bo loc so sao, sau do tu dong gui phan hoi voi noi dung duoc cau hinh san. Ngoai ra, co the gui kem tin nhan chat den nguoi mua sau khi tra loi danh gia.

**Duong dan:** `Shopee > Seller > Auto > Tra loi danh gia` (`/shopee/seller/auto/reply-rating`)

**Yeu cau:** Tai khoan tra phi (Premium). Tinh nang VIP Plus cho phep tra loi khong gioi han; neu khong co VIP Plus, qua trinh se tu dong dung lai.

---

## Quy trinh su dung

Quy trinh gom **3 buoc chinh**:

```
1. Cau hinh noi dung tra loi --> 2. Chon bo loc so sao --> 3. Nhan "Chay tu dong"
```

### Buoc 1: Cau hinh noi dung tra loi

Trang cau hinh gom 2 o nhap lieu:

| Truong | Mo ta |
|--------|-------|
| **Noi dung tra loi danh gia** | Noi dung chinh se duoc gui lam phan hoi danh gia (bat buoc) |
| **Tin nhan gui danh gia** | Tin nhan chat gui kem den nguoi mua sau khi tra loi (tuy chon, de trong neu khong muon gui) |

**Noi dung mac dinh:**
> Shop cam on quy khach hang da danh gia tot cho Shop, hay tiep tuc ung ho Shop nhe

### Buoc 2: Chon bo loc so sao

Chon cac muc sao can tra loi (1-5 sao, co the chon nhieu muc cung luc). Mac dinh chon **5 sao**.

Khi thay doi bo loc, he thong tu dong truy van so luong danh gia chua tra loi tuong ung va hien thi tong so tai khu vuc log.

### Buoc 3: Chay tu dong

Nhan nut **"Chay tu dong"** de bat dau. Nut chi kha dung khi:
- Da nhap noi dung tra loi danh gia
- Da chon it nhat 1 muc sao

---

## Cau hinh

### Bien dong (Variables)

Noi dung tra loi va tin nhan chat ho tro cac bien dong, tu dong thay the bang thong tin thuc te cua tung danh gia:

| Bien | Mo ta | Vi du |
|------|-------|-------|
| `BUYER_NAME` | Ten nguoi mua | `nguyenvana` |
| `ORDER_ID` | Ma don hang | `2503121ABC...` |
| `RATING_STAR` | So sao danh gia | `5` |

**Vi du noi dung:**
```
Cam on BUYER_NAME da danh gia RATING_STAR sao cho don hang #ORDER_ID! Shop rat vui khi ban hai long.
```

**Ket qua thuc te:**
```
Cam on nguyenvana da danh gia 5 sao cho don hang #2503121ABC! Shop rat vui khi ban hai long.
```

### Thong so xu ly

| Thong so | Mo ta | Gia tri |
|----------|-------|---------|
| `sleepPerRating` | Thoi gian cho giua moi lan tra loi danh gia | 500ms |
| `sleepPerList` | Thoi gian cho giua moi lan lay danh sach moi | 5000ms |
| `limit` | So luong danh gia lay moi lan truy van | 20 |
| `concurrency` | So luong xu ly dong thoi | 1 (tuan tu) |

### Quy trinh xu ly chi tiet

Khi nhan "Chay tu dong", he thong thuc hien vong lap:

1. Goi API lay danh sach danh gia chua tra loi (`reply_status: 1`) theo bo loc so sao
2. Voi moi danh gia trong danh sach (xu ly tuan tu):
   - Kiem tra danh gia chua co phan hoi (`reply` rong) va so sao nam trong bo loc
   - Thay the cac bien dong trong noi dung tra loi
   - Goi API tra loi danh gia (`replyRatingId`)
   - Neu co noi dung tin nhan chat: goi API gui tin nhan chat den nguoi mua (`sendChatMessage`)
   - Ghi log ket qua (thanh cong/that bai)
   - Kiem tra quyen VIP Plus, dung lai neu khong co
   - Cho 500ms truoc khi xu ly danh gia tiep theo
3. Sau moi danh sach, cho 5000ms roi lay danh sach tiep theo
4. Lap lai cho den khi khong con danh gia chua tra loi

---

## Cau truc du lieu

### Query Rating (Bo loc truy van)

```
- star: number[]          // Mang so sao can loc, vd: [5] hoac [4, 5]
- limit: number           // So luong moi trang, mac dinh 20
```

### Reply Rating (Cau hinh tra loi)

```
- text: string            // Noi dung tra loi danh gia (ho tro bien dong)
```

### Reply Chat (Cau hinh tin nhan)

```
- text: string            // Noi dung tin nhan chat gui kem (de trong = khong gui)
```

### Reply Handler (Trang thai xu ly)

```
- loading: boolean        // Dang chay hay khong
- log: string[]           // Mang cac dong log
- sent: number            // So danh gia da tra loi
- total: number           // Tong so danh gia chua tra loi
- sleepPerList: number    // Delay giua cac trang (ms)
- sleepPerRating: number  // Delay giua cac danh gia (ms)
```

### Rating Data (Du lieu danh gia tu API)

```
- user_id: number         // ID nguoi mua
- user_name: string       // Ten nguoi mua
- order_id: string        // Ma don hang
- comment_id: number      // ID binh luan
- rating_star: number     // So sao (1-5)
- reply: string|null      // Phan hoi hien tai (null = chua tra loi)
```

### API Endpoints

| API | Phuong thuc | Duong dan | Chuc nang |
|-----|-------------|-----------|-----------|
| Lay danh sach danh gia | GET | `/api/v3/settings/search_shop_rating_comments_new/` | Lay danh gia chua tra loi theo bo loc |
| Lay meta danh gia | GET | `/api/v3/settings/search_shop_rating_comments_new/` | Lay so luong danh gia chua tra loi |
| Tra loi danh gia | POST | `/api/v3/settings/reply_shop_rating/` | Gui phan hoi cho danh gia cu the |
| Gui tin nhan chat | POST | (qua sendChatMessage) | Gui tin nhan chat den nguoi mua |

---

## Luu y quan trong

1. **Gioi han Shopee:** Tra loi qua nhieu danh gia lien tuc co the bi Shopee han che. He thong da co delay 500ms giua moi danh gia va 5000ms giua moi trang de giam thieu rui ro.
2. **VIP Plus:** Neu tai khoan khong co quyen VIP Plus, qua trinh se tu dong dung lai sau lan tra loi dau tien va hien thi canh bao.
3. **Chi tra loi danh gia chua phan hoi:** He thong tu dong bo qua cac danh gia da co phan hoi (`reply` khong rong).
4. **Tin nhan chat la tuy chon:** Neu truong "Tin nhan gui danh gia" de trong, he thong chi tra loi danh gia ma khong gui tin nhan chat.
5. **Xu ly tuan tu:** Cac danh gia duoc xu ly lan luot (concurrency = 1) de tranh bi gioi han boi Shopee.
6. **Khong luu cau hinh:** Cau hinh noi dung tra loi khong duoc luu lai khi roi trang. Nguoi dung can nhap lai moi lan su dung.
7. **Yeu cau dang nhap Shopee Seller:** Tinh nang su dung API noi bo cua Shopee Seller Center, yeu cau nguoi dung dang nhap vao Shopee Seller truoc khi su dung.
