123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- // 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"
- "encoding/json"
- )
- type RbacUser struct {
- Id int64 `json:"id" gorm:"primary_key"`
- Username string `json:"username" gorm:"username;comment:'用户名';not null;default:''"`
- Passowrd string `json:"passowrd" gorm:"passowrd;comment:'密码';not null;default:''"`
- Phone string `json:"phone" gorm:"phone;commnet:'手机号';not null;default:''"`
- Email string `json:"email" gorm:"email;commnet:'电子邮件';not null;default:''"`
- IsSuperUser bool `json:"is_super_user" gorm:"is_super_user;commnet:'是否是超级账号,超级账号不能删除';not null;default:0"`
- GroupId int64 `json:"group_id" gorm:"group_id;commnet:'权限组id';not null;default:0"`
- //Zones string `json:"zones" gorm:"zones;commnet:'管辖区域列表';not null;default:''"`
- CreatedAt time.Time `json:"created_at" gorm:"created_at"`
- UpdatedAt time.Time `json:"updated_at" gorm:"updated_at"`
- Name string `json:"name" gorm:"name"`
- Pids string `json:"pids" gorm:"pids"`
- table string `json:"table"`
- }
- func (p *RbacUser) TableName() string {
- return p.table
- }
- func NewRbacUser(database string) *RbacUser {
- return &RbacUser{table: fmt.Sprintf("%s.%s", database, "t_rbac_user")}
- }
- func (p *RbacUser) 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 *RbacUser) 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 *RbacUser) 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 *RbacUser) 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 *RbacUser) 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 *RbacUser) 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 *RbacUser) 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 *RbacUser) 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 *RbacUser) UpdateWithAffectRows(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) (int64, error) {
- cond, val, err := whereBuild(where)
- if err != nil {
- return 0, err
- }
- r := db.Table(p.TableName()).Where(cond, val...).Updates(values)
- if r.Error != nil {
- fields, _ := json.Marshal(where)
- updateValues, _ := json.Marshal(values)
- logger.Error("mysql",
- zap.String("sql", "update "+p.TableName()),
- zap.String("fields", string(fields)),
- zap.String("values", string(updateValues)),
- zap.String("error", err.Error()))
- return 0, errors.DataBaseError
- }
- return r.RowsAffected, err
- }
- func (p *RbacUser) 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 *RbacUser) 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 *RbacUser) Count(db *gorm.DB, where map[string]interface{}) (int64, error) {
- if len(where) > 0 {
- cond, val, err := whereBuild(where)
- if err != nil {
- return 0, err
- }
- ret := int64(0)
- err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
- if err != nil {
- fields, _ := json.Marshal(where)
- logger.Error("mysql",
- zap.String("sql", "select count from "+p.TableName()),
- zap.String("fields", string(fields)),
- zap.String("error", err.Error()))
- return ret, errors.DataBaseError
- }
- return ret, err
- }
- ret := int64(0)
- err := db.Table(p.TableName()).Count(&ret).Error
- if err != nil {
- fields, _ := json.Marshal(where)
- logger.Error("mysql",
- zap.String("sql", "select count from "+p.TableName()),
- zap.String("fields", string(fields)),
- zap.String("error", err.Error()))
- return ret, errors.DataBaseError
- }
- return ret, err
- }
- func (p *RbacUser) ListNew(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, limit, offset int64) (list []RbacUser, 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...).Limit(limit).Offset(offset).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
- }
|