package com.ruoyi.web.modules.delivery.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.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.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.category.support.SellerShopContext;
import com.ruoyi.web.modules.delivery.dto.ShopDeliverySaveDTO;
import com.ruoyi.web.modules.delivery.service.IShopDeliveryService;
import com.ruoyi.web.modules.store.constant.ShopConstants;

/**
 * 商家端配送设置
 */
@RestController
@RequestMapping("/agri/seller/delivery")
public class SellerDeliveryController extends BaseController
{
    @Autowired
    private IShopDeliveryService shopDeliveryService;

    @PreAuthorize("@ss.hasPermi('agri:seller:delivery:query')")
    @GetMapping
    public AjaxResult getSetting()
    {
        return success(shopDeliveryService.getByShopId(requireShopId()));
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:delivery:edit')")
    @Log(title = "配送设置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult saveSetting(@RequestBody ShopDeliverySaveDTO dto)
    {
        shopDeliveryService.save(requireShopId(), 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;
    }
}
