package com.ruoyi.web.modules.store.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.store.constant.ShopConstants;
import com.ruoyi.web.modules.store.dto.SellerShopProfileDTO;
import com.ruoyi.web.modules.store.service.IShopService;

/**
 * 商家端店铺设置（资料 + 员工概览；修改后平台列表同源 biz_shop 自动一致）
 */
@RestController
@RequestMapping("/agri/seller/shop")
public class SellerShopController extends BaseController
{
    @Autowired
    private IShopService shopService;

    @PreAuthorize("@ss.hasPermi('agri:seller:shop:query')")
    @GetMapping
    public AjaxResult getProfile()
    {
        return success(shopService.selectSellerShopProfile(requireShopId()));
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:shop:edit')")
    @Log(title = "店铺资料", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult updateProfile(@RequestBody SellerShopProfileDTO dto)
    {
        shopService.updateSellerShopProfile(requireShopId(), dto, getUsername());
        return success();
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:shop:query')")
    @GetMapping("/employeeStats")
    public AjaxResult employeeStats()
    {
        return success(shopService.selectSellerEmployeeStats(requireShopId()));
    }

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