coin.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package dao
  2. import (
  3. "context"
  4. v1pb "go-common/app/service/live/xlottery/api/grpc/v1"
  5. "go-common/app/service/live/xlottery/model"
  6. "go-common/library/database/sql"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _getAllCoin = "SELECT id, title, gift_type, change_num, start_time, end_time, status FROM capsule_coin"
  11. _updateStatus = "UPDATE capsule_coin SET status = ? WHERE id = ?"
  12. _delCoin = "DELETE FROM capsule_coin WHERE id = ?"
  13. _createCoin = "INSERT INTO capsule_coin (title, gift_type, change_num, start_time, end_time, status) VALUES (?,?,?,?,?,?)"
  14. _updateCoin = "UPDATE capsule_coin SET title = ?, gift_type = ?, change_num = ?, start_time = ?, end_time = ?, status = ? WHERE id = ?"
  15. _getOnCoin = "SELECT id, title, gift_type, change_num, start_time, end_time, status FROM capsule_coin WHERE status = 1"
  16. )
  17. //GetAllCoin 获取所有扭蛋币
  18. func (d *Dao) GetAllCoin(ctx context.Context) (coins []*model.Coin, err error) {
  19. var rows *sql.Rows
  20. if rows, err = d.db.Query(ctx, _getAllCoin); err != nil {
  21. log.Error("[dao.coin | GetALLCoin] query(%s) error (%v)", _getAllCoin, err)
  22. return
  23. }
  24. defer rows.Close()
  25. for rows.Next() {
  26. p := &model.Coin{}
  27. if err = rows.Scan(&p.Id, &p.Title, &p.GiftType, &p.ChangeNum, &p.StartTime, &p.EndTime, &p.Status); err != nil {
  28. log.Error("[dao.coin | GetAllCoin] scan error, err %v", err)
  29. return
  30. }
  31. coins = append(coins, p)
  32. }
  33. return
  34. }
  35. //GetCoinMap 批量获取扭蛋币
  36. func (d *Dao) GetCoinMap(ctx context.Context) (coinMap map[int64]*model.Coin, err error) {
  37. var rows *sql.Rows
  38. if rows, err = d.db.Query(ctx, _getOnCoin); err != nil {
  39. log.Error("[dao.coin | GetCoinMap] query(%s) error(%v)", _getOnCoin, err)
  40. return
  41. }
  42. defer rows.Close()
  43. coinMap = make(map[int64]*model.Coin)
  44. for rows.Next() {
  45. p := &model.Coin{}
  46. if err = rows.Scan(&p.Id, &p.Title, &p.GiftType, &p.ChangeNum, &p.StartTime, &p.EndTime, &p.Status); err != nil {
  47. log.Error("[dao.coin | GetCoinMap] scan error, err %v", err)
  48. return
  49. }
  50. coinMap[p.Id] = p
  51. }
  52. return
  53. }
  54. //UpdateCoinStatus 更新扭蛋币状态
  55. func (d *Dao) UpdateCoinStatus(ctx context.Context, id int64, status int64) (bool, error) {
  56. res, err := d.db.Exec(ctx, _updateStatus, status, id)
  57. if err != nil {
  58. log.Error("[dao.coin | UpdateCStatus] query(%s) error(%v)", _updateStatus, err)
  59. return false, err
  60. }
  61. var rows int64
  62. rows, err = res.RowsAffected()
  63. if err != nil {
  64. log.Error("[dao.coin | UpdateCStatus] no coin is affected, err %v", err)
  65. return false, err
  66. }
  67. return rows > 0, nil
  68. }
  69. //DeleteCoin 删除扭蛋币
  70. func (d *Dao) DeleteCoin(ctx context.Context, id int64) (status bool, err error) {
  71. res, err := d.db.Exec(ctx, _delCoin, id)
  72. if err != nil {
  73. log.Error("[dao.coin | DelCoin] query(%s), error(%v)", _delCoin, err)
  74. return
  75. }
  76. var rows int64
  77. rows, err = res.RowsAffected()
  78. if err != nil {
  79. log.Error("[dao.coin | DelCoin] no coin is deleted, err %v", err)
  80. return
  81. }
  82. status = rows > 0
  83. return
  84. }
  85. // CreateCoin 更新扭蛋币
  86. func (d *Dao) CreateCoin(ctx context.Context, data *v1pb.UpdateCoinConfigReq) (ret int64, err error) {
  87. title := data.Title
  88. giftType := data.GiftType
  89. changeNum := data.ChangeNum
  90. startTime := data.StartTime
  91. endTime := data.EndTime
  92. status := 1
  93. res, err := d.db.Exec(ctx, _createCoin, title, giftType, changeNum, startTime, endTime, status)
  94. if err != nil {
  95. log.Error("[dao.coin | CreateCoin] query(%s) error (%v)", _createCoin, err)
  96. return
  97. }
  98. ret, err = res.RowsAffected()
  99. if err != nil {
  100. log.Error("[dao.coin | CreateCoin]no coin is added, err %v", err)
  101. return
  102. }
  103. return
  104. }
  105. // UpdateCoin 更新扭蛋币
  106. func (d *Dao) UpdateCoin(ctx context.Context, data *v1pb.UpdateCoinConfigReq) (status bool, err error) {
  107. id := data.Id
  108. title := data.Title
  109. giftType := data.GiftType
  110. changeNum := data.ChangeNum
  111. startTime := data.StartTime
  112. endTime := data.EndTime
  113. _, err = d.db.Exec(ctx, _updateCoin, title, giftType, changeNum, startTime, endTime, 1, id)
  114. if err != nil {
  115. log.Error("[dao.coin | UpdateCoin] query(%s) error %v", _updateCoin, err)
  116. return
  117. }
  118. return true, nil
  119. }