prize_extra.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "go-common/library/database/sql"
  8. "go-common/library/log"
  9. )
  10. const (
  11. _getWhiteList = "select prize_id, item_id from capsule_prize_extra WHERE prize_id in (%v) and type = ? and status = 1 order by id"
  12. _updateWhiteList = "UPDATE capsule_prize_extra SET status = ? WHERE prize_id = ?"
  13. _addWhiteUser = "INSERT INTO capsule_prize_extra (prize_id,type,item_id,status) VALUES "
  14. _whiteType = 1
  15. )
  16. // GetWhiteUserMap 获取白名单用户
  17. func (d *Dao) GetWhiteUserMap(ctx context.Context, prizeIds []int64) (userMap map[int64][]int64, err error) {
  18. var rows *sql.Rows
  19. stringPrizeIds := make([]string, len(prizeIds))
  20. for ix, prizeId := range prizeIds {
  21. stringPrizeIds[ix] = strconv.FormatInt(prizeId, 10)
  22. }
  23. sqlStr := fmt.Sprintf(_getWhiteList, strings.Join(stringPrizeIds, ","))
  24. if rows, err = d.db.Query(ctx, sqlStr, _whiteType); err != nil {
  25. log.Error("[dao.pool_prize | GetPoolPrize]query(%s) error(%v)", _getWhiteList, err)
  26. return
  27. }
  28. defer rows.Close()
  29. userMap = make(map[int64][]int64)
  30. for rows.Next() {
  31. var uid, prizeId int64
  32. if err = rows.Scan(&prizeId, &uid); err != nil {
  33. log.Error("[dao.pool_prize | GetWhiteUser] scan error, err %v", err)
  34. return
  35. }
  36. if _, ok := userMap[prizeId]; !ok {
  37. userMap[prizeId] = make([]int64, 0)
  38. }
  39. userMap[prizeId] = append(userMap[prizeId], uid)
  40. }
  41. return
  42. }
  43. // AddWhiteUser 添加白名单用户
  44. func (d *Dao) AddWhiteUser(ctx context.Context, prizeId int64, uids []int64) (status bool, err error) {
  45. _, err = d.db.Exec(ctx, _updateWhiteList, 0, prizeId)
  46. if err != nil {
  47. log.Error("[dao.pool_prize | AddWhiteUids] update(%s) error(%v)", _updateWhiteList, err)
  48. return
  49. }
  50. rowFields := make([][4]interface{}, 0)
  51. for _, uid := range uids {
  52. rowFields = append(rowFields, [4]interface{}{prizeId, _whiteType, uid, 1})
  53. }
  54. sqlStr := _addWhiteUser + strings.Repeat("(?,?,?,?),", len(rowFields))
  55. slen := len(sqlStr) - 1
  56. sqlStr = sqlStr[0:slen]
  57. values := make([]interface{}, len(rowFields)*4)
  58. for i, field := range rowFields {
  59. for j, v := range field {
  60. ix := i*4 + j
  61. values[ix] = v
  62. }
  63. }
  64. res, err := d.db.Exec(ctx, sqlStr, values...)
  65. if err != nil {
  66. log.Error("[dao.prize_extra | AddWhiteUser] add(%s) error (%v)", sqlStr, err)
  67. return
  68. }
  69. rows, _ := res.RowsAffected()
  70. status = rows > 0
  71. return
  72. }