garden.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. "git.getensh.com/common/gopkgs/logger"
  6. "git.getensh.com/common/gopkgs/util"
  7. "go.uber.org/zap"
  8. "gorm.io/gorm"
  9. "time"
  10. )
  11. type TGarden struct {
  12. ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"`
  13. Province string `gorm:"column:province" json:"province"`
  14. ProvinceCode string `gorm:"column:province_code" json:"province_code"`
  15. City string `gorm:"column:city" json:"city"`
  16. CityCode string `gorm:"column:city_code" json:"city_code"`
  17. Area string `gorm:"column:area" json:"area"`
  18. AreaCode string `gorm:"column:area_code" json:"area_code"`
  19. Street string `gorm:"column:street" json:"street"`
  20. StreetCode string `gorm:"column:street_code" json:"street_code"`
  21. Committee string `gorm:"column:committee" json:"committee"`
  22. CommitteeCode string `gorm:"column:committee_code" json:"committee_code"`
  23. GardenName string `gorm:"column:garden_name" json:"garden_name"`
  24. GardenAddr string `gorm:"column:garden_addr" json:"garden_addr"`
  25. PropertyPerson string `gorm:"column:property_person" json:"property_person"`
  26. PropertyPhone string `gorm:"column:property_phone" json:"property_phone"`
  27. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  28. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  29. Cid int64 `gorm:"column:cid" json:"cid"`
  30. EndTimestamp int64 `gorm:"column:end_timestamp" json:"end_timestamp"`
  31. StartTimestamp int64 `gorm:"column:start_timestamp" json:"start_timestamp"`
  32. UniqFlag int64 `gorm:"column:uniq_flag" json:"uniq_flag"`
  33. GardenDesc string `gorm:"column:garden_desc" json:"garden_desc"`
  34. GardenPic string `gorm:"column:garden_pic" json:"garden_pic"`
  35. GardenPics string `gorm:"column:garden_pics" json:"garden_pics"`
  36. Lnt float64 `gorm:"column:lnt" json:"lnt"`
  37. Lat float64 `gorm:"column:lat" json:"lat"`
  38. Appendix string `gorm:"column:appendix" json:"appendix"`
  39. Status int32 `gorm:"column:status" json:"status"`
  40. ApprovedAt time.Time `gorm:"column:approved_at" json:"approved_at"`
  41. Feedback string `gorm:"column:feedback" json:"feedback"`
  42. BuildingType int32 `gorm:"column:building_type" json:"building_type"`
  43. BuildingStart int64 `gorm:"column:building_start" json:"building_start"`
  44. BuildingEnd int64 `gorm:"column:building_end" json:"building_end"`
  45. GreenPercent float64 `gorm:"column:green_percent" json:"green_percent"`
  46. AreaPercent float64 `gorm:"column:area_percent" json:"area_percent"`
  47. SpaceInfo string `gorm:"column:space_info" json:"space_info"`
  48. BuildingCompany string `gorm:"column:building_company" json:"building_company"`
  49. CoveredArea float64 `gorm:"column:covered_area" json:"covered_area"`
  50. BuildingArea float64 `gorm:"column:building_area" json:"building_area"`
  51. HouseTotal int64 `gorm:"column:house_total" json:"house_total"`
  52. SpaceTotal int64 `gorm:"column:space_total" json:"space_total"`
  53. PropertyFeeStart int64 `gorm:"column:property_fee_start" json:"property_fee_start"`
  54. PropertyFeeEnd int64 `gorm:"column:property_fee_end" json:"property_fee_end"`
  55. WaterType string `gorm:"column:water_type" json:"water_type"`
  56. ElectricType string `gorm:"column:electric_type" json:"electric_type"`
  57. GasFeeStart int64 `gorm:"column:gas_fee_start" json:"gas_fee_start"`
  58. GasFeeEnd int64 `gorm:"column:gas_fee_end" json:"gas_fee_end"`
  59. AvgPrice int64 `gorm:"column:avg_price" json:"avg_price"`
  60. }
  61. func (p *TGarden) TableName() string {
  62. return "t_garden"
  63. }
  64. func (p *TGarden) Find(db *gorm.DB, where map[string]interface{}) error {
  65. err := db.Table(p.TableName()).Where(where).Find(p).Error
  66. if err != nil {
  67. fields, _ := util.MarshalToString(where)
  68. logger.Error("mysql",
  69. zap.String("sql", "select from "+p.TableName()),
  70. zap.String("fields", fields),
  71. zap.String("error", err.Error()))
  72. }
  73. return err
  74. }
  75. func (p *TGarden) Last(db *gorm.DB) error {
  76. err := db.Table(p.TableName()).Last(p).Error
  77. if err != nil {
  78. logger.Error("mysql",
  79. zap.String("sql", "select last from "+p.TableName()),
  80. zap.String("fields", ""),
  81. zap.String("error", err.Error()))
  82. }
  83. return err
  84. }
  85. // Insert 插入一条记录
  86. func (p *TGarden) Insert(db *gorm.DB) error {
  87. err := db.Table(p.TableName()).Create(p).Error
  88. if err != nil {
  89. fields, _ := util.MarshalToString(*p)
  90. logger.Error("mysql",
  91. zap.String("sql", "insert into "+p.TableName()),
  92. zap.String("fields", fields),
  93. zap.String("error", err.Error()))
  94. }
  95. return err
  96. }
  97. func (p *TGarden) Delete(db *gorm.DB, filter map[string]interface{}) error {
  98. cond, val, err := whereBuild(filter)
  99. if err != nil {
  100. return err
  101. }
  102. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  103. }
  104. func (p *TGarden) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  105. cond, val, err := whereBuild(where)
  106. if err != nil {
  107. if err != nil {
  108. fields, _ := util.MarshalToString(values)
  109. logger.Error("mysql",
  110. zap.String("sql", "update "+p.TableName()),
  111. zap.String("fields", fields),
  112. zap.String("error", err.Error()))
  113. }
  114. return err
  115. }
  116. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  117. }
  118. func (p *TGarden) UpdateByModel(db *gorm.DB) error {
  119. err := db.Model(p).Updates(p).Error
  120. if err != nil {
  121. fields, _ := util.MarshalToString(*p)
  122. logger.Error("mysql",
  123. zap.String("sql", "update "+p.TableName()),
  124. zap.String("fields", fields),
  125. zap.String("error", err.Error()))
  126. }
  127. return err
  128. }
  129. func (p *TGarden) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  130. cond, val, err := whereBuildAndOr(where, or)
  131. if err != nil {
  132. return 0, err
  133. }
  134. ret := int64(0)
  135. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  136. if err != nil {
  137. fields, _ := util.MarshalToString(where)
  138. logger.Error("mysql",
  139. zap.String("sql", "select count "+p.TableName()),
  140. zap.String("fields", fields),
  141. zap.String("error", err.Error()))
  142. }
  143. return ret, err
  144. }
  145. func (p *TGarden) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TGarden, err error) {
  146. cond, val, err := whereBuildAndOr(where, or)
  147. if err != nil {
  148. return list, err
  149. }
  150. if pageSize < 0 {
  151. result := db.Table(p.TableName()).Where(cond, val...).Order("created_at desc").Find(&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. offset := (page - 1) * pageSize
  162. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Order("created_at desc").Find(&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. }