package com.hai.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hai.common.pay.util.sdk.WXPayConstants;
import com.hai.common.utils.HttpsUtils;
import com.hai.common.utils.MD5Util;
import com.hai.common.utils.ResponseMsgUtil;
import com.hai.common.utils.WxUtils;
import com.hai.model.ResponseData;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * @author hurui
 * @version 1.0
 * @ClassName QianZhuConfig
 * @description: 千猪接口配置
 * @date 2021/7/5 14:18
 */
public class QianZhuConfig {

    /**
     * 获取Token
     * @param platformUniqueId  用户唯一id
     * @param nickname   昵称
     * @param mobile   电话号码
     * @return Token
     */
    public static JSONObject getToken(String platformUniqueId,String nickname,String mobile) throws Exception {
        Map<String,Object> map = new HashMap<>();
        map.put("platformId", CommonSysConst.getSysConfig().getQinzhuPlatformId());
        map.put("timestamp", new Date().getTime());
        map.put("platformUniqueId", platformUniqueId);
        map.put("nickname", nickname);
        map.put("mobile", mobile);
        map.put("sign", MD5Util.encode(generateSignature(map,CommonSysConst.getSysConfig().getQinzhuSecret()).getBytes()).toLowerCase());
        return HttpsUtils.doGet(CommonSysConst.getSysConfig().getQinzhuUrl()+"/api/v2/platform/getToken", map);
    }

    /**
     * KFC
     * 根据订单号查询订单详情
     * @param orderNo
     * @return
     * @throws Exception
     */
    public static JSONObject getKfcOrderByOrderNo(String orderNo) throws Exception {
        Map<String,Object> map = new HashMap<>();
        map.put("platformId", CommonSysConst.getSysConfig().getQinzhuPlatformId());
        map.put("timestamp", new Date().getTime());
        map.put("orderNo", orderNo);
        map.put("sign", MD5Util.encode(generateSignature(map,CommonSysConst.getSysConfig().getQinzhuSecret()).getBytes()).toLowerCase());
        return HttpsUtils.doPost(CommonSysConst.getSysConfig().getQinzhuUrl()+"/openApi/v1/kfcOrders/getByOrderNo", JSONObject.toJSONString(map));
    }

    /**
     * KFC
     * 支付订单
     * @param orderNo
     * @return
     * @throws Exception
     */
    public static JSONObject payKfcOrder(String orderNo) throws Exception {
        Map<String,Object> map = new HashMap<>();
        map.put("platformId", CommonSysConst.getSysConfig().getQinzhuPlatformId());
        map.put("timestamp", new Date().getTime());
        map.put("notifyUrl", CommonSysConst.getSysConfig().getQianzhuOrderNotify());
        map.put("orderNo", orderNo);
        map.put("sign", MD5Util.encode(generateSignature(map,CommonSysConst.getSysConfig().getQinzhuSecret()).getBytes()).toLowerCase());
        return HttpsUtils.doPost(CommonSysConst.getSysConfig().getQinzhuUrl()+"/openApi/v1/orders/payKfcOrder", JSONObject.toJSONString(map));
    }

    /**
     * 电影票
     * 根据订单号查询订单详情
     * @param orderNo
     * @return
     * @throws Exception
     */
    public static JSONObject getCinemaOrderByOrderNo(String orderNo) throws Exception {
        Map<String,Object> map = new HashMap<>();
        map.put("platformId", CommonSysConst.getSysConfig().getQinzhuPlatformId());
        map.put("timestamp", new Date().getTime());
        map.put("orderNo", orderNo);
        map.put("sign", MD5Util.encode(generateSignature(map,CommonSysConst.getSysConfig().getQinzhuSecret()).getBytes()).toLowerCase());
        return HttpsUtils.doPost(CommonSysConst.getSysConfig().getQinzhuUrl()+"/openApi/v1/orders/getByOrderNo", JSONObject.toJSONString(map));
    }

    /**
     * 电影票
     * 支付订单
     * @param orderNo
     * @return
     * @throws Exception
     */
    public static JSONObject payMovieOrder(String orderNo) throws Exception {
        Map<String,Object> map = new HashMap<>();
        map.put("platformId", CommonSysConst.getSysConfig().getQinzhuPlatformId());
        map.put("timestamp", new Date().getTime());
        map.put("orderNo", orderNo);
        map.put("sign", MD5Util.encode(generateSignature(map,CommonSysConst.getSysConfig().getQinzhuSecret()).getBytes()).toLowerCase());
        return HttpsUtils.doPost(CommonSysConst.getSysConfig().getQinzhuUrl()+"/openApi/v1/orders/payMovieOrder", JSONObject.toJSONString(map));
    }


