package com.usky.backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usky.backend.domain.DataRealTime;
import com.usky.backend.domain.request.DataRealTimeRequestVO;
import com.usky.backend.domain.response.DataRealTimeResponseVO;
import com.usky.backend.mapper.DataRealTimeMapper;
import com.usky.backend.service.DataRealTimeService;
import com.usky.common.mybatis.core.AbstractCrudService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 实时数据表 服务实现类
 * </p>
 *
 * @author ya
 * @since 2023-06-07
 */
@Service
public class DataRealTimeServiceImpl extends AbstractCrudService<DataRealTimeMapper, DataRealTime> implements DataRealTimeService {

    @Override
    public List<DataRealTimeResponseVO> data(String productCode, List<String> deviceIds){
        List<DataRealTimeResponseVO> responseVOList = new ArrayList<>();

        LambdaQueryWrapper<DataRealTime> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(StringUtils.isNotBlank(productCode),DataRealTime::getProductCode, productCode)
                .in(DataRealTime::getDeviceId, deviceIds);
        List<DataRealTime> list = this.list(queryWrapper);
        if(CollectionUtils.isNotEmpty(list)){
            for(int i=0;i<list.size();i++){
                DataRealTimeResponseVO responseVO = new DataRealTimeResponseVO();
                responseVO.setDeviceId(list.get(i).getDeviceId());
                responseVO.setProductCode(list.get(i).getProductCode());
                responseVO.setDeviceType(list.get(i).getDeviceType());
                responseVO.setAttributeName(list.get(i).getAttributeName());
                responseVO.setAttributeData(list.get(i).getAttributeData());
                responseVO.setDataTime(list.get(i).getDataTime());
                responseVO.setInsertTime(list.get(i).getInsertTime());

                responseVOList.add(responseVO);
            }
        }
        return responseVOList;
    }


}
