217 lines
5.8 KiB
Go
217 lines
5.8 KiB
Go
package handler
|
|
|
|
import (
|
|
"dianshang/internal/model"
|
|
"dianshang/internal/service"
|
|
"dianshang/pkg/response"
|
|
"dianshang/pkg/utils"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// AfterSaleHandler 售后处理器
|
|
type AfterSaleHandler struct {
|
|
afterSaleService *service.AfterSaleService
|
|
}
|
|
|
|
// NewAfterSaleHandler 创建售后处理器
|
|
func NewAfterSaleHandler(afterSaleService *service.AfterSaleService) *AfterSaleHandler {
|
|
return &AfterSaleHandler{
|
|
afterSaleService: afterSaleService,
|
|
}
|
|
}
|
|
|
|
// AfterSaleResponse 售后响应结构
|
|
type AfterSaleResponse struct {
|
|
ID uint `json:"id"`
|
|
OrderID uint `json:"order_id"`
|
|
OrderItemID uint `json:"order_item_id"`
|
|
UserID uint `json:"user_id"`
|
|
Type int `json:"type"`
|
|
TypeName string `json:"type_name"`
|
|
Reason string `json:"reason"`
|
|
Description string `json:"description"`
|
|
Images []string `json:"images"`
|
|
Status int `json:"status"`
|
|
StatusName string `json:"status_name"`
|
|
AdminRemark string `json:"admin_remark"`
|
|
CreatedAt string `json:"created_at"`
|
|
UpdatedAt string `json:"updated_at"`
|
|
Order *OrderBasicInfo `json:"order,omitempty"`
|
|
OrderItem *OrderItemBasicInfo `json:"order_item,omitempty"`
|
|
}
|
|
|
|
// OrderBasicInfo 订单基本信息
|
|
type OrderBasicInfo struct {
|
|
ID uint `json:"id"`
|
|
OrderNo string `json:"order_no"`
|
|
Status int `json:"status"`
|
|
}
|
|
|
|
// OrderItemBasicInfo 订单项基本信息
|
|
type OrderItemBasicInfo struct {
|
|
ID uint `json:"id"`
|
|
ProductName string `json:"product_name"`
|
|
ProductImage string `json:"product_image"`
|
|
SpecName string `json:"spec_name"`
|
|
SpecValue string `json:"spec_value"`
|
|
Quantity int `json:"quantity"`
|
|
Price float64 `json:"price"`
|
|
}
|
|
|
|
// GetAfterSaleList 获取售后列表
|
|
func (h *AfterSaleHandler) GetAfterSaleList(c *gin.Context) {
|
|
userID, exists := c.Get("user_id")
|
|
if !exists {
|
|
response.Unauthorized(c)
|
|
return
|
|
}
|
|
|
|
page := utils.StringToInt(c.DefaultQuery("page", "1"))
|
|
pageSize := utils.StringToInt(c.DefaultQuery("pageSize", "10"))
|
|
status := utils.StringToInt(c.Query("status"))
|
|
|
|
afterSales, total, err := h.afterSaleService.GetUserAfterSales(userID.(uint), page, pageSize, status)
|
|
if err != nil {
|
|
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
|
return
|
|
}
|
|
|
|
// 转换为响应格式
|
|
result := make([]AfterSaleResponse, len(afterSales))
|
|
for i, afterSale := range afterSales {
|
|
result[i] = h.convertToResponse(afterSale)
|
|
}
|
|
|
|
// 返回分页数据
|
|
responseData := gin.H{
|
|
"list": result,
|
|
"total": total,
|
|
"page": page,
|
|
"pageSize": pageSize,
|
|
}
|
|
|
|
response.Success(c, responseData)
|
|
}
|
|
|
|
// GetAfterSaleDetail 获取售后详情
|
|
func (h *AfterSaleHandler) GetAfterSaleDetail(c *gin.Context) {
|
|
userID, exists := c.Get("user_id")
|
|
if !exists {
|
|
response.Unauthorized(c)
|
|
return
|
|
}
|
|
|
|
afterSaleID := utils.StringToUint(c.Param("id"))
|
|
if afterSaleID == 0 {
|
|
response.BadRequest(c, "无效的售后ID")
|
|
return
|
|
}
|
|
|
|
afterSale, err := h.afterSaleService.GetAfterSaleDetail(userID.(uint), afterSaleID)
|
|
if err != nil {
|
|
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
|
return
|
|
}
|
|
|
|
result := h.convertToResponse(*afterSale)
|
|
response.Success(c, result)
|
|
}
|
|
|
|
// CreateAfterSale 创建售后申请
|
|
func (h *AfterSaleHandler) CreateAfterSale(c *gin.Context) {
|
|
userID, exists := c.Get("user_id")
|
|
if !exists {
|
|
response.Unauthorized(c)
|
|
return
|
|
}
|
|
|
|
var req service.CreateAfterSaleRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.BadRequest(c, err.Error())
|
|
return
|
|
}
|
|
|
|
afterSale, err := h.afterSaleService.CreateAfterSale(userID.(uint), &req)
|
|
if err != nil {
|
|
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
|
return
|
|
}
|
|
|
|
result := h.convertToResponse(*afterSale)
|
|
response.Success(c, result)
|
|
}
|
|
|
|
// convertToResponse 转换为响应格式
|
|
func (h *AfterSaleHandler) convertToResponse(afterSale model.AfterSale) AfterSaleResponse {
|
|
// 类型名称映射
|
|
typeName := "未知类型"
|
|
switch afterSale.Type {
|
|
case 1:
|
|
typeName = "退货"
|
|
case 2:
|
|
typeName = "换货"
|
|
case 3:
|
|
typeName = "维修"
|
|
}
|
|
|
|
// 状态名称映射
|
|
statusName := "未知状态"
|
|
switch afterSale.Status {
|
|
case 1:
|
|
statusName = "待审核"
|
|
case 2:
|
|
statusName = "已同意"
|
|
case 3:
|
|
statusName = "已拒绝"
|
|
case 4:
|
|
statusName = "处理中"
|
|
case 5:
|
|
statusName = "已完成"
|
|
}
|
|
|
|
// 转换图片数组
|
|
images := make([]string, len(afterSale.Images))
|
|
copy(images, afterSale.Images)
|
|
|
|
result := AfterSaleResponse{
|
|
ID: afterSale.ID,
|
|
OrderID: afterSale.OrderID,
|
|
OrderItemID: afterSale.OrderItemID,
|
|
UserID: afterSale.UserID,
|
|
Type: afterSale.Type,
|
|
TypeName: typeName,
|
|
Reason: afterSale.Reason,
|
|
Description: afterSale.Description,
|
|
Images: images,
|
|
Status: afterSale.Status,
|
|
StatusName: statusName,
|
|
AdminRemark: afterSale.AdminRemark,
|
|
CreatedAt: afterSale.CreatedAt.Format("2006-01-02 15:04:05"),
|
|
UpdatedAt: afterSale.UpdatedAt.Format("2006-01-02 15:04:05"),
|
|
}
|
|
|
|
// 添加订单信息
|
|
if afterSale.Order.ID > 0 {
|
|
result.Order = &OrderBasicInfo{
|
|
ID: afterSale.Order.ID,
|
|
OrderNo: afterSale.Order.OrderNo,
|
|
Status: afterSale.Order.Status,
|
|
}
|
|
}
|
|
|
|
// 添加订单项信息
|
|
if afterSale.OrderItem.ID > 0 {
|
|
result.OrderItem = &OrderItemBasicInfo{
|
|
ID: afterSale.OrderItem.ID,
|
|
ProductName: afterSale.OrderItem.ProductName,
|
|
ProductImage: afterSale.OrderItem.ProductImage,
|
|
SpecName: afterSale.OrderItem.SpecName,
|
|
SpecValue: afterSale.OrderItem.SpecValue,
|
|
Quantity: afterSale.OrderItem.Quantity,
|
|
Price: afterSale.OrderItem.Price,
|
|
}
|
|
}
|
|
|
|
return result
|
|
} |