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.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.SellerAftersaleFinishDTO;
import com.ruoyi.web.modules.order.dto.SellerAftersaleQuery;
import com.ruoyi.web.modules.order.service.ISellerAftersaleService;
import com.ruoyi.web.modules.store.constant.ShopConstants;

/**
 * 商家端售后管理
 */
@RestController
@RequestMapping("/agri/seller/aftersale")
public class SellerAftersaleController extends BaseController
{
    @Autowired
    private ISellerAftersaleService sellerAftersaleService;

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

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

    @PreAuthorize("@ss.hasPermi('agri:seller:aftersale:finish')")
    @Log(title = "商家售后处理", businessType = BusinessType.UPDATE)
    @PutMapping("/{aftersaleId}/finish")
    public AjaxResult finish(@PathVariable("aftersaleId") Long aftersaleId,
            @RequestBody SellerAftersaleFinishDTO dto)
    {
        sellerAftersaleService.finish(requireShopId(), aftersaleId, dto, 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;
    }
}
