Skip to content

Commit 7bd240a

Browse files
authored
Merge pull request #40 from SensorX-labs/cdhai
Cdhai
2 parents 1b99099 + b00bea5 commit 7bd240a

7 files changed

Lines changed: 254 additions & 56 deletions

File tree

‎SensorX.Master.Application/Queries/Quotes/GetDetailQuoteById/GetDetailQuoteByIdHandler.cs‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,21 @@ public async Task<Result<GetDetailQuoteByIdResponse>> Handle(GetDetailQuoteByIdQ
3333
.SelectMany(r => r.Items.Select(ri => new { ri.ProductId, ri.ProductName })),
3434
cancellationToken
3535
);
36-
var rfqItemMap = rfqItems.ToDictionary(x => x.ProductId, x => x.ProductName);
36+
var rfqItemMap = rfqItems.ToDictionary(x => x.ProductId.Value, x => x.ProductName);
37+
38+
var rfqCode = await _queryExecutor.FirstOrDefaultAsync(
39+
_rfqQueryBuilder.QueryAsNoTracking
40+
.Where(r => r.Id == quote.RFQId)
41+
.Select(r => r.Code.Value),
42+
cancellationToken
43+
) ?? string.Empty;
3744

3845
var response = new GetDetailQuoteByIdResponse
3946
(
40-
quote.Id,
41-
quote.Code,
42-
quote.RFQId,
47+
quote.Id.Value,
48+
quote.Code.Value,
49+
quote.RFQId.Value,
50+
rfqCode,
4351
quote.Status,
4452
quote.QuoteDate,
4553
quote.Note,

‎SensorX.Master.Application/Queries/Quotes/GetDetailQuoteById/GetDetailQuoteByIdQuery.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public sealed record GetDetailQuoteByIdResponse
1111
Guid Id,
1212
string Code,
1313
Guid RFQId,
14+
string RFQCode,
1415
QuoteStatus Status,
1516
DateTimeOffset? QuoteDate,
1617
string? Note,
@@ -69,4 +70,4 @@ public sealed record QuoteItemResponse
6970
decimal LineAmount,
7071
decimal TaxAmount,
7172
decimal TotalLineAmount
72-
);
73+
);

