作者 庄敏学

增加用户搜索

... ... @@ -142,6 +142,8 @@ func (srv SyncDataDepartmentService) editDepartment(param *command.EditDepartmen
}
if len(param.ChargeUserIds) > 0 {
departmentList[i].ChargeUserIds = param.ChargeUserIds
} else {
departmentList[i].ChargeUserIds = make([]int64, 0)
}
continue
}
... ...
package query
type ListUserQuery struct {
CompanyId int64 `json:"companyId"` // 公司ID
Name string `json:"name"` // 用户姓名
}
... ...
package user
import (
"github.com/linmadan/egglib-go/core/application"
"github.com/linmadan/egglib-go/utils/tool_funs"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/user/query"
)
type UserService struct{}
func (service *UserService) ListUsers(listUserQuery *query.ListUserQuery) (interface{}, error) {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
_ = transactionContext.RollbackTransaction()
}()
userRepo := factory.CreateUserRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
count, list, err := userRepo.Find(map[string]interface{}{
"companyId": listUserQuery.CompanyId,
"name": listUserQuery.Name,
})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return tool_funs.SimpleWrapGridMap(int64(count), list), nil
}
... ...
... ... @@ -114,6 +114,9 @@ func (repo *UserRepository) Find(queryOptions map[string]interface{}) (int, []*d
if v, ok := queryOptions["account"]; ok {
query.Where("account like ?", v)
}
if v, ok := queryOptions["name"]; ok && v.(string) != "" {
query.Where("name like ?", "%"+v.(string)+"%")
}
if v, ok := queryOptions["offset"]; ok {
query.Offset(v.(int))
}
... ...
package controllers
import (
"github.com/linmadan/egglib-go/web/beego"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/user"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/user/query"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
)
type UserController struct {
beego.BaseController
}
// ListUsers 搜索用户
func (controller *UserController) ListUsers() {
listUserQuery := &query.ListUserQuery{}
_ = controller.Unmarshal(listUserQuery)
userAuth := controller.Ctx.Input.GetData(domain.UserAuth{}).(*domain.UserAuth)
listUserQuery.CompanyId = userAuth.CompanyId
resp, err := (&user.UserService{}).ListUsers(listUserQuery)
controller.Response(resp, err)
}
... ...
package routers
import (
"github.com/beego/beego/v2/server/web"
"github.com/linmadan/egglib-go/web/beego/filters"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/port/beego/controllers"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/port/beego/middlewares"
)
func init() {
ns := web.NewNamespace("/v1/user",
web.NSBefore(filters.AllowCors(), middlewares.CheckAdminToken()),
web.NSRouter("/search", &controllers.UserController{}, "Post:ListUsers"),
)
web.AddNamespace(ns)
}
... ...