package com.thinkgem.jeesite.modules.pay.juhezhong.service;

import com.alibaba.fastjson.JSONObject;
import com.thinkgem.jeesite.common.constant.ComCode;
import com.thinkgem.jeesite.modules.pay.juhezhong.dto.ApiQuickPayDto;
import com.thinkgem.jeesite.modules.pay.juhezhong.dto.MerchantDto;
import com.thinkgem.jeesite.modules.pay.juhezhong.dto.CommonDto;
import com.thinkgem.jeesite.modules.pay.juhezhong.dto.QuickDto;
import com.thinkgem.jeesite.modules.pay.juhezhong.utils.Bean2Map;
import com.thinkgem.jeesite.modules.pay.juhezhong.utils.HttpUtil;
import com.thinkgem.jeesite.modules.pay.juhezhong.utils.SignUtils;
import com.thinkgem.jeesite.modules.user.bean.JuHeZhongPayRequest;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * 首页通知
 */
@Service
@Lazy(false)
@Transactional
public class JuHeZhongPayService {

    /**
     * 注册子商户
     *
     * @param dto
     * @return
     * @throws Exception
     */
    public JSONObject register(MerchantDto dto) throws Exception {
        Map<String, Object> params = Bean2Map.Entity2Map(dto);
        String signBefore = SignUtils.signBefore(params);
        String sign = SignUtils.sign(signBefore, ComCode.MERCHANT_SECRET);
        params.put("sign", sign);
        String respStr = HttpUtil.post(ComCode.HOST + ComCode.MERCHANT_REG_URL, params);
        return JSONObject.parseObject(respStr);
    }

    /**
     * 更新费率
     *
     * @param dto
     * @return
     * @throws Exception
     */
    public JSONObject updateRate(MerchantDto dto) throws Exception {
        Map<String, Object> params = Bean2Map.Entity2Map(dto);
        String signBefore = SignUtils.signBefore(params);
        String sign = SignUtils.sign(signBefore, ComCode.MERCHANT_SECRET);
        params.put("sign", sign);
        String respStr = HttpUtil.post(ComCode.HOST + ComCode.RATE_UPDATE_URL, params);
        return JSONObject.parseObject(respStr);
    }

    /**
     * 更新结算信息
     *
     * @param dto
     * @return
     * @throws Exception
     */
    public JSONObject updateSettle(MerchantDto dto) throws Exception {
        Map<String, Object> params = Bean2Map.Entity2Map(dto);
        String signBefore = SignUtils.signBefore(params);
        String sign = SignUtils.sign(signBefore, ComCode.MERCHANT_SECRET);
        params.put("sign", sign);
        String respStr = HttpUtil.post(ComCode.HOST + ComCode.SETTLE_UPDATE_URL, params);
        return JSONObject.parseObject(respStr);
    }

    /**
     * 快捷支付
     *
     * @param dto
     * @return
     * @throws Exception
     */
    public JSONObject quickPay(ApiQuickPayDto dto) throws Exception {
        Map<String, Object> params = Bean2Map.Entity2Map(dto);
        String signBefore = SignUtils.signBefore(params);
        String sign = SignUtils.sign(signBefore, ComCode.MERCHANT_SECRET);
        params.put("sign", sign);
        String respStr = HttpUtil.post(ComCode.HOST + ComCode.QUICK_PAY_URL, params);
        return JSONObject.parseObject(respStr);
    }


    /**
     * 快捷支付 无积分
     *
     * @param dto
     * @return
     * @throws Exception
     */
    public JSONObject quickPay(QuickDto dto) throws Exception {
        Map<String, Object> params = Bean2Map.Entity2Map(dto);
        System.out.println(params);
        String signBefore = SignUtils.signBefore(params);
        System.out.println(signBefore);
        String sign = SignUtils.sign(signBefore, ComCode.MERCHANT_SECRET);
        params.put("sign", sign);
        String respStr = HttpUtil.post(ComCode.HOST + ComCode.NO_INTEGRAL_QUICK_PAY_URL, params);
        return JSONObject.parseObject(respStr);
    }



    /**
     * 查询订单状态
     *
     * @param dto
     * @return
     * @throws Exception
     */
    public JSONObject query(ApiQuickPayDto dto) throws Exception {
        Map<String, Object> params = Bean2Map.Entity2Map(dto);
        String signBefore = SignUtils.signBefore(params);
        String sign = SignUtils.sign(signBefore, ComCode.MERCHANT_SECRET);
        params.put("sign", sign);
        String respStr = HttpUtil.post(ComCode.HOST + ComCode.ORDER_QUERY_URL, params);

        return JSONObject.parseObject(respStr);
    }

    /**
     * 查询订单状态  无积分
     *
     * @param dto
     * @return
     * @throws Exception
     */
    public JSONObject query(CommonDto dto) throws Exception {
        Map<String, Object> params = Bean2Map.Entity2Map(dto);
        String signBefore = SignUtils.signBefore(params);
        String sign = SignUtils.sign(signBefore, ComCode.MERCHANT_SECRET);
        params.put("sign", sign);
        String respStr = HttpUtil.post(ComCode.HOST + ComCode.NO_INTEGRAL_ORDER_QUERY_URL, params);
        return JSONObject.parseObject(respStr);
    }

    public static void main (String[] args) throws Exception {
//        ApiQuickPayDto request = new ApiQuickPayDto();
//        request.setAmount(10L);
//        request.setBody("23");
//        request.setFrontUrl("");
//        request.setNotifyUrl("");
//        request.setType(0);
//        request.setName("");
//        request.setUrl("");
//
//        JuHeZhongPayService juHeZhongPayService = new JuHeZhongPayService();
//        System.out.println(juHeZhongPayService.quickPay(request).toString());
        ApiQuickPayDto dto = new ApiQuickPayDto();
        dto.setOrderId("Dy58WqJG3MmNpSpQMq3wKZV55OiO48asPzRp4YUBBZUsVEiow7q6CZTQqwrBTl4k");
        dto.setMerchantId(ComCode.MERCHANT_ID);
        dto.setMerchantKey(ComCode.MERCHANT_KEY);

        Map<String, Object> params = Bean2Map.Entity2Map(dto);
        String signBefore = SignUtils.signBefore(params);
        String sign = SignUtils.sign(signBefore, ComCode.MERCHANT_SECRET);
        params.put("sign", sign);
        String respStr = HttpUtil.post(ComCode.HOST + ComCode.ORDER_QUERY_URL, params);

        System.out.println(respStr);

    }
}
