558 lines
13 KiB
Go
558 lines
13 KiB
Go
package handler
|
||
|
||
import (
|
||
"dianshang/internal/service"
|
||
"dianshang/pkg/response"
|
||
"dianshang/pkg/utils"
|
||
"strconv"
|
||
"time"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
// AdminUserHandler 管理后台用户处理器
|
||
type AdminUserHandler struct {
|
||
userService *service.UserService
|
||
orderService *service.OrderService
|
||
}
|
||
|
||
// NewAdminUserHandler 创建管理后台用户处理器
|
||
func NewAdminUserHandler(userService *service.UserService, orderService *service.OrderService) *AdminUserHandler {
|
||
return &AdminUserHandler{
|
||
userService: userService,
|
||
orderService: orderService,
|
||
}
|
||
}
|
||
|
||
// GetUserList 获取用户列表
|
||
func (h *AdminUserHandler) GetUserList(c *gin.Context) {
|
||
page := utils.StringToInt(c.DefaultQuery("page", "1"))
|
||
// 支持两种参数名:size 和 page_size
|
||
pageSize := utils.StringToInt(c.DefaultQuery("size", c.DefaultQuery("page_size", "10")))
|
||
keyword := c.Query("keyword")
|
||
status := c.Query("status")
|
||
// 支持两种参数名:startDate/start_date 和 endDate/end_date
|
||
startDate := c.Query("startDate")
|
||
if startDate == "" {
|
||
startDate = c.Query("start_date")
|
||
}
|
||
endDate := c.Query("endDate")
|
||
if endDate == "" {
|
||
endDate = c.Query("end_date")
|
||
}
|
||
|
||
if page <= 0 {
|
||
page = 1
|
||
}
|
||
if pageSize <= 0 || pageSize > 100 {
|
||
pageSize = 10
|
||
}
|
||
|
||
// 构建查询条件
|
||
conditions := make(map[string]interface{})
|
||
if keyword != "" {
|
||
conditions["keyword"] = keyword
|
||
}
|
||
if status != "" {
|
||
conditions["status"] = status
|
||
}
|
||
if startDate != "" {
|
||
conditions["start_date"] = startDate
|
||
}
|
||
if endDate != "" {
|
||
conditions["end_date"] = endDate
|
||
}
|
||
|
||
users, pagination, err := h.userService.GetUserListForAdmin(page, pageSize, conditions)
|
||
if err != nil {
|
||
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
||
return
|
||
}
|
||
|
||
response.Success(c, gin.H{
|
||
"list": users,
|
||
"total": pagination["total"],
|
||
"page": pagination["page"],
|
||
"page_size": pagination["page_size"],
|
||
"total_pages": pagination["total_pages"],
|
||
})
|
||
}
|
||
|
||
// GetUserDetail 获取用户详情
|
||
func (h *AdminUserHandler) GetUserDetail(c *gin.Context) {
|
||
idStr := c.Param("id")
|
||
id, err := strconv.ParseUint(idStr, 10, 32)
|
||
if err != nil {
|
||
response.BadRequest(c, "无效的用户ID")
|
||
return
|
||
}
|
||
|
||
user, err := h.userService.GetUserDetailForAdmin(uint(id))
|
||
if err != nil {
|
||
response.ErrorWithMessage(c, response.ERROR, "用户不存在")
|
||
return
|
||
}
|
||
|
||
response.Success(c, user)
|
||
}
|
||
|
||
// UpdateUserStatus 更新用户状态
|
||
func (h *AdminUserHandler) UpdateUserStatus(c *gin.Context) {
|
||
idStr := c.Param("id")
|
||
id, err := strconv.ParseUint(idStr, 10, 32)
|
||
if err != nil {
|
||
response.BadRequest(c, "无效的用户ID")
|
||
return
|
||
}
|
||
|
||
var req struct {
|
||
Status *uint8 `json:"status" binding:"required"`
|
||
Remark string `json:"remark"`
|
||
}
|
||
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
response.BadRequest(c, "请求参数错误")
|
||
return
|
||
}
|
||
|
||
// 获取管理员ID
|
||
adminID := uint(0)
|
||
if id, exists := c.Get("user_id"); exists {
|
||
adminID = id.(uint)
|
||
}
|
||
|
||
if err := h.userService.UpdateUserStatusByAdmin(uint(id), *req.Status, req.Remark, adminID); err != nil {
|
||
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
||
return
|
||
}
|
||
|
||
response.Success(c, nil)
|
||
}
|
||
|
||
// BatchUpdateUserStatus 批量更新用户状态
|
||
func (h *AdminUserHandler) BatchUpdateUserStatus(c *gin.Context) {
|
||
var req struct {
|
||
UserIDs []uint `json:"user_ids" binding:"required"`
|
||
Status uint8 `json:"status" binding:"required"`
|
||
Remark string `json:"remark"`
|
||
}
|
||
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
response.BadRequest(c, "请求参数错误")
|
||
return
|
||
}
|
||
|
||
// 获取管理员ID
|
||
adminID := uint(0)
|
||
if id, exists := c.Get("user_id"); exists {
|
||
adminID = id.(uint)
|
||
}
|
||
|
||
// 批量更新用户状态
|
||
for _, userID := range req.UserIDs {
|
||
if err := h.userService.UpdateUserStatusByAdmin(userID, req.Status, req.Remark, adminID); err != nil {
|
||
response.ErrorWithMessage(c, response.ERROR, "批量更新用户状态失败")
|
||
return
|
||
}
|
||
}
|
||
|
||
response.Success(c, nil)
|
||
}
|
||
|
||
// GetUserStatistics 获取用户统计
|
||
func (h *AdminUserHandler) GetUserStatistics(c *gin.Context) {
|
||
startDate := c.Query("start_date")
|
||
endDate := c.Query("end_date")
|
||
|
||
// 如果没有提供日期,默认查询最近30天
|
||
if startDate == "" || endDate == "" {
|
||
now := time.Now()
|
||
endDate = now.Format("2006-01-02")
|
||
startDate = now.AddDate(0, 0, -30).Format("2006-01-02")
|
||
}
|
||
|
||
stats, err := h.userService.GetUserStatistics(startDate, endDate)
|
||
if err != nil {
|
||
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
||
return
|
||
}
|
||
|
||
response.Success(c, stats)
|
||
}
|
||
|
||
// GetUserOrders 获取用户订单列表
|
||
func (h *AdminUserHandler) GetUserOrders(c *gin.Context) {
|
||
idStr := c.Param("id")
|
||
userID, err := strconv.ParseUint(idStr, 10, 32)
|
||
if err != nil {
|
||
response.BadRequest(c, "无效的用户ID")
|
||
return
|
||
}
|
||
|
||
page := utils.StringToInt(c.DefaultQuery("page", "1"))
|
||
pageSize := utils.StringToInt(c.DefaultQuery("page_size", "10"))
|
||
|
||
if page <= 0 {
|
||
page = 1
|
||
}
|
||
if pageSize <= 0 || pageSize > 100 {
|
||
pageSize = 10
|
||
}
|
||
|
||
// 获取状态参数,默认为0(所有状态)
|
||
status := utils.StringToInt(c.DefaultQuery("status", "0"))
|
||
|
||
orders, pagination, err := h.orderService.GetUserOrders(uint(userID), status, page, pageSize)
|
||
if err != nil {
|
||
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
||
return
|
||
}
|
||
|
||
response.Success(c, gin.H{
|
||
"list": orders,
|
||
"pagination": pagination,
|
||
})
|
||
}
|
||
|
||
// GetUserOrderStatistics 获取用户订单统计
|
||
func (h *AdminUserHandler) GetUserOrderStatistics(c *gin.Context) {
|
||
idStr := c.Param("id")
|
||
userID, err := strconv.ParseUint(idStr, 10, 32)
|
||
if err != nil {
|
||
response.BadRequest(c, "无效的用户ID")
|
||
return
|
||
}
|
||
|
||
stats, err := h.orderService.GetUserOrderStatistics(uint(userID))
|
||
if err != nil {
|
||
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
||
return
|
||
}
|
||
|
||
response.Success(c, stats)
|
||
}
|
||
|
||
// GetUserAddresses 获取用户地址列表
|
||
func (h *AdminUserHandler) GetUserAddresses(c *gin.Context) {
|
||
idStr := c.Param("id")
|
||
userID, err := strconv.ParseUint(idStr, 10, 32)
|
||
if err != nil {
|
||
response.BadRequest(c, "无效的用户ID")
|
||
return
|
||
}
|
||
|
||
addresses, err := h.userService.GetUserAddresses(uint(userID))
|
||
if err != nil {
|
||
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
||
return
|
||
}
|
||
|
||
response.Success(c, addresses)
|
||
}
|
||
|
||
// GetUserFavorites 获取用户收藏列表
|
||
func (h *AdminUserHandler) GetUserFavorites(c *gin.Context) {
|
||
idStr := c.Param("id")
|
||
userID, err := strconv.ParseUint(idStr, 10, 32)
|
||
if err != nil {
|
||
response.BadRequest(c, "无效的用户ID")
|
||
return
|
||
}
|
||
|
||
page := utils.StringToInt(c.DefaultQuery("page", "1"))
|
||
pageSize := utils.StringToInt(c.DefaultQuery("page_size", "10"))
|
||
|
||
if page <= 0 {
|
||
page = 1
|
||
}
|
||
if pageSize <= 0 || pageSize > 100 {
|
||
pageSize = 10
|
||
}
|
||
|
||
favorites, pagination, err := h.userService.GetUserFavorites(uint(userID), page, pageSize)
|
||
if err != nil {
|
||
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
||
return
|
||
}
|
||
|
||
response.Success(c, gin.H{
|
||
"list": favorites,
|
||
"pagination": pagination,
|
||
})
|
||
}
|
||
|
||
// ResetUserPassword 重置用户密码
|
||
func (h *AdminUserHandler) ResetUserPassword(c *gin.Context) {
|
||
idStr := c.Param("id")
|
||
id, err := strconv.ParseUint(idStr, 10, 32)
|
||
if err != nil {
|
||
response.BadRequest(c, "无效的用户ID")
|
||
return
|
||
}
|
||
|
||
var req struct {
|
||
NewPassword string `json:"new_password" binding:"required,min=6"`
|
||
}
|
||
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
response.BadRequest(c, "请求参数错误")
|
||
return
|
||
}
|
||
|
||
// 获取管理员ID
|
||
adminID := uint(0)
|
||
if id, exists := c.Get("user_id"); exists {
|
||
adminID = id.(uint)
|
||
}
|
||
|
||
if err := h.userService.ResetUserPasswordByAdmin(uint(id), req.NewPassword, adminID); err != nil {
|
||
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
||
return
|
||
}
|
||
|
||
response.Success(c, nil)
|
||
}
|
||
|
||
// ExportUsers 导出用户数据
|
||
func (h *AdminUserHandler) ExportUsers(c *gin.Context) {
|
||
startDate := c.Query("start_date")
|
||
endDate := c.Query("end_date")
|
||
status := c.Query("status")
|
||
keyword := c.Query("keyword")
|
||
|
||
// 构建查询条件
|
||
conditions := make(map[string]interface{})
|
||
if status != "" {
|
||
conditions["status"] = status
|
||
}
|
||
if keyword != "" {
|
||
conditions["keyword"] = keyword
|
||
}
|
||
if startDate != "" {
|
||
conditions["start_date"] = startDate
|
||
}
|
||
if endDate != "" {
|
||
conditions["end_date"] = endDate
|
||
}
|
||
|
||
// 获取所有符合条件的用户(不分页)
|
||
users, _, err := h.userService.GetUserListForAdmin(1, 10000, conditions)
|
||
if err != nil {
|
||
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
||
return
|
||
}
|
||
|
||
// 设置响应头
|
||
c.Header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
|
||
c.Header("Content-Disposition", "attachment; filename=users_"+time.Now().Format("20060102150405")+".xlsx")
|
||
|
||
// 简单的CSV格式导出
|
||
csvData := "用户ID,昵称,手机号,邮箱,状态,注册时间\n"
|
||
for _, user := range users {
|
||
csvData += strconv.Itoa(int(user.ID)) + "," +
|
||
user.Nickname + "," +
|
||
user.Phone + "," +
|
||
user.Email + "," +
|
||
strconv.Itoa(int(user.Status)) + "," +
|
||
user.CreatedAt.Format("2006-01-02 15:04:05") + "\n"
|
||
}
|
||
|
||
c.String(200, csvData)
|
||
}
|
||
|
||
// UpdateUserProfile 更新用户资料
|
||
func (h *AdminUserHandler) UpdateUserProfile(c *gin.Context) {
|
||
idStr := c.Param("id")
|
||
id, err := strconv.ParseUint(idStr, 10, 32)
|
||
if err != nil {
|
||
response.BadRequest(c, "无效的用户ID")
|
||
return
|
||
}
|
||
|
||
var req struct {
|
||
Nickname string `json:"nickname"`
|
||
Avatar string `json:"avatar"`
|
||
Gender int `json:"gender"`
|
||
Phone string `json:"phone"`
|
||
Email string `json:"email"`
|
||
Birthday string `json:"birthday"`
|
||
}
|
||
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
response.BadRequest(c, "请求参数错误")
|
||
return
|
||
}
|
||
|
||
// 构建更新数据
|
||
updates := make(map[string]interface{})
|
||
if req.Nickname != "" {
|
||
updates["nickname"] = req.Nickname
|
||
}
|
||
if req.Avatar != "" {
|
||
updates["avatar"] = req.Avatar
|
||
}
|
||
if req.Gender != 0 {
|
||
updates["gender"] = req.Gender
|
||
}
|
||
if req.Phone != "" {
|
||
updates["phone"] = req.Phone
|
||
}
|
||
if req.Email != "" {
|
||
updates["email"] = req.Email
|
||
}
|
||
if req.Birthday != "" {
|
||
updates["birthday"] = req.Birthday
|
||
}
|
||
|
||
// 获取管理员ID
|
||
adminID := uint(0)
|
||
if id, exists := c.Get("user_id"); exists {
|
||
adminID = id.(uint)
|
||
}
|
||
|
||
err = h.userService.UpdateUserProfileByAdmin(uint(id), updates, adminID)
|
||
if err != nil {
|
||
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
||
return
|
||
}
|
||
|
||
response.Success(c, nil)
|
||
}
|
||
|
||
// GetUserLevelInfo 获取用户等级信息
|
||
func (h *AdminUserHandler) GetUserLevelInfo(c *gin.Context) {
|
||
idStr := c.Param("id")
|
||
userID, err := strconv.ParseUint(idStr, 10, 32)
|
||
if err != nil {
|
||
response.BadRequest(c, "无效的用户ID")
|
||
return
|
||
}
|
||
|
||
levelInfo, err := h.userService.GetUserLevelInfo(uint(userID))
|
||
if err != nil {
|
||
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
||
return
|
||
}
|
||
|
||
response.Success(c, levelInfo)
|
||
}
|
||
|
||
// UpdateUserLevel 更新用户等级
|
||
func (h *AdminUserHandler) UpdateUserLevel(c *gin.Context) {
|
||
idStr := c.Param("id")
|
||
id, err := strconv.ParseUint(idStr, 10, 32)
|
||
if err != nil {
|
||
response.BadRequest(c, "无效的用户ID")
|
||
return
|
||
}
|
||
|
||
var req struct {
|
||
Level uint8 `json:"level" binding:"required"`
|
||
Remark string `json:"remark"`
|
||
}
|
||
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
response.BadRequest(c, "请求参数错误")
|
||
return
|
||
}
|
||
|
||
// 获取管理员ID
|
||
adminID := uint(0)
|
||
if id, exists := c.Get("user_id"); exists {
|
||
adminID = id.(uint)
|
||
}
|
||
|
||
err = h.userService.UpdateUserLevelByAdmin(uint(id), req.Level, req.Remark, adminID)
|
||
if err != nil {
|
||
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
||
return
|
||
}
|
||
|
||
response.Success(c, nil)
|
||
}
|
||
|
||
// GetUserLoginLogs 获取用户登录日志
|
||
func (h *AdminUserHandler) GetUserLoginLogs(c *gin.Context) {
|
||
idStr := c.Param("id")
|
||
userID, err := strconv.ParseUint(idStr, 10, 32)
|
||
if err != nil {
|
||
response.BadRequest(c, "无效的用户ID")
|
||
return
|
||
}
|
||
|
||
page := utils.StringToInt(c.DefaultQuery("page", "1"))
|
||
pageSize := utils.StringToInt(c.DefaultQuery("page_size", "10"))
|
||
|
||
if page <= 0 {
|
||
page = 1
|
||
}
|
||
if pageSize <= 0 || pageSize > 100 {
|
||
pageSize = 10
|
||
}
|
||
|
||
logs, pagination, err := h.userService.GetUserLoginLogs(uint(userID), page, pageSize)
|
||
if err != nil {
|
||
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
||
return
|
||
}
|
||
|
||
response.Success(c, gin.H{
|
||
"list": logs,
|
||
"pagination": pagination,
|
||
})
|
||
}
|
||
|
||
// DeleteUser 删除用户
|
||
func (h *AdminUserHandler) DeleteUser(c *gin.Context) {
|
||
idStr := c.Param("id")
|
||
id, err := strconv.ParseUint(idStr, 10, 32)
|
||
if err != nil {
|
||
response.BadRequest(c, "无效的用户ID")
|
||
return
|
||
}
|
||
|
||
// 获取管理员ID
|
||
adminID := uint(0)
|
||
if adminIDValue, exists := c.Get("user_id"); exists {
|
||
adminID = adminIDValue.(uint)
|
||
}
|
||
|
||
err = h.userService.DeleteUserByAdmin(uint(id), adminID, "管理员删除用户")
|
||
if err != nil {
|
||
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
||
return
|
||
}
|
||
|
||
response.Success(c, nil)
|
||
}
|
||
|
||
// BatchDeleteUsers 批量删除用户
|
||
func (h *AdminUserHandler) BatchDeleteUsers(c *gin.Context) {
|
||
var req struct {
|
||
IDs []uint `json:"ids" binding:"required"`
|
||
}
|
||
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
response.BadRequest(c, "请求参数错误")
|
||
return
|
||
}
|
||
|
||
if len(req.IDs) == 0 {
|
||
response.BadRequest(c, "用户ID列表不能为空")
|
||
return
|
||
}
|
||
|
||
// 获取管理员ID
|
||
adminID := uint(0)
|
||
if adminIDValue, exists := c.Get("user_id"); exists {
|
||
adminID = adminIDValue.(uint)
|
||
}
|
||
|
||
err := h.userService.BatchDeleteUsersByAdmin(req.IDs, adminID, "管理员批量删除用户")
|
||
if err != nil {
|
||
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
||
return
|
||
}
|
||
|
||
response.Success(c, nil)
|
||
} |