package com.ruoyi.web.modules.finance.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.PostMapping;
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.finance.dto.PayAccountSaveDTO;
import com.ruoyi.web.modules.finance.service.IShopFundService;
import com.ruoyi.web.modules.store.constant.ShopConstants;

/**
 * 商家端收款账户管理
 */
@RestController
@RequestMapping("/agri/seller/finance/payAccount")
public class SellerPayAccountController extends BaseController
{
    @Autowired
    private IShopFundService shopFundService;

    @PreAuthorize("@ss.hasPermi('agri:seller:finance:payAccount:list')")
    @GetMapping("/list")
    public TableDataInfo list()
    {
        startPage();
        return getDataTable(shopFundService.listPayAccounts(requireShopId()));
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:finance:withdraw:list')")
    @GetMapping("/options")
    public AjaxResult options()
    {
        return success(shopFundService.listPayAccountOptions(requireShopId()));
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:finance:payAccount:add')")
    @Log(title = "收款账户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PayAccountSaveDTO dto)
    {
        shopFundService.addPayAccount(requireShopId(), dto, getUsername());
        return success();
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:finance:payAccount:edit')")
    @Log(title = "收款账户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PayAccountSaveDTO dto)
    {
        shopFundService.updatePayAccount(requireShopId(), dto, getUsername());
        return success();
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:finance:payAccount:remove')")
    @Log(title = "收款账户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{accountId}")
    public AjaxResult remove(@PathVariable("accountId") Long accountId)
    {
        shopFundService.deletePayAccount(requireShopId(), accountId, 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;
    }
}
