123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- // 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 TVehicle struct {
- ID int64 `gorm:"column:id;PRIMARY_EKY" json:"id"`
- PlateNo string `gorm:"column:plate_no" json:"plate_no"`
- VehicleType int32 `gorm:"column:vehicle_type" json:"vehicle_type"`
- Vin string `gorm:"column:vin" json:"vin"`
- EngineNo string `gorm:"column:engine_no" json:"engine_no"`
- Brand string `gorm:"column:brand" json:"brand"`
- Color string `gorm:"column:color" json:"color"`
- SpaceId int64 `gorm:"column:space_id" json:"space_id"`
- ParkId int64 `gorm:"column:park_id" json:"park_id"`
- HouseholdUid int64 `gorm:"column:household_uid" json:"household_uid"`
- BindType int32 `gorm:"column:bind_type" json:"bind_type"`
- Displacement float64 `gorm:"column:displacement" json:"displacement"`
- DisplacementUnit string `gorm:"column:displacement_unit" json:"displacement_unit"`
- Seat int32 `gorm:"column:seat" json:"seat"`
- CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
- UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
- table string
- }
- func (p *TVehicle) TableName() string {
- return p.table
- }
- func NewVehicle(database string) *TVehicle {
- return &TVehicle{table: fmt.Sprintf("%s.%s", database, "t_vehicle")}
- }
- func (p *TVehicle) SetTable(database string) {
- p.table = fmt.Sprintf("%s.%s", database, "t_vehicle")
- }
- func (p *TVehicle) CreateTable(db *gorm.DB) error {
- sql :="CREATE TABLE IF NOT EXISTS "+p.TableName()+"("+
- "`id` int(10) NOT NULL AUTO_INCREMENT,"+
- "`plate_no` varchar(32) COLLATE utf8mb4_bin NOT NULL,"+
- " `vehicle_type` tinyint(1) NOT NULL COMMENT '1 小型车 2 大型车 3 新能源小车 4 新能源大车',"+
- "`vin` varchar(32) COLLATE utf8mb4_bin NOT NULL COMMENT '车架号',"+
- "`engine_no` varchar(32) COLLATE utf8mb4_bin NOT NULL COMMENT '发动机号',"+
- "`brand` varchar(64) COLLATE utf8mb4_bin NOT NULL COMMENT '品牌',"+
- "`color` varchar(16) COLLATE utf8mb4_bin NOT NULL COMMENT '颜色',"+
- "`space_id` int(10) NOT NULL COMMENT '停车位',"+
- "`park_id` int(10) NOT NULL COMMENT '停车场',"+
- "`household_uid` bigint(11) NOT NULL COMMENT '业主id',"+
- "`bind_type` tinyint(1) NOT NULL COMMENT '1 无固定车位 2 买车位 3 租车位',"+
- "`displacement` decimal(18,2) COLLATE utf8mb4_bin NOT NULL COMMENT '排量(费用配置可能按排量)',"+
- "`displacement_unit` varchar(1) NOT NULL COMMENT 'L/T(费用配置可能按排量)',"+
- "`seat` int(10) NOT NULL COMMENT '座位数 (费用配置可能按座位数)',"+
- "`created_at` datetime NOT NULL,"+
- " `updated_at` datetime NOT NULL,"+
- "PRIMARY KEY (`id`),"+
- " UNIQUE KEY `plate_no` (`plate_no`) USING BTREE,"+
- " KEY `household_uid` (`household_uid`) USING BTREE,"+
- " KEY `space_id` (`space_id`) USING BTREE,"+
- " KEY `park_id` (`park_id`) USING BTREE"+
- ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;"
- err := db.Exec(sql).Error
- if err != nil {
- logger.Error("mysql",
- zap.String("sql", "create table "+p.TableName()),
- zap.String("fields", ""),
- zap.String("error", err.Error()))
- }
- return err
- }
- func (p *TVehicle) 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 *TVehicle) 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 *TVehicle) 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
- }
- // Insert 插入多条记录
- func (p *TVehicle) 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 *TVehicle) 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 *TVehicle) 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 *TVehicle) UpdateByModel(db *gorm.DB) error {
- err := db.Table(p.TableName()).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 *TVehicle) 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 *TVehicle) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TVehicle, 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
- }
|