package cn.iocoder.yudao.module.iot.job;

import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
import cn.iocoder.yudao.module.iot.dal.mysql.device.DeviceMapper;
import cn.iocoder.yudao.module.iot.dal.mysql.devicecontactmodel.DeviceContactModelMapper;
import cn.iocoder.yudao.module.iot.service.device.TDengineService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import java.util.Date;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

@Slf4j
@Component
public class DeviceJob implements JobHandler {

    @Resource
    private TDengineService tDengineService;

    @Resource
    private DeviceMapper deviceMapper;

    @Resource
    private DeviceContactModelMapper deviceContactModelMapper;

    @Override
    public String execute(String param) throws Exception {

        // 解析JSON字符串获取deviceId
        JSONObject jsonParam = JSON.parseObject(param);
        System.out.println(jsonParam + new Date().toString());
//        Long deviceId = jsonParam.getLong("deviceId");
//        log.info("定时任务执行，接收到的参数 param: {}", param);
//        if (deviceId == null){
//            throw exception(DEVICE_DOES_NOT_EXIST);
//        }

//        // 设置租户上下文
//        TenantContextHolder.setTenantId(1L);
//
//        LambdaQueryWrapper<DeviceContactModelDO> deviceModelAttributeLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        deviceModelAttributeLambdaQueryWrapper.eq(DeviceContactModelDO::getDeviceId,deviceId);
//        List<DeviceContactModelDO> deviceContactModelDOS = deviceContactModelMapper.selectList(deviceModelAttributeLambdaQueryWrapper);
//
//        if (deviceContactModelDOS != null && deviceContactModelDOS.size() > 0){
//            for (DeviceContactModelDO deviceContactModelDO : deviceContactModelDOS) {
//                Object addressValue = OpcUtils.readValue(deviceContactModelDO.getAddress() != null ? deviceContactModelDO.getAddress() : "");
//                deviceContactModelDO.setAddressValue(addressValue);
//            }
//
//        }
//        String json = JSON.toJSONString(deviceContactModelDOS);
//        tDengineService.insertDeviceData(deviceId,json);

        return "";
    }
}
