package ${packageName}.model;

import java.util.List;
import java.util.ArrayList;
import ${packageName}.model.vo.${ClassName}Vo;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import java.io.Serializable;
import java.util.stream.Collectors;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.parser.JSONToken;
import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;
/**
* ${functionName}前端映射树结构实体类
*
* @author ${author}
* @date ${datetime}
*/
@Data
public class ${ClassName}TreeSelect  implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
    * 节点ID
    */
    private Long id;

    /**
    * 父节点ID
    */
    private Long parentId;

    /**
    * 节点名称
    */
    private String label;

    /**
    * 子节点
    */
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private List<${ClassName}TreeSelect> children;

    public ${ClassName}TreeSelect() {};

    public ${ClassName}TreeSelect(${ClassName}Entity entity) {
        this.id = entity.get${pkColumn.javaField?cap_first}();
        this.parentId = entity.get${treeParentCode?cap_first}();
        this.label = entity.get${treeName?cap_first}();
        if(!ObjectUtils.isEmpty(entity.getChildren())){
            this.children = entity.getChildren().stream().map(${ClassName}TreeSelect::new).collect(Collectors.toList());
        }
    }

    // 反序列化器
    public  class Deserializer implements ObjectDeserializer {
        @Override
        public ${ClassName}TreeSelect deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
            ${ClassName}TreeSelect node = new ${ClassName}TreeSelect();
            JSONObject jsonObject = parser.parseObject();
            node.setId(jsonObject.getLong("id"));
            node.setLabel(jsonObject.getString("label"));
            JSONArray jsonArray = jsonObject.getJSONArray("children");
            List<${ClassName}TreeSelect> children = new ArrayList<>();
            if(!ObjectUtils.isEmpty(jsonArray)){
                for (int i = 0; i < jsonArray.size(); i++) {
                    ${ClassName}TreeSelect child = JSON.parseObject(jsonArray.getJSONObject(i).toJSONString(), ${ClassName}TreeSelect.class);
                    children.add(child);
                }
            }
            node.setChildren(children);
            return node;
        }

        @Override
        public int getFastMatchToken() {
            return JSONToken.LBRACE;
        }
    }

}