house.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. package model
  2. import (
  3. "fmt"
  4. "git.getensh.com/common/gopkgs/logger"
  5. "git.getensh.com/common/gopkgs/util"
  6. "go.uber.org/zap"
  7. "gorm.io/gorm"
  8. "time"
  9. )
  10. type THouse struct {
  11. ID int64 `gorm:"column:id" json:"id"`
  12. GardenId int64 `gorm:"column:garden_id" json:"garden_id"`
  13. Uid int64 `gorm:"column:uid" json:"uid"`
  14. ApprovedAt time.Time `gorm:"column:approved_at" json:"approved_at"`
  15. ApproveStatus int64 `gorm:"column:approve_status" json:"approve_status"`
  16. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  17. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  18. UserType int32 `gorm:"column:user_type" json:"user_type"`
  19. HouseId int64 `gorm:"column:house_id" json:"house_id"`
  20. BuildingNumber string `gorm:"column:building_number" json:"building_number"`
  21. UnitNumber int64 `gorm:"column:unit_number" json:"unit_number"`
  22. HouseNumber string `gorm:"column:house_number" json:"house_number"`
  23. UniqFlag int64 `gorm:"column:uniq_flag" json:"uniq_flag"`
  24. Appendix string `gorm:"column:appendix" json:"appendix"`
  25. Feedback string `gorm:"column:feedback" json:"feedback"`
  26. BuildingId int64 `gorm:"column:building_id" json:"building_id"`
  27. UnitId int64 `gorm:"column:unit_id" json:"unit_id"`
  28. Phone string `gorm:"column:phone" json:"phone"`
  29. Name string `gorm:"column:name" json:"name"`
  30. IdNumber string `gorm:"column:id_number" json:"id_number"`
  31. IdType int32 `gorm:"column:id_type" json:"id_type"`
  32. OpenId string `gorm:"column:open_id" json:"open_id"`
  33. }
  34. func (p *THouse) TableName() string {
  35. return "t_house"
  36. }
  37. func (p *THouse) Find(db *gorm.DB, where map[string]interface{}) error {
  38. err := db.Table(p.TableName()).Where(where).Find(p).Error
  39. if err != nil {
  40. fields, _ := util.MarshalToString(where)
  41. logger.Error("mysql",
  42. zap.String("sql", "select from "+p.TableName()),
  43. zap.String("fields", fields),
  44. zap.String("error", err.Error()))
  45. }
  46. return err
  47. }
  48. func (p *THouse) Last(db *gorm.DB) error {
  49. err := db.Table(p.TableName()).Last(p).Error
  50. if err != nil {
  51. logger.Error("mysql",
  52. zap.String("sql", "select last from "+p.TableName()),
  53. zap.String("fields", ""),
  54. zap.String("error", err.Error()))
  55. }
  56. return err
  57. }
  58. // Insert 插入一条记录
  59. func (p *THouse) Insert(db *gorm.DB) error {
  60. err := db.Create(p).Error
  61. if err != nil {
  62. fields, _ := util.MarshalToString(*p)
  63. logger.Error("mysql",
  64. zap.String("sql", "insert into "+p.TableName()),
  65. zap.String("fields", fields),
  66. zap.String("error", err.Error()))
  67. }
  68. return err
  69. }
  70. func (p *THouse) Delete(db *gorm.DB, filter map[string]interface{}) error {
  71. cond, val, err := whereBuild(filter)
  72. if err != nil {
  73. return err
  74. }
  75. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  76. }
  77. func (p *THouse) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  78. cond, val, err := whereBuild(where)
  79. if err != nil {
  80. if err != nil {
  81. fields, _ := util.MarshalToString(values)
  82. logger.Error("mysql",
  83. zap.String("sql", "update "+p.TableName()),
  84. zap.String("fields", fields),
  85. zap.String("error", err.Error()))
  86. }
  87. return err
  88. }
  89. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  90. }
  91. func (p *THouse) UpdateAffected(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) (int64, error) {
  92. cond, val, err := whereBuild(where)
  93. if err != nil {
  94. if err != nil {
  95. fields, _ := util.MarshalToString(values)
  96. logger.Error("mysql",
  97. zap.String("sql", "update "+p.TableName()),
  98. zap.String("fields", fields),
  99. zap.String("error", err.Error()))
  100. }
  101. return 0, err
  102. }
  103. r := db.Table(p.TableName()).Where(cond, val...).Updates(values)
  104. return r.RowsAffected, r.Error
  105. }
  106. func (p *THouse) UpdateByModel(db *gorm.DB) error {
  107. err := db.Model(p).Updates(p).Error
  108. if err != nil {
  109. fields, _ := util.MarshalToString(*p)
  110. logger.Error("mysql",
  111. zap.String("sql", "update "+p.TableName()),
  112. zap.String("fields", fields),
  113. zap.String("error", err.Error()))
  114. }
  115. return err
  116. }
  117. func (p *THouse) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  118. cond, val, err := whereBuildAndOr(where, or)
  119. if err != nil {
  120. return 0, err
  121. }
  122. ret := int64(0)
  123. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  124. if err != nil {
  125. fields, _ := util.MarshalToString(where)
  126. logger.Error("mysql",
  127. zap.String("sql", "select count "+p.TableName()),
  128. zap.String("fields", fields),
  129. zap.String("error", err.Error()))
  130. }
  131. return ret, err
  132. }
  133. func (p *THouse) CountDistinct(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, distinct string) (int64, error) {
  134. cond, val, err := whereBuildAndOr(where, or)
  135. if err != nil {
  136. return 0, err
  137. }
  138. ret := int64(0)
  139. err = db.Table(p.TableName()).Where(cond, val...).Distinct(distinct).Count(&ret).Error
  140. if err != nil {
  141. fields, _ := util.MarshalToString(where)
  142. logger.Error("mysql",
  143. zap.String("sql", "select count "+p.TableName()),
  144. zap.String("fields", fields),
  145. zap.String("error", err.Error()))
  146. }
  147. return ret, err
  148. }
  149. func (p *THouse) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []THouse, err error) {
  150. if page >= 0 {
  151. return p.ListByJoin(db, where, or, page, pageSize)
  152. }
  153. cond, val, err := whereBuildAndOr(where, or)
  154. if err != nil {
  155. return list, err
  156. }
  157. result := db.Table(p.TableName()).Where(cond, val...).Find(&list)
  158. if result.Error != nil {
  159. wherefields, _ := util.MarshalToString(where)
  160. logger.Error("mysql",
  161. zap.String("sql", "select * from "+p.TableName()),
  162. zap.String("where", wherefields),
  163. zap.String("error", result.Error.Error()))
  164. }
  165. return list, result.Error
  166. }
  167. func (p *THouse) ListByJoin(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []THouse, err error) {
  168. cond, val, err := whereBuildAndOr(where, or)
  169. if err != nil {
  170. return list, err
  171. }
  172. offset := (page - 1) * pageSize
  173. sql := fmt.Sprintf("select * from %s t1 join (select id from %s order by created_at desc limit %d offset %d) t2 on t1.id=t2.id order by t1.created_at desc", p.TableName(), p.TableName(), pageSize, offset)
  174. if cond != "" {
  175. sql = fmt.Sprintf("select * from %s t1 join (select id from %s where %s order by created_at desc limit %d offset %d) t2 on t1.id=t2.id order by t1.created_at desc", p.TableName(), p.TableName(), cond, pageSize, offset)
  176. }
  177. if page == -1 || pageSize == -1 {
  178. sql = fmt.Sprintf("select * from %s t1 join (select id from %s ) t2 on t1.id=t2.id order by t1.created_at desc", p.TableName(), p.TableName())
  179. if cond != "" {
  180. sql = fmt.Sprintf("select * from %s t1 join (select id from %s where %s ) t2 on t1.id=t2.id order by t1.created_at desc", p.TableName(), p.TableName(), cond)
  181. }
  182. }
  183. result := db.Raw(sql, val...).Scan(&list)
  184. if result.Error != nil {
  185. wherefields, _ := util.MarshalToString(where)
  186. logger.Error("mysql",
  187. zap.String("sql", "select * from "+p.TableName()),
  188. zap.String("where", wherefields),
  189. zap.String("error", result.Error.Error()))
  190. }
  191. return list, result.Error
  192. }