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.SettleAccountDTO;
import com.jsowell.adapay.dto.UpdateAccountConfigDTO;
import com.jsowell.adapay.dto.WithdrawDTO;
import com.jsowell.adapay.service.AdapayService;
import com.jsowell.adapay.vo.AdapayAccountBalanceVO;
import com.jsowell.common.core.controller.BaseController;
import com.jsowell.common.core.domain.AjaxResult;
import com.jsowell.common.enums.ykc.ReturnCodeEnum;
import com.jsowell.common.exception.BusinessException;
import com.jsowell.common.util.StringUtils;
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 AdapayService adapayMemberService;

    /**
     * 创建结算账户
     * http://localhost:8080/adapay/member/createSettleAccount
     * @param dto
     * @return
     */
    @PostMapping("/createSettleAccount")
    public AjaxResult createSettleAccount(@RequestBody SettleAccountDTO dto) {
        logger.info("创建结算账户接口 param:{}", JSON.toJSONString(dto));
        AjaxResult result;
        try {
            if (StringUtils.isBlank(dto.getMerchantId())) {
                throw new BusinessException(ReturnCodeEnum.CODE_PARAM_NOT_NULL_ERROR);
            }
            adapayMemberService.createSettleAccount(dto);
            result = AjaxResult.success();
        } catch (BusinessException e) {
            logger.warn("创建结算账户接口warn", e);
            result = AjaxResult.error(e.getMessage());
        } catch (Exception e) {
            logger.error("创建结算账户接口error", e);
            result = AjaxResult.error("创建结算账户接口异常:" + e.getMessage());
        }
        logger.info("创建结算账户接口 result:{}", JSON.toJSONString(result));
        return result;
    }
    /**
     * 查询汇付会员接口
     * http://localhost:8080/adapay/member/selectAdapayMember
     */
    @PostMapping("/selectAdapayMember")
    public AjaxResult selectAdapayMember(@RequestBody AdapayMemberInfoDTO dto) {
        AjaxResult result;
        try {
            if (StringUtils.isBlank(dto.getMerchantId())) {
                throw new BusinessException(ReturnCodeEnum.CODE_PARAM_NOT_NULL_ERROR);
            }
            Map<String, Object> map = adapayMemberService.selectAdapayMember(dto.getMerchantId());
            result = AjaxResult.success(map);
        }  catch (BusinessException e) {
            logger.warn("查询汇付会员接口异常warn", e);
            result = AjaxResult.error(e.getMessage());
        } catch (Exception e) {
            logger.error("查询汇付会员接口异常error", e);
            result = AjaxResult.error("查询汇付会员接口异常");
        }
        return result;
    }

    /**
     * 更新结算账户接口
     * http://localhost:8080/adapay/member/updateAdapayMember
     */
    @PostMapping("/updateAdapayMember")
    public AjaxResult updateAdapayMember(@RequestBody SettleAccountDTO dto) {
        AjaxResult result;
        try {
            if (StringUtils.isBlank(dto.getMerchantId())) {
                throw new BusinessException(ReturnCodeEnum.CODE_PARAM_NOT_NULL_ERROR);
            }
            Map<String, Object> map = adapayMemberService.updateAdapayMember(dto);
            result = AjaxResult.success(map);
        }  catch (BusinessException e) {
            logger.warn("更新结算账户接口warn", e);
            result = AjaxResult.error(e.getMessage());
        } catch (Exception e) {
            logger.error("更新结算账户接口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 (BusinessException e) {
            logger.error("查询汇付账户余额error", e);
            result = AjaxResult.error(e.getMessage());
        } 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;
    }

    /**
     * 换绑银行卡
     * http://localhost:8080/adapay/member/changeBankCard
     * 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;
    }

    /**
     * 提现接口
     * http://localhost:8080/adapay/member/drawCash
     */
    @PostMapping("/drawCash")
    public AjaxResult drawCash(@RequestBody WithdrawDTO dto) {
        AjaxResult result;
        try {
            adapayMemberService.drawCash(dto);
            result = AjaxResult.success();
        }  catch (BusinessException e) {
            logger.error("申请取现接口error", e);
            result = AjaxResult.error(e.getMessage());
        } catch (BaseAdaPayException e) {
            logger.error("申请取现接口 error", e);
            result = AjaxResult.error();
        }
        return result;
    }
}