    /**
     * 话费充值
     * 获取Token
     * @param platformUniqueId  用户唯一id
     * @param nickname   昵称
     * @param mobile   电话号码
     * @return Token
     */
    public static JSONObject getMobileToken(String platformUniqueId,String nickname,String mobile) throws Exception {
        Map<String,Object> contentMap = new LinkedHashMap<>();
        contentMap.put("platformUniqueId", platformUniqueId);
        contentMap.put("nickname", nickname);
        contentMap.put("mobile", mobile);

        Map<String,Object> map = new HashMap<>();
        map.put("platformId", "10458");
        map.put("action", "users.getTokenV2");
        map.put("version", "1.0");
        map.put("content", JSON.toJSONString(contentMap));
        map.put("traceId", WxUtils.makeNonStr());
        map.put("timestamp", new Date().getTime());
        map.put("sign", MD5Util.encode(generateSignature(map,CommonSysConst.getSysConfig().getQinzhuSecret()).getBytes()).toLowerCase());
        return HttpsUtils.doPost("", JSON.toJSONString(map));
    }

    /**
     * 话费订单
     * 根据订单号查询
     * @param orderNo 订单号
     * @return
     * @throws Exception
     */
    public static JSONObject getMobileOrderByOrderNo(String orderNo) throws Exception {
        Map<String,Object> contentMap = new LinkedHashMap<>();
        contentMap.put("orderNo", orderNo);
        Map<String,Object> map = new HashMap<>();
        map.put("platformId", "10458");
        map.put("action", "mobileOrders.getByOrderNo");
        map.put("version", "1.0");
        map.put("content", JSON.toJSONString(contentMap));
        map.put("traceId", WxUtils.makeNonStr());
        map.put("timestamp", new Date().getTime());
        map.put("sign", MD5Util.encode(generateSignature(map,CommonSysConst.getSysConfig().getQinzhuSecret()).getBytes()).toLowerCase());
        return HttpsUtils.doPost("https://nf.qianzhu8.com/gateway", JSON.toJSONString(map));
    }

    /**
     * 话费订单
     * 支付订单
     * @param orderNo
     * @return
     * @throws Exception
     */
    public static JSONObject payMobileOrder(String orderNo) throws Exception {
        Map<String,Object> contentMap = new LinkedHashMap<>();
        contentMap.put("orderNo", orderNo);
        Map<String,Object> map = new HashMap<>();
        map.put("platformId", "10458");
        map.put("action", "mobileOrders.pay");
        map.put("version", "1.0");
        map.put("content", JSON.toJSONString(contentMap));
        map.put("traceId", WxUtils.makeNonStr());
        map.put("timestamp", new Date().getTime());
        map.put("sign", MD5Util.encode(generateSignature(map,CommonSysConst.getSysConfig().getQinzhuSecret()).getBytes()).toLowerCase());
        return HttpsUtils.doPost("https://nf.qianzhu8.com/gateway", JSON.toJSONString(map));
    }

    /**
    * @Author Sum1Dream
    * @name getAllCities.java
    * @Description // 查询所有城市
    * @Date 15:45 2022/6/28
    * @Param []
    * @return com.alibaba.fastjson.JSONObject
    */
    public static JSONObject getAllCities() throws Exception {
        Map<String,Object> map = new HashMap<>();
        map.put("platformId", CommonSysConst.getSysConfig().getQinzhuPlatformId());
        map.put("timestamp", new Date().getTime());
        map.put("sign", MD5Util.encode(QianZhuConfig.generateSignature(map,CommonSysConst.getSysConfig().getQinzhuSecret()).getBytes()).toLowerCase());
        return HttpsUtils.doPost(CommonSysConst.getSysConfig().getQinzhuUrl()+"/openApi/v2/cities/listAll", JSONObject.toJSONString(map));
    }

