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.UserBankCard;
import com.ruoyi.xq.domain.WithdrawSetting;
import com.ruoyi.xq.dto.app.account.UpdateAliReq;
import com.ruoyi.xq.dto.app.account.WithdrawReq;
import com.ruoyi.xq.dto.app.common.IdReq;
import com.ruoyi.xq.dto.app.setting.WithdrawSettingVo;
import com.ruoyi.xq.dto.app.withdraw.WithdrawHomeSettingVo;
import com.ruoyi.xq.dto.app.withdraw.WithdrawListAppVo;
import com.ruoyi.xq.dto.app.withdraw.WithdrawListPageQuery;
import com.ruoyi.xq.enums.common.SystemConfigEnum;
import com.ruoyi.xq.manager.SystemConfigManager;
import com.ruoyi.xq.service.UserBankCardService;
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;
    @Autowired
    private UserBankCardService userBankCardService;
    @Autowired
    private SystemConfigManager systemConfigManager;

    @GetMapping("/setting")
    @Operation(summary = "获取提现配置")
    @Log(title = "获取提现配置", businessType = BusinessType.OTHER,isPrintResponseData = false, isSaveDb = false)
    public R<WithdrawHomeSettingVo> vipHome(){
        WithdrawHomeSettingVo vo = new WithdrawHomeSettingVo();
        List<WithdrawSetting> list = withdrawSettingService.list(Wrappers.lambdaQuery(WithdrawSetting.class)
                .eq(WithdrawSetting::getEnableStatus, 1)
                .orderByAsc(WithdrawSetting::getMoney));
        List<WithdrawSettingVo> result = BeanConvertUtil.convertListTo(list, WithdrawSettingVo::new);
        vo.setWithdrawSettingList(result);
        boolean openAli = systemConfigManager.getSystemConfigOfBool(SystemConfigEnum.OPEN_ALI_WITHDRAW);
        boolean openWx = systemConfigManager.getSystemConfigOfBool(SystemConfigEnum.OPEN_WX_WITHDRAW);
        vo.setOpenWx(openWx);
        vo.setOpenAli(openAli);
        return R.ok(vo);
    }

    @PostMapping("/ali/card")
    @Operation(summary = "修改支付宝信息")
    @Log(title = "修改支付宝信息", businessType = BusinessType.OTHER, isSaveDb = true)
    public R<Void> updateAliCard(@RequestBody UpdateAliReq req){
        req.setUserId(LoginHelper.getUserId());
        userBankCardService.updateAli(req);
        return R.ok();
    }

    @PostMapping("/card/info")
    @Operation(summary = "查询账户信息")
    @Log(title = "查询账户信息", businessType = BusinessType.OTHER, isPrintResponseData = false,isSaveDb = false)
    public R<UserBankCard> cardInfo(){
        UserBankCard userBankCard = userBankCardService.getByUserId(LoginHelper.getUserId());
        if(userBankCard == null){
            userBankCard = new UserBankCard();
        }
        return R.ok(userBankCard);
    }

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

    @GetMapping("/logs/page")
    @Operation(summary = "提现记录")
    @Log(title = "申请记录", businessType = BusinessType.OTHER, isPrintResponseData = false,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));
    }


}
