// 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 TEvent struct { ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"` Pic string `gorm:"column:pic" json:"pic"` Content string `gorm:"column:content" json:"content"` Title string `gorm:"column:title" json:"title"` Start int64 `gorm:"column:start" json:"start"` End int64 `gorm:"column:end" json:"end"` SignUpEnd int64 `gorm:"column:sign_up_end" json:"sign_up_end"` PeopleLimit int64 `gorm:"column:people_limit" json:"people_limit"` Fee int64 `gorm:"column:fee" json:"fee"` Addr string `gorm:"column:addr" json:"addr"` PeopleCount int64 `gorm:"column:people_count" json:"people_count"` CreatedAt time.Time `gorm:"column:created_at" json:"created_at"` UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"` table string } func (p *TEvent) TableName() string { return p.table } func NewEvent(database string) *TEvent { return &TEvent{table: fmt.Sprintf("%s.%s", database, "t_event")} } func (p *TEvent) SetTable(database string) { p.table = fmt.Sprintf("%s.%s", database, "t_event") } func (p *TEvent) CreateTable(db *gorm.DB) error { sql :="CREATE TABLE IF NOT EXISTS "+p.TableName()+"("+ " `id` int(11) NOT NULL AUTO_INCREMENT,"+ " `pic` text COLLATE utf8mb4_bin NOT NULL COMMENT '图片',"+ "`content` text COLLATE utf8mb4_bin NOT NULL COMMENT '活动内容',"+ "`title` varchar(255) COLLATE utf8mb4_bin NOT NULL COMMENT '标题',"+ "`start` bigint(11) NOT NULL COMMENT '活动开始时间',"+ "`end` bigint(11) NOT NULL COMMENT '活动结束时间',"+ "`sign_up_end` bigint(11) NOT NULL COMMENT '报名截止时间',"+ "`people_limit` int(10) NOT NULL COMMENT '报名人数限制',"+ "`fee` int(10) NOT NULL COMMENT '报名费用',"+ "`addr` varchar(512) COLLATE utf8mb4_bin NOT NULL COMMENT '活动地址',"+ "`people_count` int(10) DEFAULT NULL COMMENT '已报名人数',"+ "`created_at` datetime NOT NULL,"+ " `updated_at` datetime NOT NULL,"+ " PRIMARY KEY (`id`)"+ ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='社区活动';" 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 *TEvent) 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 *TEvent) FindForUpdate(db *gorm.DB, where map[string]interface{}) error { err := db.Table(p.TableName()).Set("gorm:query_option", "FOR UPDATE").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 *TEvent) 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 *TEvent) 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 *TEvent) 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 *TEvent) 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 *TEvent) 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 *TEvent) 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 *TEvent) 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 *TEvent) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TEvent, 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...).Order("created_at desc").Limit(pageSize).Offset(offset).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 }