pool_prize.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. _getPoolPrize = "SELECT id, pool_id, type, num, object_id,expire, web_url, mobile_url, description, jump_url, pro_type, chance, loop_num, limit_num, weight FROM capsule_pool_prize WHERE pool_id = ? and status = 1 order by ctime"
  14. _delPoolPrize = "UPDATE capsule_pool_prize set status = 0 WHERE id = ?"
  15. _addPoolPrize = "INSERT INTO capsule_pool_prize(pool_id,type,num,object_id,expire,web_url,mobile_url,description,jump_url,pro_type,chance,loop_num,limit_num,weight,status) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
  16. _updatePoolPrize = "UPDATE capsule_pool_prize SET pool_id=?, type=?, num=?, object_id=?,expire=?, web_url=?, mobile_url=?, description=?, jump_url=?, pro_type=?, chance=?, loop_num=?, limit_num=?,weight=? WHERE id = ?"
  17. _getPoolPrizeMap = "SELECT id, pool_id, type, num, object_id,expire, web_url, mobile_url, description, jump_url, pro_type, chance, loop_num, limit_num, weight FROM capsule_pool_prize WHERE pool_id in (%s) and status = 1 order by ctime"
  18. )
  19. // GetPoolPrize 获取奖池奖品
  20. func (d *Dao) GetPoolPrize(ctx context.Context, poolID int64) (poolPrize []*model.PoolPrize, err error) {
  21. var rows *sql.Rows
  22. if rows, err = d.db.Query(ctx, _getPoolPrize, poolID); err != nil {
  23. log.Error("[dao.pool_prize | GetPoolPrize]query(%s) error(%v)", _getPoolPrize, err)
  24. return
  25. }
  26. defer rows.Close()
  27. poolPrize = make([]*model.PoolPrize, 0)
  28. for rows.Next() {
  29. p := &model.PoolPrize{}
  30. if err = rows.Scan(&p.Id, &p.PoolId, &p.Type, &p.Num, &p.ObjectId, &p.Expire, &p.WebUrl, &p.MobileUrl, &p.Description, &p.JumpUrl, &p.ProType, &p.Chance, &p.LoopNum, &p.LimitNum, &p.Weight); err != nil {
  31. log.Error("[dao.pool_prize | GetPoolPrize] scan error, err %v", err)
  32. return
  33. }
  34. poolPrize = append(poolPrize, p)
  35. }
  36. return
  37. }
  38. // GetPoolPrizeMap 批量奖池奖品
  39. func (d *Dao) GetPoolPrizeMap(ctx context.Context, poolIds []int64) (poolPrizeMap map[int64][]*model.PoolPrize, err error) {
  40. var rows *sql.Rows
  41. stringPoolIds := make([]string, 0)
  42. for _, poolId := range poolIds {
  43. stringPoolIds = append(stringPoolIds, strconv.FormatInt(poolId, 10))
  44. }
  45. poolString := strings.Join(stringPoolIds, ",")
  46. if rows, err = d.db.Query(ctx, fmt.Sprintf(_getPoolPrizeMap, poolString)); err != nil {
  47. log.Error("[dao.pool_prize | GetPoolPrizeMap] query(%s) error(%v)", _getPoolPrizeMap, err)
  48. return
  49. }
  50. defer rows.Close()
  51. poolPrizeMap = make(map[int64][]*model.PoolPrize)
  52. for rows.Next() {
  53. d := &model.PoolPrize{}
  54. if err = rows.Scan(&d.Id, &d.PoolId, &d.Type, &d.Num, &d.ObjectId, &d.Expire, &d.WebUrl, &d.MobileUrl, &d.Description, &d.JumpUrl, &d.ProType, &d.Chance, &d.LoopNum, &d.LimitNum, &d.Weight); err != nil {
  55. log.Error("[dao.pool_prize | GetPoolPrizeMap] scan error, err %v", err)
  56. return
  57. }
  58. if _, ok := PrizeNameMap[d.Type]; !ok {
  59. continue
  60. }
  61. if _, ok := poolPrizeMap[d.PoolId]; !ok {
  62. poolPrizeMap[d.PoolId] = make([]*model.PoolPrize, 0)
  63. }
  64. poolPrizeMap[d.PoolId] = append(poolPrizeMap[d.PoolId], d)
  65. }
  66. return
  67. }
  68. // DeletePoolPrize 删除奖池奖品
  69. func (d *Dao) DeletePoolPrize(ctx context.Context, id int64) (status bool, err error) {
  70. res, err := d.db.Exec(ctx, _delPoolPrize, id)
  71. if err != nil {
  72. log.Error("[dao.pool_prize | DelPoolDetail] query(%s) error (%v)", _delPoolPrize, err)
  73. return
  74. }
  75. var rows int64
  76. rows, err = res.RowsAffected()
  77. if err != nil {
  78. log.Error("[dao.pool_prize | DelPoolDetail] no poolDetail is deleted, err %v", err)
  79. return
  80. }
  81. status = rows > 0
  82. return
  83. }
  84. // AddPoolPrize 创建奖池奖品
  85. func (d *Dao) AddPoolPrize(ctx context.Context, data *v1pb.UpdatePoolPrizeReq) (prizeId int64, err error) {
  86. res, err := d.db.Exec(ctx, _addPoolPrize, data.PoolId, data.Type, data.Num, data.ObjectId, data.Expire, data.WebUrl, data.MobileUrl, data.Description, data.JumpUrl, data.ProType, data.Chance, data.Loop, data.Limit, data.Weight, 1)
  87. if err != nil {
  88. log.Error("[dao.pool_prize | CreatePoolPrize] query(%s) error (%v)", _addPoolPrize, err)
  89. return
  90. }
  91. prizeId, err = res.LastInsertId()
  92. if err != nil {
  93. log.Error("[dao.pool_prize | CreatePoolPrize] insertLastId error(%v)", err)
  94. return
  95. }
  96. return
  97. }
  98. // UpdatePoolPrize 更新奖池奖品
  99. func (d *Dao) UpdatePoolPrize(ctx context.Context, data *v1pb.UpdatePoolPrizeReq) (status bool, err error) {
  100. _, err = d.db.Exec(ctx, _updatePoolPrize, data.PoolId, data.Type, data.Num, data.ObjectId, data.Expire, data.WebUrl, data.MobileUrl, data.Description, data.JumpUrl, data.ProType, data.Chance, data.Loop, data.Limit, data.Weight, data.Id)
  101. if err != nil {
  102. log.Error("[dao.pool_prize | UpdatePoolPrize] query(%s) error(%v)", _updatePoolPrize, err)
  103. return
  104. }
  105. return true, nil
  106. }