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

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.LlznParams;
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 LlznSpeakerService implements ISpeakerService {

    private static final String REQ_URL = "https://pay-broadcast-api-test.rinlink.com/createVoiceBroadcast";

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

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

        LlznParams params = getLlznParams(deviceParams);
        JSONObject reqParams = LlznUtil.getSpeakData(params.getProviderParams().getCustomContent(), payOrderInfo);

        JSONObject resJSON = LlznUtil.post(REQ_URL, params, reqParams);

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

    private LlznParams getLlznParams(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(), LlznParams.class);
    }

}
