package com.limap.core.basic.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.limap.core.basic.domain.AquDevice;

import com.limap.core.basic.domain.AquDeviceDataHistory;
import com.limap.core.basic.mapper.AquDeviceMapper;
import com.limap.core.basic.service.IAquDeviceDataHistoryService;
import com.limap.core.basic.service.IAquDeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;


/**
 * 分润记录Service业务层处理
 *
 * @author jjb
 * @date 2025-04-11
 */
@Service
@DS("test")
@Slf4j
public class AquDeviceServiceImpl extends ServiceImpl<AquDeviceMapper, AquDevice> implements IAquDeviceService {


    @Autowired
    private IAquDeviceDataHistoryService aquDeviceDataHistoryService;

    HashSet<Long> ids = new HashSet<>();

    @Override
    @Cacheable(value = "getInfoByIotId",key = "#iotId",unless = "#result == null")
    public AquDevice getInfoByIotId(String iotId) {
        AquDevice iotId1 = this.getOne(new LambdaQueryWrapper<AquDevice>().eq(AquDevice::getIotId, iotId).last("limit 1"));
        return iotId1;
    }

    @Override
    @Async
    public void dealNowAndHistoryData(AquDevice aquDevice, JSONObject data) {
        if(ids.contains(aquDevice.getId())){
            return;
        }
        AquDeviceDataHistory history = new AquDeviceDataHistory();
        try{
            Date now = null;
            if(data.containsKey("PH")){
                JSONObject ph = data.getJSONObject("PH");
                Date time = ph.getDate("time");
                if(time != null )now = time ;
                aquDevice.setValuePH(ph.getDoubleValue("value"));
            }if(data.containsKey("currentTemperature")){
                JSONObject ph = data.getJSONObject("currentTemperature");
                Date time = ph.getDate("time");
                if(time != null )now = time ;
                aquDevice.setValueTemperature(ph.getDoubleValue("value"));
            }if(data.containsKey("dissolvedOxygen")){
                JSONObject ph = data.getJSONObject("dissolvedOxygen");
                Date time = ph.getDate("time");
                if(time != null )now = time ;
                aquDevice.setValueDissolvedOxygen(ph.getDoubleValue("value"));
            }if(data.containsKey("dosat")){
                JSONObject ph = data.getJSONObject("dosat");
                Date time = ph.getDate("time");
                if(time != null )now = time ;
                aquDevice.setValueSaturability(ph.getDoubleValue("value"));
            }if(data.containsKey("salinity")){
                JSONObject ph = data.getJSONObject("salinity");
                Date time = ph.getDate("time");
                if(time != null )now = time ;
                aquDevice.setValueSalinity(ph.getDoubleValue("value"));
            }if(data.containsKey("Phasedifference")){
                JSONObject ph = data.getJSONObject("Phasedifference");
                aquDevice.setPhasedifference(ph.getDoubleValue("value"));
            }if(data.containsKey("Tfluorescence")){
                JSONObject ph = data.getJSONObject("Tfluorescence");
                aquDevice.setTfluorescence(ph.getDoubleValue("value"));
            }if(data.containsKey("Treference")){
                JSONObject ph = data.getJSONObject("Treference");
                aquDevice.setTreference(ph.getDoubleValue("value"));
            }

            BeanUtils.copyProperties(aquDevice,history);
            history.setDeviceId(aquDevice.getId());
            history.setCreatedTime(now);
            history.setId(null);
            if(now != null ){
                try{
                    aquDeviceDataHistoryService.save(history);
                }catch (Exception e){
                    log.error("保存历史数据异常：{}",JSONObject.toJSON(history));
                }
            }
            if(now != null && (System.currentTimeMillis() -now.getTime() <= 1000*60) ){
                this.updateById(aquDevice);
            }
        }catch (Exception e){
            log.error("dealNowAndHistoryData error:{}",JSONObject.toJSON(history));
            log.error("dealNowAndHistoryData error:{}",e.getMessage());
        }
    }


}
