123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- // 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 TSpacePark struct {
- ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"`
- SpaceNumber string `gorm:"column:space_number" json:"space_number"`
- SpaceType int32 `gorm:"column:space_type" json:"space_type"`
- SpaceArea float64 `gorm:"column:space_area" json:"space_area"`
- Comment string `gorm:"column:comment" json:"comment"`
- ParkId int64 `gorm:"column:park_id" json:"park_id"`
- GardenId int64 `gorm:"column:garden_id" json:"garden_id"`
- CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
- UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
- ParkNumber string `gorm:"column:park_number" json:"park_number"`
- ParkName string `gorm:"column:park_name" json:"park_name"`
- ParkType int32 `gorm:"column:park_type" json:"park_type"`
- BindStatus int32 `gorm:"column:bind_status" json:"bind_status"`
- VehicleId int64 `gorm:"column:vehicle_id" json:"vehicle_id"`
- HouseId int64 `gorm:"column:house_id" json:"house_id"`
- Status int32 `gorm:"column:status" json:"status"`
- table string
- }
- func (p *TSpacePark) TableName() string {
- return p.table
- }
- func NewSpacePark(database string) *TSpacePark {
- return &TSpacePark{table: fmt.Sprintf("%s.%s", database, "t_space_park")}
- }
- func (p *TSpacePark) CreateView(db *gorm.DB, database string) error {
- sql := "CREATE VIEW "+database+".`t_space_park` AS select t1.*,"+
- "`t2`.`park_number` AS `park_number`,`t2`.`park_name` AS `park_name`,`t2`.`park_type` AS `park_type` from "+
- "("+database+".`t_park_space` `t1` left join "+database+".`t_park` `t2` on((`t1`.`park_id` = `t2`.`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 *TSpacePark) Find(db *gorm.DB, where map[string]interface{}) error {
- err := db.Table(p.TableName()).Where(where).First(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 *TSpacePark) 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 *TSpacePark) 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 *TSpacePark) 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 *TSpacePark) 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 *TSpacePark) 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 *TSpacePark) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TSpacePark, 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
- }
|