123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- // 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"
- "go.uber.org/zap"
- "smart-site-management/errors"
- "time"
- "github.com/jaryhe/gopkgs/logger"
- "encoding/json"
- )
- 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"`
- Phone string
- Email string
- }
- func (TSiteUser) TableName() string {
- return "t_site_user"
- }
- func (p *TSiteUser) Insert(db *gorm.DB) error {
- err := db.Create(p).Error
- if err != nil {
- fields, _ := json.Marshal(p)
- logger.Error("mysql",
- zap.String("sql", "insert into t_site_user"),
- zap.String("fields", string(fields)),
- zap.String("error", err.Error()))
- return errors.DataBaseError
- }
- return err
- }
- 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
- }
- err = db.Table(p.TableName()).Where(cond, val...).First(p).Error
- if err != nil {
- fields, _ := json.Marshal(val)
- logger.Error("mysql",
- zap.String("sql", "select from t_site_user"),
- zap.String("fields", cond +","+string(fields)),
- zap.String("error", err.Error()))
- if err == gorm.ErrRecordNotFound {
- return nil
- }
- return errors.DataBaseError
- }
- return err
- }
- 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
- }
- err = db.Table(p.TableName()).Table(p.TableName()).Where(cond, val...).Updates(values).Error
- if err != nil {
- fields, _ := json.Marshal(where)
- updateValues, _ := json.Marshal(values)
- logger.Error("mysql",
- zap.String("sql", "update t_site_user"),
- zap.String("fields",string(fields)),
- zap.String("values",string(updateValues)),
- zap.String("error", err.Error()))
- return errors.DataBaseError
- }
- return err
- }
- 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 {
- logger.Error("mysql",
- zap.String("sql", countSql),
- zap.String("fields",fmt.Sprintf("%d", cid)),
- zap.String("error", err.Error()))
- 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 {
- logger.Error("mysql",
- zap.String("sql", sql),
- zap.String("fields",fmt.Sprintf("%d", cid)),
- zap.String("error", err.Error()))
- }
- return ret, array[0].Count, nil
- }
|