package com.usky.agbox.service.job;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usky.agbox.domain.PatrolInspectionAgbox;
import com.usky.agbox.service.PatrolInspectionAgboxService;
import com.usky.agbox.service.util.HttpClientUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.List;

@Component
public class patrolAgbox {

    @Value("${agBox.key}")
    private String KEY;
    @Value("${agBox.url}")
    private String URL;

    @Autowired
    private PatrolInspectionAgboxService patrolInspectionAgboxService;

    /**
     * 获取推送配置信息
     * @return
     */
    public List<PatrolInspectionAgbox> getAgBox() {
        LambdaQueryWrapper<PatrolInspectionAgbox> lambdaQuery = Wrappers.lambdaQuery();
        List<PatrolInspectionAgbox> list = patrolInspectionAgboxService.list(lambdaQuery);
        return list;
    }

    /**
     * 获取事件编码信息
     * @return
     */
    public JSONObject getEventCode() {
        List<PatrolInspectionAgbox> list = this.getAgBox();
        HashMap map = new HashMap();
        map.put("key", list.get(0).getAgboxKey());
        map.put("json", "{\"jsonrpc\":\"2.0\",\"method\":\"getPatrolEventCode\"}");
        String resultString = HttpClientUtils.doPost(list.get(0).getAgboxUrl(),map);
        JSONObject resultVO = JSONObject.parseObject(resultString);
        return resultVO;
    }

    /**
     * 获取设备列表信息
     * @return
     */
    public JSONObject getDeviceList() {
        List<PatrolInspectionAgbox> list = this.getAgBox();
        HashMap map = new HashMap();
        map.put("key", list.get(0).getAgboxKey());
        map.put("json", "{\"jsonrpc\":\"2.0\",\"method\":\"getDeviceList\"}");
        String resultString = HttpClientUtils.doPost(list.get(0).getAgboxUrl(),map);
        JSONObject resultVO = JSONObject.parseObject(resultString);
        return resultVO;
    }

    /**
     * 添加事件信息
     * @return
     */
    public JSONObject addEvent(String requestBody) {
        List<PatrolInspectionAgbox> list = this.getAgBox();
        JSONObject eventVO = JSONObject.parseObject(requestBody);
        HashMap map = new HashMap();
        map.put("key", list.get(0).getAgboxKey());
        map.put("json", "{\"jsonrpc\":\"2.0\",\"method\":\"addEvent\",\"params\":{\"deviceId\":\""+eventVO.get("deviceId").toString()+"\"," +
                "\"triggerTime\":\""+eventVO.get("triggerTime").toString()+"\",\"eventCode\":"+eventVO.get("eventCode")+",\"name" +
                "\":\""+eventVO.get("name").toString()+"\",\"certifiedNo\":\""+eventVO.get("certifiedNo").toString()+"\"}}");
        String resultString = HttpClientUtils.doPost(list.get(0).getAgboxUrl(),map);
        JSONObject resultVO = JSONObject.parseObject(resultString);
        return resultVO;
    }

    /**
     * 更新心跳信息
     * @return
     */
    public JSONObject updateHeart(String requestBody) {
        List<PatrolInspectionAgbox> list = this.getAgBox();
        JSONObject eventVO = JSONObject.parseObject(requestBody);
        HashMap map = new HashMap();
        map.put("key", list.get(0).getAgboxKey());
        map.put("json",
                "{\"jsonrpc\":\"2.0\",\"method\":\"updateHeart\",\"params\":{\"deviceId\":\""+eventVO.get("deviceId").toString()+"\"}}");
        String resultString = HttpClientUtils.doPost(list.get(0).getAgboxUrl(),map);
        JSONObject resultVO = JSONObject.parseObject(resultString);
        return resultVO;
    }

    /**
     * 获取事件信息
     * @return
     */
    public JSONObject getEvent(String requestBody) {
        List<PatrolInspectionAgbox> list = this.getAgBox();
        JSONObject eventVO = JSONObject.parseObject(requestBody);
        HashMap map = new HashMap();
        map.put("key", list.get(0).getAgboxKey());
        map.put("json", "{\"jsonrpc\":\"2.0\",\"method\":\"getEvent\",\"params\":{\"eventId\":\""+eventVO.get("eventId").toString()+"\"}}");
        String resultString = HttpClientUtils.doPost(list.get(0).getAgboxUrl(),map);
        JSONObject resultVO = JSONObject.parseObject(resultString);
        return resultVO;
    }
}
