package com.ruoyi.robot.service.impl;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.robot.domain.LdMapProject;
import com.ruoyi.robot.domain.vo.LdMapListVo;
import com.ruoyi.robot.mapper.LdMapProjectMapper;
import com.ruoyi.robot.service.ILdMapProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 地图工程Service实现
 */
@Service
public class LdMapProjectServiceImpl implements ILdMapProjectService {

    @Autowired
    private LdMapProjectMapper mapProjectMapper;

    @Autowired
    private RedisCache redisCache;

    private static final String MAP_LIST_CACHE_KEY = "ld:map:list";
    private static final String CURRENT_MAP_CACHE_KEY = "ld:map:current:";

    @Override
    public List<LdMapListVo> selectMapList() {
        return mapProjectMapper.selectMapListVo();
    }

    @Override
    public LdMapProject selectCurrentMap(String deviceId) {
        String cacheKey = CURRENT_MAP_CACHE_KEY + (deviceId != null ? deviceId : "default");

        LdMapProject cached = redisCache.getCacheObject(cacheKey);
        if (cached != null) {
            return cached;
        }

        LdMapProject map = mapProjectMapper.selectCurrentMapByDeviceId(deviceId);

        if (map != null) {
            redisCache.setCacheObject(cacheKey, map);
        }

        return map;
    }

    @Override
    public LdMapProject selectMapByName(String mapName) {
        return mapProjectMapper.selectByMapName(mapName);
    }

    @Override
    public int renameMap(String oldName, String newName) {
        LdMapProject map = mapProjectMapper.selectByMapName(oldName);
        if (map == null) {
            return 0;
        }

        map.setDisplayName(newName);
        int result = mapProjectMapper.updateById(map);

        // 清除缓存
        if (result > 0) {
            redisCache.deleteObject(MAP_LIST_CACHE_KEY);
            redisCache.deleteObject(CURRENT_MAP_CACHE_KEY + (map.getDeviceId() != null ? map.getDeviceId() : "default"));
        }

        return result;
    }

    @Override
    public String getThumbnailUrl(String mapName) {
        LdMapProject map = selectMapByName(mapName);
        if (map != null && map.getThumbnailUrl() != null) {
            return map.getThumbnailUrl();
        }
        return null;
    }

    /**
     * 新增地图工程
     *
     * @param ldMapProject 地图工程
     * @return 结果
     */
    @Override
    public int insertLdMapProject(LdMapProject ldMapProject)
    {
        ldMapProject.setCreateTime(DateUtils.getNowDate());
        return mapProjectMapper.insertLdMapProject(ldMapProject);
    }

    /**
     * 修改地图工程
     *
     * @param ldMapProject 地图工程
     * @return 结果
     */
    @Override
    public int updateLdMapProject(LdMapProject ldMapProject)
    {
        ldMapProject.setUpdateTime(DateUtils.getNowDate());
        return mapProjectMapper.updateLdMapProject(ldMapProject);
    }
}
