package com.ruoyi.web.modules.order.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.account.support.MemberContext;
import com.ruoyi.web.modules.order.dto.OrderAftersaleSubmitDTO;
import com.ruoyi.web.modules.order.service.IOrderAftersaleAppService;

/**
 * C 端订单售后（须会员 Token）
 */
@RestController
@RequestMapping("/api/order/aftersale")
public class OrderAftersaleAppController extends BaseController
{
    @Autowired
    private IOrderAftersaleAppService aftersaleAppService;

    @GetMapping("/list")
    public TableDataInfo list(@RequestParam(value = "tab", required = false) String tab)
    {
        startPage();
        return getDataTable(aftersaleAppService.list(requireMemberId(), tab));
    }

    @GetMapping("/{aftersaleId}")
    public AjaxResult detail(@PathVariable("aftersaleId") Long aftersaleId)
    {
        return success(aftersaleAppService.getDetail(requireMemberId(), aftersaleId));
    }

    @PostMapping("/submit")
    public AjaxResult submit(@RequestBody OrderAftersaleSubmitDTO dto)
    {
        return success(aftersaleAppService.submit(requireMemberId(), dto));
    }

    private Long requireMemberId()
    {
        Long memberId = MemberContext.getMemberId();
        if (memberId == null)
        {
            throw new ServiceException("请先登录", 401);
        }
        return memberId;
    }
}
