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.OrderReviewSubmitDTO;
import com.ruoyi.web.modules.order.service.IOrderReviewAppService;

/**
 * C 端订单评价（须会员 Token）
 */
@RestController
@RequestMapping("/api/order")
public class OrderReviewAppController extends BaseController
{
    @Autowired
    private IOrderReviewAppService reviewAppService;

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

    @GetMapping("/{orderId}/review")
    public AjaxResult getByOrder(@PathVariable("orderId") Long orderId,
            @RequestParam(value = "orderItemId", required = false) Long orderItemId)
    {
        return success(reviewAppService.getByOrder(requireMemberId(), orderId, orderItemId));
    }

    @PostMapping("/{orderId}/review")
    public AjaxResult submit(@PathVariable("orderId") Long orderId, @RequestBody OrderReviewSubmitDTO dto)
    {
        reviewAppService.submit(requireMemberId(), orderId, dto);
        return success();
    }

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