material.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 TMaterial struct {
  12. ID int64 `gorm:"column:id;PrimaryKey" json:"id"`
  13. Pic string `gorm:"column:pic" json:"pic"`
  14. Mtype int32 `gorm:"column:mtype" json:"mtype"`
  15. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  16. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  17. Enable int32 `gorm:"column:enable" json:"enable"`
  18. }
  19. func (p *TMaterial) TableName() string {
  20. return "t_material"
  21. }
  22. func (p *TMaterial) Find(db *gorm.DB, where map[string]interface{}) error {
  23. err := db.Table(p.TableName()).Where(where).Find(p).Error
  24. if err != nil {
  25. fields, _ := util.MarshalToString(where)
  26. logger.Error("mysql",
  27. zap.String("sql", "select from "+p.TableName()),
  28. zap.String("fields", fields),
  29. zap.String("error", err.Error()))
  30. }
  31. return err
  32. }
  33. func (p *TMaterial) Last(db *gorm.DB) error {
  34. err := db.Table(p.TableName()).Last(p).Error
  35. if err != nil {
  36. logger.Error("mysql",
  37. zap.String("sql", "select last from "+p.TableName()),
  38. zap.String("fields", ""),
  39. zap.String("error", err.Error()))
  40. }
  41. return err
  42. }
  43. // Insert 插入一条记录
  44. func (p *TMaterial) Insert(db *gorm.DB) error {
  45. err := db.Create(p).Error
  46. if err != nil {
  47. fields, _ := util.MarshalToString(*p)
  48. logger.Error("mysql",
  49. zap.String("sql", "insert into "+p.TableName()),
  50. zap.String("fields", fields),
  51. zap.String("error", err.Error()))
  52. }
  53. return err
  54. }
  55. func (p *TMaterial) Delete(db *gorm.DB, filter map[string]interface{}) error {
  56. cond, val, err := whereBuild(filter)
  57. if err != nil {
  58. return err
  59. }
  60. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  61. }
  62. func (p *TMaterial) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  63. cond, val, err := whereBuild(where)
  64. if err != nil {
  65. if err != nil {
  66. fields, _ := util.MarshalToString(values)
  67. logger.Error("mysql",
  68. zap.String("sql", "update "+p.TableName()),
  69. zap.String("fields", fields),
  70. zap.String("error", err.Error()))
  71. }
  72. return err
  73. }
  74. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  75. }
  76. func (p *TMaterial) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  77. cond, val, err := whereBuildAndOr(where, or)
  78. if err != nil {
  79. return 0, err
  80. }
  81. ret := int64(0)
  82. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  83. if err != nil {
  84. fields, _ := util.MarshalToString(where)
  85. logger.Error("mysql",
  86. zap.String("sql", "select count "+p.TableName()),
  87. zap.String("fields", fields),
  88. zap.String("error", err.Error()))
  89. }
  90. return ret, err
  91. }
  92. func (p *TMaterial) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TMaterial, err error) {
  93. cond, val, err := whereBuildAndOr(where, or)
  94. if err != nil {
  95. return list, err
  96. }
  97. if pageSize < 0 {
  98. result := db.Table(p.TableName()).Where(cond, val...).Find(&list)
  99. if result.Error != nil {
  100. wherefields, _ := util.MarshalToString(where)
  101. logger.Error("mysql",
  102. zap.String("sql", "select * from "+p.TableName()),
  103. zap.String("where", wherefields),
  104. zap.String("error", result.Error.Error()))
  105. }
  106. return list, result.Error
  107. }
  108. offset := (page - 1) * pageSize
  109. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Find(&list)
  110. if result.Error != nil {
  111. wherefields, _ := util.MarshalToString(where)
  112. logger.Error("mysql",
  113. zap.String("sql", "select * from "+p.TableName()),
  114. zap.String("where", wherefields),
  115. zap.String("error", result.Error.Error()))
  116. }
  117. return list, result.Error
  118. }