package com.cmvr.system.service.impl;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cmvr.common.core.redis.RedisCache;
import com.cmvr.system.domain.SysVehOperConfig;
import com.cmvr.system.mapper.SysVehOperConfigMapper;
import com.cmvr.system.service.ISysVehOperConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class ISysVehOperConfigServiceImpl extends ServiceImpl<SysVehOperConfigMapper, SysVehOperConfig> implements ISysVehOperConfigService {

    private final RedisCache redisCache;
    private static final String VEH_OPER_CONFIG_KEY = "veh_oper_config";

    @Override
    public SysVehOperConfig selectSysVehOperConfigById(Long id) {
        SysVehOperConfig config = getById(id);
        String redisKey = getCacheKey(config.getManufacturer(), config.getVehType());
        String configStr = redisCache.getCacheObject(redisKey);
        SysVehOperConfig vehOperConfig = JSON.parseObject(configStr, SysVehOperConfig.class);

        if (ObjUtil.isNotEmpty(vehOperConfig)) {
            return vehOperConfig;
        }
        vehOperConfig = baseMapper.selectById(id);

        if (ObjUtil.isNotEmpty(vehOperConfig)) {
            redisCache.setCacheObject(redisKey, JSON.toJSONString(vehOperConfig));
        }

        return vehOperConfig;
    }

    @Override
    public SysVehOperConfig selectSysVehOperConfig(String manufacturer, String vehType) {
        String redisKey = getCacheKey(manufacturer, vehType);
        String configStr = redisCache.getCacheObject(redisKey);
        SysVehOperConfig vehOperConfig = JSON.parseObject(configStr, SysVehOperConfig.class);

        if (ObjUtil.isNotEmpty(vehOperConfig)) {
            return vehOperConfig;
        }
        LambdaQueryWrapper<SysVehOperConfig> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SysVehOperConfig::getManufacturer, manufacturer)
                .eq(SysVehOperConfig::getVehType, vehType);
        vehOperConfig = getOne(wrapper);

        if (ObjUtil.isNotEmpty(vehOperConfig)) {
            redisCache.setCacheObject(redisKey, JSON.toJSONString(vehOperConfig));
        }

        return vehOperConfig;
    }

    @Override
    public List<SysVehOperConfig> selectSysVehOperConfigList(SysVehOperConfig vehOperConfig) {
        LambdaQueryWrapper<SysVehOperConfig> wrapper = Wrappers.lambdaQuery();
        wrapper
                .like(StrUtil.isNotEmpty(vehOperConfig.getManufacturer()), SysVehOperConfig::getManufacturer, vehOperConfig.getManufacturer())
                .like(StrUtil.isNotEmpty(vehOperConfig.getVehType()), SysVehOperConfig::getVehType, vehOperConfig.getVehType());
        return this.list(wrapper);
    }

    @Override
    public int insertVehOperConfig(SysVehOperConfig vehOperConfig) {
        int result = baseMapper.insert(vehOperConfig);

        if (result > 0) {
            String redisKey = getCacheKey(vehOperConfig.getManufacturer(), vehOperConfig.getVehType());
            redisCache.setCacheObject(redisKey, JSON.toJSONString(vehOperConfig));
        }

        return result;
    }

    @Override
    public int updateVehOperConfig(SysVehOperConfig vehOperConfig) {
        redisCache.deleteObject(getCacheKey(vehOperConfig.getConfigId()));
        int result = baseMapper.updateById(vehOperConfig);

        if (result > 0) {
            String redisKey = getCacheKey(vehOperConfig.getManufacturer(), vehOperConfig.getVehType());
            redisCache.setCacheObject(redisKey, JSON.toJSONString(vehOperConfig));
        }

        return result;
    }

    @Override
    public int deleteVehOperConfigByIds(Long[] ids) {
        for (Long id : ids) {
            redisCache.deleteObject(getCacheKey(id));
            baseMapper.deleteById(id);
        }
        return ids.length;
    }

    private String getCacheKey(String manufacturer, String vehType) {
        return StrUtil.format("{}:{}:{}", VEH_OPER_CONFIG_KEY, manufacturer, vehType);
    }

    private String getCacheKey(Long id) {
        LambdaQueryWrapper<SysVehOperConfig> wrapper = Wrappers.lambdaQuery();
        wrapper.select(SysVehOperConfig::getManufacturer,SysVehOperConfig::getVehType)
                .eq(SysVehOperConfig::getConfigId,id);
        SysVehOperConfig config = this.getOne(wrapper);
        return StrUtil.format("{}:{}:{}", VEH_OPER_CONFIG_KEY, config.getManufacturer(), config.getVehType());
    }
}
