package com.ruoyi.web.controller.app;

import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.ValidGroup;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.auth.wx.WxAuthService;
import com.ruoyi.ss.account.domain.Account;
import com.ruoyi.ss.account.domain.AccountQuery;
import com.ruoyi.ss.account.domain.AccountVO;
import com.ruoyi.ss.account.domain.enums.AccountType;
import com.ruoyi.ss.account.service.AccountService;
import com.ruoyi.ss.account.service.AccountValidator;
import com.ruoyi.ss.channelWithdraw.domain.ChannelWithdrawVO;
import com.ruoyi.ss.channelWithdraw.service.ChannelWithdrawService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.stream.Collectors;

/**
 * @author wjh
 * 2024/3/26
 */
@RestController
@RequestMapping("/app/account")
public class AppAccountController extends BaseController {

    @Autowired
    private AccountService accountService;

    @Autowired
    private AccountValidator accountValidator;

    @Autowired
    private WxAuthService wxAuthService;

    @Autowired
    private ChannelWithdrawService channelWithdrawService;

    @ApiOperation("添加收款账户")
    @PostMapping
    public AjaxResult addAccount(@RequestBody @Validated({ValidGroup.FrontCreate.class}) Account data) {
        // 微信，获取微信openId并替换数据
        if (AccountType.WECHAT.getType().equals(data.getAccountType())) {
            WxMaJscode2SessionResult result = wxAuthService.wxJsCode2Session(data.getAccountNo());
            ServiceUtil.assertion(result == null, "获取微信信息失败");
            data.setAccountNo(result.getOpenid());
        }

        data.setUserId(getUserId());
        return success(accountService.insertSmAccount(data));
    }

    @ApiOperation("修改收款账户")
    @PutMapping
    public AjaxResult updateAccount(@RequestBody @Validated({ValidGroup.FrontUpdate.class}) Account data) {

        // 微信，获取微信openId并替换数据
        if (AccountType.WECHAT.getType().equals(data.getAccountType()) && StringUtils.hasText(data.getAccountNo())) {
            WxMaJscode2SessionResult result = wxAuthService.wxJsCode2Session(data.getAccountNo());
            ServiceUtil.assertion(result == null, "获取微信信息失败");
            data.setAccountNo(result.getOpenid());
        }

        data.setUserId(getUserId());
        return success(accountService.updateSmAccount(data));
    }

    @ApiOperation("查询本人收款账户列表")
    @GetMapping
    public AjaxResult list(AccountQuery dto) {
        dto.setUserId(getUserId());
        return success(accountService.selectSmAccountGroup(dto));
    }

    @ApiOperation("设置账户为默认账户")
    @PutMapping("/{accountId}/setDefault")
    public AjaxResult list(@PathVariable @ApiParam("账户id") Long accountId) {
        return success(accountService.setDefault(getUserId(), accountId));
    }

    @ApiOperation("查询收款账户详情")
    @GetMapping("/{accountId}")
    public AjaxResult getInfo(@PathVariable Long accountId) {
        AccountVO account = accountService.selectSmAccountByAccountId(accountId);
        if (!accountValidator.isBelong(account, getUserId())) {
            return error("这不是您的收款账户，无权查看");
        }
        account.desensitization();
        return success(account);
    }

}
