123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- // 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 TUserGroup struct {
- ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"`
- Cid int64 `gorm:"column:cid" json:"cid"`
- GardenId int64 `gorm:"column:garden_id" json:"garden_id"`
- Phone string `gorm:"column:phone" json:"phone"`
- Email string `gorm:"column:email" json:"email"`
- User string `gorm:"column:user" json:"user"`
- Password string `gorm:"column:password" json:"password"`
- //Enable int64 `gorm:"column:enable" json:"enable"`
- GardenName string `gorm:"column:garden_name" json:"garden_name"`
- CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
- UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
- GroupId int64 `gorm:"column:group_id" json:"group_id"`
- GroupName string `gorm:"column:group_name" json:"group_name"`
- GroupDesc string `gorm:"column:group_desc" json:"group_desc"`
- IsSuper int64 `gorm:"column:is_super" json:"is_super"`
- PermissionCodes string `gorm:"column:permission_codes" json:"permission_codes"`
- BasePermission int32 `gorm:"column:base_permission" json:"base_permission"`
- RealName string `gorm:"column:real_name" json:"real_name"`
- DepartmentId int64 `gorm:"column:department_id" json:"department_id"`
- DepartmentName string `gorm:"column:department_name" json:"department_name"`
- OpenimId string `gorm:"column:openim_id" json:"openim_id"`
- }
- func (p *TUserGroup) TableName() string {
- return "t_user_group"
- }
- func (p *TUserGroup) 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 *TUserGroup) 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
- }
- func (p *TUserGroup) 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 *TUserGroup) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TUserGroup, 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
- }
|