game_order.go 5.2 KB

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