    /**
     * @Author Sum1Dream
     * @name getStarbucksStore.java
     * @Description // 查询星巴克门店
     * @Date 15:45 2022/6/28
     * @Param []
     * @return com.alibaba.fastjson.JSONObject
     */
    public static JSONObject getStarbucksStore(String lat , String lon) throws Exception {
        Map<String,Object> contentMap = new LinkedHashMap<>();
        contentMap.put("lon", lon);
        contentMap.put("lat", lat);

        Map<String,Object> map = new HashMap<>();
        map.put("platformId", CommonSysConst.getSysConfig().getQinzhuPlatformId());
        map.put("timestamp", new Date().getTime());
        map.put("action", "starbucksStores.listByLonAndLat");
        map.put("content", JSON.toJSONString(contentMap));
        map.put("version", "1.0");
        map.put("traceId", WxUtils.makeNonStr());
        map.put("sign", MD5Util.encode(QianZhuConfig.generateSignature(map,CommonSysConst.getSysConfig().getQinzhuSecret()).getBytes()).toLowerCase());
        return HttpsUtils.doPost(CommonSysConst.getSysConfig().getQianzhuH5url(), JSONObject.toJSONString(map));
    }


    /**
    * @Author Sum1Dream
    * @name getStarbucksProducts.java
    * @Description // 查询星巴克商品
    * @Date 11:16 2022/6/29
    * @Param [java.lang.Integer, java.lang.Integer]
    * @return com.alibaba.fastjson.JSONObject
    */
    public static JSONObject getStarbucksProducts(Integer pageIndex , Integer pageSize) throws Exception {
        Map<String,Object> contentMap = new LinkedHashMap<>();
        contentMap.put("pageIndex", pageIndex);
        contentMap.put("pageSize", pageSize);

        Map<String,Object> map = new HashMap<>();
        map.put("platformId", CommonSysConst.getSysConfig().getQinzhuPlatformId());
        map.put("timestamp", new Date().getTime());
        map.put("action", "starbucksProducts.pagedQuery");
        map.put("content", JSON.toJSONString(contentMap));
        map.put("version", "1.0");
        map.put("traceId", WxUtils.makeNonStr());
        map.put("sign", MD5Util.encode(QianZhuConfig.generateSignature(map,CommonSysConst.getSysConfig().getQinzhuSecret()).getBytes()).toLowerCase());
        return HttpsUtils.doPost(CommonSysConst.getSysConfig().getQianzhuH5url(), JSONObject.toJSONString(map));
    }

    /**
     * 生成签名
     * @param data  数据
     * @param key   秘钥app_secret
     * @return 加密结果
     */
    public static String generateSignature(final Map<String, Object> data, String key){
        Set<String> keySet = data.keySet();
        String[] keyArray = keySet.toArray(new String[keySet.size()]);
        Arrays.sort(keyArray);
        StringBuilder sb = new StringBuilder();
        for (String k : keyArray) {
            if (k.equals(WXPayConstants.FIELD_SIGN)) {
                continue;
            }
            if (StringUtils.isBlank(sb.toString())) {
                sb.append(k).append("=").append(data.get(k));
            } else {
                sb.append("&").append(k).append("=").append(data.get(k));
            }
        }
        sb.append(key);
        return sb.toString();
    }

    /**
     * @Author Sum1Dream
     * @name getKfcStore.java
     * @Description // 查询KFC门店
     * @Date 11:16 2022/6/29
     * @Param [java.lang.Integer, java.lang.Integer]
     * @return com.alibaba.fastjson.JSONObject
     */
    public static JSONObject getKfcStore() throws Exception {
        Map<String,Object> map = new HashMap<>();
        map.put("platformId", CommonSysConst.getSysConfig().getQinzhuPlatformId());
        map.put("timestamp", new Date().getTime());
        map.put("sign", MD5Util.encode(QianZhuConfig.generateSignature(map,CommonSysConst.getSysConfig().getQinzhuSecret()).getBytes()).toLowerCase());
        return HttpsUtils.doPost(CommonSysConst.getSysConfig().getQinzhuUrl()+"/openApi/v2/kfcStores/listAll", JSONObject.toJSONString(map));
    }

    /**
     * @Author Sum1Dream
     * @name getKfcStore.java
     * @Description // 查询KFC门店菜单
     * @Date 11:16 2022/6/29
     * @Param [java.lang.Integer, java.lang.Integer]
     * @return com.alibaba.fastjson.JSONObject
     */
    public static JSONObject listByStoreCode(String storeCode) throws Exception {
        Map<String,Object> map = new HashMap<>();
        map.put("platformId", CommonSysConst.getSysConfig().getQinzhuPlatformId());
        map.put("storeCode", storeCode);
        map.put("timestamp", new Date().getTime());
        map.put("sign", MD5Util.encode(QianZhuConfig.generateSignature(map,CommonSysConst.getSysConfig().getQinzhuSecret()).getBytes()).toLowerCase());
        return HttpsUtils.doPost(CommonSysConst.getSysConfig().getQinzhuUrl()+"/openApi/v1/kfcMenus/listByStoreCode", JSONObject.toJSONString(map));
    }

