Explorar el Código

user tenant相关接口

terry hace 2 años
padre
commit
7095cbe840

+ 87 - 0
app/system/controller/tenant.go

@@ -1 +1,88 @@
 package controller
+
+import (
+	"github.com/gin-gonic/gin"
+	"iot_manager_service/app/system/dao"
+	"iot_manager_service/app/system/model"
+	"iot_manager_service/app/system/service"
+	"iot_manager_service/util"
+	"math"
+	"net/http"
+	"strconv"
+)
+
+var Tenant = new(tenant)
+
+type tenant struct{}
+
+func (c *tenant) GetDetail(ctx *gin.Context) {
+	id := ctx.Query("id")
+	iId, err := strconv.ParseInt(id, 10, 64)
+	if err != nil {
+		ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
+		return
+	}
+	tenant, err := service.TenantService.Get(iId)
+	if err != nil {
+		ctx.JSON(http.StatusOK, err)
+		return
+	}
+	ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, tenant))
+}
+
+func (c *tenant) List(ctx *gin.Context) {
+	current, _ := strconv.Atoi(ctx.Query("current"))
+	size, _ := strconv.Atoi(ctx.Query("size"))
+	tenantId := ctx.Query("tenantId")
+	tenantName := ctx.Query("tenantName")
+	linkman := ctx.Query("linkman")
+	if current == 0 {
+		current = 1
+	}
+	if size <= 0 || size > 100 {
+		size = 10
+	}
+
+	tenants, err := service.TenantService.List(tenantId, tenantName, linkman, current, size)
+	if err != nil {
+		ctx.JSON(http.StatusOK, err)
+		return
+	}
+	pages := math.Ceil(float64(len(tenants)) / float64(size))
+	rsp := model.RsqTenantList{
+		Current: current,
+		Size:    size,
+		Total:   len(tenants),
+		Pages:   int(pages),
+		Records: tenants,
+	}
+	ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, rsp))
+}
+
+func (c *tenant) Submit(ctx *gin.Context) {
+	var req dao.Tenant
+	if err := ctx.ShouldBindJSON(&req); err != nil {
+		ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
+		return
+	}
+	err := service.TenantService.Submit(req)
+	ctx.JSON(http.StatusOK, err)
+}
+
+func (c *tenant) Remove(ctx *gin.Context) {
+	var req *model.ReqTenantRemove
+	if err := ctx.ShouldBindJSON(&req); err != nil {
+		ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
+		return
+	}
+	err := service.TenantService.Remove(req.IDs)
+	ctx.JSON(http.StatusOK, err)
+}
+
+func (c *tenant) GetList(ctx *gin.Context) {
+	tenants, err := service.TenantService.GetList()
+	if err != nil {
+		ctx.JSON(http.StatusOK, util.FailResponse(err.Error(), nil))
+	}
+	ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, tenants))
+}

+ 41 - 2
app/system/controller/user.go

@@ -9,6 +9,7 @@ import (
 	"math"
 	"net/http"
 	"strconv"
+	"strings"
 )
 
 var User = new(user)
@@ -80,7 +81,45 @@ func (c *user) Update(ctx *gin.Context) {
 }
 
 func (c *user) Remove(ctx *gin.Context) {
-	id, _ := strconv.ParseInt(ctx.Query("id"), 10, 64)
-	err := service.UserService.Remove(id)
+	var req *model.ReqUserRemove
+	if err := ctx.ShouldBindJSON(&req); err != nil {
+		ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
+		return
+	}
+	err := service.UserService.Remove(req.IDs)
+	ctx.JSON(http.StatusOK, err)
+}
+
+func (c *user) ResetPwd(ctx *gin.Context) {
+	var req *model.ReqUserResetPwd
+	if err := ctx.ShouldBindJSON(&req); err != nil {
+		ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
+		return
+	}
+	err := service.UserService.ResetPwd(req.IDs)
+	ctx.JSON(http.StatusOK, err)
+}
+
+func (c *user) GetList(ctx *gin.Context) {
+	users, err := service.UserService.GetList()
+	if err != nil {
+		ctx.JSON(http.StatusOK, util.FailResponse(err.Error(), nil))
+	}
+	ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, users))
+}
+
+func (c *user) Grant(ctx *gin.Context) {
+	var req *model.ReqUserGrant
+	if err := ctx.ShouldBindJSON(&req); err != nil {
+		ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
+		return
+	}
+	uIds := strings.Split(req.UserIds, ",")
+	rIds := strings.Split(req.RoleIds, ",")
+	if len(uIds) < 1 || len(rIds) < 1 {
+		ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(model.GrantInvalid, nil))
+		return
+	}
+	err := service.UserService.Grant(uIds, req.RoleIds)
 	ctx.JSON(http.StatusOK, err)
 }

