config.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package dao
  2. import (
  3. "bytes"
  4. "context"
  5. "database/sql"
  6. "encoding/json"
  7. "time"
  8. "go-common/app/admin/main/reply/model"
  9. )
  10. const (
  11. _addConfigSQL = "INSERT IGNORE INTO reply_config (type, oid, adminid, operator, category, config, ctime, mtime) VALUES(?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE adminid = ?, operator = ?, config=?, mtime = ?"
  12. _paginationConfigSQL = "SELECT id, type, oid, adminid, operator, category, config, ctime, mtime FROM reply_config"
  13. _paginationConfigCountSQL = "SELECT count(id) FROM reply_config"
  14. _loadConfigSQL = "SELECT id, type, oid, adminid, operator, category, config, ctime, mtime FROM reply_config WHERE type=? AND oid=? AND category = ?"
  15. _loadConfigByIDSQL = "SELECT id, type, oid, adminid, operator, category, config, ctime, mtime FROM reply_config WHERE id = ?"
  16. _deleteConfigSQL = "DELETE FROM reply_config where id = ?"
  17. )
  18. // AddConfig create a new config.
  19. func (d *Dao) AddConfig(c context.Context, typ, category int32, oid, adminid int64, operator, config string, now time.Time) (id int64, err error) {
  20. res, err := d.db.Exec(c, _addConfigSQL, typ, oid, adminid, operator, category, config, now, now, adminid, operator, config, now)
  21. if err != nil {
  22. return
  23. }
  24. return res.LastInsertId()
  25. }
  26. // LoadConfig load a config record.
  27. func (d *Dao) LoadConfig(c context.Context, typ, category int32, oid int64) (m *model.Config, err error) {
  28. m = new(model.Config)
  29. row := d.db.QueryRow(c, _loadConfigSQL, typ, oid, category)
  30. if err = row.Scan(&m.ID, &m.Type, &m.Oid, &m.AdminID, &m.Operator, &m.Category, &m.Config, &m.CTime, &m.MTime); err != nil {
  31. if err == sql.ErrNoRows {
  32. m = nil
  33. err = nil
  34. return
  35. }
  36. }
  37. if m.ID > 0 && len(m.Config) > 0 {
  38. dat := new(model.Config)
  39. if err = json.Unmarshal([]byte(m.Config), dat); err == nil {
  40. m.ShowEntry = dat.ShowEntry
  41. m.ShowAdmin = dat.ShowAdmin
  42. }
  43. }
  44. return
  45. }
  46. // LoadConfigByID load a config record by id.
  47. func (d *Dao) LoadConfigByID(c context.Context, id int64) (m *model.Config, err error) {
  48. m = new(model.Config)
  49. row := d.db.QueryRow(c, _loadConfigByIDSQL, id)
  50. if err = row.Scan(&m.ID, &m.Type, &m.Oid, &m.AdminID, &m.Operator, &m.Category, &m.Config, &m.CTime, &m.MTime); err != nil {
  51. if err == sql.ErrNoRows {
  52. return nil, nil
  53. }
  54. }
  55. if m.ID > 0 {
  56. dat := new(model.Config)
  57. if err = json.Unmarshal([]byte(m.Config), dat); err == nil {
  58. m.ShowEntry = dat.ShowEntry
  59. m.ShowAdmin = dat.ShowAdmin
  60. }
  61. }
  62. return
  63. }
  64. // PaginateConfig paginate config list of records indexing from start(offset) to end(offset+count) by conditions.
  65. func (d *Dao) PaginateConfig(c context.Context, typ, category int32, oid int64, operator string, offset, count int) (configs []*model.Config, err error) {
  66. var paginationSQLBuffer bytes.Buffer
  67. paginationSQLBuffer.WriteString(_paginationConfigSQL)
  68. sqlwhere, queryParams := d.constructPaginationSQLWhere(typ, category, oid, operator)
  69. paginationSQLBuffer.WriteString(sqlwhere)
  70. paginationSQLBuffer.WriteString(" limit ?, ?")
  71. queryParams = append(queryParams, offset, count-1)
  72. rows, err := d.db.Query(c, paginationSQLBuffer.String(), queryParams...)
  73. if err != nil {
  74. return
  75. }
  76. defer rows.Close()
  77. for rows.Next() {
  78. m := new(model.Config)
  79. if err = rows.Scan(&m.ID, &m.Type, &m.Oid, &m.AdminID, &m.Operator, &m.Category, &m.Config, &m.CTime, &m.MTime); err != nil {
  80. return
  81. }
  82. if m.ID > 0 {
  83. dat := new(model.Config)
  84. if err = json.Unmarshal([]byte(m.Config), dat); err == nil {
  85. m.ShowEntry = dat.ShowEntry
  86. m.ShowAdmin = dat.ShowAdmin
  87. }
  88. }
  89. configs = append(configs, m)
  90. }
  91. return
  92. }
  93. // PaginateConfigCount returns a total count of records by conditions.
  94. func (d *Dao) PaginateConfigCount(c context.Context, typ, category int32, oid int64, operator string) (totalCount int64, err error) {
  95. var paginationCountSQLBuffer bytes.Buffer
  96. paginationCountSQLBuffer.WriteString(_paginationConfigCountSQL)
  97. sqlwhere, countParams := d.constructPaginationSQLWhere(typ, category, oid, operator)
  98. paginationCountSQLBuffer.WriteString(sqlwhere)
  99. row := d.db.QueryRow(c, paginationCountSQLBuffer.String(), countParams...)
  100. if err = row.Scan(&totalCount); err != nil {
  101. if err == sql.ErrNoRows {
  102. err = nil
  103. }
  104. }
  105. return
  106. }
  107. func (d *Dao) constructPaginationSQLWhere(tp, category int32, oid int64, operator string) (sqlWhere string, queryParams []interface{}) {
  108. var sqlBuffer bytes.Buffer
  109. sqlBuffer.WriteString(" where 1 = 1 ")
  110. if tp > 0 && oid > 0 && category > 0 {
  111. sqlBuffer.WriteString(" and type = ? and oid = ? and category = ?")
  112. queryParams = append(queryParams, tp, oid, category)
  113. }
  114. if len(operator) > 0 {
  115. sqlBuffer.WriteString(" and operator = ?")
  116. queryParams = append(queryParams, operator)
  117. }
  118. return sqlBuffer.String(), queryParams
  119. }
  120. // DeleteConfig delete a reply config record by id.
  121. func (d *Dao) DeleteConfig(c context.Context, id int64) (rows int64, err error) {
  122. res, err := d.db.Exec(c, _deleteConfigSQL, id)
  123. if err != nil {
  124. return
  125. }
  126. return res.RowsAffected()
  127. }