package com.liangjian.dataplatform.member.entity.vo.tagrules;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.util.List;
import java.util.Optional;

/**
 * @Author: 黄子奇
 * @Date: 2019/9/7 15:29
 *
 * 每个条件中的逻辑树
 */
@ApiModel("逻辑树")
public class LogicTree implements ToSqlable {

    /**
     * 第一个判断逻辑节点
     */
    @ApiModelProperty("第一个用于判断的节点")
    private BinOperation node;

    /**
     * 后续的判断逻辑节点
     */
    @ApiModelProperty("后续判断的节点")
    private List<LogicNode> extNodes;

    public LogicTree() {
    }

    public LogicTree(BinOperation node) {
        this.node = node;
    }

    public LogicTree(BinOperation node, List<LogicNode> extNodes) {
        this.node = node;
        this.extNodes = extNodes;
    }

    @Override
    public String toSql(SqlContext context) {
        String first = node.toSql(context);

        String ext = Optional.ofNullable(this.extNodes)
                .map(list ->
                        list.stream()
                                .map(i -> i.toSql(context))
                                .reduce((i, j) -> i+j+" ")
                                .orElse("")
                ).orElse("");
        return first + " " + ext;
    }

    public BinOperation getNode() {
        return node;
    }

    public void setNode(BinOperation node) {
        this.node = node;
    }

    public List<LogicNode> getExtNodes() {
        return extNodes;
    }

    public void setExtNodes(List<LogicNode> extNodes) {
        this.extNodes = extNodes;
    }
}
