// Copyright 2019 getensh.com. All rights reserved. // Use of this source code is governed by getensh.com. package model import ( "fmt" "git.getensh.com/common/gopkgs/logger" "git.getensh.com/common/gopkgs/util" "go.uber.org/zap" "gorm.io/gorm" "time" ) type THouseUnitBuilding struct { ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"` BuildingId int64 `gorm:"column:building_id" json:"building_id"` UnitId int64 `gorm:"column:unit_id" json:"unit_id"` HouseNumber string `gorm:"column:house_number" json:"house_number"` Layer int64 `gorm:"column:layer" json:"layer"` RoomCount int64 `gorm:"column:room_count" json:"room_count"` HallCount int64 `gorm:"column:hall_count" json:"hall_count"` HouseType int64 `gorm:"column:house_type" json:"house_type"` HouseArea float64 `gorm:"column:house_area" json:"house_area"` HouseUsedArea float64 `gorm:"column:house_used_area" json:"house_used_area"` HasLift int64 `gorm:"column:has_lift" json:"has_lift"` CreatedAt time.Time `gorm:"column:created_at" json:"created_at"` UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"` Status int `gorm:"column:status" json:"status"` BuildingNumber string `gorm:"column:building_number" json:"building_number"` UnitNumber int64 `gorm:"column:unit_number" json:"unit_number"` table string } func (p *THouseUnitBuilding) TableName() string { return p.table } func NewHouseUnitBuilding(database string) *THouseUnitBuilding { return &THouseUnitBuilding{table: fmt.Sprintf("%s.%s", database, "t_house_unit_building")} } func (p *THouseUnitBuilding) CreateView(db *gorm.DB, database string) error { sql := "CREATE VIEW "+database+".`t_house_unit_building` AS select t1.*,"+ "`t2`.`unit_number` AS `unit_number`,`t3`.`building_number` AS `building_number` from "+ "(("+database+".`t_house` `t1` left join "+database+".`t_unit` `t2` on((`t2`.`id` = `t1`.`unit_id`))) left join "+database+".`t_building` `t3` on((`t3`.`id` = `t2`.`building_id`)))" err := db.Exec(sql).Error if err != nil { logger.Error("mysql", zap.String("sql", "create view "+p.TableName()), zap.String("fields", ""), zap.String("error", err.Error())) } return err } func (p *THouseUnitBuilding) 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 *THouseUnitBuilding) 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 *THouseUnitBuilding) Insert(db *gorm.DB) error { err := db.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 *THouseUnitBuilding) InsertMulti(db *gorm.DB, values interface{}) error { err := db.Table(p.TableName()).Create(&values).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 *THouseUnitBuilding) 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 *THouseUnitBuilding) 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 *THouseUnitBuilding) 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 *THouseUnitBuilding) 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 *THouseUnitBuilding) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []THouseUnitBuilding, 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 }