Cron_Admin/app/services/user_service.go

53 lines
1.4 KiB
Go

package services
import (
"cron_admin/app/constants/common"
"cron_admin/app/http/entities/backend"
"cron_admin/app/models/crondbmodel"
"cron_admin/app/models/cronusermodel"
"cron_admin/app/utils/mapstructure"
"xorm.io/builder"
)
func GetListByWhere(request *backend.UserListRequest, page int, limit int) (count int64, UserListInfo []cronusermodel.CronUser, err error) {
cond := builder.NewCond()
if request.Tel != "" {
cond = cond.And(builder.Like{"tel", request.Tel})
}
if request.Status != 0 {
cond = cond.And(builder.Eq{"status": request.Status})
}
if len(request.UserIds) > 0 {
// 使用IN查询
cond = cond.And(builder.In("user_id", request.UserIds))
}
//model := repository.NewCommonRepo[userinfomodel.CronUserModel](userinfomodel.GetInstance().GetDb().NewSession())
session := cronusermodel.GetInstance().GetDb().Where(cond)
if page != 0 && limit != 0 {
session = session.Limit(limit, (page-1)*limit)
}
count, err = session.FindAndCount(&UserListInfo)
if err != nil {
return
}
return
}
func UserAdd(request *backend.UserAddRequest) (err error) {
var db crondbmodel.CronDb
_ = mapstructure.Decode(request, &db)
db.Status = common.STATUS_ENABLE
_, err = crondbmodel.GetInstance().GetDb().InsertOne(db)
return
}
func UserEdit(request *backend.UserEditRequest) (err error) {
var db crondbmodel.CronDb
_ = mapstructure.Decode(request, &db)
_, err = crondbmodel.GetInstance().GetDb().ID(request.UserId).Update(&db)
return
}