// Copyright 2019 getensh.com. All rights reserved. // Use of this source code is governed by getensh.com. package model import ( "cp-organization-management/errors" "fmt" "time" "github.com/jaryhe/gopkgs/logger" "github.com/jaryhe/gopkgs/util" "github.com/jinzhu/gorm" "go.uber.org/zap" ) type RbacNode struct { Id int64 `json:"id" gorm:"primary_key"` Name string `json:"name" gorm:"name;comment:'节点名';not null;default:''"` Router string `json:"router" gorm:"router;comment:'节点路由';not null;default:''"` Method string `json:"method" gorm:"method;comment:'节点方法';not null;default:''"` Resource string `json:"resource" gorm:"resource;comment:'节点资源';not null;default:''"` Object string `json:"object" gorm:"object;comment:'节点对象';not null;default:''"` Pid int64 `json:"pid" gorm:"pid;comment:'父节点';not null;default:0"` CreatedAt time.Time `json:"created_at" gorm:"created_at"` UpdatedAt time.Time `json:"updated_at" gorm:"updated_at"` IsSuper bool `json:"is_super" gorm:"is_super"` table string `json:"table"` } func (p *RbacNode) TableName() string { return p.table } func NewRbacNode(database string) *RbacNode { return &RbacNode{table: fmt.Sprintf("%s.%s", database, "t_rbac_node")} } func (p *RbacNode) CreateTable(db *gorm.DB) error { err := db.CreateTable(p).Error if err != nil { logger.Error("mysql", zap.String("sql", "create table "+p.TableName()), zap.String("fields", ""), zap.String("error", err.Error())) } return nil } func (p *RbacNode) 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 *RbacNode) 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 *RbacNode) Insert(db *gorm.DB) error { timeNow := time.Now() p.CreatedAt = timeNow p.UpdatedAt = timeNow 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 *RbacNode) Delete(db *gorm.DB, filter map[string]interface{}) error { err := db.Where(filter).Delete(p).Error if err != nil { fields, _ := util.MarshalToString(filter) logger.Error("mysql", zap.String("sql", "delete from "+p.TableName()), zap.String("fields", fields), zap.String("error", err.Error())) } return err } func (p *RbacNode) Save(db *gorm.DB) error { p.UpdatedAt = time.Now() err := db.Save(p).Error if err != nil { fields, _ := util.MarshalToString(*p) logger.Error("mysql", zap.String("sql", "save "+p.TableName()), zap.String("fields", fields), zap.String("error", err.Error())) } return err } func (p *RbacNode) Update(db *gorm.DB) error { p.UpdatedAt = time.Now() 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 } // 通过结构体变量更新字段值, gorm库会忽略零值字段。就是字段值等于0, nil, "", false这些值会被忽略掉,不会更新。如果想更新零值,可以使用map类型替代结构体。 func (p *RbacNode) UpdateSome(db *gorm.DB, filed map[string]interface{}) error { if filed == nil { return errors.ParamsError } filed["updated_at"] = time.Now() err := db.Model(p).Updates(filed).Error if err != nil { fields, _ := util.MarshalToString(filed) logger.Error("mysql", zap.String("sql", "update "+p.TableName()), zap.String("fields", fields), zap.String("error", err.Error())) } return err } func (p *RbacNode) QueryAll(db *gorm.DB, filter map[string]interface{}, out interface{}) error { err := db.Table(p.TableName()).Where(filter).Find(out).Error if err != nil { fields, _ := util.MarshalToString(filter) logger.Error("mysql", zap.String("sql", "select from "+p.TableName()), zap.String("fields", fields), zap.String("error", err.Error())) } return err } func (p *RbacNode) List(db *gorm.DB, limit, offset int, out interface{}) error { err := db.Table(p.TableName()).Limit(limit).Offset(offset).Find(out).Error if err != nil { logger.Error("mysql", zap.String("sql", "select from "+p.TableName()), zap.String("fields", ""), zap.String("error", err.Error())) } return err } func (p *RbacNode) Count(db *gorm.DB) (int64, error) { var count int64 err := db.Table(p.TableName()).Count(&count).Error if err != nil { logger.Error("mysql", zap.String("sql", "select count from "+p.TableName()), zap.String("fields", ""), zap.String("error", err.Error())) } return count, err } func (p *RbacNode) ListAll(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (list []RbacNode, err error) { if len(where) > 0 || len(or) > 0 { cond, val, err := whereBuildAndOr(where, or) if err != nil { return list, err } err = db.Table(p.TableName()).Where(cond, val...).Find(&list).Error if err != nil { logger.Error("mysql", zap.String("sql", "select * from "+p.TableName()), zap.String("error", err.Error())) return list, errors.DataBaseError } return list, err } err = db.Table(p.TableName()).Find(&list).Error if err != nil { logger.Error("mysql", zap.String("sql", "select * from "+p.TableName()), zap.String("error", err.Error())) return list, errors.DataBaseError } return list, nil }