package com.fs.watch.service.impl;

import com.fs.common.utils.StringUtils;
import com.fs.company.domain.Company;
import com.fs.watch.domain.WatchDeviceData;
import com.fs.watch.mapper.WatchDeviceDataMapper;
import com.fs.watch.service.WatchDeviceDataService;
import com.fs.watch.service.WatchUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @description 针对表【watch_device_data(设备信息)】的数据库操作Service实现
 * @createDate 2024-10-15 11:06:50
 */
@Service
@Slf4j
public class WatchDeviceDataServiceImpl implements WatchDeviceDataService {
    @Autowired
    private WatchDeviceDataMapper mapper;

    @Autowired
    private WatchUserService userService;

    /**
     * 新增
     *
     * @param watchDeviceData
     * @return
     */
    @Override
    public Boolean insert(WatchDeviceData watchDeviceData) {
        Integer num = mapper.queryByCreateTime(watchDeviceData.getCreateTime());
        if (num != null && num > 0) {
            return false;
        }
        try {
            mapper.insert(watchDeviceData);
        } catch (Exception e) {
            log.error("===============watchDeviceData:insert=========================={}", e.getMessage());
            return false;
        }
        return true;
    }

    @Override
    public int update(WatchDeviceData data) {
        return mapper.upadate(data);
    }

    @Override
    public Boolean updateByDeviceId(WatchDeviceData watchDeviceData) {
        if (StringUtils.isBlank(watchDeviceData.getDeviceId())) {
            return false;
        }
            mapper.updateByDeviceId(watchDeviceData);
//        }
        return true;

    }

    @Override
    public WatchDeviceData getByDeviceId(String deviceId) {
        return mapper.getByDeviceId(deviceId);
    }

    @Override
    public List<WatchDeviceData> getByUserId(String userId) {
        //查询用户绑定设备
        String deviceIds = userService.selectWatchFsUserById(Long.parseLong(userId)).getDeviceId();
        List<String> list = new ArrayList<>(Arrays.asList(deviceIds.split(",")));
        ArrayList<WatchDeviceData> res = new ArrayList<>();
        for (String s : list) {
            WatchDeviceData data = mapper.getByDeviceId(s);
            res.add(data);
        }
        return res;
    }

    /**
     * 检查数据库中是否存在给定设备编号的设备。
     *
     * @param deviceId 要检查的设备编号
     * @return 如果设备存在返回 true，否则返回 false
     */
    @Override
    public boolean existsByDeviceId(String deviceId) {
//        String sql = "SELECT COUNT(1) FROM watch_device_data WHERE device_id = ?";
//
//        // 执行查询
//        Integer count = clickhouseJdbcTemplate.queryForObject(sql, new Object[]{deviceId}, Integer.class);

        // 检查计数是否大于零
        Integer count = mapper.existsByDeviceId(deviceId);
        return count != null && count > 0;
    }
}
