<%_ if (position === 'import') { _%>
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
    <%_ if (ormTool === 'mybatis') { _%>
import com.diboot.core.service.DictionaryServiceExtProvider;
    <%_ } _%>
import <%= packageName %>.domain.enumeration.DynamicEnum;
import com.diboot.core.vo.LabelValue;
import com.diboot.core.vo.DictionaryVO;
import org.apache.commons.collections4.CollectionUtils;
import java.util.Map;
import java.util.Objects;
import java.util.Collection;
import java.util.ArrayList;
import java.util.stream.Collectors;
<%_ } _%>
<%_ if (position === 'classExtend') { _%>
<%_ } _%>
<%_ if (position === 'classImplements') { _%>
    <%_ if (ormTool === 'mybatis') { _%>
<% if (serviceImpl || multiModule) { %>, <% } else { %> implements <% } %>DictionaryServiceExtProvider
    <%_ } _%>
<%_ } _%>
<%_ if (position === 'methodInterface') { _%>
<%_ } _%>
<%_ if (position === 'method') { _%>
    <%_ if (ormTool === 'mybatis') { _%>
    @Override
    public void bindItemLabel(List voList, String setFieldName, String getFieldName, String type) {
        if(CollectionUtils.isEmpty(voList)){
            return;
        }
        LambdaQueryWrapper<Dictionary> queryWrapper = new LambdaQueryWrapper<Dictionary>()
            .eq(Dictionary::getDictKey, type);
        List<Dictionary> entityList = super.getEntityList(queryWrapper, null);
        if (CollectionUtils.isNotEmpty(entityList)) {
            Dictionary dictionary = entityList.get(0);
            Binder.bindRelations(dictionary);
            Map<String, String> map = dictionary.getItems().stream().collect(Collectors.toMap(CommonFieldData::getValue, CommonFieldData::getLabel));
            for (Object item : voList) {
                Object value = BeanUtil.getProperty(item, getFieldName);
                if (Objects.isNull(value)) {
                    continue;
                }
                Object label = map.get(value);
                if (label == null) {
                    if (value instanceof String && ((String) value).contains(",")) {
                        List<String> labelList = new ArrayList<>();
                        for (String key : ((String) value).split(",")) {
                            labelList.add(map.get(key));
                        }
                        label = String.join(",", labelList);
                    } else if (value instanceof Collection) {
                        List<String> labelList = new ArrayList<>();
                        for (Object key : (Collection) value) {
                            labelList.add(map.get((String) key));
                        }
                        label = labelList;
                    }
                }
                if (Objects.nonNull(label)) {
                    BeanUtil.setProperty(item, setFieldName, label);
                }
            }
        }
    }

    @Override
    public List<LabelValue> getLabelValueList(String dictType) {
        LambdaQueryWrapper<Dictionary> queryWrapper = new LambdaQueryWrapper<Dictionary>()
                .eq(Dictionary::getDictKey, dictType);
        List<Dictionary> entityList = super.getEntityList(queryWrapper, null);
        if (CollectionUtils.isNotEmpty(entityList)) {
            Dictionary dictionary = entityList.get(0);
            Binder.bindRelations(dictionary);
            return dictionary.getItems().stream().map(item -> new LabelValue(item.getLabel(), item.getValue())).collect(Collectors.toList());
        } else {
          return new ArrayList<>();
        }
    }

     @Override
    public boolean existsDictType(String dictType) {
        return this.exists(new LambdaQueryWrapper<Dictionary>().eq(Dictionary::getDictKey, dictType));
    }

    @Override
    @Deprecated
    public boolean createDictAndChildren(DictionaryVO dictionaryVO) {
        return false;
    }

    @Override
    @Deprecated
    public List<com.diboot.core.entity.Dictionary> getDictDefinitionList() {
        return null;
    }

    @Override
    @Deprecated
    public List<DictionaryVO> getDictDefinitionVOList() {
        return null;
    }

    @Override
    @Deprecated
    public Map<String, LabelValue> getValue2ItemMap(String type) {
        return null;
    }

    @Override
    @Deprecated
    public Map<String, LabelValue> getLabel2ItemMap(String type) {
        return null;
    }

    public void updateEnums(DictionaryDTO dictionaryDTO) {
        try {
            Class clazz = Class.forName("<%= packageName %>.domain.enumeration." + dictionaryDTO.getDictKey());
            DynamicEnum.addEnum0(clazz, "d", new Class[0], "");
        } catch (ClassNotFoundException e) {
            log.error("updateEnums error: " + dictionaryDTO.getDictKey(), e);
            throw new RuntimeException(e);
        }
    }
    <%_ } _%>
<%_ } _%>
