1.0
This commit is contained in:
@@ -0,0 +1,157 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"awesomeProject/internal/common"
|
||||
"awesomeProject/internal/models"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// UserReconciliationHandler 用户端对账管理处理器
|
||||
type UserReconciliationHandler struct{}
|
||||
|
||||
// GetReconciliationStats 获取对账统计数据
|
||||
func (h *UserReconciliationHandler) GetReconciliationStats(c *gin.Context) {
|
||||
currentUser, _ := c.Get("current_user")
|
||||
user := currentUser.(models.User)
|
||||
|
||||
// 获取日期参数
|
||||
startDate := c.Query("start_date")
|
||||
endDate := c.Query("end_date")
|
||||
|
||||
// 如果没有传入日期,默认为今天
|
||||
if startDate == "" || endDate == "" {
|
||||
today := time.Now().Format("2006-01-02")
|
||||
startDate = today
|
||||
endDate = today
|
||||
}
|
||||
|
||||
// 解析日期
|
||||
startTime, err := time.Parse("2006-01-02", startDate)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, common.Error(400, "开始日期格式错误"))
|
||||
return
|
||||
}
|
||||
endTime, err := time.Parse("2006-01-02", endDate)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, common.Error(400, "结束日期格式错误"))
|
||||
return
|
||||
}
|
||||
|
||||
// 设置时间范围
|
||||
startTime = time.Date(startTime.Year(), startTime.Month(), startTime.Day(), 0, 0, 0, 0, startTime.Location())
|
||||
endTime = time.Date(endTime.Year(), endTime.Month(), endTime.Day(), 23, 59, 59, 999999999, endTime.Location())
|
||||
|
||||
// 构建返回数据
|
||||
result := map[string]interface{}{
|
||||
"primary_confirmed": h.getPrimaryMarketStats(user.ID, startTime, endTime, 2), // 已确认
|
||||
"primary_pending": h.getPrimaryMarketStats(user.ID, startTime, endTime, 1), // 待确认
|
||||
"secondary_buy_confirmed": h.getSecondaryBuyStats(user.ID, startTime, endTime, 2), // 二级市场买单已确认
|
||||
"secondary_buy_pending": h.getSecondaryBuyStats(user.ID, startTime, endTime, 1), // 二级市场买单待确认
|
||||
"secondary_sell_confirmed": h.getSecondarySellStats(user.ID, startTime, endTime, 2), // 二级市场卖单已确认
|
||||
"secondary_sell_pending": h.getSecondarySellStats(user.ID, startTime, endTime, 1), // 二级市场卖单待确认
|
||||
"warehouse_active": h.getWarehouseStats(user.ID, 1), // 寄售中
|
||||
"warehouse_inactive": h.getWarehouseStats(user.ID, 0), // 未寄售
|
||||
"score_change": h.getScoreChangeStats(user.ID, startTime, endTime), // 积分变更
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, common.Success(result))
|
||||
}
|
||||
|
||||
// getPrimaryMarketStats 获取一级市场统计数据
|
||||
func (h *UserReconciliationHandler) getPrimaryMarketStats(userID uint, startTime, endTime time.Time, status int) map[string]interface{} {
|
||||
var totalAmount float64
|
||||
var totalQuantity int64
|
||||
db := common.GetDB()
|
||||
// 查询一级商品订单统计
|
||||
db.Model(&models.PurchaseOrder{}).
|
||||
Where("buyer_id = ? AND product_type = ? AND order_status = ? AND created_at BETWEEN ? AND ?",
|
||||
userID, 1, status, startTime, endTime).
|
||||
Select("COALESCE(SUM(total_amount), 0) as total_amount, COALESCE(SUM(quantity), 0) as total_quantity").
|
||||
Row().Scan(&totalAmount, &totalQuantity)
|
||||
|
||||
return map[string]interface{}{
|
||||
"amount": totalAmount,
|
||||
"quantity": totalQuantity,
|
||||
}
|
||||
}
|
||||
|
||||
// getSecondaryBuyStats 获取二级市场买单统计数据
|
||||
func (h *UserReconciliationHandler) getSecondaryBuyStats(userID uint, startTime, endTime time.Time, status int) map[string]interface{} {
|
||||
var totalAmount float64
|
||||
var totalQuantity int64
|
||||
db := common.GetDB()
|
||||
// 查询二级商品买单统计
|
||||
db.Model(&models.PurchaseOrder{}).
|
||||
Where("buyer_id = ? AND product_type = ? AND order_status = ? AND created_at BETWEEN ? AND ?",
|
||||
userID, 2, status, startTime, endTime).
|
||||
Select("COALESCE(SUM(total_amount), 0) as total_amount, COALESCE(SUM(quantity), 0) as total_quantity").
|
||||
Row().Scan(&totalAmount, &totalQuantity)
|
||||
|
||||
return map[string]interface{}{
|
||||
"amount": totalAmount,
|
||||
"quantity": totalQuantity,
|
||||
}
|
||||
}
|
||||
|
||||
// getSecondarySellStats 获取二级市场卖单统计数据
|
||||
func (h *UserReconciliationHandler) getSecondarySellStats(userID uint, startTime, endTime time.Time, status int) map[string]interface{} {
|
||||
var totalAmount float64
|
||||
var totalQuantity int64
|
||||
db := common.GetDB()
|
||||
// 查询二级商品卖单统计
|
||||
db.Model(&models.SalesOrder{}).
|
||||
Where("seller_id = ? AND order_status = ? AND created_at BETWEEN ? AND ?",
|
||||
userID, status, startTime, endTime).
|
||||
Select("COALESCE(SUM(total_amount), 0) as total_amount, COALESCE(SUM(quantity), 0) as total_quantity").
|
||||
Row().Scan(&totalAmount, &totalQuantity)
|
||||
|
||||
return map[string]interface{}{
|
||||
"amount": totalAmount,
|
||||
"quantity": totalQuantity,
|
||||
}
|
||||
}
|
||||
|
||||
// getWarehouseStats 获取仓库商品统计数据
|
||||
func (h *UserReconciliationHandler) getWarehouseStats(userID uint, status int) map[string]interface{} {
|
||||
var totalAmount float64
|
||||
var totalQuantity int64
|
||||
db := common.GetDB()
|
||||
// 查询仓库商品统计
|
||||
db.Model(&models.UserWarehouse{}).
|
||||
Where("user_id = ? AND status = ?", userID, status).
|
||||
Select("COALESCE(SUM(warehouse_price * quantity), 0) as total_amount, COALESCE(SUM(quantity), 0) as total_quantity").
|
||||
Row().Scan(&totalAmount, &totalQuantity)
|
||||
|
||||
return map[string]interface{}{
|
||||
"amount": totalAmount,
|
||||
"quantity": totalQuantity,
|
||||
}
|
||||
}
|
||||
|
||||
// getScoreChangeStats 获取积分变更统计数据
|
||||
func (h *UserReconciliationHandler) getScoreChangeStats(userID uint, startTime, endTime time.Time) map[string]interface{} {
|
||||
var increaseScore int64
|
||||
var decreaseScore int64
|
||||
db := common.GetDB()
|
||||
// 查询增加的积分
|
||||
db.Model(&models.ScoreRecord{}).
|
||||
Where("user_id = ? AND change_number > 0 AND created_at BETWEEN ? AND ?",
|
||||
userID, startTime, endTime).
|
||||
Select("COALESCE(SUM(change_number), 0)").
|
||||
Row().Scan(&increaseScore)
|
||||
|
||||
// 查询减少的积分(取绝对值)
|
||||
db.Model(&models.ScoreRecord{}).
|
||||
Where("user_id = ? AND change_number < 0 AND created_at BETWEEN ? AND ?",
|
||||
userID, startTime, endTime).
|
||||
Select("COALESCE(ABS(SUM(change_number)), 0)").
|
||||
Row().Scan(&decreaseScore)
|
||||
|
||||
return map[string]interface{}{
|
||||
"increase": increaseScore,
|
||||
"decrease": decreaseScore,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user