point.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package dao
  2. import (
  3. "context"
  4. xsql "database/sql"
  5. "go-common/app/admin/main/point/model"
  6. "go-common/library/database/sql"
  7. "github.com/pkg/errors"
  8. )
  9. const (
  10. _allPointConf = "SELECT id,app_id,point,operator,change_type,ctime,mtime FROM point_conf"
  11. _getPointConf = "SELECT id,app_id,point,operator,change_type,ctime,mtime FROM point_conf WHERE id=?"
  12. _addPointConf = "INSERT INTO point_conf (app_id,point,operator,change_type) VALUES (?,?,?,?)"
  13. _updatePointConf = "UPDATE point_conf SET point=?,operator=?,change_type=? where ID=?"
  14. _allAppInfo = "SELECT `id`,`name`,`app_key`,`purge_url` FROM `point_app_info`;"
  15. )
  16. // PointConfList .
  17. func (d *Dao) PointConfList(c context.Context) (res []*model.PointConf, err error) {
  18. var rows *sql.Rows
  19. if rows, err = d.db.Query(c, _allPointConf); err != nil {
  20. err = errors.WithStack(err)
  21. return
  22. }
  23. defer rows.Close()
  24. for rows.Next() {
  25. r := &model.PointConf{}
  26. if err = rows.Scan(&r.ID, &r.AppID, &r.Point, &r.Operator, &r.ChangeType, &r.Ctime, &r.Mtime); err != nil {
  27. err = errors.WithStack(err)
  28. res = nil
  29. return
  30. }
  31. res = append(res, r)
  32. }
  33. return
  34. }
  35. // PointCoinInfo .
  36. func (d *Dao) PointCoinInfo(c context.Context, id int64) (r *model.PointConf, err error) {
  37. row := d.db.QueryRow(c, _getPointConf, id)
  38. r = new(model.PointConf)
  39. if err = row.Scan(&r.ID, &r.AppID, &r.Point, &r.Operator, &r.ChangeType, &r.Ctime, &r.Mtime); err != nil {
  40. if err == sql.ErrNoRows {
  41. err = nil
  42. r = nil
  43. } else {
  44. err = errors.WithStack(err)
  45. }
  46. }
  47. return
  48. }
  49. // PointCoinAdd .
  50. func (d *Dao) PointCoinAdd(c context.Context, pc *model.PointConf) (id int64, err error) {
  51. var res xsql.Result
  52. if res, err = d.db.Exec(c, _addPointConf, pc.AppID, pc.Point, pc.Operator, pc.ChangeType); err != nil {
  53. err = errors.WithStack(err)
  54. return
  55. }
  56. if id, err = res.LastInsertId(); err != nil {
  57. err = errors.WithStack(err)
  58. }
  59. return
  60. }
  61. // PointCoinEdit .
  62. func (d *Dao) PointCoinEdit(c context.Context, mp *model.PointConf) (eff int64, err error) {
  63. var res xsql.Result
  64. if res, err = d.db.Exec(c, _updatePointConf, mp.Point, mp.Operator, mp.ChangeType, mp.ID); err != nil {
  65. err = errors.WithStack(err)
  66. return
  67. }
  68. if eff, err = res.RowsAffected(); err != nil {
  69. err = errors.WithStack(err)
  70. }
  71. return
  72. }
  73. // AllAppInfo all appinfo.
  74. func (d *Dao) AllAppInfo(c context.Context) (res []*model.AppInfo, err error) {
  75. var rows *sql.Rows
  76. if rows, err = d.db.Query(c, _allAppInfo); err != nil {
  77. err = errors.WithStack(err)
  78. return
  79. }
  80. defer rows.Close()
  81. for rows.Next() {
  82. r := &model.AppInfo{}
  83. if err = rows.Scan(&r.ID, &r.Name, &r.AppKey, &r.PurgeURL); err != nil {
  84. err = errors.WithStack(err)
  85. res = nil
  86. return
  87. }
  88. res = append(res, r)
  89. }
  90. return
  91. }