{{#eq apps "link"}}
package {{packageName}}.api.{{apps}}.dict;

import cn.ibizlab.util.domain.DictItem;
import com.alibaba.fastjson.annotation.JSONCreator;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonValue;
import io.swagger.annotations.ApiModel;
import cn.ibizlab.util.annotation.SwaggerDisplayEnum;
import io.swagger.annotations.ApiModelProperty;
import java.util.*;

/**
 * 字典：{{dict.name}}
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
@ApiModel(value = "{{pascalCase dict.codeName}}Dict", description = "字典：{{dict.name}}")
public class {{pascalCase dict.codeName}}Dict {

    @SwaggerDisplayEnum
    public static enum ENUM{

        {{#each dict.optionItems}}
            {{#eq dict.valueType "String"}}
        {{name}} ("{{value}}","{{label}}",{{#if parent}}"{{parent}}"{{else}}null{{/if}}){{#if @last}};{{else}},{{/if}}
            {{/eq}}
            {{#eq dict.valueType "Integer"}}
        {{name}} ({{value}},"{{label}}",{{#if parent}}{{parent}}{{else}}null{{/if}}){{#if @last}};{{else}},{{/if}}
            {{/eq}}
        {{/each}}

        private {{dict.valueType}} value;
        @JsonValue
        @JSONField
        public {{dict.valueType}} getValue() {
            return value;
        }

        private String label;
        public String getLabel() {
            return label;
        }

        private {{dict.valueType}} parent;
        public {{dict.valueType}} getParent() {
            return parent;
        }

        ENUM({{dict.valueType}} value, String label, {{dict.valueType}} parent) {
            this.value = value;
            this.label = label;
            this.parent = parent;
        }

        @JsonCreator
        @JSONCreator
        public static ENUM from(Object tag) {
            if(tag==null)
                return null;
            for (ENUM item : values())
                if (tag.toString().equals(item.getValue(){{#eq dict.valueType "Integer"}}.toString(){{/eq}}))
                    return item;
            ENUM ret =null;
            for (ENUM item : values())
                if (item.getLabel().equals(tag))
                    ret = item;
            return ret;
        }
    }

    @SwaggerDisplayEnum
    public static class LIST extends ArrayList<ENUM> {

        public boolean isEnum() {
            return true;
        }
        @JsonValue
        public String getValue() {
            if(this.size()==0)
                return null;
            List<String> values=new ArrayList<>();
            this.forEach(item->values.add(item.getValue().toString()));
            return String.join("{{dict.valueSeparator}}",values);
        }

        public String getLabel() {
            if(this.size()==0)
                return null;
            List<String> values=new ArrayList<>();
            this.forEach(item->values.add(item.getLabel()));
            return String.join("{{dict.textSeparator}}",values);
        }

        @Override
        public String toString() {
            return getValue();
        }

        @JsonCreator
        @JSONCreator
        public static LIST form(Object tags) {
            if(tags==null)
                return null;
            LIST list=new LIST();
            Arrays.asList(tags.toString().split(";|,")).forEach(tag->{
                ENUM item=ENUM.from(tag);
                if(item!=null)
                    list.add(item);
            });
            if(list.size()==0)
                return null;
            return list;
        }
    }

    {{#lte dict.sets.size 1000}}
    @ApiModelProperty(value = "字典：{{dict.name}},示例<a href='/xftong/link/dict/{{pascalCase dict.codeName}}Dict.html' target='_blank'>view</a>", position = 1)
    public ENUM example;

    {{/lte}}
    @ApiModelProperty(value = "字典项清单", position = 2)
    private List<DictItem> allItems;
    public List<DictItem> getAllItems()
    {
        if(allItems==null) {
            allItems=new ArrayList<>();
            Arrays.asList(ENUM.values()).forEach(item->allItems.add(new DictItem().setValue(item.getValue().toString()).setLabel(item.getLabel()).setParent(item.getParent()!=null?item.getParent().toString():null)));
        }
        return allItems;
    }
}
{{/eq}}