Browse Source

user 相关接口

terry 2 years ago
parent
commit
55c79cb418

+ 38 - 5
app/system/controller/user.go

@@ -2,6 +2,7 @@ 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"
@@ -21,12 +22,20 @@ func (c *user) GetDetail(ctx *gin.Context) {
 		ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
 		return
 	}
-	service.UserService.Get(iId)
+	detail, err := service.UserService.Get(iId)
+	if err != nil {
+		ctx.JSON(http.StatusOK, err)
+		return
+	}
+	ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, detail))
+
 }
 
 func (c *user) List(ctx *gin.Context) {
 	current, _ := strconv.Atoi(ctx.Query("current"))
 	size, _ := strconv.Atoi(ctx.Query("size"))
+	account := ctx.Query("account")
+	realName := ctx.Query("realName")
 	if current == 0 {
 		current = 1
 	}
@@ -34,7 +43,7 @@ func (c *user) List(ctx *gin.Context) {
 		size = 10
 	}
 
-	users, err := service.UserService.List(current, size)
+	users, err := service.UserService.List(account, realName, current, size)
 	if err != nil {
 		ctx.JSON(http.StatusOK, err)
 		return
@@ -45,9 +54,33 @@ func (c *user) List(ctx *gin.Context) {
 		Size:    size,
 		Total:   len(users),
 		Pages:   int(pages),
-	}
-	for _, device := range users {
-		rsp.Records = append(rsp.Records, device)
+		Records: users,
 	}
 	ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, rsp))
 }
+
+func (c *user) Submit(ctx *gin.Context) {
+	var req dao.User
+	if err := ctx.ShouldBindJSON(&req); err != nil {
+		ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
+		return
+	}
+	err := service.UserService.Submit(req)
+	ctx.JSON(http.StatusOK, err)
+}
+
+func (c *user) Update(ctx *gin.Context) {
+	var req dao.User
+	if err := ctx.ShouldBindJSON(&req); err != nil {
+		ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
+		return
+	}
+	err := service.UserService.Update(req)
+	ctx.JSON(http.StatusOK, err)
+}
+
+func (c *user) Remove(ctx *gin.Context) {
+	id, _ := strconv.ParseInt(ctx.Query("id"), 10, 64)
+	err := service.UserService.Remove(id)
+	ctx.JSON(http.StatusOK, err)
+}

+ 20 - 1
app/system/dao/user.go

@@ -48,6 +48,25 @@ func (c *User) GetUserByPwd() error {
 
 func (c User) GetUsers(offset, limit int) ([]User, error) {
 	var users []User
-	err := Db.Model(&c).Where("is_deleted = 0").Offset(offset).Limit(limit).Find(&users).Error
+	db := Db.Model(&c)
+	if c.Account != "" {
+		db = db.Where("account like ?", "%"+c.Account+"%")
+	}
+	if c.Account != "" {
+		db = db.Where("real_name like ?", "%"+c.RealName+"%")
+	}
+	err := db.Where("is_deleted = 0").Offset(offset).Limit(limit).Find(&users).Error
 	return users, err
 }
+
+func (c *User) Save() error {
+	return Db.Model(&c).Save(&c).Error
+}
+
+func (c *User) Update() error {
+	return Db.Model(&c).Where(" id = ? ", c.ID).Update(&c).Error
+}
+
+func (c *User) Remove() error {
+	return Db.Model(&c).Where(" id = ? ", c.ID).Delete(&c).Error
+}

+ 5 - 5
app/system/model/user.go

@@ -12,9 +12,9 @@ type UserDetail struct {
 }
 
 type RsqUserList struct {
-	Records []dao.User `json:"records"` //记录列表
-	Current int        `json:"current"` //当前分页
-	Size    int        `json:"size"`    //每页数量
-	Pages   int        `json:"pages"`   //总页数
-	Total   int        `json:"total"`   //总数
+	Records []UserDetail `json:"records"` //记录列表
+	Current int          `json:"current"` //当前分页
+	Size    int          `json:"size"`    //每页数量
+	Pages   int          `json:"pages"`   //总页数
+	Total   int          `json:"total"`   //总数
 }

+ 47 - 2
app/system/service/userService.go

@@ -50,13 +50,58 @@ func (s *userService) GetOne(tenantId, account, password string) *dao.User {
 	return user
 }
 
-func (s *userService) List(current, size int) ([]dao.User, *util.Errors) {
+func (s *userService) List(account, realName string, current, size int) ([]model.UserDetail, *util.Errors) {
 	user := dao.User{}
 	offset := (current - 1) * size
 	limit := size
+	if account != "" {
+		user.Account = account
+	}
+	if realName != "" {
+		user.RealName = realName
+	}
+
 	users, err := user.GetUsers(offset, limit)
 	if err != nil {
 		return nil, util.FailResponse(err.Error(), nil)
 	}
-	return users, nil
+	var details []model.UserDetail
+	for _, user := range users {
+		details = append(details, model.UserDetail{
+			User:       user,
+			TenantName: util.GetTenantName(user.TenantId, user.TenantId),
+			RoleName:   util.GetRoleNames(user.TenantId, user.RoleId),
+			DeptName:   "", //智慧路灯 ?
+			PostName:   "",
+			SexName:    util.GetSexName(user.TenantId, user.Sex),
+		})
+	}
+	return details, nil
+}
+
+func (s *userService) Submit(req dao.User) *util.Errors {
+	user := &req
+	err := user.Save()
+	if err != nil {
+		return util.FailResponse(err.Error(), nil)
+	}
+	return nil
+}
+
+func (s *userService) Update(req dao.User) *util.Errors {
+	user := &req
+	err := user.Update()
+	if err != nil {
+		return util.FailResponse(err.Error(), nil)
+	}
+	return nil
+}
+
+func (s *userService) Remove(id int64) *util.Errors {
+	user := &dao.User{ID: id}
+	err := user.Remove()
+	if err != nil {
+		return util.FailResponse(err.Error(), nil)
+	}
+	return nil
 }

+ 3 - 0
router/router.go

@@ -352,5 +352,8 @@ 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)
 	}
 }