// 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:SiteUser" json:"site_user" form:"site_user"` CreatedAt time.Time `gorm:"column:CreatedAt" json:"created_at" form:"created_at"` UpdatedAt time.Time `gorm:"column:UpdatedAt" json:"updated_at" form:"updated_at"` UserType int64 `gorm:"column:UserType" json:"user_type" form:"user_type"` Enable bool `gorm:"column:Enable"` ProjectId int64 `gorm:"column:ProjectId"` } func (TSiteUser) TableName() string { return "ProjectUser" } func (p *TSiteUser) Insert(db *gorm.DB) error { return db.Table(p.TableName()).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) 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 UserType int32 } 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 as id, t1.UserName as username, t1.Passwd as passwd, t1.UserType as user_type, t1.Enable as enable, t2.Name as project_name, t2.SafetyNo as safety_record_no "+ "from ProjectUser as t1 left join ProjectInfo as t2 on t1.ProjectId = t2.ID" where := " where t2.Cid = ?" if filter != "" { where = fmt.Sprintf(" %s and (t1.UserName like '%%%s%%' or t2.SafetyNo like '%%%s%%' or t2.Name like '%%%s%%')", where, filter, filter, filter) } sql = fmt.Sprintf("%s %s order by t1.CreatedAt desc limit %d offset %d", sql, where, PageSize, offset) countSql := fmt.Sprintf("select count(1) as count from ProjectUser as t1 left join ProjectInfo as t2 on t1.ProjectId = 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 }