feat(manual-entry): 支持筛选及全量导出 CSV
This commit is contained in:
@@ -2,11 +2,13 @@ package csmanualentry
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"hfb_sys/backend/internal/middleware"
|
||||
"hfb_sys/backend/internal/timeutil"
|
||||
"hfb_sys/backend/pkg/response"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -44,24 +46,13 @@ func (h *Handler) List(c *gin.Context) {
|
||||
response.Unauthorized(c, "缺少管理员上下文")
|
||||
return
|
||||
}
|
||||
query, ok := parseListFilters(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
page, pageSize := parsePagination(c)
|
||||
query := ListQuery{
|
||||
EntryNo: strings.TrimSpace(c.Query("entry_no")),
|
||||
EntryGroup: strings.TrimSpace(c.Query("entry_group")),
|
||||
Keyword: strings.TrimSpace(c.Query("keyword")),
|
||||
StartDate: strings.TrimSpace(c.Query("start_date")),
|
||||
EndDate: strings.TrimSpace(c.Query("end_date")),
|
||||
Page: page,
|
||||
PageSize: pageSize,
|
||||
}
|
||||
if raw := c.Query("admin_id"); raw != "" {
|
||||
adminID, err := strconv.ParseUint(raw, 10, 64)
|
||||
if err != nil {
|
||||
response.BadRequest(c, "客服 ID 不正确")
|
||||
return
|
||||
}
|
||||
query.AdminID = adminID
|
||||
}
|
||||
query.Page = page
|
||||
query.PageSize = pageSize
|
||||
result, err := h.service.List(c.Request.Context(), query)
|
||||
if err != nil {
|
||||
writeManualEntryError(c, err)
|
||||
@@ -70,6 +61,33 @@ func (h *Handler) List(c *gin.Context) {
|
||||
response.OK(c, result)
|
||||
}
|
||||
|
||||
// Export 导出客服手动录单 CSV;筛选参数与列表一致,不传筛选条件时导出全部。
|
||||
func (h *Handler) Export(c *gin.Context) {
|
||||
if _, ok := currentAdminID(c); !ok {
|
||||
response.Unauthorized(c, "缺少管理员上下文")
|
||||
return
|
||||
}
|
||||
query, ok := parseListFilters(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
data, rowCount, err := h.service.Export(c.Request.Context(), query)
|
||||
if err != nil {
|
||||
writeManualEntryError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
timestamp := timeutil.ShanghaiNow().Format("20060102-150405")
|
||||
filename := fmt.Sprintf("客服手动录单_%s.csv", timestamp)
|
||||
asciiFilename := fmt.Sprintf("manual-entry-export-%s.csv", timestamp)
|
||||
c.Header(
|
||||
"Content-Disposition",
|
||||
fmt.Sprintf(`attachment; filename="%s"; filename*=UTF-8''%s`, asciiFilename, encodeManualEntryExportFilename(filename)),
|
||||
)
|
||||
c.Header("X-Export-Row-Count", strconv.Itoa(rowCount))
|
||||
c.Data(http.StatusOK, "text/csv; charset=utf-8", data)
|
||||
}
|
||||
|
||||
func (h *Handler) TodayAgentStats(c *gin.Context) {
|
||||
if _, ok := currentAdminID(c); !ok {
|
||||
response.Unauthorized(c, "缺少管理员上下文")
|
||||
@@ -236,6 +254,25 @@ func parsePagination(c *gin.Context) (int, int) {
|
||||
return page, pageSize
|
||||
}
|
||||
|
||||
func parseListFilters(c *gin.Context) (ListQuery, bool) {
|
||||
query := ListQuery{
|
||||
EntryNo: strings.TrimSpace(c.Query("entry_no")),
|
||||
EntryGroup: strings.TrimSpace(c.Query("entry_group")),
|
||||
Keyword: strings.TrimSpace(c.Query("keyword")),
|
||||
StartDate: strings.TrimSpace(c.Query("start_date")),
|
||||
EndDate: strings.TrimSpace(c.Query("end_date")),
|
||||
}
|
||||
if raw := c.Query("admin_id"); raw != "" {
|
||||
adminID, err := strconv.ParseUint(raw, 10, 64)
|
||||
if err != nil {
|
||||
response.BadRequest(c, "客服 ID 不正确")
|
||||
return ListQuery{}, false
|
||||
}
|
||||
query.AdminID = adminID
|
||||
}
|
||||
return query, true
|
||||
}
|
||||
|
||||
func writeManualEntryError(c *gin.Context, err error) {
|
||||
response.RecordError(c, err)
|
||||
switch {
|
||||
@@ -259,6 +296,13 @@ func writeManualEntryError(c *gin.Context, err error) {
|
||||
response.NotFound(c, "选项不存在")
|
||||
case errors.Is(err, ErrOptionExists):
|
||||
response.Error(c, http.StatusConflict, "option_exists", "该选项已存在")
|
||||
case errors.Is(err, ErrExportTooLarge):
|
||||
response.Error(
|
||||
c,
|
||||
http.StatusBadRequest,
|
||||
"manual_entry_export_too_large",
|
||||
fmt.Sprintf("导出数据超过 %d 条,请缩小筛选范围", maxManualEntryExportRows),
|
||||
)
|
||||
default:
|
||||
response.Error(c, http.StatusInternalServerError, "internal_error", "录单服务暂时不可用")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user