作者 yangfu

用户基础管理

  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
  6 + "reflect"
  7 + "strings"
  8 +
  9 + "github.com/beego/beego/v2/core/validation"
  10 +)
  11 +
  12 +type BatchEnableCommand struct {
  13 + OperateInfo *domain.OperateInfo `json:"-"`
  14 + UserBaseIds []int64 `cname:"用户id列表" json:"userBaseIds" valid:"Required"`
  15 + // 启用状态(启用:1 禁用:2 注销:3)
  16 + EnableStatus int `cname:"启用状态(启用:1 禁用:2 注销:3)" json:"enableStatus" valid:"Required"`
  17 +}
  18 +
  19 +func (batchEnableCommand *BatchEnableCommand) Valid(validation *validation.Validation) {
  20 + //validation.SetError("CustomValid", "未实现的自定义认证")
  21 + if !(batchEnableCommand.EnableStatus == int(domain.UserStatusEnable) || batchEnableCommand.EnableStatus == int(domain.UserStatusDisable)) {
  22 + validation.SetError("CustomValid", "非法启用状态")
  23 + }
  24 +}
  25 +
  26 +func (batchEnableCommand *BatchEnableCommand) ValidateCommand() error {
  27 + valid := validation.Validation{}
  28 + b, err := valid.Valid(batchEnableCommand)
  29 + if err != nil {
  30 + return err
  31 + }
  32 + if !b {
  33 + elem := reflect.TypeOf(batchEnableCommand).Elem()
  34 + for _, validErr := range valid.Errors {
  35 + field, isExist := elem.FieldByName(validErr.Field)
  36 + if isExist {
  37 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  38 + } else {
  39 + return fmt.Errorf(validErr.Message)
  40 + }
  41 + }
  42 + }
  43 + return nil
  44 +}
  1 +package dto
  2 +
  3 +import (
  4 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
  5 + "time"
  6 +)
  7 +
  8 +type UserBaseDto struct {
  9 + // 用户基础数据id
  10 + UserBaseId int64 `json:"userBaseId,omitempty"`
  11 + // 手机号码
  12 + //Account string `json:"phone,omitempty"`
  13 + // 最后登录时间
  14 + LastLogIn string `json:"lastLogIn"`
  15 + // 用户信息 (冗余,数据存在userBase里面)
  16 + UserInfo *domain.UserInfo `json:"userInfo,omitempty"`
  17 + // 账号状态 1:正常 2.禁用 3:注销
  18 + Status int `json:"status,omitempty"`
  19 + // 推荐人
  20 + Referer string `json:"referer"`
  21 + // 关联的用户 (冗余)
  22 + // RelatedUsers []int64 `json:"relatedUsers,omitempty"`
  23 + // 用户关联的组织
  24 + UserOrg []interface{} `json:"userOrg"`
  25 + // 创建时间
  26 + RegistrationDate string `json:"registrationDate,omitempty"`
  27 +}
  28 +
  29 +func (dto *UserBaseDto) LoadDto(userBase *domain.UserBase, relateUsers []*domain.User) error {
  30 + dto.UserBaseId = userBase.UserBaseId
  31 + //dto.Account = userBase.Account
  32 + dto.LastLogIn = ""
  33 + dto.UserInfo = userBase.UserInfo
  34 + if userBase.Favorite != nil {
  35 + if userBase.Favorite.LastLogInAt > 0 {
  36 + t := time.Unix(userBase.Favorite.LastLogInAt, 0)
  37 + dto.LastLogIn = t.Format("2006-01-02 15:04:05")
  38 + }
  39 + dto.Referer = userBase.Favorite.Referer
  40 + }
  41 + dto.Status = userBase.Status
  42 + for i := range relateUsers {
  43 + dto.UserOrg = append(dto.UserOrg, map[string]interface{}{
  44 + "orgId": relateUsers[i].OrganizationId,
  45 + "orgName": relateUsers[i].Ext.OrgName,
  46 + })
  47 + }
  48 + dto.RegistrationDate = userBase.CreatedAt.Format("2006-01-02")
  49 + return nil
  50 +}
  1 +package query
  2 +
  3 +import (
  4 + "fmt"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
  6 + "reflect"
  7 + "strings"
  8 +
  9 + "github.com/beego/beego/v2/core/validation"
  10 +)
  11 +
  12 +type ListUserQuery struct {
  13 + OperateInfo *domain.OperateInfo `json:"-"`
  14 + // 查询偏离量
  15 + Offset int `cname:"查询偏离量" json:"offset"`
  16 + // 查询限制
  17 + Limit int `cname:"查询限制" json:"limit"`
  18 + // 用户姓名
  19 + UserName string `cname:"用户姓名" json:"userName,omitempty"`
  20 + // 在用户列表内
  21 + InUserBaseIds []int64 `cname:"用户姓名" json:"inUserBaseIds,omitempty"`
  22 + // 所属组织
  23 + OrgName string `cname:"所属组织" json:"orgName,omitempty"`
  24 + // 关闭查询限制
  25 + DisableLimit bool `cname:"关闭查询限制" json:"disableLimit,omitempty"`
  26 +
  27 + // 获取组织
  28 + FetchOrgBelong bool `cname:"获取组织" json:"fetchOrgBelong,omitempty"`
  29 +}
  30 +
  31 +func (listUserQuery *ListUserQuery) Valid(validation *validation.Validation) {
  32 + //validation.SetError("CustomValid", "未实现的自定义认证")
  33 +}
  34 +
  35 +func (listUserQuery *ListUserQuery) ValidateQuery() error {
  36 + valid := validation.Validation{}
  37 + b, err := valid.Valid(listUserQuery)
  38 + if err != nil {
  39 + return err
  40 + }
  41 + if !b {
  42 + elem := reflect.TypeOf(listUserQuery).Elem()
  43 + for _, validErr := range valid.Errors {
  44 + field, isExist := elem.FieldByName(validErr.Field)
  45 + if isExist {
  46 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  47 + } else {
  48 + return fmt.Errorf(validErr.Message)
  49 + }
  50 + }
  51 + }
  52 + return nil
  53 +}
  1 +package service
  2 +
  3 +import (
  4 + "github.com/linmadan/egglib-go/core/application"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/factory"
  6 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/userbase/command"
  7 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/userbase/dto"
  8 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/userbase/query"
  9 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
  10 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/utils"
  11 +)
  12 +
  13 +type UserBaseService struct {
  14 +}
  15 +
  16 +// 返回列表
  17 +func (userService *UserBaseService) SearchUser(listUserQuery *query.ListUserQuery) (interface{}, error) {
  18 + if err := listUserQuery.ValidateQuery(); err != nil {
  19 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  20 + }
  21 + transactionContext, err := factory.CreateTransactionContext(nil)
  22 + if err != nil {
  23 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  24 + }
  25 + if err := transactionContext.StartTransaction(); err != nil {
  26 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  27 + }
  28 + defer func() {
  29 + transactionContext.RollbackTransaction()
  30 + }()
  31 + var count int64
  32 + var userBases []*domain.UserBase
  33 + userBaseRepository, _, _ := factory.FastPgUserBase(transactionContext, 0)
  34 + userRepository, _, _ := factory.FastPgUser(transactionContext, 0)
  35 + _, users, err := userRepository.Find(map[string]interface{}{
  36 + "limit": listUserQuery.Limit,
  37 + "offset": listUserQuery.Offset,
  38 + "orgName": listUserQuery.OrgName,
  39 + "userName": listUserQuery.UserName,
  40 + "distinctOnUserBaseId": true,
  41 + })
  42 + if err != nil {
  43 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  44 + }
  45 + var result = make([]*dto.UserBaseDto, 0)
  46 + if len(users) > 0 {
  47 + var userBaseIds []int64
  48 + for i := range users {
  49 + userBaseIds = append(userBaseIds, users[i].UserBaseId)
  50 + }
  51 + listUserQuery.InUserBaseIds = userBaseIds
  52 + queryOptions := utils.ObjectToMap(listUserQuery)
  53 + count, userBases, err = userBaseRepository.Find(queryOptions)
  54 + if err != nil {
  55 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  56 + }
  57 +
  58 + for i := range userBases {
  59 + u := userBases[i]
  60 + var relateUsers []*domain.User
  61 + if listUserQuery.FetchOrgBelong && len(u.RelatedUsers) > 0 {
  62 + for i := range u.RelatedUsers {
  63 + relateUser, _ := userRepository.FindOne(map[string]interface{}{"userId": u.RelatedUsers[i]})
  64 + if relateUser != nil {
  65 + relateUsers = append(relateUsers, relateUser)
  66 + }
  67 + }
  68 + }
  69 + userBaseDto := &dto.UserBaseDto{}
  70 + userBaseDto.LoadDto(u, relateUsers)
  71 + result = append(result, userBaseDto)
  72 + }
  73 + }
  74 +
  75 + return map[string]interface{}{
  76 + "count": count,
  77 + "users": result,
  78 + }, nil
  79 +}
  80 +
  81 +// 批量修改启用状态
  82 +func (userService *UserBaseService) BatchEnable(batchEnableCommand *command.BatchEnableCommand) (interface{}, error) {
  83 + if err := batchEnableCommand.ValidateCommand(); err != nil {
  84 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  85 + }
  86 + transactionContext, err := factory.CreateTransactionContext(nil)
  87 + if err != nil {
  88 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  89 + }
  90 + if err := transactionContext.StartTransaction(); err != nil {
  91 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  92 + }
  93 + defer func() {
  94 + transactionContext.RollbackTransaction()
  95 + }()
  96 + userRepository, _, err := factory.FastPgUserBase(transactionContext, 0)
  97 + if err != nil {
  98 + return nil, err
  99 + }
  100 +
  101 + for i := 0; i < len(batchEnableCommand.UserBaseIds); i++ {
  102 + if user, err := userRepository.FindOne(map[string]interface{}{"userBaseId": batchEnableCommand.UserBaseIds[i]}); err != nil {
  103 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  104 + } else {
  105 + if err := user.SetEnableStatus(batchEnableCommand.EnableStatus); err != nil {
  106 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  107 + }
  108 + if _, err := userRepository.Save(user); err != nil {
  109 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  110 + }
  111 + }
  112 + }
  113 +
  114 + if err := transactionContext.CommitTransaction(); err != nil {
  115 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  116 + }
  117 + return struct{}{}, nil
  118 +}
  119 +
  120 +func NewUserBaseService(options map[string]interface{}) *UserBaseService {
  121 + newUserService := &UserBaseService{}
  122 + return newUserService
  123 +}