‎SensorX.Master.Infrastructure/Services/Telegram/TelegramBotBackgroundService.AI.cs‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,24 @@ Phân tích tin nhắn và trả về tên skill phù hợp nhất.
4040
4141
Danh sách skills:
4242
- HELP: Khi người dùng hỏi help, menu, hướng dẫn, trợ giúp, bot làm được gì
43+
44+
=== BÁO GIÁ ===
4345
- QUOTE_PENDING: Báo giá đang chờ duyệt, báo giá chờ phê duyệt, quote pending
4446
- QUOTE_APPROVED: Danh sách báo giá đã duyệt, báo giá được chấp nhận, approved quotes
4547
- QUOTE_DETAIL: Xem chi tiết một báo giá cụ thể. Trả về: QUOTE_DETAIL:<mã báo giá>
46-
- QUOTE_APPROVE: Duyệt/phê duyệt một báo giá cụ thể. Trả về: QUOTE_APPROVE:<mã báo giá> (VD: QUOTE_APPROVE:QT-2024-001)
48+
- QUOTE_APPROVE: Duyệt/phê duyệt một báo giá cụ thể. Trả về: QUOTE_APPROVE:<mã báo giá>
4749
- QUOTE_REJECT: Từ chối một báo giá cụ thể. Trả về: QUOTE_REJECT:<mã báo giá>|<lý do>
50+
51+
=== ĐƠN HÀNG ===
52+
- ORDER_LIST: Xem danh sách đơn hàng, list orders, đơn hàng mới
53+
- ORDER_DETAIL: Xem chi tiết một đơn hàng (bao gồm thanh toán + sản phẩm). Trả về: ORDER_DETAIL:<mã đơn hàng>
54+
4855
- UNKNOWN: Không có skill nào phù hợp.
4956
50-
Quy tắc: Chỉ trả về DUY NHẤT tên skill. KHÔNG giải thích, KHÔNG thêm văn bản khác.
57+
Quy tắc:
58+
- Chỉ trả về DUY NHẤT tên skill. KHÔNG giải thích, KHÔNG thêm văn bản khác.
59+
- Nếu người dùng nhắc "đơn hàng" + mã ORD-xxx → ORDER_DETAIL:<mã>.
60+
- Nếu người dùng nhắc "báo giá" + mã QT-xxx → QUOTE_DETAIL:<mã>.
5161
""";
5262

5363
private async Task<string?> ResolveSkillAsync(string userMessage, CancellationToken ct)
Lines changed: 108 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
using System.Text;
2+
using SensorX.Master.Application.Queries.Orders.GetDetailOrderById;
3+
using SensorX.Master.Application.Queries.Orders.GetPageListOrder;
24
using SensorX.Master.Application.Queries.Quotes.GetDetailQuoteById;
35
using SensorX.Master.Application.Queries.Quotes.GetPageListQuote;
4-
using SensorX.Master.Application.Queries.Quotes.GetQuoteStats;
56

67
namespace SensorX.Master.Infrastructure.Services.Telegram;
78

@@ -11,25 +12,28 @@ private static string FormatHelpMessage() =>
1112
"""
1213
*SensorX Telegram Bot*
1314
14-
Tôi có thể hỗ trợ các tác vụ báo giá sau:
15+
*Báo giá:*
16+
- `Báo giá chờ duyệt`
17+
- `Báo giá đã duyệt`
18+
- `Chi tiết báo giá QT-2024-001`
19+
- `Duyệt báo giá QT-2024-001`
20+
- `Từ chối báo giá QT-2024-001 vì sai đơn giá`
1521
16-
• `Báo giá chờ duyệt`
17-
• `Báo giá đã duyệt`
18-
• `Chi tiết báo giá QT-2024-001`
19-
• `Duyệt báo giá QT-2024-001`
20-
• `Từ chối báo giá QT-2024-001 vì sai đơn giá`
22+
*Đơn hàng:*
23+
- `Danh sách đơn hàng`
24+
- `Chi tiết đơn hàng ORD-2024-001`
2125
2226
Lệnh hữu ích:
23-
• `/help` hoặc `/menu`: xem hướng dẫn này
27+
- `/help` hoặc `/menu`: xem hướng dẫn này
2428
2529
Gợi ý:
26-
• Bạn có thể nhắn tự nhiên, không cần nhớ tên skill nội bộ.
27-
• Khi cần thao tác với 1 báo giá, hãy gửi kèm mã báo giá.
30+
- Bạn có thể nhắn tự nhiên, không cần nhớ cú pháp chính xác.
31+
- Khi hỏi về 1 bản ghi cụ thể, gửi kèm mã (VD: QT-2024-001, ORD-2024-001).
2832
""";
2933

3034
private static string FormatQuoteList(List<GetPageListQuoteResponse> items, string title)
3135
{
32-
var header = $"📋 *{title}*";
36+
var header = $"*{title}*";
3337
if (items.Count == 0) return $"{header}\n\n_Không có dữ liệu._";
3438

3539
var sb = new StringBuilder();
@@ -39,12 +43,12 @@ private static string FormatQuoteList(List<GetPageListQuoteResponse> items, stri
3943

4044
for (int i = 0; i < items.Count; i++)
4145
{
42-
var q = items[i];
43-
sb.AppendLine($"*{i + 1}. {EscapeMarkdown(q.Code)}*")
44-
.AppendLine($" {QuoteEmoji(q.Status.ToString())} `{q.Status}`")
45-
.AppendLine($" 👤 {EscapeMarkdown(q.CompanyName)}")
46-
.AppendLine($" 💰 `{q.GrandTotal:N0} VND` | 📦 {q.ItemCount} SP")
47-
.AppendLine($" 📅 {q.CreatedAt:dd/MM/yyyy HH:mm}")
46+
var quote = items[i];
47+
sb.AppendLine($"*{i + 1}. {EscapeMarkdown(quote.Code)}*")
48+
.AppendLine($" {QuoteEmoji(quote.Status.ToString())} `{quote.Status}`")
49+
.AppendLine($" Công ty: {EscapeMarkdown(quote.CompanyName)}")
50+
.AppendLine($" Tổng tiền: `{quote.GrandTotal:N0} VND` | Sản phẩm: {quote.ItemCount}")
51+
.AppendLine($" Tạo lúc: {quote.CreatedAt:dd/MM/yyyy HH:mm}")
4852
.AppendLine();
4953
}
5054

@@ -54,31 +58,31 @@ private static string FormatQuoteList(List<GetPageListQuoteResponse> items, stri
5458
private static string FormatQuoteDetail(GetDetailQuoteByIdResponse quote)
5559
{
5660
var sb = new StringBuilder();
57-
sb.AppendLine($"📄 *Chi Tiết Báo Giá {EscapeMarkdown(quote.Code)}*")
61+
sb.AppendLine($"*Chi Tiết Báo Giá {EscapeMarkdown(quote.Code)}*")
5862
.AppendLine($" {QuoteEmoji(quote.Status.ToString())} `{quote.Status}`")
59-
.AppendLine($" 👤 {EscapeMarkdown(quote.Customer.CompanyName)}")
60-
.AppendLine($" 📧 `{EscapeMarkdown(quote.Customer.Email)}`")
61-
.AppendLine($" 📞 `{EscapeMarkdown(quote.Customer.Phone)}`")
62-
.AppendLine($" 👨‍💼 {EscapeMarkdown(quote.Sender.Name)}")
63-
.AppendLine($" 💰 Tạm tính: `{quote.Subtotal:N0} VND`")
64-
.AppendLine($" 🧾 Thuế: `{quote.TotalTax:N0} VND`")
65-
.AppendLine($" 🏁 Tổng cộng: `{quote.GrandTotal:N0} VND`");
63+
.AppendLine($" Công ty: {EscapeMarkdown(quote.Customer.CompanyName)}")
64+
.AppendLine($" Email: `{EscapeMarkdown(quote.Customer.Email)}`")
65+
.AppendLine($" Điện thoại: `{EscapeMarkdown(quote.Customer.Phone)}`")
66+
.AppendLine($" Nhân viên: {EscapeMarkdown(quote.Sender.Name)}")
67+
.AppendLine($" Tạm tính: `{quote.Subtotal:N0} VND`")
68+
.AppendLine($" Thuế: `{quote.TotalTax:N0} VND`")
69+
.AppendLine($" Tổng cộng: `{quote.GrandTotal:N0} VND`");
6670

6771
if (quote.QuoteDate.HasValue)
68-
sb.AppendLine($" 📅 Hiệu lực đến: `{quote.QuoteDate.Value:dd/MM/yyyy HH:mm}`");
72+
sb.AppendLine($" Hiệu lực đến: `{quote.QuoteDate.Value:dd/MM/yyyy HH:mm}`");
6973

7074
if (!string.IsNullOrWhiteSpace(quote.Note))
71-
sb.AppendLine($" 📝 Ghi chú: _{EscapeMarkdown(quote.Note)}_");
75+
sb.AppendLine($" Ghi chú: _{EscapeMarkdown(quote.Note)}_");
7276

7377
if (!string.IsNullOrWhiteSpace(quote.ReasonReject))
74-
sb.AppendLine($" ❌ Lý do từ chối: _{EscapeMarkdown(quote.ReasonReject)}_");
78+
sb.AppendLine($" Lý do từ chối: _{EscapeMarkdown(quote.ReasonReject)}_");
7579

7680
sb.AppendLine()
7781
.AppendLine("*Sản phẩm:*");
7882

7983
foreach (var item in quote.Items.Take(5))
8084
{
81-
sb.AppendLine($"• `{EscapeMarkdown(item.ProductCode)}` x{item.Quantity} - `{item.TotalLineAmount:N0} VND`");
85+
sb.AppendLine($"- `{EscapeMarkdown(item.ProductCode)}` x{item.Quantity} - `{item.TotalLineAmount:N0} VND`");
8286
}
8387

8488
if (quote.Items.Count > 5)
@@ -87,19 +91,87 @@ private static string FormatQuoteDetail(GetDetailQuoteByIdResponse quote)
8791
return sb.ToString().TrimEnd();
8892
}
8993

94+
private static string FormatOrderList(List<GetPageListOrderResponse> items, string title)
95+
{
96+
var header = $"*{title}*";
97+
if (items.Count == 0) return $"{header}\n\n_Không có dữ liệu._";
98+
99+
var sb = new StringBuilder();
100+
sb.AppendLine(header)
101+
.AppendLine($"_Hiển thị {items.Count} bản ghi gần nhất_")
102+
.AppendLine();
103+
104+
for (int i = 0; i < items.Count; i++)
105+
{
106+
var order = items[i];
107+
sb.AppendLine($"*{i + 1}. {EscapeMarkdown(order.Code)}*")
108+
.AppendLine($" {OrderEmoji(order.Status)} `{order.Status}`")
109+
.AppendLine($" Công ty: {EscapeMarkdown(order.CompanyName)}")
110+
.AppendLine($" Tổng tiền: `{order.GrandTotal:N0} VND` | Sản phẩm: {order.ItemCount}")
111+
.AppendLine($" Ngày đặt: {order.OrderDate:dd/MM/yyyy HH:mm}")
112+
.AppendLine();
113+
}
114+
115+
return sb.ToString().TrimEnd();
116+
}
117+
118+
private static string FormatOrderDetail(GetDetailOrderByIdResponse order)
119+
{
120+
var sb = new StringBuilder();
121+
sb.AppendLine($"*Chi Tiết Đơn Hàng {EscapeMarkdown(order.Code)}*")
122+
.AppendLine($" {OrderEmoji(order.Status)} `{order.Status}`")
123+
.AppendLine($" Công ty: {EscapeMarkdown(order.CompanyName)}")
124+
.AppendLine($" Người nhận: {EscapeMarkdown(order.RecipientName)} - `{EscapeMarkdown(order.RecipientPhone)}`")
125+
.AppendLine($" Email: `{EscapeMarkdown(order.Email)}`")
126+
.AppendLine($" Địa chỉ: {EscapeMarkdown(order.Address)}")
127+
.AppendLine($" Nhân viên: {EscapeMarkdown(order.SenderName)}")
128+
.AppendLine($" Tạm tính: `{order.Subtotal:N0} VND`")
129+
.AppendLine($" Thuế: `{order.TotalTax:N0} VND`")
130+
.AppendLine($" Tổng cộng: `{order.GrandTotal:N0} VND`")
131+
.AppendLine($" Ngày đặt: `{order.OrderDate:dd/MM/yyyy HH:mm}`");
132+
133+
if (!string.IsNullOrWhiteSpace(order.PaymentStatus) || !string.IsNullOrWhiteSpace(order.PaymentType))
134+
sb.AppendLine($" Thanh toán: `{order.PaymentStatus ?? "N/A"}` ({order.PaymentType ?? "N/A"})");
135+
136+
if (order.PaymentAmount.HasValue)
137+
sb.AppendLine($" Số tiền đã thanh toán: `{order.PaymentAmount.Value:N0} VND`");
138+
139+
sb.AppendLine()
140+
.AppendLine("*Sản phẩm:*");
141+
142+
foreach (var item in order.Items.Take(5))
143+
{
144+
sb.AppendLine($"- `{EscapeMarkdown(item.ProductCode)}` x{item.Quantity} - `{item.TotalLineAmount:N0} VND`");
145+
}
146+
147+
if (order.Items.Count > 5)
148+
sb.AppendLine($"_Còn {order.Items.Count - 5} sản phẩm khác..._");
149+
150+
return sb.ToString().TrimEnd();
151+
}
152+
90153
private static string EscapeMarkdown(string text) =>
91154
text.Replace("_", "\\_")
92155
.Replace("*", "\\*")
93156
.Replace("`", "\\`")
94157
.Replace("[", "\\[");
95158

96-
private static string QuoteEmoji(string s) => s.ToLower() switch
159+
private static string QuoteEmoji(string s) => s.ToLowerInvariant() switch
160+
{
161+
var x when x.Contains("draft") => "[nháp]",
162+
var x when x.Contains("pending") || x.Contains("submitted") => "[chờ duyệt]",
163+
var x when x.Contains("approved") => "[đã duyệt]",
164+
var x when x.Contains("accepted") => "[đã chấp nhận]",
165+
var x when x.Contains("rejected") || x.Contains("cancel") => "[từ chối]",
166+
_ => "[báo giá]"
167+
};
168+
169+
private static string OrderEmoji(string s) => s.ToLowerInvariant() switch
97170
{
98-
var x when x.Contains("draft") => "📝",
99-
var x when x.Contains("pending") || x.Contains("submitted") => "⏳",
100-
var x when x.Contains("approved") => "✅",
101-
var x when x.Contains("accepted") => "🎉",
102-
var x when x.Contains("rejected") || x.Contains("cancel") => "❌",
103-
_ => "📄"
171+
var x when x.Contains("pending") => "[chờ xử lý]",
172+
var x when x.Contains("paid") || x.Contains("completed") => "[hoàn tất]",
173+
var x when x.Contains("shipping") || x.Contains("deliver") => "[đang giao]",
174+
var x when x.Contains("cancel") || x.Contains("reject") => "[đã hủy]",
175+
_ => "[đơn hàng]"
104176
};
105177
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using MediatR;
2+
using SensorX.Master.Application.Queries.Orders.GetDetailOrderById;
3+
using SensorX.Master.Application.Queries.Orders.GetPageListOrder;
4+
5+
namespace SensorX.Master.Infrastructure.Services.Telegram;
6+
7+
public partial class TelegramBotBackgroundService
8+
{
9+
/// <summary>
10+
/// Danh sách 10 đơn hàng gần nhất (tất cả trạng thái).
11+
/// </summary>
12+
private async Task<string> HandleOrdersAsync(IMediator mediator, CancellationToken ct)
13+
{
14+
var result = await mediator.Send(
15+
new GetPageListOrderQuery { PageNumber = 1, PageSize = 10 },
16+
ct
17+
);
18+
19+
if (!result.IsSuccess || result.Value is null)
20+
return "Không thể truy vấn đơn hàng từ database.";
21+
22+
return FormatOrderList(result.Value.Items, "Đơn Hàng Gần Nhất");
23+
}
24+
25+
/// <summary>
26+
/// Chi tiết đơn hàng theo mã code, bao gồm thông tin thanh toán và sản phẩm.
27+
/// </summary>
28+
private async Task<string> HandleOrderDetailAsync(IMediator mediator, string orderCode, CancellationToken ct)
29+
{
30+
if (string.IsNullOrWhiteSpace(orderCode))
31+
return "Vui lòng cung cấp mã đơn hàng. Ví dụ: _Chi tiết đơn hàng ORD-2024-001_";
32+
33+
var search = await mediator.Send(
34+
new GetPageListOrderQuery
35+
{
36+
Code = orderCode,
37+
PageNumber = 1,
38+
PageSize = 1
39+
},
40+
ct
41+
);
42+
43+
var matchedOrder = search.Value?.Items.FirstOrDefault();
44+
45+
if (!search.IsSuccess || matchedOrder is null)
46+
return $"Không tìm thấy đơn hàng với mã *{EscapeMarkdown(orderCode)}*";
47+
48+
var detail = await mediator.Send(new GetDetailOrderByIdQuery(matchedOrder.Id), ct);
49+
if (!detail.IsSuccess || detail.Value is null)
50+
return $"Không lấy được chi tiết đơn hàng *{EscapeMarkdown(orderCode)}*";
51+
52+
return FormatOrderDetail(detail.Value);
53+
}
54+
}

0 commit comments

Comments
 (0)