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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
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.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.category.support.SellerShopContext;
import com.ruoyi.web.modules.order.dto.SellerReviewQuery;
import com.ruoyi.web.modules.order.dto.SellerReviewReplyDTO;
import com.ruoyi.web.modules.order.service.ISellerReviewService;
import com.ruoyi.web.modules.store.constant.ShopConstants;

/**
 * 商家端评价管理
 */
@RestController
@RequestMapping("/agri/seller/review")
public class SellerReviewController extends BaseController
{
    @Autowired
    private ISellerReviewService sellerReviewService;

    @PreAuthorize("@ss.hasPermi('agri:seller:review:list')")
    @GetMapping("/list")
    public TableDataInfo list(SellerReviewQuery query)
    {
        startPage();
        return getDataTable(sellerReviewService.selectSellerList(requireShopId(), query));
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:review:query')")
    @GetMapping("/{reviewId}")
    public AjaxResult detail(@PathVariable("reviewId") Long reviewId)
    {
        return success(sellerReviewService.getSellerDetail(requireShopId(), reviewId));
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:review:reply')")
    @Log(title = "商家评价回复", businessType = BusinessType.UPDATE)
    @PutMapping("/{reviewId}/reply")
    public AjaxResult reply(@PathVariable("reviewId") Long reviewId, @RequestBody SellerReviewReplyDTO dto)
    {
        sellerReviewService.reply(requireShopId(), reviewId, dto, getUsername());
        return success();
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:review:remove')")
    @Log(title = "商家评价删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{reviewId}")
    public AjaxResult remove(@PathVariable("reviewId") Long reviewId)
    {
        sellerReviewService.deleteReview(requireShopId(), reviewId, getUsername());
        return success();
    }

    private Long requireShopId()
    {
        Long shopId = SellerShopContext.getShopId();
        if (shopId == null || shopId <= 0)
        {
            throw new ServiceException(ShopConstants.MSG_SHOP_CONTEXT_REQUIRED);
        }
        return shopId;
    }
}
