1、优化"获取每日经营数据列表",入参增加排序字段;

This commit is contained in:
chenlin 2025-06-20 15:20:31 +08:00
parent c07d7d6722
commit dc66375898
2 changed files with 50 additions and 15 deletions

View File

@ -43,7 +43,7 @@ func CreateOrUpdateBusinessSummary(c *gin.Context) {
var existing models.DailyBusinessSummary var existing models.DailyBusinessSummary
err = orm.Eloquent. err = orm.Eloquent.
Where("date = ? AND store_id = ?", summaryDate, req.StoreID). Where("date = ? AND store_id = ?", req.Date, req.StoreID).
First(&existing).Error First(&existing).Error
if err != nil { if err != nil {

View File

@ -10,7 +10,9 @@ import (
"go-admin/tools" "go-admin/tools"
"go-admin/tools/config" "go-admin/tools/config"
"math" "math"
"sort"
"strconv" "strconv"
"strings"
"time" "time"
) )
@ -43,6 +45,7 @@ type BusinessSummaryListRequest struct {
EndTime string `json:"end_time"` // 结束时间 EndTime string `json:"end_time"` // 结束时间
PageIndex int `json:"page_index"` // 当前页码 PageIndex int `json:"page_index"` // 当前页码
PageSize int `json:"page_size"` // 每页条数 PageSize int `json:"page_size"` // 每页条数
SortType string `json:"sort_type"` // 排序类型desc 降序、asc 升序
} }
type BusinessSummaryItem struct { type BusinessSummaryItem struct {
@ -102,8 +105,8 @@ type MarketStoreSalesData struct {
Count int64 `json:"count"` // 销售数量 Count int64 `json:"count"` // 销售数量
} }
// parseToDate 解析多种格式的时间字符串,仅保留日期部分(年月日) // ParseToDate 解析多种格式的时间字符串,仅保留日期部分(年月日)
func parseToDate(s string) (time.Time, error) { func ParseToDate(s string) (time.Time, error) {
formats := []string{ formats := []string{
"2006-01-02", // 标准日期格式 "2006-01-02", // 标准日期格式
time.RFC3339, // 带时区时间戳 2025-05-20T00:00:00+08:00 time.RFC3339, // 带时区时间戳 2025-05-20T00:00:00+08:00
@ -134,8 +137,8 @@ func QueryListBusinessSummary(req *BusinessSummaryListRequest, c *gin.Context) (
PageSize: req.PageSize, PageSize: req.PageSize,
} }
startDate, err1 := parseToDate(req.StartTime) startDate, err1 := ParseToDate(req.StartTime)
endDate, err2 := parseToDate(req.EndTime) endDate, err2 := ParseToDate(req.EndTime)
if err1 != nil || err2 != nil { if err1 != nil || err2 != nil {
return nil, errors.New("日期格式错误") return nil, errors.New("日期格式错误")
} }
@ -148,6 +151,14 @@ func QueryListBusinessSummary(req *BusinessSummaryListRequest, c *gin.Context) (
for d := startDate; !d.After(endDate); d = d.AddDate(0, 0, 1) { for d := startDate; !d.After(endDate); d = d.AddDate(0, 0, 1) {
allDates = append(allDates, d.Format("2006-01-02")) allDates = append(allDates, d.Format("2006-01-02"))
} }
// 根据排序方式进行调整
if strings.ToLower(req.SortType) == "desc" {
// 倒序排列
for i, j := 0, len(allDates)-1; i < j; i, j = i+1, j-1 {
allDates[i], allDates[j] = allDates[j], allDates[i]
}
}
totalDays := len(allDates) totalDays := len(allDates)
// 日期分页 // 日期分页
@ -217,15 +228,28 @@ func QueryListBusinessSummary(req *BusinessSummaryListRequest, c *gin.Context) (
es = es.Where("store_id IN ?", req.StoreId) es = es.Where("store_id IN ?", req.StoreId)
} }
err := qs.Select("DATE_FORMAT(date, '%Y-%m-%d') AS date, " + var err error
"SUM(promotion_fee) AS promotion_fee, " + if req.SortType == "asc" {
"SUM(sales_amount) AS sales_amount, " + err = qs.Select("DATE_FORMAT(date, '%Y-%m-%d') AS date, " +
"SUM(sales_profit) AS sales_profit, " + "SUM(promotion_fee) AS promotion_fee, " +
"SUM(staff_profit) AS staff_profit, " + "SUM(sales_amount) AS sales_amount, " +
"SUM(count) AS count"). "SUM(sales_profit) AS sales_profit, " +
Group("date"). "SUM(staff_profit) AS staff_profit, " +
Order("date ASC"). "SUM(count) AS count").
Find(&dataList).Error Group("date").
Order("date ASC").
Find(&dataList).Error
} else {
err = qs.Select("DATE_FORMAT(date, '%Y-%m-%d') AS date, " +
"SUM(promotion_fee) AS promotion_fee, " +
"SUM(sales_amount) AS sales_amount, " +
"SUM(sales_profit) AS sales_profit, " +
"SUM(staff_profit) AS staff_profit, " +
"SUM(count) AS count").
Group("date").
Order("date DESC").
Find(&dataList).Error
}
if err != nil { if err != nil {
return nil, err return nil, err
@ -280,6 +304,17 @@ func QueryListBusinessSummary(req *BusinessSummaryListRequest, c *gin.Context) (
} }
} }
switch req.SortType {
case "asc":
sort.Slice(result, func(i, j int) bool {
return result[i].Date < result[j].Date
})
case "desc":
sort.Slice(result, func(i, j int) bool {
return result[i].Date > result[j].Date
})
}
resp.List = result resp.List = result
resp.Total = totalDays resp.Total = totalDays
resp.PageIndex = req.PageIndex resp.PageIndex = req.PageIndex
@ -392,7 +427,7 @@ func QueryDailyBusinessSummaryData(req *MarketStoreSalesDataReq, c *gin.Context)
} }
err = qs.Select("store_id, " + err = qs.Select("store_id, " +
"SUM(promotion_fee) AS promotion_fee, " + "SUM(promotion_fee) AS promotion_fee, " +
"SUM(sales_amount) AS total_sales_amount, " + "SUM(sales_amount) AS sales_amount, " +
"SUM(sales_profit) AS sales_profit, " + "SUM(sales_profit) AS sales_profit, " +
"SUM(staff_profit) AS staff_profit, " + "SUM(staff_profit) AS staff_profit, " +
"SUM(count) AS count"). "SUM(count) AS count").