// Copyright 2019 github.com. All rights reserved. // Use of this source code is governed by github.com. package model import ( "fmt" "github.com/jinzhu/gorm" "time" ) type TSiteUser struct { Id int64 `gorm:"column:id" json:"id" form:"id"` Username string `gorm:"column:username" json:"username" form:"username"` Passwd string `gorm:"column:passwd" json:"passwd" form:"passwd"` SiteUser int64 `gorm:"column:site_user" json:"site_user" form:"site_user"` CreatedAt time.Time `gorm:"column:created_at" json:"created_at" form:"created_at"` UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at" form:"updated_at"` UserType int64 `gorm:"column:user_type" json:"user_type" form:"user_type"` Enable bool `json:"enable"` ProjectId int64 `json:"project_id"` } func (TSiteUser) TableName() string { return "t_site_user" } func (p *TSiteUser) Insert(db *gorm.DB) error { return db.Create(p).Error } func (p *TSiteUser) Find(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) error { cond, val, err := whereBuildAndOr(where, or) if err != nil { return err } return db.Table(p.TableName()).Where(cond, val...).First(p).Error } func (p *TSiteUser) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error { cond, val, err := whereBuild(where) if err != nil { return err } return db.Table(p.TableName()).Table(p.TableName()).Where(cond, val...).Updates(values).Error } func (p *TSiteUser) FindSort(db *gorm.DB, where map[string]interface{}, sort string) error { cond, val, err := whereBuild(where) if err != nil { return err } ps := []TSiteUser{} err = db.Table(p.TableName()).Where(cond, val...).Order(sort).Limit(1).Find(&ps).Error if err != nil { return err } if len(ps) > 0 { *p = ps[0] } return nil } func (p *TSiteUser) Save(db *gorm.DB) error { return db.Save(p).Error } func (p *TSiteUser) List(db *gorm.DB, where map[string]interface{}, page int) (list []TSiteUser, err error) { if len(where) > 0 { cond, val, err := whereBuild(where) if err != nil { return list, err } result := db.Table(p.TableName()).Where(cond, val...).Limit(PageSize).Offset(page).Find(&list) return list, result.Error } result := db.Table(p.TableName()).Limit(10).Offset(page).Find(&list) return list, result.Error } func (p *TSiteUser) All(db *gorm.DB) (list []TCompany, err error) { result := db.Table(p.TableName()).Find(&list) return list, result.Error } func (p *TSiteUser) 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 return ret, err } ret := int64(0) err := db.Table(p.TableName()).Count(&ret).Error return ret, err } func (p *TSiteUser) Del(db *gorm.DB, where map[string]interface{}) error { cond, val, err := whereBuild(where) if err != nil { return err } return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error } type ProjectUserItem struct { Id int64 Username string Passwd string ProjectName string SafetyRecordNo string Enable bool } func (p *TSiteUser) ListWithProject(db *gorm.DB, filter string, cid int64, page int32) ([]ProjectUserItem, int64, error) { type ResultCount struct { Count int64 } offset := (page - 1) * int32(PageSize) sql := "select t1.id, t1.username, t1.passwd, t1.enable, t2.name as project_name, t2.safety_record_no "+ "from t_site_user as t1 left join t_project as t2 on t1.project_id = t2.id" where := " where t2.cid = ?" if filter != "" { where = fmt.Sprintf(" and (t1.username like '%%%s%%' or t2.safety_record_no like '%%%s%%' or t2.name like '%%%s%%')", filter, filter, filter) sql = sql + where } sql = fmt.Sprintf("%s limit %d offset %d", sql, PageSize, offset) countSql := fmt.Sprintf("select count(1) as count from t_site_user as t1 left join t_project as t2 on t1.project_id = t2.id %s", where) array := []ResultCount{} err := db.Raw(countSql, cid).Scan(&array).Error if err != nil { return nil, 0, err } if len(array) == 0 { return nil, 0, nil } if array[0].Count == 0 { return nil, 0, nil } ret := []ProjectUserItem{} err = db.Raw(sql, cid).Scan(&ret).Error if err != nil && err != gorm.ErrRecordNotFound { return nil, 0, err } return ret, array[0].Count, nil }