package com.ebts.generator.entity;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ebts.common.utils.StringUtils;

/**
 * @author: Clay
 * @date: 2021/7/26 17:26
 * @description：TopNode
 * @email: clay@hchyun.com
 */
public class TopNode {
    private String id;
    private String table;
    private String relTable;
    private StringBuffer tableSQL = new StringBuffer(" ");
    private StringBuffer querySql = new StringBuffer(" ");
    private JSONArray columns;

    public String getQuerySql() {
        return querySql.toString();
    }

    public String getRelTable() {
        return relTable;
    }

    public String getTableSQL() {
        return tableSQL.toString();
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTable() {
        return table;
    }

    public void setTable(String table) {
        this.table = table;
        this.relTable = StringUtils.sqlFormat(table);
        boolean start = false;
        for (int i = 0; i < this.columns.size(); i++) {
            JSONObject column = this.columns.getJSONObject(i);
            if (column.getInteger("isUse") == 1) {
                if (start) {
                    tableSQL.append(", ");
                }
                tableSQL.append(this.relTable).append(".").append(column.getString("columnName"));
                if (StringUtils.isNotNull(column.getString("columnComment"))) {
                    tableSQL.append(" as ").append("'").append(column.getString("columnComment")).append("'");
                }
                start = true;
            }

            if (column.getInteger("isQuery") == 1 ) {
                String sql = " and " + relTable + "." + column.getString("columnName");
                switch (column.getString("ucCon")) {
                    case "EQ":
                        sql += " = '" + column.getString("ucMock") + "'";
                        break;
                    case "NE":
                        sql += " != '" + column.getString("ucMock") + "'";
                        break;
                    case "GT":
                        sql += " > '" + column.getString("ucMock") + "'";
                        break;
                    case "GTE":
                        sql += " >= '" + column.getString("ucMock") + "'";
                        break;
                    case "LT":
                        sql += " < '" + column.getString("ucMock") + "'";
                        break;
                    case "LTE":
                        sql += " <= '" + column.getString("ucMock") + "'";
                        break;
                    case "LIKE":
                        sql += " like '%" + column.getString("ucMock") + "%'";
                        break;
                    case "BETWEEN":
                        try {
                            JSONObject jsonObject = JSONObject.parseObject(column.getString("ucMock"));
                            if ("input".equals(column.getString("ucType"))) {
                                Object begin = jsonObject.get("begin");
                                Object end = jsonObject.get("end");
                                if (begin != null && end != null) {
                                    sql += " between '" + begin + "' AND '" + end + "'";
                                } else {
                                    sql = "";
                                }
                            } else if ("datetime".equals(column.getString("ucType"))) {
                                Object startTime = jsonObject.get("startTime");
                                Object endTime = jsonObject.get("endTime");
                                sql += " between '" + startTime + "' AND '" + endTime + "'";
                            }
                        } catch (RuntimeException e) {
                            sql = "";
                        }
                        break;
                }
                querySql.append(sql);
            }
        }
    }

    public JSONArray getColumns() {
        return columns;
    }

    public void setColumns(JSONArray columns) {
        this.columns = columns;
    }


}