    /**
    * @Author Sum1Dream
    * @name starbucksOrders.java
    * @Description // 星巴克下单
    * @Date 17:01 2022/7/4
    * @Param [java.lang.String, java.lang.String, java.lang.String, java.lang.String]
    * @return com.alibaba.fastjson.JSONObject
    */
    public static JSONObject starbucksOrders( String platformUniqueId ,String storeCode , String orderItems , String customerMobile) throws Exception {

        Map<String,Object> contentMap = new LinkedHashMap<>();
        contentMap.put("storeCode", storeCode);
        contentMap.put("orderItems", orderItems);
        contentMap.put("customerMobile", customerMobile);
        contentMap.put("platformUniqueId", platformUniqueId);
        Map<String,Object> map = new HashMap<>();
        map.put("platformId", CommonSysConst.getSysConfig().getQinzhuPlatformId());
        map.put("action", "starbucksOrders.createOrderV2");
        map.put("traceId", WxUtils.makeNonStr());
        map.put("version", "1.0");
        map.put("content", JSON.toJSONString(contentMap));
        map.put("timestamp", new Date().getTime());
        map.put("sign", MD5Util.encode(QianZhuConfig.generateSignature(map,CommonSysConst.getSysConfig().getQinzhuSecret()).getBytes()).toLowerCase());
        return HttpsUtils.doPost(CommonSysConst.getSysConfig().getQianzhuH5url(), JSONObject.toJSONString(map));
    }

    /**
    * @Author Sum1Dream
    * @name starbucksOrdersPay.java
    * @Description // 星巴克支付
    * @Date 17:03 2022/7/4
    * @Param [java.lang.String]
    * @return com.alibaba.fastjson.JSONObject
    */
    public static JSONObject starbucksOrdersPay(String orderNo) throws Exception {
        Map<String,Object> contentMap = new LinkedHashMap<>();
        contentMap.put("orderNo", orderNo);
        Map<String,Object> map = new HashMap<>();
        map.put("platformId", CommonSysConst.getSysConfig().getQinzhuPlatformId());
        map.put("action", "starbucksOrders.pay");
        map.put("version", "1.0");
        map.put("content", JSON.toJSONString(contentMap));
        map.put("traceId", WxUtils.makeNonStr());
        map.put("timestamp", new Date().getTime());
        map.put("sign", MD5Util.encode(generateSignature(map,CommonSysConst.getSysConfig().getQinzhuSecret()).getBytes()).toLowerCase());
        return HttpsUtils.doPost(CommonSysConst.getSysConfig().getQianzhuH5url(), JSON.toJSONString(map));
    }

    /**
    * @Author Sum1Dream
    * @name createKfcOrder.java
    * @Description // 创建kfc订单
    * @Date 17:35 2022/7/4
    * @Param [java.lang.String]
    * @return com.alibaba.fastjson.JSONObject
    */
    public static JSONObject createKfcOrder(Integer eatType , String storeCode , String mobile , String items , String platformUniqueId) throws Exception {
        Map<String,Object> map = new HashMap<>();
        map.put("platformId", CommonSysConst.getSysConfig().getQinzhuPlatformId());
        map.put("eatType", eatType);
        map.put("storeCode", storeCode);
        map.put("mobile", mobile);
        map.put("userRemark", "");
        map.put("subPlatformId", "");
        map.put("items", items);
        map.put("platformUniqueId", platformUniqueId);
        map.put("timestamp", new Date().getTime());
        map.put("sign", MD5Util.encode(QianZhuConfig.generateSignature(map,CommonSysConst.getSysConfig().getQinzhuSecret()).getBytes()).toLowerCase());
        return HttpsUtils.doPost(CommonSysConst.getSysConfig().getQinzhuUrl()+"/openApi/v4/orders/createKfcOrder", JSONObject.toJSONString(map));
    }

    /**
    * @Author Sum1Dream
    * @name starbucksOrderByOrderNo.java
    * @Description // 根据订单号查询星巴克订单详情
    * @Date 16:38 2022/7/5
    * @Param [java.lang.String]
    * @return com.alibaba.fastjson.JSONObject
    */
    public static JSONObject starbucksOrderByOrderNo(String orderNo) throws Exception {
        Map<String,Object> contentMap = new LinkedHashMap<>();
        contentMap.put("orderNo", orderNo);
        Map<String,Object> map = new HashMap<>();
        map.put("platformId", CommonSysConst.getSysConfig().getQinzhuPlatformId());
        map.put("action", "starbucksOrders.getByOrderNo");
        map.put("version", "1.0");
        map.put("content", JSON.toJSONString(contentMap));
        map.put("traceId", WxUtils.makeNonStr());
        map.put("timestamp", new Date().getTime());
        map.put("sign", MD5Util.encode(generateSignature(map,CommonSysConst.getSysConfig().getQinzhuSecret()).getBytes()).toLowerCase());
        return HttpsUtils.doPost(CommonSysConst.getSysConfig().getQianzhuH5url(), JSON.toJSONString(map));
    }

