house_rent_appointment.go 6.3 KB

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