Cron_Admin/app/services/user_service/user_service.go

54 lines
1.5 KiB
Go

package user_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})
}
//model := repository.NewCommonRepo[userinfomodel.CronUserModel](userinfomodel.GetInstance().GetDb().NewSession())
session := cronusermodel.GetInstance().GetDb().Where(cond)
if page != 0 && limit != 0 {
session = session.Limit(page, (page-1)*limit)
}
count, err = session.FindAndCount(&UserListInfo)
if err != nil {
return
}
return
}
func UserAdd(request *backend.UserAddRequest) (err error) {
var db cronusermodel.CronUser
_ = mapstructure.Decode(request, &db)
db.Status = common.STATUS_ENABLE
_, err = cronusermodel.GetInstance().GetDb().InsertOne(db)
return
}
func UserEdit(request *backend.UserEditRequest) (err error) {
var db cronusermodel.CronUser
_ = mapstructure.Decode(request, &db)
_, err = cronusermodel.GetInstance().GetDb().ID(request.UserId).Update(&db)
return
}
func UserDel(request *backend.UserDeleteRequest) (err error) {
_, err = crondbmodel.GetInstance().GetDb().ID(request.UserId).Delete(&cronusermodel.CronUser{})
return
}