package com.jeesharp.business.modules.pay.weixinpay.service.impl;

import com.jeesharp.business.modules.pay.weixinpay.bean.request.WxPayRedpackQueryRequest;
import com.jeesharp.business.modules.pay.weixinpay.bean.request.WxPaySendMiniProgramRedpackRequest;
import com.jeesharp.business.modules.pay.weixinpay.bean.request.WxPaySendRedpackRequest;
import com.jeesharp.business.modules.pay.weixinpay.bean.result.BaseWxPayResult;
import com.jeesharp.business.modules.pay.weixinpay.bean.result.WxPayRedpackQueryResult;
import com.jeesharp.business.modules.pay.weixinpay.bean.result.WxPaySendMiniProgramRedpackResult;
import com.jeesharp.business.modules.pay.weixinpay.bean.result.WxPaySendRedpackResult;
import com.jeesharp.business.modules.pay.weixinpay.constant.WxPayConstants;
import com.jeesharp.business.modules.pay.weixinpay.exception.WxPayException;
import com.jeesharp.business.modules.pay.weixinpay.service.RedpackService;
import com.jeesharp.business.modules.pay.weixinpay.service.WxPayService;
import lombok.RequiredArgsConstructor;

/**
 * 老板加点注释吧.
 *
 * @author <a href="https://github.com/binarywang">Binary Wang</a>
 * @date 2019-12-26
 */
@RequiredArgsConstructor
public class RedpackServiceImpl implements RedpackService {
  private final WxPayService payService;

  @Override
  public WxPaySendMiniProgramRedpackResult sendMiniProgramRedpack(WxPaySendMiniProgramRedpackRequest request)
    throws WxPayException {
    request.checkAndSign(this.payService.getConfig());
    String url = this.payService.getPayBaseUrl() + "/mmpaymkttransfers/sendminiprogramhb";
    String responseContent = this.payService.post(url, request.toXML(), true);

    WxPaySendMiniProgramRedpackResult result = BaseWxPayResult.fromXML(responseContent, WxPaySendMiniProgramRedpackResult.class);
    result.checkResult(this.payService, request.getSignType(), true);
    return result;
  }

  @Override
  public WxPaySendRedpackResult sendRedpack(WxPaySendRedpackRequest request) throws WxPayException {
    request.checkAndSign(this.payService.getConfig());

    String url = this.payService.getPayBaseUrl() + "/mmpaymkttransfers/sendredpack";
    if (request.getAmtType() != null) {
      //裂变红包
      url = this.payService.getPayBaseUrl() + "/mmpaymkttransfers/sendgroupredpack";
    }

    String responseContent = this.payService.post(url, request.toXML(), true);
    final WxPaySendRedpackResult result = BaseWxPayResult.fromXML(responseContent, WxPaySendRedpackResult.class);
    result.checkResult(this.payService, request.getSignType(), true);
    return result;
  }

  @Override
  public WxPayRedpackQueryResult queryRedpack(String mchBillNo) throws WxPayException {
    WxPayRedpackQueryRequest request = new WxPayRedpackQueryRequest();
    request.setMchBillNo(mchBillNo);
    return this.queryRedpack(request);
  }

  @Override
  public WxPayRedpackQueryResult queryRedpack(WxPayRedpackQueryRequest request) throws WxPayException {
    request.setBillType(WxPayConstants.BillType.MCHT);
    request.checkAndSign(this.payService.getConfig());

    String url = this.payService.getPayBaseUrl() + "/mmpaymkttransfers/gethbinfo";
    String responseContent = this.payService.post(url, request.toXML(), true);
    WxPayRedpackQueryResult result = BaseWxPayResult.fromXML(responseContent, WxPayRedpackQueryResult.class);
    result.checkResult(this.payService, request.getSignType(), true);
    return result;
  }
}
