package com.sqx.modules.user.controller;

import com.sqx.common.utils.PageUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.annotation.Login;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.pay.controller.query.CashOutQueryDTO;
import com.sqx.modules.pay.service.CashOutService;
import com.sqx.modules.sys.controller.AbstractController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = {"用户端-用户钱包"})
@RestController
@RequestMapping("/app/userMoney/")
public class UserShopMoneyController extends AbstractController {
    @Autowired
    CashOutService cashOutService;
    @Autowired
    UserService userService;

    @RequestMapping(value = "selectCashOutList", method = RequestMethod.GET)
    @ApiOperation("查询提现记录列表")
    @ResponseBody
    @Login
    public Result selectHelpProfit(Integer page, Integer limit,@RequestAttribute("userId") Long userId) {
        CashOutQueryDTO queryDTO = new CashOutQueryDTO();
        if (limit != null) {
            queryDTO.setLimit(limit);
        }

        if (page != null) {
            queryDTO.setPage(page);
        }
        queryDTO.setUserId(userId);
        PageUtils pageUtils = cashOutService.selectCashOutList(queryDTO);
        return Result.success().put("data", pageUtils);
    }
    @Login
    @GetMapping("info")
    public Result info(@RequestAttribute("userId") Long userId) {
        UserEntity userEntity = userService.selectUserById(userId);
        return Result.success().put("data", userEntity);
    }

    @Login
    @RequestMapping(value = "updateUser", method = RequestMethod.POST)
    @ApiOperation("修改当前登录用户的提现信息")
    public Result updateUser(@RequestAttribute("userId") Long userId,@RequestBody UserEntity userEntity) {
        userEntity.setUserId(userId);
        userService.updateById(userEntity);
        return Result.success();
    }
    @Login
    @GetMapping(value = "userCashMoney")
    @ApiOperation("用户发起提现")
    public Result shopCashMoney(Double money, Integer classify,@RequestAttribute("userId") Long userId) {
        UserEntity userEntity =userService.selectUserById(userId);

        return cashOutService.cashMoney(userEntity.getUserId(),null,money,classify, 1);
    }
    @Login
    @GetMapping(value = "shopBindingQrCode")
    @ApiOperation("商户绑定提现二维码")
    public Result shopBindingQrCode(String cashQrCode,@RequestAttribute("userId") Long userId){
        UserEntity user = userService.selectUserById(userId);
        user.setCashQrCode(cashQrCode);
        userService.updateById(user);
        return Result.success();
    }
}
