235 lines
7.1 KiB
Go
235 lines
7.1 KiB
Go
package admin
|
||
|
||
import (
|
||
"strconv"
|
||
"strings"
|
||
"time"
|
||
|
||
"git.echol.cn/loser/Go-Web-Template/server/global"
|
||
appModel "git.echol.cn/loser/Go-Web-Template/server/model/app"
|
||
"git.echol.cn/loser/Go-Web-Template/server/model/common/response"
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
type AppAssetTransactionsApi struct{}
|
||
|
||
type appAssetTransactionListItem struct {
|
||
ID uint `json:"id"`
|
||
AppUserID uint `json:"appUserId"`
|
||
Username string `json:"username"`
|
||
AssetType string `json:"assetType"`
|
||
RechargeType string `json:"rechargeType"`
|
||
DeltaLi int64 `json:"deltaLi"`
|
||
BeforeLi int64 `json:"beforeLi"`
|
||
AfterLi int64 `json:"afterLi"`
|
||
Reason string `json:"reason"`
|
||
OperatorUserID uint `json:"operatorUserId"`
|
||
OperatorName string `json:"operatorName"`
|
||
CreatedAt time.Time `json:"createdAt"`
|
||
}
|
||
|
||
// GetAppAssetTransactionsList
|
||
// @Tags AppAssetTransactionsAdmin
|
||
// @Summary 后台获取玩家资产流水列表(单位:厘)
|
||
// @Security ApiKeyAuth
|
||
// @Produce application/json
|
||
// @Param page query int false "页码" default(1)
|
||
// @Param pageSize query int false "每页大小" default(10)
|
||
// @Param userId query int false "玩家ID"
|
||
// @Param username query string false "用户名(模糊)"
|
||
// @Param assetType query string false "资产类型:account/game_coin"
|
||
// @Param rechargeType query string false "变动类型(充值类型):offline/online_not_arrived/activity_subsidy/manual_adjustment"
|
||
// @Param operatorUserId query int false "操作人ID"
|
||
// @Param keyword query string false "关键字(原因模糊)"
|
||
// @Param startAt query string false "开始时间(ISO8601/RFC3339)"
|
||
// @Param endAt query string false "结束时间(ISO8601/RFC3339)"
|
||
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
|
||
// @Router /admin/app-asset-transactions [get]
|
||
func (a *AppAssetTransactionsApi) GetAppAssetTransactionsList(c *gin.Context) {
|
||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
||
pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", "10"))
|
||
if page <= 0 {
|
||
page = 1
|
||
}
|
||
if pageSize <= 0 || pageSize > 200 {
|
||
pageSize = 10
|
||
}
|
||
|
||
var (
|
||
userID uint
|
||
operatorUserID uint
|
||
)
|
||
if raw := strings.TrimSpace(c.Query("userId")); raw != "" {
|
||
if v, err := strconv.Atoi(raw); err == nil && v > 0 {
|
||
userID = uint(v)
|
||
}
|
||
}
|
||
if raw := strings.TrimSpace(c.Query("operatorUserId")); raw != "" {
|
||
if v, err := strconv.Atoi(raw); err == nil && v > 0 {
|
||
operatorUserID = uint(v)
|
||
}
|
||
}
|
||
|
||
username := strings.TrimSpace(c.Query("username"))
|
||
assetType := strings.TrimSpace(c.Query("assetType"))
|
||
rechargeType := strings.TrimSpace(c.Query("rechargeType"))
|
||
keyword := strings.TrimSpace(c.Query("keyword"))
|
||
|
||
var startAt *time.Time
|
||
if raw := strings.TrimSpace(c.Query("startAt")); raw != "" {
|
||
if t, err := time.Parse(time.RFC3339, raw); err == nil {
|
||
startAt = &t
|
||
}
|
||
}
|
||
var endAt *time.Time
|
||
if raw := strings.TrimSpace(c.Query("endAt")); raw != "" {
|
||
if t, err := time.Parse(time.RFC3339, raw); err == nil {
|
||
endAt = &t
|
||
}
|
||
}
|
||
|
||
// tx + join app_users + join sys_users
|
||
db := global.GVA_DB.Table("app_asset_transactions tx").
|
||
Joins("JOIN app_users u ON u.id = tx.app_user_id").
|
||
Joins("LEFT JOIN sys_users su ON su.id = tx.operator_user_id")
|
||
|
||
if userID > 0 {
|
||
db = db.Where("tx.app_user_id = ?", userID)
|
||
}
|
||
if username != "" {
|
||
db = db.Where("u.username ILIKE ?", "%"+username+"%")
|
||
}
|
||
if assetType != "" {
|
||
db = db.Where("tx.asset_type = ?", assetType)
|
||
}
|
||
if rechargeType != "" {
|
||
db = db.Where("tx.recharge_type = ?", rechargeType)
|
||
}
|
||
if operatorUserID > 0 {
|
||
db = db.Where("tx.operator_user_id = ?", operatorUserID)
|
||
}
|
||
if keyword != "" {
|
||
db = db.Where("tx.reason ILIKE ?", "%"+keyword+"%")
|
||
}
|
||
if startAt != nil {
|
||
db = db.Where("tx.created_at >= ?", *startAt)
|
||
}
|
||
if endAt != nil {
|
||
db = db.Where("tx.created_at <= ?", *endAt)
|
||
}
|
||
|
||
var total int64
|
||
if err := db.Count(&total).Error; err != nil {
|
||
response.FailWithMessage(err.Error(), c)
|
||
return
|
||
}
|
||
|
||
type row struct {
|
||
appModel.AppAssetTransaction
|
||
Username string `gorm:"column:username"`
|
||
OperatorName string `gorm:"column:operator_name"`
|
||
}
|
||
var rows []row
|
||
if err := db.Select([]string{
|
||
"tx.id",
|
||
"tx.app_user_id",
|
||
"tx.asset_type",
|
||
"tx.recharge_type",
|
||
"tx.delta_li",
|
||
"tx.before_li",
|
||
"tx.after_li",
|
||
"tx.reason",
|
||
"tx.operator_user_id",
|
||
"tx.created_at",
|
||
"u.username AS username",
|
||
"su.username AS operator_name",
|
||
}).Order("tx.id DESC").Limit(pageSize).Offset((page - 1) * pageSize).Scan(&rows).Error; err != nil {
|
||
response.FailWithMessage(err.Error(), c)
|
||
return
|
||
}
|
||
|
||
items := make([]appAssetTransactionListItem, 0, len(rows))
|
||
for _, r := range rows {
|
||
items = append(items, appAssetTransactionListItem{
|
||
ID: r.ID,
|
||
AppUserID: r.AppUserID,
|
||
Username: r.Username,
|
||
AssetType: string(r.AssetType),
|
||
RechargeType: string(r.RechargeType),
|
||
DeltaLi: r.DeltaLi,
|
||
BeforeLi: r.BeforeLi,
|
||
AfterLi: r.AfterLi,
|
||
Reason: r.Reason,
|
||
OperatorUserID: r.OperatorUserID,
|
||
OperatorName: r.OperatorName,
|
||
CreatedAt: r.CreatedAt,
|
||
})
|
||
}
|
||
|
||
response.OkWithDetailed(response.PageResult{
|
||
List: items,
|
||
Total: total,
|
||
Page: page,
|
||
PageSize: pageSize,
|
||
}, "获取成功", c)
|
||
}
|
||
|
||
// GetAppAssetTransactionById
|
||
// @Tags AppAssetTransactionsAdmin
|
||
// @Summary 后台获取玩家资产流水详情
|
||
// @Security ApiKeyAuth
|
||
// @Produce application/json
|
||
// @Param id path int true "流水ID"
|
||
// @Success 200 {object} response.Response{data=appAssetTransactionListItem,msg=string} "获取成功"
|
||
// @Router /admin/app-asset-transactions/{id} [get]
|
||
func (a *AppAssetTransactionsApi) GetAppAssetTransactionById(c *gin.Context) {
|
||
id, _ := strconv.Atoi(c.Param("id"))
|
||
if id <= 0 {
|
||
response.FailWithMessage("参数错误", c)
|
||
return
|
||
}
|
||
|
||
type row struct {
|
||
appModel.AppAssetTransaction
|
||
Username string `gorm:"column:username"`
|
||
OperatorName string `gorm:"column:operator_name"`
|
||
}
|
||
var r row
|
||
err := global.GVA_DB.Table("app_asset_transactions tx").
|
||
Joins("JOIN app_users u ON u.id = tx.app_user_id").
|
||
Joins("LEFT JOIN sys_users su ON su.id = tx.operator_user_id").
|
||
Select([]string{
|
||
"tx.id",
|
||
"tx.app_user_id",
|
||
"tx.asset_type",
|
||
"tx.recharge_type",
|
||
"tx.delta_li",
|
||
"tx.before_li",
|
||
"tx.after_li",
|
||
"tx.reason",
|
||
"tx.operator_user_id",
|
||
"tx.created_at",
|
||
"u.username AS username",
|
||
"su.username AS operator_name",
|
||
}).Where("tx.id = ?", id).Scan(&r).Error
|
||
if err != nil {
|
||
response.FailWithMessage(err.Error(), c)
|
||
return
|
||
}
|
||
|
||
response.OkWithDetailed(appAssetTransactionListItem{
|
||
ID: r.ID,
|
||
AppUserID: r.AppUserID,
|
||
Username: r.Username,
|
||
AssetType: string(r.AssetType),
|
||
RechargeType: string(r.RechargeType),
|
||
DeltaLi: r.DeltaLi,
|
||
BeforeLi: r.BeforeLi,
|
||
AfterLi: r.AfterLi,
|
||
Reason: r.Reason,
|
||
OperatorUserID: r.OperatorUserID,
|
||
OperatorName: r.OperatorName,
|
||
CreatedAt: r.CreatedAt,
|
||
}, "获取成功", c)
|
||
}
|