pool.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. v1pb "go-common/app/service/live/xlottery/api/grpc/v1"
  8. "go-common/app/service/live/xlottery/model"
  9. "go-common/library/database/sql"
  10. "go-common/library/log"
  11. )
  12. const (
  13. _getPool = "SELECT id, coin_id, title, rule, start_time, end_time, status, is_bottom FROM capsule_pool"
  14. _getOnePool = "SELECT id, coin_id,title,rule,start_time,end_time,status, is_bottom FROM capsule_pool WHERE id = ?"
  15. _updatePStatus = "UPDATE capsule_pool SET status = ? WHERE id = ?"
  16. _delPool = "DELETE FROM capsule_pool WHERE id = ?"
  17. _createPool = "INSERT INTO capsule_pool (coin_id,title,rule,start_time,end_time,status) VALUES (?,?,?,?,?,0)"
  18. _updatePool = "UPDATE capsule_pool SET coin_id = ?, title = ?, rule = ?, start_time = ?, end_time = ? WHERE id = ?"
  19. _getPoolMap = "SELECT id, coin_id, title, rule, start_time, end_time, status, is_bottom FROM capsule_pool WHERE status = 1 and coin_id in (%v)"
  20. )
  21. //GetAllPool 获取奖池信息
  22. func (d *Dao) GetAllPool(ctx context.Context) (pools []*model.Pool, err error) {
  23. var rows *sql.Rows
  24. if rows, err = d.db.Query(ctx, _getPool); err != nil {
  25. log.Error("[dao.pool | GetAllPool] query(%s) error(%v)", _getPool, err)
  26. return
  27. }
  28. defer rows.Close()
  29. for rows.Next() {
  30. p := &model.Pool{}
  31. if err = rows.Scan(&p.Id, &p.CoinId, &p.Title, &p.Description, &p.StartTime, &p.EndTime, &p.Status, &p.IsBottom); err != nil {
  32. log.Error("[dao.pool | GetAllPool] scan error, err %v", err)
  33. return
  34. }
  35. pools = append(pools, p)
  36. }
  37. return
  38. }
  39. //GetPoolMap 批量奖池信息
  40. func (d *Dao) GetPoolMap(ctx context.Context, coinIds []int64) (poolMap map[int64][]*model.Pool, err error) {
  41. var rows *sql.Rows
  42. stringCoinIds := make([]string, 0)
  43. for _, coinId := range coinIds {
  44. stringCoinIds = append(stringCoinIds, strconv.FormatInt(coinId, 10))
  45. }
  46. coinString := strings.Join(stringCoinIds, ",")
  47. if rows, err = d.db.Query(ctx, fmt.Sprintf(_getPoolMap, coinString)); err != nil {
  48. log.Error("[dao.pool | GetPoolMap]query(%s) error(%v)", _getPoolMap, err)
  49. return
  50. }
  51. defer rows.Close()
  52. poolMap = make(map[int64][]*model.Pool)
  53. for rows.Next() {
  54. d := &model.Pool{}
  55. if err = rows.Scan(&d.Id, &d.CoinId, &d.Title, &d.Description, &d.StartTime, &d.EndTime, &d.Status, &d.IsBottom); err != nil {
  56. log.Error("[dao.pool |GetPoolMap] scan error, err %v", err)
  57. return
  58. }
  59. if _, ok := poolMap[d.CoinId]; !ok {
  60. poolMap[d.CoinId] = make([]*model.Pool, 0)
  61. }
  62. poolMap[d.CoinId] = append(poolMap[d.CoinId], d)
  63. }
  64. return
  65. }
  66. // GetPoolById 通过ID获取奖池信息
  67. func (d *Dao) GetPoolById(ctx context.Context, poolID int64) (pool *model.Pool, err error) {
  68. var rows *sql.Rows
  69. if rows, err = d.db.Query(ctx, _getOnePool, poolID); err != nil {
  70. log.Error("[dao.pool | GetPoolById]query(%s) error(%v)", _getOnePool, err)
  71. return
  72. }
  73. defer rows.Close()
  74. for rows.Next() {
  75. p := &model.Pool{}
  76. if err = rows.Scan(&p.Id, &p.CoinId, &p.Title, &p.Description, &p.StartTime, &p.EndTime, &p.Status, &p.IsBottom); err != nil {
  77. log.Error("[dao.pool | GetPoolById] scan error, err %v", err)
  78. return
  79. }
  80. pool = p
  81. }
  82. return
  83. }
  84. //UpdatePoolStatus 更新奖池状态
  85. func (d *Dao) UpdatePoolStatus(ctx context.Context, id int64, status int64) (bool, error) {
  86. //s := fmt.Sprintf(_updatePStatus, status, id)
  87. res, err := d.db.Exec(ctx, _updatePStatus, status, id)
  88. if err != nil {
  89. log.Error("[dao.pool | UpdatePoolStatus] query(%s) error(%v)", _updatePStatus, err)
  90. return false, err
  91. }
  92. var rows int64
  93. rows, err = res.RowsAffected()
  94. if err != nil {
  95. log.Error("[dao.pool | UpdatePoolStatus] no coin is affected, err %v", err)
  96. return false, err
  97. }
  98. return rows > 0, nil
  99. }
  100. //DelPool 删除奖池
  101. func (d *Dao) DelPool(ctx context.Context, id int64) (rows int64, err error) {
  102. res, err := d.db.Exec(ctx, _delPool, id)
  103. if err != nil {
  104. log.Error("[dao.pool | DelPool]query(%s) error(%v)", _delPool, err)
  105. return
  106. }
  107. rows, err = res.RowsAffected()
  108. if err != nil {
  109. log.Error("[dao.pool | DelPool]no pool is deleted, err %v", err)
  110. return
  111. }
  112. return
  113. }
  114. //CreatePool 新建奖池
  115. func (d *Dao) CreatePool(ctx context.Context, data *v1pb.UpdatePoolReq) (status bool, err error) {
  116. coinId := data.CoinId
  117. title := data.Title
  118. startTime := data.StartTime
  119. endTime := data.EndTime
  120. rule := data.Rule
  121. //s := fmt.Sprintf(_createPool,coinTitle,title,rule,startTime,endTime)
  122. res, err := d.db.Exec(ctx, _createPool, coinId, title, rule, startTime, endTime)
  123. if err != nil {
  124. log.Error("[dao.pool | CreatePool] query(%s) error(%v)", _createPool, err)
  125. return
  126. }
  127. var rows int64
  128. rows, err = res.RowsAffected()
  129. if err != nil {
  130. log.Error("[dao.pool | CreatePool] no pool is created, err %v", err)
  131. return
  132. }
  133. status = rows > 0
  134. return
  135. }
  136. //UpdatePool 更新奖池
  137. func (d *Dao) UpdatePool(ctx context.Context, data *v1pb.UpdatePoolReq) (status bool, err error) {
  138. id := data.Id
  139. coinId := data.CoinId
  140. title := data.Title
  141. startTime := data.StartTime
  142. endTime := data.EndTime
  143. rule := data.Rule
  144. res, err := d.db.Exec(ctx, _updatePool, coinId, title, rule, startTime, endTime, id)
  145. if err != nil {
  146. log.Error("[dao.pool | UpdatePool] query(%s) error(%v)", _updatePool, err)
  147. return
  148. }
  149. var rows int64
  150. rows, err = res.RowsAffected()
  151. if err != nil {
  152. log.Error("[dao.pool | UpdatePool] no pool is updated, err %v", err)
  153. return
  154. }
  155. status = rows > 0
  156. return
  157. }