    /**
    * @Author Sum1Dream
    * @name getTokenV2.java
    * @Description // 联合登录
    * @Date 15:07 2022/7/7
    * @Param [java.lang.String, java.lang.String, java.lang.String]
    * @return com.alibaba.fastjson.JSONObject
    */
    public static JSONObject getTokenV2(String platformUniqueId , String nickname , String mobile) throws Exception {
        Map<String,Object> contentMap = new LinkedHashMap<>();
        contentMap.put("platformUniqueId", platformUniqueId);
        contentMap.put("nickname", nickname);
        contentMap.put("mobile", mobile);
        Map<String,Object> map = new HashMap<>();
        map.put("platformId", CommonSysConst.getSysConfig().getQinzhuPlatformId());
        map.put("action", "users.getTokenV2");
        map.put("version", "1.0");
        map.put("content", JSON.toJSONString(contentMap));
        map.put("traceId", WxUtils.makeNonStr());
        map.put("timestamp", new Date().getTime());
        map.put("sign", MD5Util.encode(generateSignature(map,CommonSysConst.getSysConfig().getQinzhuSecret()).getBytes()).toLowerCase());
        return HttpsUtils.doPost(CommonSysConst.getSysConfig().getQinzhuUrlToken(), JSON.toJSONString(map));
    }

    /**
    * @Author Sum1Dream
    * @name insertV2.java
    * @Description // 第三方充值下单
    * @Date 17:38 2022/7/8
    * @Param [java.lang.String, java.lang.Integer, java.lang.String, java.lang.String]
    * @return com.alibaba.fastjson.JSONObject
    */
    public static JSONObject insertV2(String productCode , String orderNo , String chargeAccount) throws Exception {
        Map<String,Object> map = new HashMap<>();
        map.put("AppKey", CommonSysConst.getSysConfig().getThirdAppKey());
        map.put("TimesTamp", new Date().getTime());
        map.put("ProductCode", productCode);
        map.put("BuyCount", 1);
        map.put("MOrderID", orderNo);
        map.put("ChargeAccount", chargeAccount);
        map.put("CustomerIP", "127.0.0.1");
        map.put("sign", MD5Util.encode(generateSignatureByThird(map,CommonSysConst.getSysConfig().getThirdAppSecret()).getBytes()));
        return HttpsUtils.doPost(CommonSysConst.getSysConfig().getThirdPostUrl() + "Order/InsertV2", JSON.toJSONString(map));
    }

    /**
    * @Author Sum1Dream
    * @name QueryV2.java
    * @Description // 查询第三方订单详情
    * @Date 17:37 2022/7/8
    * @Param [java.lang.String]
    * @return com.alibaba.fastjson.JSONObject
    */
    public static JSONObject QueryV2(String orderNo) throws Exception {
        Map<String,Object> map = new HashMap<>();
        map.put("AppKey", CommonSysConst.getSysConfig().getThirdAppKey());
        map.put("TimesTamp", new Date().getTime());
        map.put("MOrderID", orderNo);
        map.put("OrderID", "");
        String sb = CommonSysConst.getSysConfig().getThirdAppKey() + new Date().getTime() + orderNo + CommonSysConst.getSysConfig().getThirdAppSecret();
        map.put("sign", MD5Util.encode(sb.getBytes()));
        return HttpsUtils.doPost(CommonSysConst.getSysConfig().getThirdPostUrl() + "Order/QueryV2", map);
    }


    /**
     * 生成签名
     * @param data  数据
     * @param key   秘钥app_secret
     * @return 加密结果
     */
    public static String generateSignatureByThird(final Map<String, Object> data, String key){
        Set<String> keySet = data.keySet();
        String[] keyArray = keySet.toArray(new String[keySet.size()]);
        Arrays.sort(keyArray);
        StringBuilder sb = new StringBuilder();
        for (String k : keyArray) {
            if (k.equals(WXPayConstants.FIELD_SIGN)) {
                continue;
            }
            sb.append(data.get(k));
        }
        sb.append(key);
        return sb.toString();
    }

}
