package com.jeequan.jeepay.thirdparty.device.speaker.jsdSpeaker;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.interfaces.device.ISpeakerService;
import com.jeequan.jeepay.core.model.device.JsdParams;
import com.jeequan.jeepay.core.model.device.PayOrderInfo4Device;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/*
* 云喇叭厂商： 金士盾
*
* @author xy
*
* @date 2021/6/8 18:10
*/
@Slf4j
@Service
public class JsdSpeakerService implements ISpeakerService {

    private static final String REQ_URL = "http://139.224.51.127/push";

    @Override
    public void send(JSONObject deviceParams, PayOrderInfo4Device payOrderInfo) throws BizException {
        sendCustomMsg(deviceParams, payOrderInfo);
    }

    @Override
    public void sendCustomMsg(JSONObject deviceParams, PayOrderInfo4Device payOrderInfo) {

        JsdParams jsdParams = getJsdParams(deviceParams);
        JSONObject reqParams = JsdUtil.getSpeakData(jsdParams.getProviderParams().getCustomContent(), payOrderInfo);

        JSONObject resJSON = JsdUtil.post(REQ_URL, jsdParams, reqParams);

        if (resJSON == null) {
            throw new BizException("请求失败");
        }
        if (resJSON.getIntValue("code") != 0) {
            throw new BizException(resJSON.getString("msg"));
        }
    }

    private JsdParams getJsdParams(JSONObject deviceParams) {
        JSONObject jsdJSON = new JSONObject();
        jsdJSON.put("providerParams", JSON.parseObject(deviceParams.getString("providerParams")));
        jsdJSON.put("deviceParams", JSON.parseObject(deviceParams.getString("deviceParams")));

        return JSON.parseObject(jsdJSON.toJSONString(), JsdParams.class);
    }

}
