game_order.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package model
  2. import (
  3. "time"
  4. "github.com/shopspring/decimal"
  5. "github.com/sirupsen/logrus"
  6. "github.com/spf13/viper"
  7. "gogs.daxia.dev/huanan/pkg.daxia.dev/db"
  8. "gorm.io/gorm"
  9. )
  10. type GameOrder struct {
  11. ID uint `gorm:"primarykey"`
  12. CreatedAt time.Time
  13. UpdatedAt time.Time
  14. UserID uint `gorm:"int; comment:用户ID"`
  15. RoomID uint `gorm:"int; comment:房间ID"`
  16. RoomType int `gorm:"int; comment:房间类型"`
  17. Issue string `gorm:"varchar(40); comment:期号"`
  18. OpenNumber string `gorm:"varchar(40); comment:开奖号码"`
  19. BaseAmount decimal.Decimal `gorm:"comment:底注金额"`
  20. IsDraw int `gorm:"comment:是否开奖, 1: 开奖, 0: 未开奖"`
  21. DrawTime *time.Time `gorm:"comment:开奖时间"`
  22. IsMaster int `gorm:"comment:抢庄牛牛才有,是否为庄, 1: 庄, 0: 闲"`
  23. MasterMul int `gorm:"default:0; comment:抢庄倍数"`
  24. Mul int `gorm:"default:1; comment:下注倍数"`
  25. IsWin int `gorm:"comment:1: 赢, 2: 输, 3: 退回, 0: 未计算"`
  26. WinAmount decimal.Decimal `gorm:"comment:奖金"`
  27. BetIP string `gorm:"comment:投注IP"`
  28. GameID int `gorm:"int; comment:游戏ID"`
  29. BeforeAmount decimal.Decimal `gorm:"comment:投注前金额"`
  30. AfterAmount decimal.Decimal `gorm:"comment:投注后金额"`
  31. }
  32. func (GameOrder) TableName() string {
  33. return viper.GetString("table_prefix") + "game_nn"
  34. }
  35. func (p GameOrder) Init() error {
  36. if db.GetDB().Migrator().HasTable(p) {
  37. return nil
  38. }
  39. if err := db.GetDB().Migrator().CreateTable(p); err != nil {
  40. return err
  41. }
  42. return nil
  43. }
  44. func (p GameOrder) GetOrderListByIssue(issue string) ([]GameOrder, error) {
  45. gameID := viper.GetInt32("game_id")
  46. resultList := make([]GameOrder, 0)
  47. err := db.GetDB().Model(p).Where("issue = ? and game_id = ?", issue, gameID).Scan(&resultList).Error
  48. if err != nil {
  49. logrus.Error(err)
  50. return nil, err
  51. }
  52. return resultList, nil
  53. }
  54. func (p GameOrder) GetUnDrawOrderByIssue(issue string) ([]GameOrder, error) {
  55. gameID := viper.GetInt32("game_id")
  56. gameOrderModelList := make([]GameOrder, 0)
  57. err := db.GetDB().Model(p).Where("is_draw = ? and issue = ? and game_id = ?", 0, issue, gameID).Scan(&gameOrderModelList).Error
  58. if err != nil {
  59. logrus.Error(err)
  60. return nil, err
  61. }
  62. return gameOrderModelList, nil
  63. }
  64. func (p GameOrder) GetUserOrderListOpenedByIssue(issue string) ([]GameOrder, error) {
  65. gameID := viper.GetInt32("game_id")
  66. gameOrderModelList := make([]GameOrder, 0)
  67. err := db.GetDB().Model(p).Where("is_draw = 1 and issue = ? and game_id = ?", issue, gameID).
  68. Scan(&gameOrderModelList).Error
  69. if err != nil {
  70. logrus.Error(err)
  71. return nil, err
  72. }
  73. return gameOrderModelList, nil
  74. }
  75. func (p GameOrder) GetUserBetAmountByNumber(tx *gorm.DB, userID uint32, issue string, playType, pos, number string) (string, error) {
  76. var err error
  77. gameID := viper.GetInt32("game_id")
  78. type Info struct {
  79. UserID int
  80. Total decimal.Decimal
  81. }
  82. infoModel := Info{}
  83. err = tx.Model(p).Where("issue = ? and user_id = ? and play_type = ? and pos = ? and number = ? and game_id = ?",
  84. issue, userID, playType, pos, number, gameID).
  85. Select("user_id, sum(amount) as total").
  86. First(&infoModel).
  87. Error
  88. if err != nil {
  89. logrus.Error(err)
  90. return "", nil
  91. }
  92. return infoModel.Total.StringFixed(2), nil
  93. }
  94. func (p GameOrder) GetUserBetCount(userID uint32, issue string, playType, pos string) (int, error) {
  95. var err error
  96. gameID := viper.GetInt32("game_id")
  97. type Info struct {
  98. Total int
  99. }
  100. infoModel := Info{}
  101. err = db.GetDB().Model(p).Where("issue = ? and user_id = ? and play_type = ? and pos = ? and game_id = ?",
  102. issue, userID, playType, pos, gameID).
  103. Select("count(*) as total").
  104. First(&infoModel).
  105. Error
  106. if err != nil {
  107. logrus.Error(err)
  108. return 0, nil
  109. }
  110. return infoModel.Total, nil
  111. }
  112. func (p GameOrder) GetUserBetCountByTx(tx *gorm.DB, userID uint32, issue string, playType, pos string) (int, error) {
  113. var err error
  114. gameID := viper.GetInt32("game_id")
  115. type Info struct {
  116. Total int
  117. }
  118. infoModel := Info{}
  119. err = tx.Model(p).Where("issue = ? and user_id = ? and play_type = ? and pos = ? and game_id = ?",
  120. issue, userID, playType, pos, gameID).
  121. Select("count(*) as total").
  122. First(&infoModel).
  123. Error
  124. if err != nil {
  125. logrus.Error(err)
  126. return 0, nil
  127. }
  128. return infoModel.Total, nil
  129. }
  130. //投注了多个号码
  131. func (p GameOrder) GetUserBetTypeCountByTx(tx *gorm.DB, userID uint32, issue string, playType string) (int, error) {
  132. var err error
  133. gameID := viper.GetInt32("game_id")
  134. type Info struct {
  135. Total int
  136. }
  137. infoModel := Info{}
  138. err = tx.Model(p).Where("issue = ? and user_id = ? and play_type = ? and game_id = ?",
  139. issue, userID, playType, gameID).
  140. Select("count(DISTINCT number) as total").
  141. First(&infoModel).
  142. Error
  143. if err != nil {
  144. logrus.Error(err)
  145. return 0, nil
  146. }
  147. return infoModel.Total, nil
  148. }
  149. func (p GameOrder) GetListByIssue(issueStr string, userID int32) ([]GameOrder, error) {
  150. gameID := viper.GetInt32("game_id")
  151. gameOrderModelList := make([]GameOrder, 0)
  152. err := db.GetDB().Model(p).Where("issue = ? and user_id = ? and game_id = ?", issueStr, userID, gameID).
  153. Scan(&gameOrderModelList).Error
  154. return gameOrderModelList, err
  155. }
  156. func (p GameOrder) SetNewOdd(id uint, newOdd float64) error {
  157. err := db.GetDB().Model(p).Where("id = ?", id).Update("odds", newOdd).Error
  158. if err != nil {
  159. logrus.Error(err)
  160. return err
  161. }
  162. return err
  163. }