package com.ruoyi.web.bst;

import java.math.BigDecimal;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.bst.withdraw.domain.Withdraw;
import com.ruoyi.bst.withdraw.domain.WithdrawQuery;
import com.ruoyi.bst.withdraw.domain.WithdrawVO;
import com.ruoyi.bst.withdraw.domain.dto.WithdrawVerifyDTO;
import com.ruoyi.bst.withdraw.service.WithdrawConverter;
import com.ruoyi.bst.withdraw.service.WithdrawService;
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.domain.vo.UserVO;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.LogBizType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.user.service.UserService;

/**
 * 提现Controller
 *
 * @author ruoyi
 * @date 2025-04-07
 */
@RestController
@RequestMapping("/bst/withdraw")
public class WithdrawController extends BaseController
{
    @Autowired
    private WithdrawService withdrawService;

    @Autowired
    private UserService userService;

    @Autowired
    private WithdrawConverter withdrawConverter;

    /**
     * 查询提现列表
     */
    @PreAuthorize("@ss.hasPermi('bst:withdraw:list')")
    @GetMapping("/list")
    public TableDataInfo list(WithdrawQuery query)
    {
        startPage();
        startOrderBy();
        query.setScope(true);
        List<WithdrawVO> list = withdrawService.selectWithdrawList(query);
        return getDataTable(list);
    }

    /**
     * 导出提现列表
     */
    @PreAuthorize("@ss.hasPermi('bst:withdraw:export')")
    @Log(title = "提现", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WithdrawQuery query)
    {
        query.setScope(true);
        List<WithdrawVO> list = withdrawService.selectWithdrawList(query);
        ExcelUtil<WithdrawVO> util = new ExcelUtil<WithdrawVO>(WithdrawVO.class);
        util.exportExcel(response, list, "提现数据");
    }

    /**
     * 获取提现详细信息
     */
    @PreAuthorize("@ss.hasPermi('bst:withdraw:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(withdrawService.selectWithdrawById(id));
    }

    /**
     * 申请提现
     */
    @PreAuthorize("@ss.hasPermi('bst:withdraw:add')")
    @Log(title = "申请提现", businessType = BusinessType.INSERT, bizIdName = "arg0", bizType = LogBizType.WITHDRAW)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated Withdraw withdraw) {
        // 若是不是系统管理员，则无权给他人提现
        if (!isSysAdmin() || withdraw.getUserId() == null) {
            withdraw.setUserId(getUserId());
        }
        return toAjax(withdrawService.insertWithdraw(withdraw));
    }

    @PreAuthorize("@ss.hasPermi('bst:withdraw:add')")
    @Log(title = "查询提现服务费", businessType = BusinessType.OTHER)
    @GetMapping("/calcAmount")
    public AjaxResult calcAmount(BigDecimal amount, @RequestParam(required = false) Long userId) {
        // 若是不是系统管理员，则无权查询他人的提现服务费
        if (!isSysAdmin() || userId == null) {
            userId = getUserId();
        }
        UserVO user = userService.selectUserById(userId);
        return success(withdrawConverter.calcAmount(user, amount));
    }

    @PreAuthorize("@ss.hasPermi('bst:withdraw:verify')")
    @Log(title = "提现审核", businessType = BusinessType.UPDATE, bizIdName = "arg0", bizType = LogBizType.WITHDRAW)
    @PutMapping("/verify")
    public AjaxResult verify(@RequestBody @Validated WithdrawVerifyDTO dto) {
        dto.setVerifyId(getUserId());
        return toAjax(withdrawService.verify(dto));
    }

}
