package com.jsowell.web.controller.pile;

import com.alibaba.fastjson2.JSON;
import com.huifu.adapay.core.exception.BaseAdaPayException;
import com.jsowell.adapay.dto.AdapayMemberInfoDTO;
import com.jsowell.adapay.dto.UpdateAccountConfigDTO;
import com.jsowell.adapay.service.AdapayMemberService;
import com.jsowell.adapay.vo.AdapayAccountBalanceVO;
import com.jsowell.common.core.controller.BaseController;
import com.jsowell.common.core.domain.AjaxResult;
import com.jsowell.common.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/adapay/member")
public class AdapayMemberController extends BaseController {

    @Autowired
    private AdapayMemberService adapayMemberService;

    /**
     * 创建汇付会员接口
     * http://localhost:8080/adapay/member/createAdapayMember
     */
    @PostMapping("/createAdapayMember")
    public AjaxResult createAdapayMember(@RequestBody AdapayMemberInfoDTO dto) {
        logger.info("创建汇付会员接口 param:{}", JSON.toJSONString(dto));
        AjaxResult result;
        try {
            adapayMemberService.createMember(dto);
            result = AjaxResult.success();
        } catch (BusinessException e) {
            logger.warn("创建汇付会员接口warn", e);
            result = AjaxResult.error(e.getMessage());
        } catch (Exception e) {
            logger.warn("创建汇付会员接口error", e);
            result = AjaxResult.error("创建汇付会员接口异常");
        }
        return result;
    }

    /**
     * 查询汇付会员接口
     * http://localhost:8080/adapay/member/selectAdapayMember
     */
    @PostMapping("/selectAdapayMember")
    public AjaxResult selectAdapayMember(@RequestBody AdapayMemberInfoDTO dto) {
        AjaxResult result;
        try {
            Map<String, Object> map = adapayMemberService.selectAdapayMember(dto.getMerchantId());
            result = AjaxResult.success(map);
        } catch (Exception e) {
            logger.error("查询汇付会员接口异常", e);
            result = AjaxResult.error("查询汇付会员接口异常");
        }
        return result;
    }

    /**
     * 查询汇付账户余额
     * http://localhost:8080/adapay/member/queryAdapayAccountBalance
     */
    @PostMapping("/queryAdapayAccountBalance")
    public AjaxResult queryAdapayAccountBalance(@RequestBody AdapayMemberInfoDTO dto) {
        AjaxResult result;
        try {
            AdapayAccountBalanceVO vo = adapayMemberService.queryAdapayAccountBalance(dto.getMerchantId());
            result = AjaxResult.success(vo);
        } catch (BaseAdaPayException e) {
            logger.error("查询汇付账户余额error", e);
            result = AjaxResult.error();
        }
        return result;
    }

    /**
     * 修改结算
     * http://localhost:8080/adapay/member/updateSettleAccountConfig
     * 修改商户或用户结算账户的起始金额、结算留存金额、结算信息摘要等 配置参数
     */
    @PostMapping("/updateSettleAccountConfig")
    public AjaxResult updateSettleAccountConfig(@RequestBody UpdateAccountConfigDTO dto) {
        AjaxResult result;
        try {
            adapayMemberService.updateSettleAccountConfig(dto);
            result = AjaxResult.success();
        } catch (BaseAdaPayException e) {
            logger.error("查询汇付账户余额error", e);
            result = AjaxResult.error();
        }
        return result;
    }

    /**
     * 换绑银行卡
     * changeBankCard
     */
    @PostMapping("/changeBankCard")
    public AjaxResult changeBankCard(@RequestBody UpdateAccountConfigDTO dto) {
        AjaxResult result;
        try {
            adapayMemberService.updateSettleAccountConfig(dto);
            result = AjaxResult.success();
        } catch (BaseAdaPayException e) {
            logger.error("查询汇付账户余额error", e);
            result = AjaxResult.error();
        }
        return result;
    }
}
