service_phone_view.go 5.6 KB

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