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

import java.util.HashMap;
import java.util.Map;
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.web.modules.finance.dto.PlatformWithdrawAuditQuery;
import com.ruoyi.web.modules.finance.dto.WithdrawAuditDTO;
import com.ruoyi.web.modules.finance.service.IShopFundService;

/**
 * 平台端提现审核
 */
@RestController
@RequestMapping("/agri/finance/withdrawAudit")
public class PlatformWithdrawAuditController extends BaseController
{
    @Autowired
    private IShopFundService shopFundService;

    @PreAuthorize("@ss.hasPermi('agri:finance:withdrawAudit:list')")
    @GetMapping("/list")
    public TableDataInfo list(PlatformWithdrawAuditQuery query)
    {
        startPage();
        return getDataTable(shopFundService.listPlatformWithdraws(query));
    }

    @PreAuthorize("@ss.hasPermi('agri:finance:withdrawAudit:list')")
    @GetMapping("/pendingCount")
    public AjaxResult pendingCount()
    {
        Map<String, Object> data = new HashMap<>();
        data.put("pendingCount", shopFundService.countPendingWithdraws());
        return success(data);
    }

    @PreAuthorize("@ss.hasPermi('agri:finance:withdrawAudit:audit')")
    @Log(title = "提现审核通过", businessType = BusinessType.UPDATE)
    @PutMapping("/approve/{withdrawId}")
    public AjaxResult approve(@PathVariable("withdrawId") Long withdrawId, @RequestBody WithdrawAuditDTO dto)
    {
        String remark = dto == null ? null : dto.getProcessRemark();
        shopFundService.approveWithdraw(withdrawId, remark, getUsername());
        return success();
    }

    @PreAuthorize("@ss.hasPermi('agri:finance:withdrawAudit:audit')")
    @Log(title = "提现审核驳回", businessType = BusinessType.UPDATE)
    @PutMapping("/reject")
    public AjaxResult reject(@RequestBody WithdrawAuditDTO dto)
    {
        shopFundService.rejectWithdraw(dto, getUsername());
        return success();
    }
}
