package com.ruoyi.iot.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.iot.domain.vo.TDeviceRecordVO;
import com.ruoyi.iot.entity.param.DeviceParam;
import com.ruoyi.iot.ex.ServiceException;
import com.ruoyi.iot.mapper.TDeviceRecordMapper;
import com.ruoyi.iot.openfeign.MsgService;
import com.ruoyi.iot.service.ITDeviceRecordService;
import com.ruoyi.iot.transdata.DataParam;
import com.ruoyi.iot.transdata.RpcResult;
import com.ruoyi.iot.transdata.entity.MainBoard;
import com.ruoyi.iot.transdata.entity.PortStatus;
import com.ruoyi.iot.transdata.entity.opertype.OperEnum;
import com.ruoyi.iot.util.SplitDataUtil;
import com.ruoyi.iot.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Slf4j
@Service
public class TDeviceRecordServiceImpl implements ITDeviceRecordService {
    @Autowired
    TDeviceRecordMapper tDeviceRecordMapper;

    @Override
    public TDeviceRecordVO getStandardByDeviceId(DeviceParam deviceParam) {
        return null;
    }

    @Override
    @DataSource(value = DataSourceType.SLAVE)
    public JSONObject getPortDetail(DeviceParam deviceParam) {
        String deviceId = deviceParam.getDeviceId();
        String time = deviceParam.getTime();
        int splitTableNum = SplitDataUtil.getSplitTableNum(deviceId + "");
        Map<String, Object> deviceData = tDeviceRecordMapper.getDeviceData("select * from t_device_record_" + splitTableNum + " where device_id = '" + deviceId + "' and type = 103 and create_time>='" + time + "' order by create_time desc limit 1");
        if(deviceData != null){
            Object detail = deviceData.get("detail");
            JSONObject jsonObject = JSONObject.parseObject(detail.toString());
            JSONObject real_data = jsonObject.getJSONObject("real_data");
            return real_data;
        }
        return null;
    }




    @Override
    @DataSource(value = DataSourceType.SLAVE)
    public JSONObject getMainboardConfig(DeviceParam deviceParam) {
        String deviceId = deviceParam.getDeviceId();
        String time = deviceParam.getTime();
        int splitTableNum = SplitDataUtil.getSplitTableNum(deviceId + "");
        Map<String, Object> deviceData = tDeviceRecordMapper.getDeviceData("select * from t_device_record_" + splitTableNum + " where device_id = '" + deviceId + "' and type = 96 and create_time>='" + time + "' order by create_time desc limit 1");
        if(deviceData != null){
            Object detail = deviceData.get("detail");
            JSONObject jsonObject = JSONObject.parseObject(detail.toString());
            JSONObject real_data = jsonObject.getJSONObject("real_data");
            return real_data;
        }
        return null;
    }

    @Override
    @DataSource(value = DataSourceType.SLAVE)
    public JSONObject checkStatus(DeviceParam deviceParam) {
        String deviceId = deviceParam.getDeviceId();
        String time = deviceParam.getTime();
        int splitTableNum = SplitDataUtil.getSplitTableNum(deviceId + "");
        Map<String, Object> deviceData = tDeviceRecordMapper.getDeviceData("select * from t_device_record_" + splitTableNum + " where device_id = '" + deviceId + "' and type in(116,113) and create_time>='" + time + "' order by create_time desc limit 1");
        if(deviceData != null){
            JSONObject object = new JSONObject();
            Integer type = (Integer) deviceData.get("type");
            object.put("type",type);
            return object;
        }
        return null;
    }

}
