You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
162 lines
4.2 KiB
162 lines
4.2 KiB
package service
|
|
|
|
import (
|
|
"bkb-seller/global"
|
|
"bkb-seller/model"
|
|
"bkb-seller/model/request"
|
|
"encoding/json"
|
|
"errors"
|
|
)
|
|
|
|
//@author: [piexlmax](https://github.com/piexlmax)
|
|
//@function: CreateUser
|
|
//@description: 创建User记录
|
|
//@param: user model.User
|
|
//@return: err error
|
|
|
|
func CreateUser(user model.User) (err error) {
|
|
err = global.MG_DB.Create(&user).Error
|
|
return err
|
|
}
|
|
|
|
//@author: [piexlmax](https://github.com/piexlmax)
|
|
//@function: DeleteUser
|
|
//@description: 删除User记录
|
|
//@param: user model.User
|
|
//@return: err error
|
|
|
|
func DeleteUser(user model.User) (err error) {
|
|
err = global.MG_DB.Delete(&user).Error
|
|
return err
|
|
}
|
|
|
|
//@author: [piexlmax](https://github.com/piexlmax)
|
|
//@function: DeleteUserByIds
|
|
//@description: 批量删除User记录
|
|
//@param: ids request.IdsReq
|
|
//@return: err error
|
|
|
|
func DeleteUserByIds(ids request.IdsReq) (err error) {
|
|
err = global.MG_DB.Delete(&[]model.User{}, "id in (?)", ids.Ids).Error
|
|
return err
|
|
}
|
|
|
|
//@author: [piexlmax](https://github.com/piexlmax)
|
|
//@function: UpdateUser
|
|
//@description: 更新User记录
|
|
//@param: user *model.User
|
|
//@return: err error
|
|
|
|
func UpdateUser(user model.User) (err error) {
|
|
err = global.MG_DB.Updates(&user).Error
|
|
return err
|
|
}
|
|
|
|
//@author: [piexlmax](https://github.com/piexlmax)
|
|
//@function: GetUser
|
|
//@description: 根据id获取User记录
|
|
//@param: id uint
|
|
//@return: err error, user model.User
|
|
|
|
func GetUser(uuid string) (err error, user model.UserSimple) {
|
|
var (
|
|
dictData []model.SysDictData
|
|
)
|
|
err = global.MG_DB.Model(&model.User{}).Where("uuid=?", uuid).Find(&user).Error
|
|
if err != nil {
|
|
return errors.New("获取用户失败"), user
|
|
}
|
|
err = global.MG_DB.Model(&model.SysDictData{}).Where("type_code=?", "release_channel").Find(&dictData).Error
|
|
if err != nil {
|
|
return errors.New("获取用户失败"), user
|
|
}
|
|
platformsC := make([]model.Platform, 0)
|
|
for i := 0; i < len(dictData); i++ {
|
|
platformsC = append(platformsC, model.Platform{
|
|
Platform: dictData[i].Value,
|
|
PlatformName: dictData[i].Label,
|
|
})
|
|
}
|
|
if user.Platform != "" {
|
|
var platforms []model.Platform
|
|
err = json.Unmarshal([]byte(user.Platform), &platforms)
|
|
for i := 0; i < len(platforms); i++ {
|
|
for j := 0; j < len(platformsC); j++ {
|
|
if platforms[i].Platform == platformsC[j].Platform {
|
|
platformsC[j].IsAuth = platforms[i].IsAuth
|
|
platformsC[j].Url = platforms[i].Url
|
|
platformsC[j].Image = platforms[i].Image
|
|
}
|
|
}
|
|
}
|
|
user.Platforms = platformsC
|
|
} else {
|
|
user.Platforms = platformsC
|
|
}
|
|
return
|
|
}
|
|
|
|
//@author: [piexlmax](https://github.com/piexlmax)
|
|
//@function: GetUserInfoList
|
|
//@description: 分页获取User记录
|
|
//@param: info request.UserSearch
|
|
//@return: err error, list interface{}, total int64
|
|
|
|
func GetUserInfoList(info request.UserSearch) (err error, list interface{}, total int64) {
|
|
limit := info.PageSize
|
|
offset := info.PageSize * (info.Page - 1)
|
|
// 创建db
|
|
db := global.MG_DB.Model(&model.User{})
|
|
var users []model.User
|
|
// 如果有条件搜索 下方会自动创建搜索语句
|
|
err = db.Count(&total).Error
|
|
err = db.Limit(limit).Offset(offset).Find(&users).Error
|
|
return err, users, total
|
|
}
|
|
|
|
func GetUserInfo(uuid, appid string) (err error, user model.User) {
|
|
var (
|
|
reqUser model.User
|
|
authorities []model.SysAuthority
|
|
)
|
|
err = global.MG_DB.First(&reqUser, "uuid = ?", uuid).Error
|
|
if err != nil {
|
|
return err, reqUser
|
|
}
|
|
e := Casbin()
|
|
res := e.GetRolesForUserInDomain(uuid, appid)
|
|
if len(res) > 0 {
|
|
err = global.MG_DB.Where("authority_id IN ?", res).Find(&authorities).Error
|
|
} else {
|
|
err = errors.New("该用户未分配角色")
|
|
}
|
|
for _, v := range authorities {
|
|
if reqUser.AuthorityID == v.AuthorityId {
|
|
reqUser.Authority = v
|
|
break
|
|
}
|
|
}
|
|
reqUser.Authorities = authorities
|
|
return err, reqUser
|
|
}
|
|
|
|
func getUserSimpleList(ut string, uuids []string) ([]model.UserSimple, error) {
|
|
var (
|
|
err error
|
|
result []model.UserSimple
|
|
)
|
|
err = global.MG_DB.Model(&model.User{}).Select("uuid,nick_name,avatar,phone,platform,tags").Where("`type` = ? AND uuid IN (?)", ut, uuids).Find(&result).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func getUser(ut, uuid string) (error, model.User) {
|
|
var (
|
|
err error
|
|
result model.User
|
|
)
|
|
err = global.MG_DB.Model(&model.User{}).Where("`type` = ? AND uuid = ?", ut, uuid).Find(&result).Error
|
|
return err, result
|
|
}
|
|
|