house_approved.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 THouseApproved struct {
  11. ID int64 `gorm:"column:id;PRIMARY_KEY" 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. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  16. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  17. UserType int32 `gorm:"column:user_type" json:"user_type"`
  18. HouseId int64 `gorm:"column:house_id" json:"house_id"`
  19. BuildingNumber string `gorm:"column:building_number" json:"building_number"`
  20. UnitNumber int64 `gorm:"column:unit_number" json:"unit_number"`
  21. HouseNumber string `gorm:"column:house_number" json:"house_number"`
  22. Appendix string `gorm:"column:appendix" json:"appendix"`
  23. Feedback string `gorm:"column:feedback" json:"feedback"`
  24. BuildingId int64 `gorm:"column:building_id" json:"building_id"`
  25. UnitId int64 `gorm:"column:unit_id" json:"unit_id"`
  26. Phone string `gorm:"column:phone" json:"phone"`
  27. Name string `gorm:"column:name" json:"name"`
  28. IdNumber string `gorm:"column:id_number" json:"id_number"`
  29. IdType int32 `gorm:"column:id_type" json:"id_type"`
  30. OpenId string `gorm:"column:open_id" json:"open_id"`
  31. PublicOpenId string `gorm:"column:public_open_id" json:"public_open_id"`
  32. }
  33. func (p *THouseApproved) TableName() string {
  34. return "t_house_approved"
  35. }
  36. func (p *THouseApproved) Find(db *gorm.DB, where map[string]interface{}) error {
  37. err := db.Table(p.TableName()).Where(where).Find(p).Error
  38. if err != nil {
  39. fields, _ := util.MarshalToString(where)
  40. logger.Error("mysql",
  41. zap.String("sql", "select from "+p.TableName()),
  42. zap.String("fields", fields),
  43. zap.String("error", err.Error()))
  44. }
  45. return err
  46. }
  47. func (p *THouseApproved) Last(db *gorm.DB) error {
  48. err := db.Table(p.TableName()).Last(p).Error
  49. if err != nil {
  50. logger.Error("mysql",
  51. zap.String("sql", "select last from "+p.TableName()),
  52. zap.String("fields", ""),
  53. zap.String("error", err.Error()))
  54. }
  55. return err
  56. }
  57. // Insert 插入一条记录
  58. func (p *THouseApproved) Insert(db *gorm.DB) error {
  59. err := db.Create(p).Error
  60. if err != nil {
  61. fields, _ := util.MarshalToString(*p)
  62. logger.Error("mysql",
  63. zap.String("sql", "insert into "+p.TableName()),
  64. zap.String("fields", fields),
  65. zap.String("error", err.Error()))
  66. }
  67. return err
  68. }
  69. func (p *THouseApproved) Delete(db *gorm.DB, filter map[string]interface{}) error {
  70. cond, val, err := whereBuild(filter)
  71. if err != nil {
  72. return err
  73. }
  74. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  75. }
  76. func (p *THouseApproved) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  77. cond, val, err := whereBuild(where)
  78. if err != nil {
  79. if err != nil {
  80. fields, _ := util.MarshalToString(values)
  81. logger.Error("mysql",
  82. zap.String("sql", "update "+p.TableName()),
  83. zap.String("fields", fields),
  84. zap.String("error", err.Error()))
  85. }
  86. return err
  87. }
  88. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  89. }
  90. func (p *THouseApproved) UpdateByModel(db *gorm.DB) error {
  91. err := db.Model(p).Updates(p).Error
  92. if err != nil {
  93. fields, _ := util.MarshalToString(*p)
  94. logger.Error("mysql",
  95. zap.String("sql", "update "+p.TableName()),
  96. zap.String("fields", fields),
  97. zap.String("error", err.Error()))
  98. }
  99. return err
  100. }
  101. func (p *THouseApproved) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  102. cond, val, err := whereBuildAndOr(where, or)
  103. if err != nil {
  104. return 0, err
  105. }
  106. ret := int64(0)
  107. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  108. if err != nil {
  109. fields, _ := util.MarshalToString(where)
  110. logger.Error("mysql",
  111. zap.String("sql", "select count "+p.TableName()),
  112. zap.String("fields", fields),
  113. zap.String("error", err.Error()))
  114. }
  115. return ret, err
  116. }
  117. func (p *THouseApproved) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []THouseApproved, err error) {
  118. if page >= 0 {
  119. return p.ListByJoin(db, where, or, page, pageSize)
  120. }
  121. cond, val, err := whereBuildAndOr(where, or)
  122. if err != nil {
  123. return list, err
  124. }
  125. result := db.Table(p.TableName()).Where(cond, val...).Find(&list)
  126. if result.Error != nil {
  127. wherefields, _ := util.MarshalToString(where)
  128. logger.Error("mysql",
  129. zap.String("sql", "select * from "+p.TableName()),
  130. zap.String("where", wherefields),
  131. zap.String("error", result.Error.Error()))
  132. }
  133. return list, result.Error
  134. }
  135. func (p *THouseApproved) ListByJoin(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []THouseApproved, err error) {
  136. cond, val, err := whereBuildAndOr(where, or)
  137. if err != nil {
  138. return list, err
  139. }
  140. offset := (page - 1) * pageSize
  141. sql := fmt.Sprintf("select * from %s t1 join (select id from %s order by created_at limit %d offset %d) t2 on t1.id=t2.id order by t1.created_at desc", p.TableName(), p.TableName(), pageSize, offset)
  142. if cond != "" {
  143. 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)
  144. }
  145. if page == -1 || pageSize == -1 {
  146. 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())
  147. if cond != "" {
  148. 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)
  149. }
  150. }
  151. result := db.Raw(sql, val...).Scan(&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. func (p *THouseApproved) ListGroup(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, group string, page int, pageSize int) (list []THouseApproved, err error) {
  162. if page >= 0 {
  163. return p.ListByJoinGroup(db, where, or, group, page, pageSize)
  164. }
  165. cond, val, err := whereBuildAndOr(where, or)
  166. if err != nil {
  167. return list, err
  168. }
  169. result := db.Table(p.TableName()).Where(cond, val...).Group(group).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 *THouseApproved) ListByJoinGroup(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, group string, page int, pageSize int) (list []THouseApproved, 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 group by %s limit %d offset %d) t2 on t1.id=t2.id", p.TableName(), p.TableName(), group, pageSize, offset)
  186. if cond != "" {
  187. sql = fmt.Sprintf("select * from %s t1 join (select id from %s where %s group by %s limit %d offset %d) t2 on t1.id=t2.id", p.TableName(), p.TableName(), cond, group, 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. }
  199. func (p *THouseApproved) CountGroup(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, group string) (int64, error) {
  200. cond, val, err := whereBuildAndOr(where, or)
  201. if err != nil {
  202. return 0, err
  203. }
  204. sql := fmt.Sprintf("select count(*) as count from (select count(*) from %s group by %s) t1", p.TableName(), group)
  205. if len(cond) > 0 {
  206. sql = fmt.Sprintf("select count(*) as count from (select count(*) from %s where %s group by %s) t1", p.TableName(), cond, group)
  207. }
  208. type Result struct {
  209. Count int64
  210. }
  211. array := []Result{}
  212. err = db.Raw(sql, val...).Scan(&array).Error
  213. if err != nil {
  214. fields, _ := util.MarshalToString(where)
  215. logger.Error("mysql",
  216. zap.String("sql", "select count "+p.TableName()),
  217. zap.String("fields", fields),
  218. zap.String("error", err.Error()))
  219. return 0, err
  220. }
  221. if len(array) == 0 {
  222. return 0, nil
  223. }
  224. return array[0].Count, err
  225. }