capsule_extra.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/service/live/xlottery/model"
  6. "strings"
  7. "go-common/library/database/sql"
  8. "go-common/library/log"
  9. )
  10. const (
  11. _getExtraDataByTypeMysql = "select type, item_value, item_extra, ctime, mtime from capsule_extra_data where uid = ? and type in (%v)"
  12. _addExtraDataMysql = "insert into capsule_extra_data(uid,type,item_value,item_extra) values "
  13. _updateExtraValueMysql = "update capsule_extra_data set item_value = ? where uid = ? and type = ?"
  14. )
  15. // GetExtraDataByType 添加数据
  16. func (d *Dao) GetExtraDataByType(ctx context.Context, uid int64, types []string) (extraData []*model.ExtraData, err error) {
  17. var rows *sql.Rows
  18. var typeString []string
  19. for _, etype := range types {
  20. typeString = append(typeString, "'"+etype+"'")
  21. }
  22. sqlStr := fmt.Sprintf(_getExtraDataByTypeMysql, strings.Join(typeString, ","))
  23. if rows, err = d.db.Query(ctx, sqlStr, uid); err != nil {
  24. log.Error("[dao.extra | GetExtraDataByType] query(%s) error (%v)", sqlStr, err)
  25. return
  26. }
  27. defer rows.Close()
  28. extraData = make([]*model.ExtraData, 0)
  29. for rows.Next() {
  30. p := &model.ExtraData{}
  31. if err = rows.Scan(&p.Type, &p.ItemValue, &p.ItemExtra, &p.Ctime, &p.Mtime); err != nil {
  32. log.Error("[dao.extra | GetExtraDataByType] scan error, err %v", err)
  33. return
  34. }
  35. extraData = append(extraData, p)
  36. }
  37. return
  38. }
  39. // AddExtraData 添加数据
  40. func (d *Dao) AddExtraData(ctx context.Context, extraData []*model.ExtraData) (status bool, err error) {
  41. sqlStr := _addExtraDataMysql + strings.Repeat("(?,?,?,?),", len(extraData))
  42. slen := len(sqlStr) - 1
  43. sqlStr = sqlStr[0:slen]
  44. values := make([]interface{}, len(extraData)*4)
  45. for i, extra := range extraData {
  46. values[i*4] = extra.Uid
  47. values[i*4+1] = extra.Type
  48. values[i*4+2] = extra.ItemValue
  49. values[i*4+3] = extra.ItemExtra
  50. }
  51. res, err := d.db.Exec(ctx, sqlStr, values...)
  52. if err != nil {
  53. log.Error("[dao.extra | AddExtraData] add(%s) error (%v)", sqlStr, err)
  54. return
  55. }
  56. rows, _ := res.RowsAffected()
  57. status = rows > 0
  58. return
  59. }
  60. // UpdateExtraValue 更新数据
  61. func (d *Dao) UpdateExtraValue(ctx context.Context, uid int64, etype string, itemValue int64) (status bool, err error) {
  62. res, err := d.db.Exec(ctx, _updateExtraValueMysql, itemValue, uid, etype)
  63. if err != nil {
  64. log.Error("[dao.extra | UpdateExtraValue] update(%s) error(%v)", _updateExtraValueMysql, err)
  65. return false, err
  66. }
  67. var rows int64
  68. rows, err = res.RowsAffected()
  69. if err != nil {
  70. log.Error("[dao.extra | UpdateExtraValue] err %v", err)
  71. return false, err
  72. }
  73. return rows > 0, nil
  74. }