+ 0 - 1
app/system/dao/common.go

@@ -13,7 +13,6 @@ func InitDB(db *gorm.DB) {
 	err := Db.AutoMigrate(
 		&User{},
 		&Tenant{},
-		&TenantCode{},
 		&Role{},
 	).Error
 	if err != nil {

+ 43 - 12
app/system/dao/tenant.go

@@ -28,12 +28,20 @@ type Tenant struct {
 	PassKey          string    `gorm:"type:varchar(10)" json:"passKey"`                   //租户登录Key
 	LoginDisplayName string    `gorm:"type:varchar(60)" json:"loginDisplayName"`          //登录页面显示名称
 	ContactEmail     string    `gorm:"type:varchar(255)" json:"contactEmail"`             //联系邮箱
+	CityCode         string    `gorm:"type:varchar(6)" json:"cityCode"`                   //城市行政编码
+	CityName         string    `gorm:"type:varchar(255)" json:"cityName"`                 //城市名称
+	CityLng          float64   `gorm:"type:double(18, 14)" json:"cityLng"`                //经度
+	CityLat          float64   `gorm:"type:double(18, 14)" json:"cityLat"`                //经度
 }
 
 func (Tenant) TableName() string {
 	return "tenant"
 }
 
+func (c *Tenant) Get() error {
+	return Db.Model(&c).Where("id = ?", c.ID).Find(&c).Error
+}
+
 func (c *Tenant) GetTenant() error {
 	return Db.Model(&c).Where("tenant_id = ?", c.TenantId).Find(&c).Error
 }
@@ -42,20 +50,43 @@ func (c *Tenant) GetTenantByPasskey() error {
 	return Db.Model(&c).Where("pass_key = ?", c.PassKey).Find(&c).Error
 }
 
-// 租户位置信息
-type TenantCode struct {
-	ID       int64   `gorm:"primary_key" json:"id"`              //编号
-	TenantId string  `gorm:"type:varchar(12)" json:"tenantId"`   //租户id
-	CityCode string  `gorm:"type:varchar(6)" json:"cityCode"`    //城市行政编码
-	CityName string  `gorm:"type:varchar(255)" json:"cityName"`  //城市名称
-	CityLng  float64 `gorm:"type:double(18, 14)" json:"cityLng"` //经度
-	CityLat  float64 `gorm:"type:double(18, 14)" json:"cityLat"` //经度
+func (c *Tenant) IsExist() bool {
+	var count int
+	_ = Db.Model(&c).Where("pass_key = ?", c.PassKey).Count(&c).Error
+	return count > 0
 }
 
-func (TenantCode) TableName() string {
-	return "tenant_code"
+func (c *Tenant) GetTenants(offset, limit int) ([]Tenant, error) {
+	var tenants []Tenant
+	db := Db.Model(&c)
+	if c.TenantId != "" {
+		db = db.Where("tenant_id like ?", "%"+c.TenantId+"%")
+	}
+	if c.TenantName != "" {
+		db = db.Where("tenant_name like ?", "%"+c.TenantName+"%")
+	}
+	if c.Linkman != "" {
+		db = db.Where("linkman like ?", "%"+c.Linkman+"%")
+	}
+	err := db.Where("is_deleted = 0").Offset(offset).Limit(limit).Find(&tenants).Error
+	return tenants, err
 }
 
-func (c *TenantCode) GetTenantCode() error {
-	return Db.Model(&c).Where("tenant_id = ?", c.TenantId).Find(&c).Error
+func (c *Tenant) Save() error {
+	return Db.Model(&c).Save(&c).Error
+}
+
+func (c *Tenant) Update() error {
+	return Db.Model(&c).Where(" id = ? ", c.ID).Update(&c).Error
+}
+
+func (c *Tenant) GetAll() ([]Tenant, error) {
+	var tenants []Tenant
+	err := Db.Model(&c).Find(&tenants).Error
+	return tenants, err
+}
+
+func (c *Tenant) Remove() error {
+	return Db.Model(&c).Where("id = ?", c.ID).Updates(map[string]interface{}{"update_time": c.UpdateTime,
+		"update_user": c.UpdateUser, "is_deleted": c.IsDeleted}).Error
 }

+ 18 - 2
app/system/dao/user.go

@@ -52,7 +52,7 @@ func (c User) GetUsers(offset, limit int) ([]User, error) {
 	if c.Account != "" {
 		db = db.Where("account like ?", "%"+c.Account+"%")
 	}
-	if c.Account != "" {
+	if c.RealName != "" {
 		db = db.Where("real_name like ?", "%"+c.RealName+"%")
 	}
 	err := db.Where("is_deleted = 0").Offset(offset).Limit(limit).Find(&users).Error
@@ -68,5 +68,21 @@ func (c *User) Update() error {
 }
 
 func (c *User) Remove() error {
-	return Db.Model(&c).Where(" id = ? ", c.ID).Delete(&c).Error
+	return Db.Model(&c).Where("id = ?", c.ID).Updates(map[string]interface{}{"update_time": c.UpdateTime,
+		"update_user": c.UpdateUser, "is_deleted": c.IsDeleted}).Error
+}
+
+func (c *User) UpdatePwd(pwd string) error {
+	return Db.Model(&c).Where(" id = ? ", c.ID).Updates(map[string]interface{}{"password": pwd}).Error
+}
+
+func (c *User) GetAll() ([]User, error) {
+	var users []User
+	err := Db.Model(&c).Find(&users).Error
+	return users, err
+}
+
+func (c *User) UpdateRoles(userIds []string, roleIds string) error {
+	err := Db.Model(&c).Where("id in ?", userIds).Updates(map[string]interface{}{"role_id": roleIds}).Error
+	return err
 }

+ 19 - 0
app/system/model/tenant.go

@@ -1,6 +1,25 @@
 package model
 
+import "iot_manager_service/app/system/dao"
+
 const (
 	AdminTenantId = "1000000000000000" //默认租户
 	DES_KEY       = "0000000000000000" //租户授权码默认16位密钥
 )
+
+type RsqTenantList struct {
+	Records []dao.Tenant `json:"records"` //记录列表
+	Current int          `json:"current"` //当前分页
+	Size    int          `json:"size"`    //每页数量
+	Pages   int          `json:"pages"`   //总页数
+	Total   int          `json:"total"`   //总数
+}
+
+type ReqTenantRemove struct {
+	IDs  int64  `json:"ids"`
+	Name string `json:"name"`
+}
+
+type ReqTenantResetPwd struct {
+	IDs int64 `json:"ids"`
+}

+ 18 - 0
app/system/model/user.go

@@ -18,3 +18,21 @@ type RsqUserList struct {
 	Pages   int          `json:"pages"`   //总页数
 	Total   int          `json:"total"`   //总数
 }
+
+type ReqUserRemove struct {
+	IDs  int64  `json:"ids"`
+	Name string `json:"name"`
+}
+
+type ReqUserResetPwd struct {
+	IDs int64 `json:"ids"`
+}
+
+type ReqUserGrant struct {
+	UserIds string `json:"userIds"`
+	RoleIds string `json:"roleIds"`
+}
+
+const DefaultPwd = "10470c3b4b1fed12c3baac014be15fac67c6e815" //123456
+
+const GrantInvalid = "ID或角色ID无效!"

+ 78 - 0
app/system/service/tenantService.go

@@ -2,6 +2,8 @@ package service
 
 import (
 	"iot_manager_service/app/system/dao"
+	"iot_manager_service/util"
+	"time"
 )
 
 // 租户管理服务
@@ -30,3 +32,79 @@ func (s *tenantService) GetOne(tenantId string) (*dao.Tenant, error) {
 	}
 	return tenant, nil
 }
+
+func (s *tenantService) Get(id int64) (*dao.Tenant, error) {
+	tenant := &dao.Tenant{
+		ID: id,
+	}
+	err := tenant.Get()
+	if err != nil {
+		return nil, err
+	}
+	return tenant, nil
+}
+
+func (s *tenantService) List(tenantId, tenantName, linkman string, current, size int) ([]dao.Tenant, error) {
+	tenant := &dao.Tenant{}
+	offset := (current - 1) * size
+	limit := size
+	if tenantId != "" {
+		tenant.TenantId = tenantId
+	}
+	if tenantName != "" {
+		tenant.TenantName = tenantName
+	}
+	if linkman != "" {
+		tenant.Linkman = linkman
+	}
+
+	tenants, err := tenant.GetTenants(offset, limit)
+	if err != nil {
+		return nil, err
+	}
+	return tenants, nil
+}
+
+func (s *tenantService) Submit(req dao.Tenant) *util.Errors {
+	tenant := &req
+	if tenant.ID == 0 {
+		tenant.PassKey = util.RandomString(8)
+		if tenant.IsExist() {
+			tenant.PassKey = util.RandomString(8)
+		}
+		err := tenant.Save()
+		if err != nil {
+			return util.FailResponse(err.Error(), nil)
+		}
+		return util.SuccessResponse(util.Succeeded, nil)
+	}
+	err := tenant.Update()
+	if err != nil {
+		return util.FailResponse(err.Error(), nil)
+	}
+	return util.SuccessResponse(util.Succeeded, nil)
+}
+
+func (s *tenantService) GetList() ([]dao.Tenant, error) {
+	tenant := &dao.Tenant{}
+	tenants, err := tenant.GetAll()
+	if err != nil {
+		return nil, err
+	}
+	return tenants, nil
+}
+
+func (s *tenantService) Remove(id int64) *util.Errors {
+	tenant := &dao.Tenant{
+		ID:         id,
+		IsDeleted:  1,
+		UpdateUser: id, // todo 使用登录态
+		UpdateTime: time.Now(),
+	}
+	err := tenant.Remove()
+	if err != nil {
+		return util.FailResponse(err.Error(), nil)
+	}
+	return nil
+
+}

+ 30 - 1
app/system/service/userService.go

@@ -7,6 +7,7 @@ import (
 	"iot_manager_service/app/system/dao"
 	"iot_manager_service/app/system/model"
 	"iot_manager_service/util"
+	"time"
 )
 
 // 用户管理服务
@@ -98,10 +99,38 @@ func (s *userService) Update(req dao.User) *util.Errors {
 }
 
 func (s *userService) Remove(id int64) *util.Errors {
-	user := &dao.User{ID: id}
+	user := &dao.User{
+		ID:         id,
+		IsDeleted:  1,
+		UpdateUser: id, // todo 使用登录态
+		UpdateTime: time.Now(),
+	}
 	err := user.Remove()
 	if err != nil {
 		return util.FailResponse(err.Error(), nil)
 	}
 	return nil
 }
+
+func (s *userService) ResetPwd(id int64) *util.Errors {
+	user := &dao.User{ID: id}
+	err := user.UpdatePwd(model.DefaultPwd)
+	if err != nil {
+		return util.FailResponse(err.Error(), nil)
+	}
+	return nil
+}
+
+func (s *userService) GetList() ([]dao.User, error) {
+	user := &dao.User{}
+	users, err := user.GetAll()
+	if err != nil {
+		return nil, err
+	}
+	return users, nil
+}
+
+func (s *userService) Grant(userIds []string, roleIds string) error {
+	user := &dao.User{}
+	return user.UpdateRoles(userIds, roleIds)
+}

+ 16 - 3
router/router.go

@@ -352,8 +352,21 @@ func InitRouter(engine *gin.Engine) {
 	{
 		user.GET("/detail", system.User.GetDetail)
 		user.GET("/page", system.User.List)
-		user.GET("/submit", system.User.Submit)
-		user.GET("/update", system.User.Update)
-		user.GET("/remove", system.User.Remove)
+		user.POST("/submit", system.User.Submit)
+		user.POST("/update", system.User.Update)
+		user.POST("/remove", system.User.Remove)
+		user.POST("/reset-password", system.User.ResetPwd)
+		user.POST("/user-list", system.User.GetList)
+		user.POST("/grant", system.User.Grant)
+	}
+
+	//租户
+	tenant := engine.Group("/api/blade-system/tenant")
+	{
+		tenant.GET("/detail", system.Tenant.GetDetail)
+		tenant.GET("/page", system.Tenant.List)
+		tenant.POST("/submit", system.Tenant.Submit)
+		tenant.POST("/remove", system.Tenant.Remove)
+		tenant.POST("/select", system.Tenant.GetList)
 	}
 }