123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package model
- import (
- "git.getensh.com/common/gopkgs/logger"
- "git.getensh.com/common/gopkgs/util"
- "go.uber.org/zap"
- "gorm.io/gorm"
- "time"
- )
- type TGarden struct {
- ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"`
- Province string `gorm:"column:province" json:"province"`
- ProvinceCode string `gorm:"column:province_code" json:"province_code"`
- City string `gorm:"column:city" json:"city"`
- CityCode string `gorm:"column:city_code" json:"city_code"`
- Area string `gorm:"column:area" json:"area"`
- AreaCode string `gorm:"column:area_code" json:"area_code"`
- Street string `gorm:"column:street" json:"street"`
- StreetCode string `gorm:"column:street_code" json:"street_code"`
- Committee string `gorm:"column:committee" json:"committee"`
- CommitteeCode string `gorm:"column:committee_code" json:"committee_code"`
- GardenName string `gorm:"column:garden_name" json:"garden_name"`
- GardenAddr string `gorm:"column:garden_addr" json:"garden_addr"`
- PropertyPerson string `gorm:"column:property_person" json:"property_person"`
- PropertyPhone string `gorm:"column:property_phone" json:"property_phone"`
- CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
- UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
- Cid int64 `gorm:"column:cid" json:"cid"`
- EndTimestamp int64 `gorm:"column:end_timestamp" json:"end_timestamp"`
- StartTimestamp int64 `gorm:"column:start_timestamp" json:"start_timestamp"`
- UniqFlag int64 `gorm:"column:uniq_flag" json:"uniq_flag"`
- GardenDesc string `gorm:"column:garden_desc" json:"garden_desc"`
- GardenPic string `gorm:"column:garden_pic" json:"garden_pic"`
- GardenPics string `gorm:"column:garden_pics" json:"garden_pics"`
- Lnt float64 `gorm:"column:lnt" json:"lnt"`
- Lat float64 `gorm:"column:lat" json:"lat"`
- Appendix string `gorm:"column:appendix" json:"appendix"`
- Status int32 `gorm:"column:status" json:"status"`
- ApprovedAt time.Time `gorm:"column:approved_at" json:"approved_at"`
- Feedback string `gorm:"column:feedback" json:"feedback"`
- BuildingType int32 `gorm:"column:building_type" json:"building_type"`
- BuildingStart int64 `gorm:"column:building_start" json:"building_start"`
- BuildingEnd int64 `gorm:"column:building_end" json:"building_end"`
- GreenPercent float64 `gorm:"column:green_percent" json:"green_percent"`
- AreaPercent float64 `gorm:"column:area_percent" json:"area_percent"`
- SpaceInfo string `gorm:"column:space_info" json:"space_info"`
- BuildingCompany string `gorm:"column:building_company" json:"building_company"`
- CoveredArea float64 `gorm:"column:covered_area" json:"covered_area"`
- BuildingArea float64 `gorm:"column:building_area" json:"building_area"`
- HouseTotal int64 `gorm:"column:house_total" json:"house_total"`
- SpaceTotal int64 `gorm:"column:space_total" json:"space_total"`
- PropertyFeeStart int64 `gorm:"column:property_fee_start" json:"property_fee_start"`
- PropertyFeeEnd int64 `gorm:"column:property_fee_end" json:"property_fee_end"`
- WaterType string `gorm:"column:water_type" json:"water_type"`
- ElectricType string `gorm:"column:electric_type" json:"electric_type"`
- GasFeeStart int64 `gorm:"column:gas_fee_start" json:"gas_fee_start"`
- GasFeeEnd int64 `gorm:"column:gas_fee_end" json:"gas_fee_end"`
- AvgPrice int64 `gorm:"column:avg_price" json:"avg_price"`
- }
- func (p *TGarden) TableName() string {
- return "t_garden"
- }
- func (p *TGarden) Find(db *gorm.DB, where map[string]interface{}) error {
- err := db.Table(p.TableName()).Where(where).Find(p).Error
- if err != nil {
- fields, _ := util.MarshalToString(where)
- logger.Error("mysql",
- zap.String("sql", "select from "+p.TableName()),
- zap.String("fields", fields),
- zap.String("error", err.Error()))
- }
- return err
- }
- func (p *TGarden) Last(db *gorm.DB) error {
- err := db.Table(p.TableName()).Last(p).Error
- if err != nil {
- logger.Error("mysql",
- zap.String("sql", "select last from "+p.TableName()),
- zap.String("fields", ""),
- zap.String("error", err.Error()))
- }
- return err
- }
- // Insert 插入一条记录
- func (p *TGarden) Insert(db *gorm.DB) error {
- err := db.Table(p.TableName()).Create(p).Error
- if err != nil {
- fields, _ := util.MarshalToString(*p)
- logger.Error("mysql",
- zap.String("sql", "insert into "+p.TableName()),
- zap.String("fields", fields),
- zap.String("error", err.Error()))
- }
- return err
- }
- func (p *TGarden) Delete(db *gorm.DB, filter map[string]interface{}) error {
- cond, val, err := whereBuild(filter)
- if err != nil {
- return err
- }
- return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
- }
- func (p *TGarden) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
- cond, val, err := whereBuild(where)
- if err != nil {
- if err != nil {
- fields, _ := util.MarshalToString(values)
- logger.Error("mysql",
- zap.String("sql", "update "+p.TableName()),
- zap.String("fields", fields),
- zap.String("error", err.Error()))
- }
- return err
- }
- return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
- }
- func (p *TGarden) UpdateByModel(db *gorm.DB) error {
- err := db.Model(p).Updates(p).Error
- if err != nil {
- fields, _ := util.MarshalToString(*p)
- logger.Error("mysql",
- zap.String("sql", "update "+p.TableName()),
- zap.String("fields", fields),
- zap.String("error", err.Error()))
- }
- return err
- }
- func (p *TGarden) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
- cond, val, err := whereBuildAndOr(where, or)
- if err != nil {
- return 0, err
- }
- ret := int64(0)
- err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
- if err != nil {
- fields, _ := util.MarshalToString(where)
- logger.Error("mysql",
- zap.String("sql", "select count "+p.TableName()),
- zap.String("fields", fields),
- zap.String("error", err.Error()))
- }
- return ret, err
- }
- func (p *TGarden) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TGarden, err error) {
- cond, val, err := whereBuildAndOr(where, or)
- if err != nil {
- return list, err
- }
- if pageSize < 0 {
- result := db.Table(p.TableName()).Where(cond, val...).Order("created_at desc").Find(&list)
- if result.Error != nil {
- wherefields, _ := util.MarshalToString(where)
- logger.Error("mysql",
- zap.String("sql", "select * from "+p.TableName()),
- zap.String("where", wherefields),
- zap.String("error", result.Error.Error()))
- }
- return list, result.Error
- }
- offset := (page - 1) * pageSize
- result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Order("created_at desc").Find(&list)
- if result.Error != nil {
- wherefields, _ := util.MarshalToString(where)
- logger.Error("mysql",
- zap.String("sql", "select * from "+p.TableName()),
- zap.String("where", wherefields),
- zap.String("error", result.Error.Error()))
- }
- return list, result.Error
- }
|