package com.hzs.common.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ReflectUtil;
import com.hzs.common.service.ITransactionCommonService;
import com.hzs.system.base.ITransactionServiceApi;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: 翻译公用操作
 * @Author: jiang chao
 * @Time: 2022/10/22 11:58
 * @Classname: TransactionCommonServiceImpl
 * @PackageName: com.hzs.common.service.impl
 */
@Service
public class TransactionCommonServiceImpl implements ITransactionCommonService {

    @DubboReference
    private ITransactionServiceApi iTransactionServiceApi;

    /**
     * 导出枚举翻译（查询缓存）
     *
     * @param enumArr 需要翻译的枚举.values()
     * @return
     */
    @Override
    public Map<String, String> exportEnumTransaction(Enum[]... enumArr) {
        Set<String> keyList = new HashSet<>();
        for (Enum[] enums : enumArr) {
            for (Enum anEnum : enums) {
                Object keyObj = ReflectUtil.getFieldValue(anEnum, "key");
                if (null != keyObj) {
                    keyList.add(ReflectUtil.getFieldValue(anEnum, "key").toString());
                }
            }
        }
        return iTransactionServiceApi.readTransactionListByCache(keyList, LocaleContextHolder.getLocale().toLanguageTag()).getData();
    }

    @Override
    public Map<String, String> exportEnumTransactionToTag(String toTag, Enum[]... enumArr) {
        Set<String> keyList = new HashSet<>();
        for (Enum[] enums : enumArr) {
            for (Enum anEnum : enums) {
                Object keyObj = ReflectUtil.getFieldValue(anEnum, "key");
                if (null != keyObj) {
                    keyList.add(ReflectUtil.getFieldValue(anEnum, "key").toString());
                }
            }
        }
        return iTransactionServiceApi.readTransactionListByCache(keyList, null != toTag ? toTag : LocaleContextHolder.getLocale().toLanguageTag()).getData();
    }

    /**
     * 导出枚举翻译（查询数据库）
     *
     * @param enumsPrefix 翻译前缀值
     * @return
     */
    @Override
    public Map<Integer, String> exportEnumTransactionByDB(String... enumsPrefix) {
        Set<String> keyPrefixList = new HashSet<>();
        Collections.addAll(keyPrefixList, enumsPrefix);
        return iTransactionServiceApi.readTransactionListDataBase(keyPrefixList, LocaleContextHolder.getLocale().toLanguageTag()).getData();
    }

    /**
     * 导出枚举翻译（查询数据库）
     *
     * @param toTag       目标语言
     * @param enumsPrefix 翻译前缀值
     * @return
     */
    @Override
    public Map<Integer, String> enumTransactionByDBToTag(String toTag, String... enumsPrefix) {
        Set<String> keyPrefixList = new HashSet<>();
        Collections.addAll(keyPrefixList, enumsPrefix);
        return iTransactionServiceApi.readTransactionListDataBase(keyPrefixList, null != toTag ? toTag : LocaleContextHolder.getLocale().toLanguageTag()).getData();
    }

    @Override
    public Map<Integer, String> enumTransactionByDBFromPkId(Collection<Integer> pkIdList) {
        if (CollectionUtil.isEmpty(pkIdList)) {
            return new HashMap<>();
        }

        pkIdList = pkIdList.stream().filter(Objects::nonNull).collect(Collectors.toList());
        if (CollectionUtil.isEmpty(pkIdList)) {
            return new HashMap<>();
        }
        return iTransactionServiceApi.readTransactionListDataBase(pkIdList, LocaleContextHolder.getLocale().toLanguageTag()).getData();
    }

}
