package com.zt.plat.module.qms.core.legend;

import com.zt.plat.module.qms.common.dic.service.DictionaryBusinessService;
import com.zt.plat.module.qms.core.legend.vo.DictModel;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static com.zt.plat.module.qms.core.constant.DataTypeConstant.DICT_ANNOTATION_KEY_TYPE_KEY;
import static com.zt.plat.module.qms.core.constant.DataTypeConstant.DICT_ANNOTATION_SPLIT;

@Slf4j
@Service
public class LegendApi {

    @Resource
    DictionaryBusinessService dictionaryBusinessService;
    /**
     * 普通字典的翻译，根据多个dictCode和多条数据，多个以逗号分割
     * @param dictCodes 例如：user_status,sex
     * @param keys 例如：1,2,0
     * @return
     */
    public Map<String, List<DictModel>> translateManyDict(String dictCodes, String keys){
        List<String> dictCodeList = Arrays.asList(dictCodes.split(","));
        List<String> values = Arrays.asList(keys.split(","));
        return dictionaryBusinessService.queryManyDict(dictCodeList, values);
    }


    /**
     * 业务字典的翻译，根据多个dictCode和多条数据，多个以逗号分割
     * @param dictCodes 例如：user_status,sex
     * @param keys 例如：1,2,0
     * @return
     */
    public Map<String, List<DictModel>> translateManyDictBiz(String dictCodes, String keys) {
        List<String> dictCodeList = Arrays.asList(dictCodes.split(","));
        List<String> values = Arrays.asList(keys.split(","));
//        if(DICT_ANNOTATION_KEY_TYPE_KEY.equals(keyOrId))
        return dictionaryBusinessService.queryManyDict(dictCodeList, values);
//        return dictionaryBusinessService.queryManyDictByIDs(dictCodeList, values);
    }

    /**
     * 字典表的 翻译，可批量
     * @param table
     * @param text
     * @param code
     * @param keys 多个用逗号分割
     * @return
     */
    public List<DictModel> translateDictFromTableByKeys(String table, String text, String code, String keys) {
        return dictionaryBusinessService.queryTableDictText(table, text, code, List.of(keys.split(",")));
    }

    /**
     * 字典表翻译
     * */
    public String translateDictFromTable(String table, String text, String code, String key) {
        return dictionaryBusinessService.queryTableDictTextByKey(table, text, code, key);
    }

    /**
     * 普通字典翻译
     * */
    public String translateDict(String code, String key){
        String keyOrId = DICT_ANNOTATION_KEY_TYPE_KEY;
        if(code.contains(DICT_ANNOTATION_SPLIT)){
            keyOrId = code.split(DICT_ANNOTATION_SPLIT)[1];
            code = code.split(DICT_ANNOTATION_SPLIT)[0];
        }
        if(DICT_ANNOTATION_KEY_TYPE_KEY.equals(keyOrId))
            return dictionaryBusinessService.queryDictTextByKey(code, key);
        return dictionaryBusinessService.queryDictTextByID(code, key);
    }


}
