package com.ruoyi.xq.controller.app;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.PageModel;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.BeanConvertUtil;
import com.ruoyi.xq.domain.WithdrawSetting;
import com.ruoyi.xq.dto.app.common.IdReq;
import com.ruoyi.xq.dto.app.setting.WithdrawSettingVo;
import com.ruoyi.xq.dto.app.withdraw.WithdrawListAppVo;
import com.ruoyi.xq.dto.app.withdraw.WithdrawListPageQuery;
import com.ruoyi.xq.service.UserWithdrawService;
import com.ruoyi.xq.service.WithdrawSettingService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/withdraw")
@Tag(name = "提现接口")
public class WithdrawAppController {

    @Autowired
    private WithdrawSettingService withdrawSettingService;
    @Autowired
    private UserWithdrawService userWithdrawService;

    @GetMapping("/setting")
    @Operation(summary = "获取提现配置")
    @Log(title = "获取提现配置", businessType = BusinessType.OTHER, isSaveDb = false)
    public R<List<WithdrawSettingVo>> vipHome(){
        List<WithdrawSetting> list = withdrawSettingService.list(Wrappers.lambdaQuery(WithdrawSetting.class)
                .eq(WithdrawSetting::getEnableStatus, 1)
                .orderByAsc(WithdrawSetting::getMoney));
        List<WithdrawSettingVo> result = BeanConvertUtil.convertListTo(list, WithdrawSettingVo::new);
        return R.ok(result);
    }


    @PostMapping("/save")
    @Operation(summary = "申请提现")
    @Log(title = "申请提现", businessType = BusinessType.OTHER, isSaveDb = false)
    public R<Void> save(@RequestBody IdReq req){
        userWithdrawService.saveWithdraw(LoginHelper.getUserId(),req.getId());
        return R.ok();
    }

    @GetMapping("/logs/page")
    @Operation(summary = "提现记录")
    @Log(title = "申请记录", businessType = BusinessType.OTHER, isSaveDb = false)
    public R<PageModel<WithdrawListAppVo>> logsPage(PageQuery pageQuery, WithdrawListPageQuery query){
        query.setUserId(LoginHelper.getUserId());
        Page<WithdrawListAppVo> result = userWithdrawService.pageApp(pageQuery,query);
        return R.ok(PageModel.build(result));
    }


}
