package.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package model
  4. import (
  5. "git.getensh.com/common/gopkgs/logger"
  6. "git.getensh.com/common/gopkgs/util"
  7. "go.uber.org/zap"
  8. "gorm.io/gorm"
  9. "time"
  10. )
  11. type TPackage struct {
  12. ID int64 `gorm:"column:id;PrimaryKey" json:"id"`
  13. Desc string `gorm:"column:desc" json:"desc"`
  14. Enable int64 `gorm:"column:enable" json:"enable"`
  15. EnableAt time.Time `gorm:"column:enable_at" json:"enable_at"`
  16. HouseCount int64 `gorm:"column:house_count" json:"house_count"`
  17. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  18. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  19. Price int64 `gorm:"column:price" json:"price"`
  20. Name string `gorm:"column:name" json:"name"`
  21. }
  22. func (p *TPackage) TableName() string {
  23. return "t_package"
  24. }
  25. func (p *TPackage) Find(db *gorm.DB, where map[string]interface{}) error {
  26. err := db.Table(p.TableName()).Where(where).Find(p).Error
  27. if err != nil {
  28. fields, _ := util.MarshalToString(where)
  29. logger.Error("mysql",
  30. zap.String("sql", "select from "+p.TableName()),
  31. zap.String("fields", fields),
  32. zap.String("error", err.Error()))
  33. }
  34. return err
  35. }
  36. func (p *TPackage) Last(db *gorm.DB) error {
  37. err := db.Table(p.TableName()).Last(p).Error
  38. if err != nil {
  39. logger.Error("mysql",
  40. zap.String("sql", "select last from "+p.TableName()),
  41. zap.String("fields", ""),
  42. zap.String("error", err.Error()))
  43. }
  44. return err
  45. }
  46. // Insert 插入一条记录
  47. func (p *TPackage) Insert(db *gorm.DB) error {
  48. err := db.Create(p).Error
  49. if err != nil {
  50. fields, _ := util.MarshalToString(*p)
  51. logger.Error("mysql",
  52. zap.String("sql", "insert into "+p.TableName()),
  53. zap.String("fields", fields),
  54. zap.String("error", err.Error()))
  55. }
  56. return err
  57. }
  58. func (p *TPackage) Delete(db *gorm.DB, filter map[string]interface{}) error {
  59. cond, val, err := whereBuild(filter)
  60. if err != nil {
  61. return err
  62. }
  63. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  64. }
  65. func (p *TPackage) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  66. cond, val, err := whereBuild(where)
  67. if err != nil {
  68. if err != nil {
  69. fields, _ := util.MarshalToString(values)
  70. logger.Error("mysql",
  71. zap.String("sql", "update "+p.TableName()),
  72. zap.String("fields", fields),
  73. zap.String("error", err.Error()))
  74. }
  75. return err
  76. }
  77. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  78. }
  79. func (p *TPackage) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  80. cond, val, err := whereBuildAndOr(where, or)
  81. if err != nil {
  82. return 0, err
  83. }
  84. ret := int64(0)
  85. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  86. if err != nil {
  87. fields, _ := util.MarshalToString(where)
  88. logger.Error("mysql",
  89. zap.String("sql", "select count "+p.TableName()),
  90. zap.String("fields", fields),
  91. zap.String("error", err.Error()))
  92. }
  93. return ret, err
  94. }
  95. func (p *TPackage) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TPackage, err error) {
  96. cond, val, err := whereBuildAndOr(where, or)
  97. if err != nil {
  98. return list, err
  99. }
  100. if pageSize < 0 {
  101. result := db.Table(p.TableName()).Where(cond, val...).Find(&list)
  102. if result.Error != nil {
  103. wherefields, _ := util.MarshalToString(where)
  104. logger.Error("mysql",
  105. zap.String("sql", "select * from "+p.TableName()),
  106. zap.String("where", wherefields),
  107. zap.String("error", result.Error.Error()))
  108. }
  109. return list, result.Error
  110. }
  111. offset := (page - 1) * pageSize
  112. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Find(&list)
  113. if result.Error != nil {
  114. wherefields, _ := util.MarshalToString(where)
  115. logger.Error("mysql",
  116. zap.String("sql", "select * from "+p.TableName()),
  117. zap.String("where", wherefields),
  118. zap.String("error", result.Error.Error()))
  119. }
  120. return list, result.Error
  121. }