// 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 TMsgPackageOrder struct { ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"` MsgCount int64 `gorm:"column:msg_count" json:"msg_count"` Name string `gorm:"column:name" json:"name"` Comment string `gorm:"column:comment" json:"comment"` CreatedAt time.Time `gorm:"column:created_at" json:"created_at"` UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"` PackageId int64 `gorm:"column:package_id" json:"package_id"` GardenId int64 `gorm:"column:garden_id" json:"garden_id"` Price int64 `gorm:"column:price" json:"price"` PackageCount int64 `gorm:"column:package_count" json:"package_count"` Amount int64 `gorm:"column:amount" json:"amount"` Status int32 `gorm:"column:status" json:"status"` Feedback string `gorm:"column:feedback" json:"feedback"` MsgTotalCount int64 `gorm:"column:msg_total_count" json:"msg_total_count"` MsgAvailableCount int64 `gorm:"column:msg_available_count" json:"msg_available_count"` Cid int64 `gorm:"column:cid" json:"cid"` } func (p *TMsgPackageOrder) TableName() string { return "t_msg_package_order" } func (p *TMsgPackageOrder) 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 *TMsgPackageOrder) 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 *TMsgPackageOrder) 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 *TMsgPackageOrder) 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 *TMsgPackageOrder) 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 *TMsgPackageOrder) 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 *TMsgPackageOrder) 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 *TMsgPackageOrder) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int, order string) (list []TMsgPackageOrder, 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(order).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(order).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 } func (p *TMsgPackageOrder) ListColums(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, columns []string) (list []TMsgPackageOrder, err error) { cond, val, err := whereBuildAndOr(where, or) if err != nil { return list, err } result := db.Table(p.TableName()).Where(cond, val...).Select(columns).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 }