package com.ruoyi.web.controller.system;

import com.alibaba.fastjson2.JSON;
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.utils.poi.ExcelUtil;
import com.ruoyi.ss.userWithdraw.domain.UserWithdraw;
import com.ruoyi.ss.userWithdraw.service.IUserWithdrawService;
import com.ruoyi.ss.withdraw.domain.Withdraw;
import com.ruoyi.ss.withdraw.domain.WithdrawQuery;
import com.ruoyi.ss.withdraw.domain.WithdrawVO;
import com.ruoyi.ss.withdraw.domain.dto.WithdrawApprovalDTO;
import com.ruoyi.ss.withdraw.service.IWithdrawService;
import io.swagger.annotations.ApiOperation;
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.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 提现记录Controller
 *
 * @author qzz
 * @date 2024-09-28
 */
@RestController
@RequestMapping("/system/withdraw")
public class WithdrawController extends BaseController
{
    @Autowired
    private IWithdrawService withdrawService;

    @Autowired
    private IUserWithdrawService userWithdrawService;


    /**
     * 查询提现记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:withdraw:list')")
    @GetMapping("/list")
    public TableDataInfo list(WithdrawQuery rlWithdraw)
    {
        startPage();
        List<Withdraw> list = withdrawService.selectRlWithdrawList(rlWithdraw);
        return getDataTable(list);
    }

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

    /**
     * 获取提现记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:withdraw:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        WithdrawVO withdrawVO = withdrawService.selectRlWithdrawById(id);
        if (withdrawVO != null) {
            UserWithdraw userWithdraw = userWithdrawService.selectRlUserWithdrawByUserChannelId(withdrawVO.getUserWithdrawChannelId());
            withdrawVO.setUserWithdraw(userWithdraw);
        }
        return success(withdrawVO);
    }

    /**
     * 新增提现记录
     */
    @PreAuthorize("@ss.hasPermi('system:withdraw:add')")
    @Log(title = "提现记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Withdraw rlWithdraw)
    {
        return toAjax(withdrawService.insertRlWithdraw(rlWithdraw));
    }

    /**
     * 修改提现记录
     */
    @PreAuthorize("@ss.hasPermi('system:withdraw:edit')")
    @Log(title = "提现记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Withdraw rlWithdraw)
    {
        return toAjax(withdrawService.updateRlWithdraw(rlWithdraw));
    }

    /**
     * 删除提现记录
     */
    @PreAuthorize("@ss.hasPermi('system:withdraw:remove')")
    @Log(title = "提现记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(withdrawService.deleteRlWithdrawByIds(ids));
    }

    @ApiOperation("同意提现申请")
//    @PreAuthorize("@ss.hasPermi('system:bill:approval')")
    @PutMapping("/pass")
    @Log(title = "同意提现申请", businessType = BusinessType.UPDATE)
    public AjaxResult passWithdraw(@RequestBody @Validated WithdrawApprovalDTO dto)
    {
        logger.info("同意提现申请：【{}】", JSON.toJSON(dto));
        return success(withdrawService.passWithdraw(dto));
    }

    @ApiOperation("拒绝提现申请")
//    @PreAuthorize("@ss.hasPermi('system:bill:approval')")
    @PutMapping("/reject")
    @Log(title = "拒绝提现申请", businessType = BusinessType.UPDATE)
    public AjaxResult rejectWithdraw(@RequestBody @Validated WithdrawApprovalDTO dto)
    {
        logger.info("拒绝提现申请：【{}】", JSON.toJSON(dto));
        return success(withdrawService.rejectWithdraw(dto));
    }
}
