package com.ruoyi.web.controller.app;

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.user.service.IUserService;
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.bo.WithdrawBO;
import com.ruoyi.ss.withdraw.domain.dto.MchWithdrawDTO;
import com.ruoyi.ss.withdraw.domain.dto.WithdrawApprovalDTO;
import com.ruoyi.ss.withdraw.service.IWithdrawConverter;
import com.ruoyi.ss.withdraw.service.IWithdrawService;
import com.ruoyi.ss.withdraw.service.IWithdrawValidator;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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


@RestController
@RequestMapping("/app/withdraw")
public class AppWithdrawController extends BaseController {

    @Autowired
    private IWithdrawService withdrawService;

    @Autowired
    private IWithdrawValidator withdrawValidator;

    @Autowired
    private IWithdrawConverter withdrawConverter;

    @Resource
    private IUserService userService;

    @Autowired
    private IUserWithdrawService userWithdrawService;


    /**
     * 商户提现
     */
    @Log(title = "商户提现", businessType = BusinessType.WITHDRAW)
    @PostMapping("/mch")
    public AjaxResult add(@RequestBody MchWithdrawDTO dto)
    {
        logger.info("商户提现请求：【{}】", JSON.toJSON(dto));
        WithdrawBO bo = withdrawValidator.preWithdraw(dto);
        return toAjax(userService.mchWithdraw(bo));
    }

    /**
     * 查询提现记录列表
     */
//    @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)
    {
        return success(withdrawService.selectRlWithdrawById(id));
    }


    /**
     *  根据token获取提现渠道的列表
     */
    @GetMapping("/getUserWithdrawChannelList")
    public AjaxResult getUserWithdrawChannelList(Long userId)
    {
        if (userId == null) {
            userId = getUserId();
        }
        logger.info("根据token获取提现渠道列表：【userId={}】", userId);
        return AjaxResult.success(userWithdrawService.selectRlUserWithdrawListByUserId(userId));
    }

    /**
     * 修改提现记录
     */
//    @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));
    }
}
