service_phone.go 5.7 KB

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