package com.jxy.order.controller;

import com.github.pagehelper.PageHelper;
import com.jxy.common.core.utils.StringUtils;
import com.jxy.common.core.web.Result.AjaxResult;
import com.jxy.common.core.web.controller.BaseController;
import com.jxy.common.core.web.page.TableDataInfo;
import com.jxy.common.security.annotation.Logical;
import com.jxy.common.security.annotation.RequiresLogin;
import com.jxy.common.security.annotation.RequiresRoles;
import com.jxy.order.service.WithdrawService;
import com.jxy.order.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;


/**
 * @Description TODO
 * @Date 2023/5/4 14:17
 * @Author 杜懿
 */
@RestController
@RequestMapping("/withdraw")
public class WithdrawController extends BaseController {

    @Autowired
    private WithdrawService withdrawService;

    @Autowired
    private RedisTemplate redisTemplate;


    //提现申请
    @PostMapping("/apply")
    @RequiresLogin
    public AjaxResult apply(@RequestBody WithdrawApplyVo vo){

        return  withdrawService.apply(vo);

    }

    //获取个人提现历史
    @PostMapping("/history/pravite")
    @RequiresLogin
    public TableDataInfo pravite(@RequestBody(required = false) OrderStatusVo vo){
        if(StringUtils.isNull(vo)){
            vo = new OrderStatusVo();
        }
        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());
        return getDataTable( withdrawService.privateWithdraw(vo));
    }

    //获取后台历史
    @PostMapping("/history/all")
    @RequiresRoles(value = {"admin", "accounting"}, logical = Logical.OR)
    public TableDataInfo allHistory(@RequestBody(required = false) OrderStatusVo vo){
        if(StringUtils.isNull(vo)){
            vo = new OrderStatusVo();
        }
        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());
        return getDataTable( withdrawService.getAllWithdraw(vo));
    }

    @PostMapping("/getDetail")
    @RequiresRoles(value = {"admin", "accounting"}, logical = Logical.OR)
    public AjaxResult handle(@RequestBody WithdrawIdVo vo){

        if(StringUtils.isNull(vo.getId())){
            return AjaxResult.error("传参异常");
        }

        return  withdrawService.getDetail(vo.getId());

    }

    @PostMapping("/handle")
    @RequiresRoles(value = {"admin", "accounting"}, logical = Logical.OR)
    public AjaxResult handle(@RequestBody WithdrawHandleVo vo){
        return  withdrawService.handle(vo);

    }

    @PostMapping("/getEmailCode")
    @RequiresLogin
    public AjaxResult getEmailCode(){

        return  withdrawService.sendAddCreditEmail();

    }
}
