t_house_rent.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 THouseRent struct {
  13. ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"`
  14. GardenId int64 `gorm:"column:garden_id" json:"garden_id"`
  15. HouseName string `gorm:"column:house_name" json:"house_name"`
  16. Layer int64 `gorm:"column:layer" json:"layer"`
  17. HouseArea float64 `gorm:"column:house_area" json:"house_area"`
  18. Diretion int32 `gorm:"column:diretion" json:"diretion"`
  19. RoomCount int64 `gorm:"column:room_count" json:"room_count"`
  20. HallCount int64 `gorm:"column:hall_count" json:"hall_count"`
  21. WcCount int64 `gorm:"column:wc_count" json:"wc_count"`
  22. Decorating int32 `gorm:"column:decorating" json:"decorating"`
  23. Contacter string `gorm:"column:contacter" json:"contacter"`
  24. ContactPhone string `gorm:"column:contact_phone" json:"contact_phone"`
  25. PayTimeType int32 `gorm:"column:pay_time_type" json:"pay_time_type"`
  26. RentType int32 `gorm:"column:rent_type" json:"rent_type"`
  27. RoomType int32 `gorm:"column:room_type" json:"room_type"`
  28. RoomArea float64 `gorm:"column:room_area" json:"room_area"`
  29. RentPrice int64 `gorm:"column:rent_price" json:"rent_price"`
  30. Desposit int64 `gorm:"column:desposit" json:"desposit"`
  31. InTime int64 `gorm:"column:in_time" json:"in_time"`
  32. ServicePrice int64 `gorm:"column:service_price" json:"service_price"`
  33. IntermediaryPrice int64 `gorm:"column:intermediary_price" json:"intermediary_price"`
  34. BaseConf int64 `gorm:"column:base_conf" json:"base_conf"`
  35. SpecialConf int64 `gorm:"column:special_conf" json:"special_conf"`
  36. HousePic string `gorm:"column:house_pic" json:"house_pic"`
  37. CertPic string `gorm:"column:cert_pic" json:"cert_pic"`
  38. ProvinceCode string `gorm:"column:province_code" json:"province_code"`
  39. CityCode string `gorm:"column:city_code" json:"city_code"`
  40. AreaCode string `gorm:"column:area_code" json:"area_code"`
  41. StreetCode string `gorm:"column:street_code" json:"street_code"`
  42. GardenName string `gorm:"column:garden_name" json:"garden_name"`
  43. HouseholdUid int64 `gorm:"column:household_uid" json:"household_uid"`
  44. ApproveStatus int32 `gorm:"column:approve_status" json:"approve_status"`
  45. ApprovedAt time.Time `gorm:"column:approved_at" json:"approved_at"`
  46. HasLift int32 `gorm:"column:has_lift" json:"has_lift"`
  47. Feedback string `gorm:"column:feedback" json:"feedback"`
  48. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  49. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  50. Lat float64 `gorm:"column:lat" json:"lat"`
  51. Lnt float64 `gorm:"column:lnt" json:"lnt"`
  52. Desc string `gorm:"column:desc" json:"desc"`
  53. HouseId int64 `gorm:"column:house_id" json:"house_id"`
  54. UnitId int64 `gorm:"column:unit_id" json:"unit_id"`
  55. }
  56. func (p *THouseRent) TableName() string {
  57. return "t_house_rent"
  58. }
  59. func (p *THouseRent) Find(db *gorm.DB, where map[string]interface{}) error {
  60. err := db.Table(p.TableName()).Where(where).Find(p).Error
  61. if err != nil {
  62. fields, _ := util.MarshalToString(where)
  63. logger.Error("mysql",
  64. zap.String("sql", "select from "+p.TableName()),
  65. zap.String("fields", fields),
  66. zap.String("error", err.Error()))
  67. }
  68. return err
  69. }
  70. func (p *THouseRent) Last(db *gorm.DB) error {
  71. err := db.Table(p.TableName()).Last(p).Error
  72. if err != nil {
  73. logger.Error("mysql",
  74. zap.String("sql", "select last from "+p.TableName()),
  75. zap.String("fields", ""),
  76. zap.String("error", err.Error()))
  77. }
  78. return err
  79. }
  80. // Insert 插入一条记录
  81. func (p *THouseRent) Insert(db *gorm.DB) error {
  82. err := db.Table(p.TableName()).Create(p).Error
  83. if err != nil {
  84. fields, _ := util.MarshalToString(*p)
  85. logger.Error("mysql",
  86. zap.String("sql", "insert into "+p.TableName()),
  87. zap.String("fields", fields),
  88. zap.String("error", err.Error()))
  89. }
  90. return err
  91. }
  92. // Insert 插入多条记录
  93. func (p *THouseRent) InsertMulti(db *gorm.DB, values interface{}) error {
  94. err := db.Table(p.TableName()).Create(values).Error
  95. if err != nil {
  96. fields, _ := util.MarshalToString(*p)
  97. logger.Error("mysql",
  98. zap.String("sql", "insert into "+p.TableName()),
  99. zap.String("fields", fields),
  100. zap.String("error", err.Error()))
  101. }
  102. return err
  103. }
  104. func (p *THouseRent) Delete(db *gorm.DB, filter map[string]interface{}) error {
  105. cond, val, err := whereBuild(filter)
  106. if err != nil {
  107. return err
  108. }
  109. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  110. }
  111. func (p *THouseRent) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  112. cond, val, err := whereBuild(where)
  113. if err != nil {
  114. if err != nil {
  115. fields, _ := util.MarshalToString(values)
  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. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  124. }
  125. func (p *THouseRent) UpdateByModel(db *gorm.DB) error {
  126. err := db.Table(p.TableName()).Model(p).Updates(p).Error
  127. if err != nil {
  128. fields, _ := util.MarshalToString(*p)
  129. logger.Error("mysql",
  130. zap.String("sql", "update "+p.TableName()),
  131. zap.String("fields", fields),
  132. zap.String("error", err.Error()))
  133. }
  134. return err
  135. }
  136. func (p *THouseRent) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  137. cond, val, err := whereBuildAndOr(where, or)
  138. if err != nil {
  139. return 0, err
  140. }
  141. ret := int64(0)
  142. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  143. if err != nil {
  144. fields, _ := util.MarshalToString(where)
  145. logger.Error("mysql",
  146. zap.String("sql", "select count "+p.TableName()),
  147. zap.String("fields", fields),
  148. zap.String("error", err.Error()))
  149. }
  150. return ret, err
  151. }
  152. func (p *THouseRent) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []THouseRent, err error) {
  153. cond, val, err := whereBuildAndOr(where, or)
  154. if err != nil {
  155. return list, err
  156. }
  157. if pageSize < 0 {
  158. result := db.Table(p.TableName()).Where(cond, val...).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. }
  168. offset := (page - 1) * pageSize
  169. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Find(&list)
  170. if result.Error != nil {
  171. wherefields, _ := util.MarshalToString(where)
  172. logger.Error("mysql",
  173. zap.String("sql", "select * from "+p.TableName()),
  174. zap.String("where", wherefields),
  175. zap.String("error", result.Error.Error()))
  176. }
  177. return list, result.Error
  178. }
  179. func (p *THouseRent) ListByJoin(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []THouseRent, err error) {
  180. cond, val, err := whereBuildAndOr(where, or)
  181. if err != nil {
  182. return list, err
  183. }
  184. offset := (page - 1) * pageSize
  185. 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)
  186. if cond != "" {
  187. 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)
  188. }
  189. result := db.Raw(sql, val...).Scan(&list)
  190. if result.Error != nil {
  191. wherefields, _ := util.MarshalToString(where)
  192. logger.Error("mysql",
  193. zap.String("sql", "select * from "+p.TableName()),
  194. zap.String("where", wherefields),
  195. zap.String("error", result.Error.Error()))
  196. }
  197. return list, result.Error
  198. }