parameter.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/library/log"
  6. )
  7. var (
  8. // select
  9. _allParameterSQL = "SELECT name, value FROM rating_parameter"
  10. // insert
  11. _inParameterSQL = "INSERT INTO rating_parameter(name, value, remark) VALUES %s ON DUPLICATE KEY UPDATE value=VALUES(value), remark=VALUES(remark)"
  12. )
  13. // GetAllParameter get all parameter
  14. func (d *Dao) GetAllParameter(c context.Context) (parameters map[string]int64, err error) {
  15. parameters = make(map[string]int64)
  16. rows, err := d.db.Query(c, _allParameterSQL)
  17. if err != nil {
  18. log.Error("d.db.Query GetAllParameter error(%v)", err)
  19. return
  20. }
  21. defer rows.Close()
  22. for rows.Next() {
  23. var name string
  24. var vaule int64
  25. err = rows.Scan(&name, &vaule)
  26. if err != nil {
  27. log.Error("rows.Scan GetAllParameter error(%v)", err)
  28. return
  29. }
  30. parameters[name] = vaule
  31. }
  32. return
  33. }
  34. // InsertParameter insert vals into rating_parameter
  35. func (d *Dao) InsertParameter(c context.Context, val string) (rows int64, err error) {
  36. if val == "" {
  37. return
  38. }
  39. res, err := d.db.Exec(c, fmt.Sprintf(_inParameterSQL, val))
  40. if err != nil {
  41. return
  42. }
  43. return res.RowsAffected()
  44. }