common.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package gorm
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "strings"
  7. "github.com/jinzhu/gorm"
  8. "go-common/app/admin/main/aegis/model/net"
  9. "go-common/library/log"
  10. )
  11. // Available .
  12. func Available(db *gorm.DB) *gorm.DB {
  13. return db.Where("disable_time = '0000-00-00 00:00:00'")
  14. }
  15. // Disable .
  16. func Disable(db *gorm.DB) *gorm.DB {
  17. return db.Not("disable_time = '0000-00-00 00:00:00'")
  18. }
  19. func state(state string) func(db *gorm.DB) *gorm.DB {
  20. state = strings.TrimSpace(state)
  21. if state == net.StateAvailable {
  22. return func(db *gorm.DB) *gorm.DB {
  23. return Available(db)
  24. }
  25. } else if state == net.StateDisable {
  26. return func(db *gorm.DB) *gorm.DB {
  27. return Disable(db)
  28. }
  29. } else {
  30. return func(db *gorm.DB) *gorm.DB {
  31. return db
  32. }
  33. }
  34. }
  35. func pager(ps int64, pn int64, sort string) func(db *gorm.DB) *gorm.DB {
  36. offset := ps * (pn - 1)
  37. order := fmt.Sprintf("id %s", sort)
  38. return func(db *gorm.DB) *gorm.DB {
  39. return db.Limit(ps).Offset(offset).Order(order)
  40. }
  41. }
  42. // UpdateFields .
  43. func (d *Dao) UpdateFields(c context.Context, db *gorm.DB, table string, id int64, fields map[string]interface{}) (err error) {
  44. if db == nil {
  45. db = d.orm
  46. }
  47. if err = db.Table(table).Where("id=?", id).Updates(fields).Error; err != nil {
  48. log.Error("UpdateFlow(%s,%d) error(%v) changed(%+v)", table, id, err, fields)
  49. }
  50. return
  51. }
  52. // AddItem .
  53. func (d *Dao) AddItem(c context.Context, db *gorm.DB, n interface{}) (err error) {
  54. if db == nil {
  55. db = d.orm
  56. }
  57. if err = db.Create(n).Error; err != nil {
  58. log.Error("AddItem error(%v) (%+v)", err, n)
  59. }
  60. return
  61. }
  62. // ColumnMapString .
  63. func (d *Dao) ColumnMapString(c context.Context, table string, column string, ids []int64, where string) (result map[int64]string, err error) {
  64. var (
  65. rows *sql.Rows
  66. id int64
  67. value string
  68. slt = fmt.Sprintf("`id`,`%s`", column)
  69. )
  70. result = map[int64]string{}
  71. db := d.orm.Table(table).Select(slt).Where("id in (?)", ids)
  72. where = strings.TrimSpace(where)
  73. if where != "" {
  74. db = db.Where(where)
  75. }
  76. rows, err = db.Rows()
  77. if err == sql.ErrNoRows {
  78. err = nil
  79. return
  80. }
  81. if err != nil {
  82. log.Error("ColumnMapString(%s, %s) rows error(%v) ids(%v)", table, column, err, ids)
  83. return
  84. }
  85. defer rows.Close()
  86. for rows.Next() {
  87. if err = rows.Scan(&id, &value); err != nil {
  88. log.Error("ColumnMapString(%s, %s) rows.scan error(%v) ids(%v)", table, column, err, ids)
  89. return
  90. }
  91. result[id] = value
  92. }
  93. return
  94. }