From c3da1ebc51e27731cc841b4203811e8ab5437a0b Mon Sep 17 00:00:00 2001 From: henry Date: Wed, 29 Dec 2021 16:12:25 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9A=E5=AE=8C=E5=96=84=E9=A1=B9?= =?UTF-8?q?=E7=9B=AE=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/enterprise/api/user.go | 15 ++++++++ app/api/enterprise/controller/user/consume.go | 38 +++++++++++++++++++ .../enterprise/controller/user/withdrawal.go | 28 ++++++++++---- app/api/enterprise/model/user_consume.go | 28 +++++++++++++- app/api/enterprise/model/user_withdrawal.go | 22 +++++++---- app/common/model/user_consume.go | 6 ++- app/common/model/user_withdrawal.go | 2 +- router/address.go | 1 + 8 files changed, 121 insertions(+), 19 deletions(-) diff --git a/app/api/enterprise/api/user.go b/app/api/enterprise/api/user.go index 7007a33..93cd186 100644 --- a/app/api/enterprise/api/user.go +++ b/app/api/enterprise/api/user.go @@ -147,6 +147,21 @@ func (*User) Consume(c *gin.Context) { api.APIResponse(err, data)(c) } +func (*User) ConsumeExchange(c *gin.Context) { + form := &struct { + OrderNo string `json:"order_no" form:"order_no"` + CreatedAt string `json:"created_at" form:"created_at"` + api.PageForm + }{} + if err := api.Bind(form)(c); err != nil { + api.APIFailure(err.(error))(c) + return + } + data, err := user.NewConsume()(api.GetSession()(c).(*session.Enterprise), "").Exchange(form.OrderNo, + form.CreatedAt, form.Page, form.PageSize) + api.APIResponse(err, data)(c) +} + func (*User) ConsumeDelete(c *gin.Context) { form := new(api.IDStringForm) diff --git a/app/api/enterprise/controller/user/consume.go b/app/api/enterprise/controller/user/consume.go index d21852f..5fe7327 100644 --- a/app/api/enterprise/controller/user/consume.go +++ b/app/api/enterprise/controller/user/consume.go @@ -22,6 +22,11 @@ type ( ID string `json:"id"` *model2.UserConsume } + // ConsumePageInfo 消耗日志分页信息 + ConsumePageInfo struct { + controller.ReturnPages + TotalConsume float64 `json:"total_consume"` + } ) // Instance 列表信息 @@ -65,6 +70,39 @@ func (c *Consume) Instance(source int, createdAt string, page, pageSize int) (*c return &controller.ReturnPages{Data: list, Count: count}, nil } +// Exchange 兑换信息 +func (c *Consume) Exchange(orderNo string, createdAt string, page, pageSize int) (*ConsumePageInfo, error) { + mUserConsume := model.NewUserConsume() + + where := []*model2.ModelWhere{ + model2.NewWhere("uid", c.UID), + model2.NewWhereIn("source", []model2.UserConsumeSource{ + model2.UserConsumeSourceForExchangeCoin, + }), + } + if orderNo != "" { + where = append(where, model2.NewWhereLike("order_no", orderNo)) + } + if createdAt != "" { + where = append(where, model2.NewWhereSectionAt("created_at", strings.Split(createdAt, " ~ "))...) + } + out := new(ConsumePageInfo) + + res, err := mUserConsume.Exchange(page, pageSize, &out.Count, &out.TotalConsume) + + if err != nil { + return nil, err + } + list := make([]*ConsumeInfo, 0) + + for _, v := range res { + list = append(list, &ConsumeInfo{ID: v.GetEncodeID(), UserConsume: v}) + } + out.Data = list + + return out, nil +} + // Delete 删除操作 func (c *Consume) Delete(id uint64) error { mUserConsume := model.NewUserConsume() diff --git a/app/api/enterprise/controller/user/withdrawal.go b/app/api/enterprise/controller/user/withdrawal.go index 2c32d8e..5408e50 100644 --- a/app/api/enterprise/controller/user/withdrawal.go +++ b/app/api/enterprise/controller/user/withdrawal.go @@ -2,6 +2,7 @@ package user import ( "SciencesServer/app/api/enterprise/model" + "SciencesServer/app/basic/controller" model2 "SciencesServer/app/common/model" "SciencesServer/app/session" "SciencesServer/config" @@ -16,12 +17,16 @@ type Withdrawal struct { type WithdrawalHandle func(session *session.Enterprise) *Withdrawal type ( + // WithdrawalBasic 基本信息 + WithdrawalBasic struct { + ID string `json:"id"` + *model.UserWithdrawalInfo + } // WithdrawalInfo 提现数据信息 WithdrawalInfo struct { - Data []*model2.UserWithdrawal `json:"data"` - Amount float64 `json:"amount"` - ActualAmount float64 `json:"actual_amount"` - Count int64 `json:"count"` + controller.ReturnPages + TotalAmount float64 `json:"total_amount"` + TotalActualAmount float64 `json:"total_actual_amount"` } // WithdrawalTransferInfo 提现转账信息 WithdrawalTransferInfo struct { @@ -36,16 +41,23 @@ func (c *Withdrawal) Instance(status int, createdAt string, page, pageSize int) out := new(WithdrawalInfo) - where := []*model2.ModelWhere{model2.NewWhere("uid", c.UID), model2.NewWhere("status", status)} + where := []*model2.ModelWhere{model2.NewWhere("w.uid", c.UID), model2.NewWhere("w.status", status)} if createdAt != "" { - where = append(where, model2.NewWhereSectionAt("created_at", strings.Split(createdAt, " ~ "))...) + where = append(where, model2.NewWhereSectionAt("w.created_at", strings.Split(createdAt, " ~ "))...) } - var err error + ret, err := mUserWithdrawal.Withdrawal(page, pageSize, &out.Count, &out.TotalAmount, &out.TotalActualAmount, where...) - if out.Data, err = mUserWithdrawal.Withdrawal(page, pageSize, &out.Count, &out.Amount, &out.ActualAmount, where...); err != nil { + if err != nil { return nil, err } + list := make([]*WithdrawalBasic, 0) + + for _, v := range ret { + list = append(list, &WithdrawalBasic{ID: v.GetEncodeID(), UserWithdrawalInfo: v}) + } + out.Data = list + return out, nil } diff --git a/app/api/enterprise/model/user_consume.go b/app/api/enterprise/model/user_consume.go index dd5053a..73f57d1 100644 --- a/app/api/enterprise/model/user_consume.go +++ b/app/api/enterprise/model/user_consume.go @@ -1,11 +1,37 @@ package model -import "SciencesServer/app/common/model" +import ( + "SciencesServer/app/common/model" + "SciencesServer/serve/orm" +) type UserConsume struct { *model.UserConsume } +func (m *UserConsume) Exchange(page, pageSize int, count *int64, consume *float64, where ...*model.ModelWhere) ([]*model.UserConsume, error) { + db := orm.GetDB().Table(m.TableName()). + Where("is_deleted = ?", model.DeleteStatusForNot) + + if len(where) > 0 { + for _, v := range where { + db = db.Where(v.Condition, v.Value) + } + } + out := make([]*model.UserConsume, 0) + + if err := db.Count(count).Error; err != nil { + return nil, err + } + if err := db.Pluck("SUM(consume) AS consume", consume).Error; err != nil { + return nil, err + } + if err := db.Order("id " + model.OrderModeToDesc).Offset((page - 1) * pageSize).Limit(pageSize).Scan(&out).Error; err != nil { + return nil, err + } + return out, nil +} + func NewUserConsume() *UserConsume { return &UserConsume{model.NewUserConsume()} } diff --git a/app/api/enterprise/model/user_withdrawal.go b/app/api/enterprise/model/user_withdrawal.go index d65cd7d..e912953 100644 --- a/app/api/enterprise/model/user_withdrawal.go +++ b/app/api/enterprise/model/user_withdrawal.go @@ -3,6 +3,7 @@ package model import ( "SciencesServer/app/common/model" "SciencesServer/serve/orm" + "fmt" ) // UserWithdrawal 用户提现模型 @@ -10,28 +11,35 @@ type UserWithdrawal struct { *model.UserWithdrawal } +type UserWithdrawalInfo struct { + *model.UserWithdrawal + BankCard string `json:"bank_card"` +} + // Withdrawal 提现记录 -func (m *UserWithdrawal) Withdrawal(page, pageSize int, count *int64, amount, actualAmount *float64, where ...*model.ModelWhere) ([]*model.UserWithdrawal, error) { - db := orm.GetDB().Table(m.TableName()). - Where("is_deleted = ?", model.DeleteStatusForNot) +func (m *UserWithdrawal) Withdrawal(page, pageSize int, count *int64, amount, actualAmount *float64, where ...*model.ModelWhere) ([]*UserWithdrawalInfo, error) { + db := orm.GetDB().Table(m.TableName()+" AS w"). + Select("w.*", "b.bank_card"). + Joins(fmt.Sprintf("LEFT JOIN %s AS b ON w.bank_id = b.id", model.NewUserBank().TableName())). + Where("w.is_deleted = ?", model.DeleteStatusForNot) if len(where) > 0 { for _, v := range where { db = db.Where(v.Condition, v.Value) } } - out := make([]*model.UserWithdrawal, 0) + out := make([]*UserWithdrawalInfo, 0) if err := db.Count(count).Error; err != nil { return nil, err } - if err := db.Pluck("SUM(amount)", amount).Error; err != nil { + if err := db.Pluck("SUM(w.amount) AS amount", amount).Error; err != nil { return nil, err } - if err := db.Pluck("SUM(actual_amount)", actualAmount).Error; err != nil { + if err := db.Pluck("SUM(w.actual_amount) AS actual_amount", actualAmount).Error; err != nil { return nil, err } - if err := db.Order("id " + model.OrderModeToDesc).Offset((page - 1) * pageSize).Limit(pageSize).Scan(&out).Error; err != nil { + if err := db.Order("w.id " + model.OrderModeToDesc).Offset((page - 1) * pageSize).Limit(pageSize).Scan(&out).Error; err != nil { return nil, err } return out, nil diff --git a/app/common/model/user_consume.go b/app/common/model/user_consume.go index c726998..4b07da0 100644 --- a/app/common/model/user_consume.go +++ b/app/common/model/user_consume.go @@ -16,8 +16,10 @@ type UserConsume struct { type UserConsumeSource int const ( - // UserConsumeSourceForRecharge 充值 - UserConsumeSourceForRecharge UserConsumeSource = iota + 1 + // UserConsumeSourceForRechargeCoin 充值币 + UserConsumeSourceForRechargeCoin UserConsumeSource = iota + 1 + // UserConsumeSourceForExchangeCoin 兑换币 + UserConsumeSourceForExchangeCoin ) func (m *UserConsume) TableName() string { diff --git a/app/common/model/user_withdrawal.go b/app/common/model/user_withdrawal.go index 2200e88..4e9387b 100644 --- a/app/common/model/user_withdrawal.go +++ b/app/common/model/user_withdrawal.go @@ -5,7 +5,7 @@ type UserWithdrawal struct { Model UID uint64 `gorm:"column:uid;type:int;default:0;comment:用户uuid" json:"-"` OrderNo string `gorm:"column:order_no;type:varchar(30);default:'';comment:订单号" json:"order_no"` - BackCard string `gorm:"column:back_card;type:varchar(25);default:'';comment:银行卡号" json:"back_card"` + BackID uint64 `gorm:"column:back_id;type:int(11);default:0;comment:银行信息" json:"-"` Mobile string `gorm:"column:mobile;type:varchar(15);default:'';comment:联系方式" json:"mobile"` Amount float64 `gorm:"column:amount;type:decimal(10,2);default:0;comment:提现金额" json:"amount"` ActualAmount float64 `gorm:"column:actual_amount;type:decimal(10,2);default:0;comment:实际到账金额" json:"actual_amount"` diff --git a/router/address.go b/router/address.go index 372c7b1..af368e3 100644 --- a/router/address.go +++ b/router/address.go @@ -268,6 +268,7 @@ func registerEnterpriseAPI(app *gin.Engine) { userV1.POST("/bill/ticket", _api.BillTicket) userV1.POST("/bill/ticket/issue", _api.BillTicketIssue) userV1.POST("/consume", _api.Consume) + userV1.POST("/consume/exchange", _api.ConsumeExchange) userV1.POST("/consume/delete", _api.ConsumeDelete) userV1.POST("/withdrawal", _api.Withdrawal) userV1.POST("/withdrawal/transfer", _api.WithdrawalTransfer)