@@ -275,6 +275,21 @@ func (userBase *UserBase) LastLogIn(t time.Time) error { @@ -275,6 +275,21 @@ func (userBase *UserBase) LastLogIn(t time.Time) error {
275 return nil 275 return nil
276 } 276 }
277 277
  278 +func (user *UserBase) SetEnableStatus(status int) error {
  279 + userStatus := UserStatus(user.Status)
  280 + if userStatus == UserStatusDestroy {
  281 + return fmt.Errorf("账号已注销")
  282 + }
  283 + if user.Status == status {
  284 + return nil //fmt.Errorf("重复设置状态")
  285 + }
  286 + if !(status == int(UserStatusEnable) || status == int(UserStatusDisable)) {
  287 + return fmt.Errorf("非法启用状态")
  288 + }
  289 + user.Status = status
  290 + return nil
  291 +}
  292 +
278 /***** 2.缓存模块 *****/ 293 /***** 2.缓存模块 *****/
279 294
280 func (m *UserBase) CacheKeyFunc() string { 295 func (m *UserBase) CacheKeyFunc() string {
@@ -172,7 +172,10 @@ func (repository *UserBaseRepository) Find(queryOptions map[string]interface{}) @@ -172,7 +172,10 @@ func (repository *UserBaseRepository) Find(queryOptions map[string]interface{})
172 var userBaseModels []*models.UserBase 172 var userBaseModels []*models.UserBase
173 userBases := make([]*domain.UserBase, 0) 173 userBases := make([]*domain.UserBase, 0)
174 query := sqlbuilder.BuildQuery(tx.Model(&userBaseModels), queryOptions) 174 query := sqlbuilder.BuildQuery(tx.Model(&userBaseModels), queryOptions)
175 - query.SetOffsetAndLimit(20) 175 + query.SetOffsetAndLimit(20) //InUserBaseIds
  176 + if v, ok := queryOptions["inUserBaseIds"]; ok && len(v.([]int64)) > 0 {
  177 + query.Where(`user_base_id in (?)`, pg.In(v))
  178 + }
176 query.SetOrderDirect("user_base_id", "DESC") 179 query.SetOrderDirect("user_base_id", "DESC")
177 if count, err := query.SelectAndCount(); err != nil { 180 if count, err := query.SelectAndCount(); err != nil {
178 return 0, userBases, err 181 return 0, userBases, err
@@ -196,6 +196,11 @@ func (repository *UserRepository) Find(queryOptions map[string]interface{}) (int @@ -196,6 +196,11 @@ func (repository *UserRepository) Find(queryOptions map[string]interface{}) (int
196 var userModels []*models.User 196 var userModels []*models.User
197 users := make([]*domain.User, 0) 197 users := make([]*domain.User, 0)
198 query := sqlbuilder.BuildQuery(tx.Model(&userModels), queryOptions) 198 query := sqlbuilder.BuildQuery(tx.Model(&userModels), queryOptions)
  199 +
  200 + if v, ok := queryOptions["distinctOnUserBaseId"]; ok && (v.(bool)) {
  201 + query.DistinctOn("user_base_id")
  202 + query.Order("user_base_id desc")
  203 + }
199 query.SetWhereByQueryOption("company_id=?", "companyId") 204 query.SetWhereByQueryOption("company_id=?", "companyId")
200 query.SetWhereByQueryOption("organization_id=?", "organizationId") 205 query.SetWhereByQueryOption("organization_id=?", "organizationId")
201 if v, ok := queryOptions["inOrgIds"]; ok && len(v.([]int64)) > 0 { 206 if v, ok := queryOptions["inOrgIds"]; ok && len(v.([]int64)) > 0 {
@@ -214,6 +219,9 @@ func (repository *UserRepository) Find(queryOptions map[string]interface{}) (int @@ -214,6 +219,9 @@ func (repository *UserRepository) Find(queryOptions map[string]interface{}) (int
214 if v, ok := queryOptions["depName"]; ok && len(v.(string)) > 0 { 219 if v, ok := queryOptions["depName"]; ok && len(v.(string)) > 0 {
215 query.Where(fmt.Sprintf(`ext->>'depName' like '%%%v%%'`, v)) 220 query.Where(fmt.Sprintf(`ext->>'depName' like '%%%v%%'`, v))
216 } 221 }
  222 + if v, ok := queryOptions["orgName"]; ok && len(v.(string)) > 0 {
  223 + query.Where(fmt.Sprintf(`ext->>'orgName' like '%%%v%%'`, v))
  224 + }
217 if v, ok := queryOptions["userName"]; ok && len(v.(string)) > 0 { 225 if v, ok := queryOptions["userName"]; ok && len(v.(string)) > 0 {
218 query.Where(fmt.Sprintf(`ext->>'userName' like '%%%v%%'`, v)) 226 query.Where(fmt.Sprintf(`ext->>'userName' like '%%%v%%'`, v))
219 } 227 }
  1 +package controllers
  2 +
  3 +import (
  4 + "github.com/linmadan/egglib-go/web/beego"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/userbase/command"
  6 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/userbase/query"
  7 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/userbase/service"
  8 +)
  9 +
  10 +type UserBaseController struct {
  11 + beego.BaseController
  12 +}
  13 +
  14 +func (controller *UserBaseController) SearchUser() {
  15 + userService := service.NewUserBaseService(nil)
  16 + listUserQuery := &query.ListUserQuery{}
  17 + Must(controller.Unmarshal(listUserQuery))
  18 + listUserQuery.OperateInfo = ParseOperateInfo(controller.BaseController)
  19 + data, err := userService.SearchUser(listUserQuery)
  20 + controller.Response(data, err)
  21 +}
  22 +
  23 +func (controller *UserBaseController) BatchEnable() {
  24 + userService := service.NewUserBaseService(nil)
  25 + batchEnableCommand := &command.BatchEnableCommand{}
  26 + controller.Unmarshal(batchEnableCommand)
  27 + batchEnableCommand.OperateInfo = ParseOperateInfo(controller.BaseController)
  28 + data, err := userService.BatchEnable(batchEnableCommand)
  29 + controller.Response(data, err)
  30 +}
  1 +package routers
  2 +
  3 +import (
  4 + "github.com/beego/beego/v2/server/web"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/port/beego/controllers"
  6 +)
  7 +
  8 +func init() {
  9 + //web.Router("/user/", &controllers.UserController{}, "Post:CreateUser")
  10 + //web.Router("/user/:userId", &controllers.UserController{}, "Put:UpdateUser")
  11 + //web.Router("/user/:userId", &controllers.UserController{}, "Get:GetUser")
  12 + //web.Router("/user/:userId", &controllers.UserController{}, "Delete:RemoveUser")
  13 + web.Router("/user-base/search", &controllers.UserBaseController{}, "Post:SearchUser")
  14 + //web.Router("/user/:userId/access-menus", &controllers.UserController{}, "Get:GetUserAccessMenus")
  15 + //web.Router("/user/:userId/profile", &controllers.UserController{}, "Get:GetUserProfile")
  16 + //web.Router("/user/batch-add", &controllers.UserController{}, "Post:BatchAdd")
  17 + //web.Router("/user/batch-add2", &controllers.UserController{}, "Post:BatchAdd2")
  18 + web.Router("/user-base/batch-enable", &controllers.UserBaseController{}, "Post:BatchEnable")
  19 + //web.Router("/user/batch-reset-password", &controllers.UserController{}, "Post:BatchResetPassword")
  20 + //web.Router("/user/:userId/base-info", &controllers.UserController{}, "Put:UpdateUsersBase")
  21 + //web.Router("/user/cooperator", &controllers.UserController{}, "Post:CreateCooperator")
  22 + //web.Router("/user/cooperator/:userId", &controllers.UserController{}, "Put:UpdateCooperator")
  23 +}