service_phone_class.go 5.6 KB

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