1
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
@@ -20,6 +21,23 @@ type GoldListService struct {
|
||||
BaseService
|
||||
}
|
||||
|
||||
func (s *GoldListService) validateOrderInput(exchangeNum uint32, rate float64) error {
|
||||
if exchangeNum == 0 {
|
||||
return gerror.New("出售数量必须大于0")
|
||||
}
|
||||
if rate <= 0 {
|
||||
return gerror.New("汇率必须大于0")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *GoldListService) validateOrderRecord(order *model.GoldBeanOrder) error {
|
||||
if order == nil || order.ID == 0 {
|
||||
return gerror.New("重复订单")
|
||||
}
|
||||
return s.validateOrderInput(order.ExchangeNum, order.Rate)
|
||||
}
|
||||
|
||||
func (s *GoldListService) getPrevDayAvgRate() float64 {
|
||||
var (
|
||||
now = gtime.Now()
|
||||
@@ -49,7 +67,16 @@ func (s *GoldListService) ModifyBefore(ctx context.Context, method string, param
|
||||
userId := admin.UserId
|
||||
|
||||
if method == "Add" {
|
||||
if service.NewBaseSysUserService().GetGold(userId) < gconv.Int64(param["exchange_num"])*100 {
|
||||
var (
|
||||
exchangeNum = gconv.Uint32(param["exchange_num"])
|
||||
rate = gconv.Float64(param["rate"])
|
||||
)
|
||||
|
||||
if err = s.validateOrderInput(exchangeNum, rate); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if service.NewBaseSysUserService().GetGold(userId) < int64(exchangeNum)*100 {
|
||||
return fmt.Errorf("金额不足")
|
||||
}
|
||||
|
||||
@@ -59,7 +86,6 @@ func (s *GoldListService) ModifyBefore(ctx context.Context, method string, param
|
||||
}
|
||||
|
||||
var (
|
||||
rate = gconv.Float64(param["rate"])
|
||||
prevAvg = s.getPrevDayAvgRate()
|
||||
minRate = math.Round(prevAvg*0.9*10000) / 10000
|
||||
maxRate = math.Round(prevAvg*1.1*10000) / 10000
|
||||
@@ -90,6 +116,9 @@ func (s *GoldListService) ModifyBefore(ctx context.Context, method string, param
|
||||
func (s *GoldListService) Done(listid uint32) (*model.GoldBeanOrder, error) {
|
||||
var rr model.GoldBeanOrder
|
||||
s.dbm_fix(s.Model).Where("id", listid).Scan(&rr)
|
||||
if err := s.validateOrderRecord(&rr); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r, err := s.dbm_fix(s.Model).Data("status", 1).Where("status", 0).Where("id", listid).Update()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -114,8 +143,8 @@ func (s *GoldListService) Trade(ctx context.Context, listid uint32, buyerID uint
|
||||
if err := tx.Model(s.Model).Where("id", listid).Where("status", 0).Scan(&order); err != nil {
|
||||
return err
|
||||
}
|
||||
if order.ID == 0 {
|
||||
return fmt.Errorf("重复订单")
|
||||
if err := s.validateOrderRecord(&order); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !order.UpdateTime.Add(1 * time.Hour).Before(gtime.Now()) {
|
||||
@@ -196,7 +225,12 @@ func (s *GoldListService) Trade(ctx context.Context, listid uint32, buyerID uint
|
||||
}
|
||||
func (s *GoldListService) Get() []model.GoldBeanOrder {
|
||||
var rr []model.GoldBeanOrder
|
||||
s.dbm_fix(s.Model).Where("status", 0).Order("rate", "asc").Scan(&rr)
|
||||
s.dbm_fix(s.Model).
|
||||
Where("status", 0).
|
||||
WhereGT("exchange_num", 0).
|
||||
WhereGT("rate", 0).
|
||||
Order("rate", "asc").
|
||||
Scan(&rr)
|
||||
|
||||
return rr
|
||||
}
|
||||
@@ -214,6 +248,9 @@ func NewGoldListService(id uint32) *GoldListService {
|
||||
},
|
||||
ListQueryOp: &cool.QueryOp{
|
||||
|
||||
Extend: func(ctx g.Ctx, m *gdb.Model) *gdb.Model {
|
||||
return m.WhereGT("exchange_num", 0).WhereGT("rate", 0)
|
||||
},
|
||||
AddOrderby: g.MapStrStr{"updateTime": "asc"},
|
||||
},
|
||||
InsertParam: func(ctx context.Context) g.MapStrAny {
|
||||
|
||||
Reference in New Issue
Block a user