package com.hfkj.meituan;

import com.alibaba.fastjson.JSONObject;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.utils.HttpsUtils;
import com.hfkj.config.CommonSysConst;

import com.meituan.sqt.utils.EncryptUtil;
import com.meituan.sqt.utils.JsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
  * @ClassName MeiTuanService
  * @Author Sum1Dream
  * @Description 美团接口
  * @Date 2024/8/26 下午4:52
 **/
public class MeiTuanService {

    private static Logger log = LoggerFactory.getLogger(MeiTuanService.class);

    /**
     * @MethodName loginFree
     * @Description: H5免登接口
     * @param map
     * @return: com.alibaba.fastjson.JSONObject
     * @Author: Sum1Dream
     * @Date: 2024/8/27 上午11:16
     */
    public static JSONObject loginFree(Map<String , Object> map) throws Exception {

        JSONObject object = doPostMeiTuanUrl("https://waimai-openapi.apigw.test.meituan.com/api/sqt/open/login/h5/loginFree/redirection?test_open_swimlane=test-open" , map);

        if (object.getBoolean("msg")) {
            return object;
        } else {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "查询失败！");
        }

    }

    /**
     * @MethodName paySuccess
     * @Description: 支付成功通知接口
     * @param map
     * @return: com.alibaba.fastjson.JSONObject
     * @Author: Sum1Dream
     * @Date: 2024/9/18 下午3:42
     */
    public static JSONObject paySuccess(Map<String , Object> map , String url) throws Exception {

        JSONObject object = request(url , map);

        if (object.getBoolean("msg")) {
            return object;
        } else {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "查询失败！");
        }

    }

    /**
     * 请求
     * @param postUrl  接口请求地址
     * @param param       参数
     * @return
     * @throws Exception
     */
    public static JSONObject request(String postUrl, Map<String,Object> param) throws Exception {

        log.info("============ 美团请求-START =============");
        param.put("accessKey", CommonSysConst.getSysConfig().getMtAccessKey());
        param.put("content", EncryptUtil.aesEncrypt(JsonUtil.object2Json(param), CommonSysConst.getSysConfig().getMtSecretKey()));

        log.info("请求接口：" + postUrl);
        log.info("请求参数：" + JSONObject.toJSONString(param));

        JSONObject response = HttpsUtils.doPost(postUrl, JSONObject.toJSONString(param));

        log.info("响应参数：" + response.toJSONString());
        log.info("============ 美团请求-END ==============");
        return response;

    }

    /**
     * @MethodName doPostMeiTuanUrl
     * @Description: 美团免登陆接口
     * @param postUrl
     * @param param
     * @return: com.alibaba.fastjson.JSONObject
     * @Author: Sum1Dream
     * @Date: 2024/9/18 下午5:44
     */
    public static JSONObject doPostMeiTuanUrl(String postUrl, Map<String,Object> param) throws Exception {

        log.info("============ 美团请求-START =============");
        param.put("accessKey", CommonSysConst.getSysConfig().getMtAccessKey());
        param.put("content", EncryptUtil.aesEncrypt(JsonUtil.object2Json(param), CommonSysConst.getSysConfig().getMtSecretKey()));

        log.info("请求接口：" + postUrl);
        log.info("请求参数：" + JSONObject.toJSONString(param));

        JSONObject response = HttpsUtils.doPostMeiTuanUrl(postUrl, JSONObject.toJSONString(param));

        log.info("响应参数：" + response.toJSONString());
        log.info("============ 美团请求-END ==============");
        return response;

    }

    /**
     * @MethodName payStatus
     * @Description: 美团支付状态转换
     * @param payStatus
     * @return: java.lang.Integer
     * @Author: Sum1Dream
     * @Date: 2024/9/18 下午6:03
     */
    public static Integer payStatus(Integer payStatus) {
        int result;
        switch (payStatus) {
            case 1:
                result = 0;
                break;
            case 2:
                result = 1;
                break;
            case 3:
                result = 1;
                break;
            case 4:
                result = 2;
                break;
            case 5:
                result = 10;
                break;
            default:
                throw new RuntimeException("状态不正确");
        }

        return result;
    }

}
