repair_class.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. "fmt"
  6. "git.getensh.com/common/gopkgs/logger"
  7. "git.getensh.com/common/gopkgs/util"
  8. "go.uber.org/zap"
  9. "gorm.io/gorm"
  10. "time"
  11. )
  12. type TRepairClass struct {
  13. ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"`
  14. Name string `gorm:"column:name" json:"name"`
  15. Enable int32 `gorm:"column:enable" json:"enable"`
  16. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  17. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  18. Area int32 `gorm:"column:area" json:"area"`
  19. table string
  20. }
  21. func (p *TRepairClass) TableName() string {
  22. return p.table
  23. }
  24. func NewRepairClass(database string) *TRepairClass {
  25. return &TRepairClass{table: fmt.Sprintf("%s.%s", database, "t_repair_class")}
  26. }
  27. func (p *TRepairClass) SetTable(database string) {
  28. p.table = fmt.Sprintf("%s.%s", database, "t_repair_class")
  29. }
  30. func (p *TRepairClass) CreateTable(db *gorm.DB) error {
  31. sql :="CREATE TABLE IF NOT EXISTS "+p.TableName()+"("+
  32. "`id` int(10) NOT NULL AUTO_INCREMENT,"+
  33. "`name` varchar(128) COLLATE utf8mb4_bin NOT NULL COMMENT '报修分类名',"+
  34. "`enable` tinyint(1) NOT NULL COMMENT '1 已启用 2 已禁用',"+
  35. " `created_at` datetime NOT NULL,"+
  36. " `updated_at` datetime NOT NULL,"+
  37. "`area` tinyint(1) NOT NULL COMMENT '1 公区 2 户内',"+
  38. " PRIMARY KEY (`id`),"+
  39. "UNIQUE KEY `name` (`name`) USING BTREE"+
  40. ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='报事报修分类';"
  41. err := db.Exec(sql).Error
  42. if err != nil {
  43. logger.Error("mysql",
  44. zap.String("sql", "create table "+p.TableName()),
  45. zap.String("fields", ""),
  46. zap.String("error", err.Error()))
  47. }
  48. return err
  49. }
  50. func (p *TRepairClass) Find(db *gorm.DB, where map[string]interface{}) error {
  51. err := db.Table(p.TableName()).Where(where).Find(p).Error
  52. if err != nil {
  53. fields, _ := util.MarshalToString(where)
  54. logger.Error("mysql",
  55. zap.String("sql", "select from "+p.TableName()),
  56. zap.String("fields", fields),
  57. zap.String("error", err.Error()))
  58. }
  59. return err
  60. }
  61. func (p *TRepairClass) Last(db *gorm.DB) error {
  62. err := db.Table(p.TableName()).Last(p).Error
  63. if err != nil {
  64. logger.Error("mysql",
  65. zap.String("sql", "select last from "+p.TableName()),
  66. zap.String("fields", ""),
  67. zap.String("error", err.Error()))
  68. }
  69. return err
  70. }
  71. // Insert 插入一条记录
  72. func (p *TRepairClass) Insert(db *gorm.DB) error {
  73. err := db.Table(p.TableName()).Create(p).Error
  74. if err != nil {
  75. fields, _ := util.MarshalToString(*p)
  76. logger.Error("mysql",
  77. zap.String("sql", "insert into "+p.TableName()),
  78. zap.String("fields", fields),
  79. zap.String("error", err.Error()))
  80. }
  81. return err
  82. }
  83. // Insert 插入多条记录
  84. func (p *TRepairClass) InsertMulti(db *gorm.DB, values interface{}) error {
  85. err := db.Table(p.TableName()).Create(values).Error
  86. if err != nil {
  87. fields, _ := util.MarshalToString(*p)
  88. logger.Error("mysql",
  89. zap.String("sql", "insert into "+p.TableName()),
  90. zap.String("fields", fields),
  91. zap.String("error", err.Error()))
  92. }
  93. return err
  94. }
  95. func (p *TRepairClass) Delete(db *gorm.DB, filter map[string]interface{}) error {
  96. cond, val, err := whereBuild(filter)
  97. if err != nil {
  98. return err
  99. }
  100. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  101. }
  102. func (p *TRepairClass) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  103. cond, val, err := whereBuild(where)
  104. if err != nil {
  105. if err != nil {
  106. fields, _ := util.MarshalToString(values)
  107. logger.Error("mysql",
  108. zap.String("sql", "update "+p.TableName()),
  109. zap.String("fields", fields),
  110. zap.String("error", err.Error()))
  111. }
  112. return err
  113. }
  114. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  115. }
  116. func (p *TRepairClass) UpdateByModel(db *gorm.DB) error {
  117. err := db.Table(p.TableName()).Model(p).Updates(p).Error
  118. if err != nil {
  119. fields, _ := util.MarshalToString(*p)
  120. logger.Error("mysql",
  121. zap.String("sql", "update "+p.TableName()),
  122. zap.String("fields", fields),
  123. zap.String("error", err.Error()))
  124. }
  125. return err
  126. }
  127. func (p *TRepairClass) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  128. cond, val, err := whereBuildAndOr(where, or)
  129. if err != nil {
  130. return 0, err
  131. }
  132. ret := int64(0)
  133. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  134. if err != nil {
  135. fields, _ := util.MarshalToString(where)
  136. logger.Error("mysql",
  137. zap.String("sql", "select count "+p.TableName()),
  138. zap.String("fields", fields),
  139. zap.String("error", err.Error()))
  140. }
  141. return ret, err
  142. }
  143. func (p *TRepairClass) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TRepairClass, err error) {
  144. cond, val, err := whereBuildAndOr(where, or)
  145. if err != nil {
  146. return list, err
  147. }
  148. if pageSize < 0 {
  149. result := db.Table(p.TableName()).Where(cond, val...).Order("created_at desc").Find(&list)
  150. if result.Error != nil {
  151. wherefields, _ := util.MarshalToString(where)
  152. logger.Error("mysql",
  153. zap.String("sql", "select * from "+p.TableName()),
  154. zap.String("where", wherefields),
  155. zap.String("error", result.Error.Error()))
  156. }
  157. return list, result.Error
  158. }
  159. offset := (page - 1) * pageSize
  160. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Order("created_at desc").Find(&list)
  161. if result.Error != nil {
  162. wherefields, _ := util.MarshalToString(where)
  163. logger.Error("mysql",
  164. zap.String("sql", "select * from "+p.TableName()),
  165. zap.String("where", wherefields),
  166. zap.String("error", result.Error.Error()))
  167. }
  168. return list, result.Error
  169. }