user_controller.go 3.9 KB
package controllers

import (
	"errors"

	"github.com/astaxie/beego/logs"
	adminPermissionquery "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminPermission/query"
	adminPermissionService "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminPermission/service"
	userCommand "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/users/command"
	userQuery "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/users/query"
	userService "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/users/service"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
)

type UserController struct {
	BaseController
}

//Prepare 重写 BaseController 的Prepare方法
func (c *UserController) Prepare() {
	c.BaseController.Prepare()
	if ok := c.ValidJWTToken(); !ok {
		return
	}
	if ok := c.ValidAdminPermission(domain.PERMINSSION_ADMIN_USER); !ok {
		return
	}
}

//ListUser 获取用户分页列表
func (c *UserController) ListUser() {
	//用与适配前端定义的数据结构
	type Paramter struct {
		SearchText string `json:"searchText"`
		PageSize   int    `json::"pageSize"`
		PageNumber int    `json:"pageNumber"`
	}
	var (
		param Paramter
		err   error
	)
	if err = c.BindJsonData(&param); err != nil {
		logs.Error(err)
		c.ResponseError(errors.New("json数据解析失败"))
		return
	}
	if param.PageSize == 0 {
		param.PageSize = 20
	}
	if param.PageNumber == 0 {
		param.PageNumber = 1
	}
	newUserService := userService.NewUsersService(nil)
	queryOption := userQuery.UserListQuery{
		UserNameMatch: param.SearchText,
		Limit:         param.PageSize,
		Offset:        param.PageSize * (param.PageNumber - 1),
		CompanyId:     c.GetUserCompany(),
	}
	cnt, usersData, err := newUserService.GetUserList(queryOption)
	if err != nil {
		c.ResponseError(err)
		return
	}
	c.ResponsePageList(usersData, cnt, param.PageNumber)
	return
}

//BeforeEditUser 编辑用户数据的前置接口
func (c *UserController) BeforeEditUser() {
	newPermissionSrv := adminPermissionService.NewAdminPermissionService(nil)
	allPermission, err := newPermissionSrv.ListAdminPermission(adminPermissionquery.ListAdminPermissionQuery{
		ParentId: 0,
		NotCode:  []string{domain.PERMINSSION_ADMIN_USER},
	})
	if err != nil {
		logs.Error("获取权限数据失败:%s", err)
		c.ResponseError(errors.New("服务异常"))
		return
	}
	var rspData []map[string]interface{}
	for i := range allPermission {
		m := map[string]interface{}{
			"id":             allPermission[i].Id,
			"permissionName": allPermission[i].Name,
		}
		rspData = append(rspData, m)
	}
	c.ResponseData(rspData)
	return
}

//GetUserData 用户详情页数据
func (c *UserController) GetUserData() {
	//用与适配前端定义的数据结构
	type Paramter struct {
		Id int64 `json:"id"`
	}
	var (
		param Paramter
		err   error
	)
	if err = c.BindJsonData(&param); err != nil {
		logs.Error(err)
		c.ResponseError(errors.New("json数据解析失败"))
		return
	}
	companyId := c.GetUserCompany()
	newUserService := userService.NewUsersService(nil)
	usersData, err := newUserService.GetUserData(param.Id, companyId)
	if err != nil {
		c.ResponseError(err)
		return
	}
	c.ResponseData(usersData)
	return
}

//EditUserPermission 编辑用户的权限数据
func (c *UserController) EditUserPermission() {
	type UserDetailParam struct {
		Id             int64   `json:"id"`
		PermissionType []int64 `json:"permissionType"`
		CheckedPartner []int64 `json:"checkedPartner"` //合伙人
	}
	var (
		param UserDetailParam
		err   error
	)
	if err = c.BindJsonData(&param); err != nil {
		logs.Error(err)
		c.ResponseError(errors.New("json数据解析失败"))
		return
	}
	companyId := c.GetUserCompany()
	newUserService := userService.NewUsersService(nil)
	err = newUserService.EditUserPermission(userCommand.EditUserPermissionCommand{
		Id:             param.Id,
		CompanyId:      companyId,
		PermissionType: param.PermissionType,
		CheckedPartner: param.CheckedPartner,
	})
	if err != nil {
		c.ResponseError(err)
		return
	}
	c.ResponseData(nil)
	return
}