package com.gree.mall.miniapp.logic.common.outside;

import cn.hutool.core.lang.TypeReference;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.gree.mall.miniapp.bean.PayDetail;
import com.gree.mall.miniapp.bean.common.WechatOpenBean;
import com.gree.mall.miniapp.bean.common.WxJsApiSignBean;
import com.gree.mall.miniapp.bean.user.CurrentCompanyWechat;
import com.gree.mall.miniapp.helper.ResponseHelper;
import com.gree.mall.miniapp.utils.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class WechatLogic extends OutsideBaseApi{

    /**
     * 获取小程序openid/unionId/sessionId,手机号
     */
    public WechatOpenBean getAuth(String code,String phoneCode){
        CurrentCompanyWechat currentCompanyWechat = commonLogic.getCurrentCompanyWechat();

        Map<String,Object> map = new HashMap<>();
        map.put("code",code);
        map.put("phoneCode",phoneCode);
        map.put("companyWechatId",currentCompanyWechat.getCompanyWechatId());
        String s = HttpUtils.requestPostForm(pcUrl + "/wechat/auth", map, null);
        ResponseHelper<WechatOpenBean> result = JSONUtil.toBean(s, new TypeReference<ResponseHelper<WechatOpenBean>>() {
        }, false);
        WechatOpenBean wechatOpenBean = checkData(result);
        log.info("微信授权:{}",wechatOpenBean);
        return wechatOpenBean;
    }


    /**
     * 获取绑定公众号openid跳转小程序的链接
     */
    public String getPubAuthUrl(String code,String pubAppId){

        Map<String,Object> map = new HashMap<>();
        map.put("code",code);
        map.put("pubAppId",pubAppId);
        String s = HttpUtils.requestPostForm(pcUrl + "/wechat/pub/auth/url", map, null);
        ResponseHelper<String> result = JSONUtil.toBean(s, new TypeReference<ResponseHelper<String>>() {
        }, false);
        String s1 = checkData(result);
        log.info("获取绑定公众号openid跳转小程序的链接:{}",s1);
        return s1;
    }


    /**
     * 获取绑定公众号openid跳转小程序的链接
     */
    public String getPubAuthOpenId(String code,String pubAppId){

        Map<String,Object> map = new HashMap<>();
        map.put("code",code);
        map.put("pubAppId",pubAppId);
        String s = HttpUtils.requestPostForm(pcUrl + "/wechat/pub/auth", map, null);
        ResponseHelper<String> result = JSONUtil.toBean(s, new TypeReference<ResponseHelper<String>>() {
        }, false);
        String s1 = checkData(result);
        log.info("获取绑定公众号openid:{}",s1);
        return s1;
    }


    /**
     * 获取公众号jsapi sign
     */
    public WxJsApiSignBean getPubJsapiSign(String companyWechatId,String url){

        Map<String,Object> map = new HashMap<>();
        map.put("companyWechatId",companyWechatId);
        map.put("url",url);
        String s = HttpUtils.requestPostForm(pcUrl + "/wechat/pub/jsapi/sign", map, getHeader());
        ResponseHelper<WxJsApiSignBean> result = JSONUtil.toBean(s, new TypeReference<ResponseHelper<WxJsApiSignBean>>() {
        }, false);
        WxJsApiSignBean s1 = checkData(result);
        log.info("jsapi sign:{}",JSONObject.toJSONString(s1));
        return s1;
    }


    /**
     * 获取小程序二维码
     * @param qrcodeType
     * @param objId
     * @param userId
     * @param companyWechatId
     * @return
     */
    public String getQrcode(String qrcodeType,String objId,String userId,String companyWechatId){
        Map<String,Object> map = new HashMap<>();
        map.put("qrcodeType",qrcodeType);
        map.put("objId",objId);
        map.put("userId",userId);
        map.put("companyWechatId",companyWechatId);
        String s = HttpUtils.requestPostForm(pcUrl + "/wechat/qrcode/get", map, getHeader());
        ResponseHelper<String> result = JSONUtil.toBean(s, new TypeReference<ResponseHelper<String>>() {
        }, false);
        return checkData(result);
    }

    /**
     * 微信支付jsapi
     * @param orderId 订单号
     * @param payAmount 支付金额
     * @param openId
     * @param profitSharingFlag Y=分账 N=不分帐
     * @param ip
     * @param companyWechatId
     * @param miniPay 是否小程序支付
     * @return
     */
    public PayDetail payment(String orderId, BigDecimal payAmount,String openId,String profitSharingFlag,String ip,String companyWechatId,Boolean miniPay
     ){
        Map<String,Object> map = new HashMap<>();
        map.put("orderId",orderId);
        map.put("payAmount",payAmount);
        map.put("openId",openId);
        map.put("profitSharingFlag",profitSharingFlag);
        map.put("ip",ip);
        map.put("companyWechatId",companyWechatId);
        map.put("miniPay",miniPay);
        String s = HttpUtils.requestPostForm(pcUrl + "/wechat/payment", map, getHeader());
        log.info("微信支付response:{}",s);
        ResponseHelper<PayDetail> result = JSONUtil.toBean(s, new TypeReference<ResponseHelper<PayDetail>>() {
        }, false);
        PayDetail payDetail = checkData(result);
        log.info("微信支付:{}",payDetail);
        return payDetail;
    }


    public PayDetail paymentH5(String orderId, BigDecimal payAmount,String openId,String profitSharingFlag,String ip,String companyWechatId){
        Map<String,Object> map = new HashMap<>();
        map.put("orderId",orderId);
        map.put("payAmount",payAmount);
        map.put("openId",openId);
        map.put("profitSharingFlag",profitSharingFlag);
        map.put("ip",ip);
        map.put("companyWechatId",companyWechatId);
        String s = HttpUtils.requestPostForm(pcUrl + "/wechat/payment/h5", map, getHeader());
        log.info("微信支付response:{}",s);
        ResponseHelper<PayDetail> result = JSONUtil.toBean(s, new TypeReference<ResponseHelper<PayDetail>>() {
        }, false);
        PayDetail payDetail = checkData(result);
        log.info("微信支付:{}",payDetail);
        return payDetail;
    }

    /**
     * 微信支付付款吗
     * @param orderId 订单号
     * @param payAmount 支付金额
     * @param authCode 扫付款码得到的code
     * @param profitSharingFlag Y=分账 N=不分帐
     * @param ip
     * @param companyWechatId
     * @return
     */
    public String paymentFkm(String orderId, BigDecimal payAmount,String authCode,String profitSharingFlag,String ip,String companyWechatConfigId,String source){
        Map<String,Object> map = new HashMap<>();
        map.put("orderId",orderId);
        map.put("payAmount",payAmount);
        map.put("authCode",authCode);
        map.put("profitSharingFlag",profitSharingFlag);
        map.put("ip",ip);
        map.put("companyWechatConfigId",companyWechatConfigId);
        map.put("source",source);
        String s = HttpUtils.requestPostForm(pcUrl + "/wechat/payment/fkmWebsit", map, getHeader());
        log.info("微信付款码支付response:{}",s);
        ResponseHelper<String> result = JSONUtil.toBean(s, new TypeReference<ResponseHelper<String>>() {
        }, false);
        String transcationId = checkData(result);
        log.info("微信付款码支付:{}",transcationId);
        return transcationId;
    }


    /**
     * 发货
     * @param companyWechatId
     * @param transactionId
     * @param openId 小程序openid(不可以是公众号openid)
     * @return
     */
    public PayDetail deliver(String companyWechatId, String transactionId,String openId){
        Map<String,Object> map = new HashMap<>();
        map.put("companyWechatId",companyWechatId);
        map.put("transactionId",transactionId);
        map.put("openId",openId);
        String s = HttpUtils.requestPostForm(pcUrl + "/wechat/deliver", map, null);
        log.info("微信支付response:{}",s);
        ResponseHelper<PayDetail> result = JSONUtil.toBean(s, new TypeReference<ResponseHelper<PayDetail>>() {
        }, false);
        PayDetail payDetail = checkData(result);
        log.info("微信支付:{}",payDetail);
        return payDetail;
    }


    /**
     * 公众号自动回复内容
     * @param appid
     * @return
     */
    public String getZdhf(String appid){
        Map<String,Object> map = new HashMap<>();
        map.put("appid",appid);
        String s = HttpUtils.requestPostForm(pcUrl + "/wechat/zdhf/get", map, null);
        log.info("自动回复response:{}",s);
        ResponseHelper<String> result = JSONUtil.toBean(s, new TypeReference<ResponseHelper<String>>() {
        }, false);
        String content = checkData(result);
        return content;
    }




    public ResponseHelper<WxMpQrCodeTicket> getMpQrcode(String userId, String companyWechatId) throws Exception {
        Map<String,Object> map = new HashMap<>();
        map.put("companyWechatId", companyWechatId);
        map.put("userId", userId);
        String s = HttpUtils.requestPostForm(pcUrl + "/wechat/get/mp/qrcode", map, getHeader());
        return JSONUtil.toBean(s, new TypeReference<ResponseHelper<WxMpQrCodeTicket>>() {
        }, false);
    }
}
