
package com.tmerclub.cloud.config;


import cn.binarywang.wx.miniapp.api.WxMaOrderShippingService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaOrderShippingServiceImpl;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceHttpClientImpl;
import cn.binarywang.wx.miniapp.bean.shop.request.shipping.WxMaOrderShippingInfoGetListRequest;
import cn.binarywang.wx.miniapp.bean.shop.request.shipping.WxMaOrderShippingInfoGetRequest;
import cn.binarywang.wx.miniapp.bean.shop.response.WxMaOrderShippingInfoGetListResponse;
import cn.binarywang.wx.miniapp.bean.shop.response.WxMaOrderShippingInfoGetResponse;
import cn.binarywang.wx.miniapp.bean.shop.response.WxMaOrderShippingIsTradeManagedResponse;
import cn.binarywang.wx.miniapp.config.impl.WxMaRedissonConfigImpl;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.tmerclub.cloud.api.platform.config.FeignShopConfig;
import com.tmerclub.cloud.api.platform.feign.ConfigFeignClient;
import com.tmerclub.cloud.common.bean.WxMiniApp;
import com.tmerclub.cloud.common.bean.WxMp;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceHttpClientImpl;
import me.chanjar.weixin.mp.config.impl.WxMpRedissonConfigImpl;
import org.apache.dubbo.config.annotation.DubboReference;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 通过微信配置获取微信的支付信息，登陆信息等
 * @author tmerclub
 */
@RefreshScope
@Component
public class WxConfig {


    @Autowired
    private FeignShopConfig feignShopConfig;

    @Autowired
    private RedissonClient redissonClient;
    @Autowired
    private FeignShopConfig shopConfig;
    @DubboReference
    private ConfigFeignClient configFeignClient;

    private static final String MA_CONFIG_KEY_PREFIX = "tmerclub:ma_config_key_prefix";

    private static final String MP_CONFIG_KEY_PREFIX = "tmerclub:mp_config_key_prefix";

    public WxMaService getWxMaService() {
        WxMiniApp wxMiniApp = feignShopConfig.getWxMiniApp();
        // access token用redis缓存
        WxMaRedissonConfigImpl maRedissonConfig = new WxMaRedissonConfigImpl(redissonClient, MA_CONFIG_KEY_PREFIX);
        maRedissonConfig.setAppid(wxMiniApp.getAppId());
        maRedissonConfig.setSecret(wxMiniApp.getSecret());

        WxMaServiceHttpClientImpl wxMaServiceHttpClient = new WxMaServiceHttpClientImpl();
        wxMaServiceHttpClient.setWxMaConfig(maRedissonConfig);
        return wxMaServiceHttpClient;
    }

    public WxMpService getWxMpService() {
        WxMp wxMp = feignShopConfig.getWxMp();
        // access token用redis缓存
        WxMpRedissonConfigImpl wxMpRedisConfig = new WxMpRedissonConfigImpl(redissonClient, MP_CONFIG_KEY_PREFIX);
        wxMpRedisConfig.setAppId(wxMp.getAppId());
        wxMpRedisConfig.setSecret(wxMp.getSecret());

        WxMpServiceHttpClientImpl wxMpServiceHttpClient = new WxMpServiceHttpClientImpl();
        wxMpServiceHttpClient.setWxMpConfigStorage(wxMpRedisConfig);
        return wxMpServiceHttpClient;
    }


    /**
     * 查询小程序是否已开通发货信息管理服务
     * @return true开通，false未开通
     */
    public Boolean getTradeManaged() {
        // 判断是否开启了微信发货
        WxMiniApp wxMiniApp = feignShopConfig.getWxMiniApp();
        Boolean spuAudit = wxMiniApp.getWechatShippingAudit();
        if (!spuAudit) {
            return false;
        }
        String appid = wxMiniApp.getAppId();
        WxMaService wxMaService = getWxMaService();
        WxMaOrderShippingService wxMaOrderShippingService = new WxMaOrderShippingServiceImpl(wxMaService);
        try {
            // 查询小程序是否已开通发货信息管理服务
            WxMaOrderShippingIsTradeManagedResponse managed = wxMaOrderShippingService.isTradeManaged(appid);
            return managed.getTradeManaged();
        } catch (WxErrorException wxErrorException) {
            return false;
        }
    }

    /**
     * 查询小程序待发货订单信息
     * @return
     */
    public WxMaOrderShippingInfoGetListResponse getOrderIdList() {
        WxMiniApp wxMiniApp = shopConfig.getWxMiniApp();
        WxMaService wxMaService = getWxMaService();
        WxMaOrderShippingService wxMaOrderShippingService = new WxMaOrderShippingServiceImpl(wxMaService);
        try {
            WxMaOrderShippingInfoGetListRequest request = new WxMaOrderShippingInfoGetListRequest();
            request.setOrderState(1);
            // 查询小程序待发货订单信息
            WxMaOrderShippingInfoGetListResponse response = wxMaOrderShippingService.getList(request);
            return response;
        } catch (WxErrorException wxErrorException) {
            return new WxMaOrderShippingInfoGetListResponse();
        }
    }

    /**
     * 根据支付单号获取小程序发货订单状态
     * @return 订单状态
     */
    public Integer getOrderStatusByBizPayNo(String bizPayNo) {
        WxMiniApp wxMiniApp = shopConfig.getWxMiniApp();
        WxMaService wxMaService = getWxMaService();
        WxMaOrderShippingService wxMaOrderShippingService = new WxMaOrderShippingServiceImpl(wxMaService);
        try {
            WxMaOrderShippingInfoGetRequest request = new WxMaOrderShippingInfoGetRequest();
            request.setTransactionId(bizPayNo);
            // 查询小程序待发货订单信息
            WxMaOrderShippingInfoGetResponse response = wxMaOrderShippingService.get(request);
            return response.getOrder().getOrderState();
        } catch (WxErrorException wxErrorException) {
            return 0;
        }
    }

}
