package com.common.service;

import com.common.entity.DeviceCollectTask;
import com.common.mapper.DeviceCollectTaskMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
@CacheConfig(cacheNames = "collectTask")
public class DeviceCollectTaskService {

    @Autowired
    private DeviceCollectTaskMapper deviceCollectTaskMapper;

    /**
     * 根据ID查询采集任务 - 缓存
     */
    @Cacheable(key = "'id:' + #id")
    public DeviceCollectTask getById(Integer id) {
        return deviceCollectTaskMapper.selectById(id);
    }

    /**
     * 查询所有采集任务 - 缓存
     */
    @Cacheable(key = "'all'")
    public List<DeviceCollectTask> getAll() {
        return deviceCollectTaskMapper.selectAll();
    }

    /**
     * 根据设备ID查询采集任务 - 缓存
     */
    @Cacheable(key = "'device:' + #deviceId")
    public List<DeviceCollectTask> getByDeviceId(Integer deviceId) {
        return deviceCollectTaskMapper.selectByDeviceId(deviceId);
    }

    /**
     * 根据方法类型查询采集任务
     */
    public List<DeviceCollectTask> getByMethod(Integer method) {
        return deviceCollectTaskMapper.selectByMethod(method);
    }

    /**
     * 多条件组合查询
     */
    public List<DeviceCollectTask> getByCondition(DeviceCollectTask condition) {
        return deviceCollectTaskMapper.selectByCondition(condition);
    }

    /**
     * 新增采集任务 - 清除相关缓存
     */
    @CacheEvict(allEntries = true)
    public int create(DeviceCollectTask task) {
        return deviceCollectTaskMapper.insert(task);
    }

    /**
     * 更新采集任务 - 更新缓存
     */
    @CachePut(key = "'id:' + #task.id")
    @CacheEvict(key = "'device:' + #task.deviceId")
    public DeviceCollectTask update(DeviceCollectTask task) {
        deviceCollectTaskMapper.update(task);
        return deviceCollectTaskMapper.selectById(task.getId());
    }

    /**
     * 删除采集任务 - 清除缓存
     */
    @CacheEvict(key = "'id:' + #id")
    public int delete(Integer id) {
        return deviceCollectTaskMapper.deleteById(id);
    }

    /**
     * 标记任务成功 - 更新缓存
     */
    @CacheEvict(key = "'id:' + #id")
    public int markSuccess(Integer id) {
        return deviceCollectTaskMapper.updateSuccessStatus(id);
    }

    /**
     * 标记任务失败 - 更新缓存
     */
    @CacheEvict(key = "'id:' + #id")
    public int markFailed(Integer id) {
        return deviceCollectTaskMapper.updateFailedStatus(id);
    }

    /**
     * 更新EPM指标 - 更新缓存
     */
    @CacheEvict(key = "'id:' + #id")
    public int updateEpm(Integer id, Integer epm) {
        return deviceCollectTaskMapper.updateEpm(id, epm);
    }

    /**
     * 查询成功的任务
     */
    public List<DeviceCollectTask> getSuccessTasks() {
        return deviceCollectTaskMapper.selectSuccessTasks();
    }

    /**
     * 查询失败的任务
     */
    public List<DeviceCollectTask> getFailedTasks() {
        return deviceCollectTaskMapper.selectFailedTasks();
    }

    /**
     * 获取设备的最新采集任务
     */
    @Cacheable(key = "'latest:device:' + #deviceId")
    public DeviceCollectTask getLatestByDeviceId(Integer deviceId) {
        return deviceCollectTaskMapper.selectLatestByDeviceId(deviceId);
    }

    /**
     * 统计设备任务数量
     */
    public int countByDeviceId(Integer deviceId) {
        return deviceCollectTaskMapper.countByDeviceId(deviceId);
    }
}