package com.vetti.system.service.impl.international;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.vetti.common.core.redis.RedisCache;
import com.vetti.common.enums.InternationalLangTypeEnum;
import com.vetti.common.international.InternationalResourceGetter;
import com.vetti.common.utils.spring.SpringUtils;
import com.vetti.system.domain.SysInternationalInfo;
import com.vetti.system.service.ISysInternationalInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 国际化-获取翻译值 服务实现类
 *
 * @author WangXiangShun
 * @since 2025-08-28
 */
@Slf4j
@Service
public class InternationalResourceGetterImpl implements InternationalResourceGetter {

    private ISysInternationalInfoService internationalInfoService;

    private RedisCache redisCache;

    /**
     * 实例化对象
     */
    public void init() {
        if (internationalInfoService == null) {
            internationalInfoService = SpringUtils.getBean(ISysInternationalInfoService.class);
        }
        if (redisCache == null){
            redisCache = SpringUtils.getBean(RedisCache.class);
        }
    }

    /**
     * 如果是中文，直接返回国际化主表中的资源名称 如果是其他语言，根据语言类型，去国际化详细表中取对应的数据
     *
     * @param resourceId
     *            资源ID
     * @param lang
     *            语言
     * @return
     */
    @Override
    public String getText(String resourceId, String lang) {
        init();
        String internationalValue = "";
        //查询所有国际化语言列表信息
        //根据资源ID查询对应的国际化信息
        //先根据resourceId去redis查询，如果不存在再去数据库中查询
        String str = redisCache.getCacheObject(resourceId);
        SysInternationalInfo internationalInfo = null;
        if(StrUtil.isNotEmpty(str)){
            internationalInfo = JSONUtil.toBean(str,SysInternationalInfo.class);
        }else{
            SysInternationalInfo query = new SysInternationalInfo();
            query.setResourceId(resourceId);
            List<SysInternationalInfo> internationalInfoList = internationalInfoService.selectSysInternationalInfoList(query);
            if(CollectionUtil.isNotEmpty(internationalInfoList)){
                internationalInfo = internationalInfoList.get(0);
                redisCache.setCacheObject(resourceId,JSONUtil.toJsonStr(internationalInfo),10, TimeUnit.MINUTES);
            }
        }
        if(internationalInfo != null){
            if(InternationalLangTypeEnum.INTERNATIONAL_LANG_TYPE_ENUM_ZH.getCode().equals(lang)){
                internationalValue = internationalInfo.getResourceZhText();
            }else if(InternationalLangTypeEnum.INTERNATIONAL_LANG_TYPE_ENUM_EN.getCode().equals(lang)){
                internationalValue = internationalInfo.getResourceEnText();
            }
        }else{
            if(internationalInfo == null){
                internationalValue = resourceId;
            }
        }
        return internationalValue;
    }

}
