package org.example.Entity.Node;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import org.example.Utils.DataTableUtil;
import org.example.Utils.DbOperator;
import org.springframework.jdbc.core.JdbcTemplate;

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

/**
 * ==============================================================================
 * 功能描述：TreeNode 树形结构的节点
 * 创 建 者：zyw
 * 创建日期：2016-11-24 11:31:26
 * ==============================================================================
 */
public class TreeNode {
    private JdbcTemplate jdbcTemplate;
    private String countSql;
    private String idField;
    private String displayField;
    private Map<String, Object> row; // 对应DataRow，使用数组或Map存储行数据
    private DbOperator dbOperator;

    public TreeNode() {
    }

    public TreeNode(Map<String, Object> row, String idField, String displayField, JdbcTemplate jdbcTemplate, String countSql) {
        this.row = row;
        this.idField = idField;
        this.displayField = displayField;
        this.jdbcTemplate = jdbcTemplate;
        this.countSql = countSql;
        this.dbOperator = new DbOperator(jdbcTemplate);
    }

    private String speciesno;

    /**
     * 树的主键
     */
    public String getSpeciesno() {
        if (speciesno == null || speciesno.isEmpty()) {
            speciesno = DataTableUtil.getRowVal(row, new String[]{idField, "speciesno"}, "") + "";
        }
        return speciesno;
    }

    public void setSpeciesno(String speciesno) {
        this.speciesno = speciesno;
    }

    private String speciesname;

    /**
     * 树的显示名
     */
    public String getSpeciesname() {
        if (speciesname == null || speciesname.isEmpty()) {
            speciesname = DataTableUtil.getRowVal(row, new String[]{displayField, "speciesname"}, "") + "";
        }
        return speciesname;
    }

    public void setSpeciesname(String speciesname) {
        this.speciesname = speciesname;
    }

    @JsonIgnore
    public String parentno;

    private boolean checked;

    /**
     * 复选框
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public Boolean getChecked() {
        if (!checkbox) {
            return null;
        }
        return checked;
    }

    public void setChecked(Boolean checked) {
        this.checked = checked != null ? checked : false;
    }

    private boolean checkbox = true;

    @JsonIgnore
    public boolean isCheckbox() {
        return checkbox;
    }

    public void setCheckbox(boolean checkbox) {
        this.checkbox = checkbox;
    }

    @JsonInclude(JsonInclude.Include.NON_NULL)
    public Boolean expanded;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    public List<TreeNode> children;

    /**
     * 叶节点标识
     */
    private Boolean leaf = null;

    public boolean isLeaf() {
        if (leaf == null) {
            if (countSql != null && !countSql.isEmpty()) {
                // 执行计数查询判断是否为叶节点
                String sql = dbOperator.BuildCountSql(countSql);
                Integer count = jdbcTemplate.queryForObject(sql, new Object[]{speciesno}, Integer.class);
                leaf = count <= 0;
            } else {
                // 无计数SQL时根据子节点判断
                leaf = !(children != null && !children.isEmpty());
            }
        }
        return leaf != null ? leaf : false;
    }

    public void setLeaf(boolean leaf) {
        this.leaf = leaf;
    }

    /**
     * 其他需要传递给控件的参数
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public Object ElseValue;

}