package com.engine.organization.entity.chart;

import com.alibaba.fastjson.annotation.JSONField;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;

import java.util.ArrayList;
import java.util.List;

/**
 * @author:dxfeng
 * @createTime: 2023/07/25
 * @version: 1.0
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class TreeSelect {
    public static final String COMPANY = "0";
    public static final String SUB_COMPANY = "1";
    public static final String DEPARTMENT = "2";

    @JSONField(ordinal = 1)
    private String key;
    @JSONField(ordinal = 2)
    private String title;
    @JSONField(ordinal = 3)
    private String id;
    @JSONField(ordinal = 4)
    private List<TreeSelect> children;
    private String type;
    private String canceled;
    private boolean disabled;

    private String showCanceled;

    public List<TreeSelect> getChildren() {
        if (null != children) {
            return children;
        }
        children = new ArrayList<>();
        RecordSet rs = new RecordSet();
        if (COMPANY.equals(type)) {
            String sql = "select id ,subcompanyname ,canceled from hrmsubcompany where " + getCancelSqlStr() + " and (supsubcomid is null or supsubcomid = 0) and companyid = ? order by showorder,id";
            rs.executeQuery(sql, key);
            while (rs.next()) {
                children.add(TreeSelect.builder().key(rs.getString("id")).title(rs.getString("subcompanyname")).canceled(rs.getString("canceled")).type(SUB_COMPANY).showCanceled(showCanceled).build());
            }
        } else if (SUB_COMPANY.equals(type)) {
            String sql = "select id ,subcompanyname ,canceled from hrmsubcompany where " + getCancelSqlStr() + " and supsubcomid = ? order by showorder,id";
            rs.executeQuery(sql, key);
            while (rs.next()) {
                children.add(TreeSelect.builder().key(rs.getString("id")).title(rs.getString("subcompanyname")).canceled(rs.getString("canceled")).type(SUB_COMPANY).showCanceled(showCanceled).build());
            }
            sql = "select id,departmentname ,canceled from hrmdepartment where " + getCancelSqlStr() + " and (supdepid is null or supdepid =0) and subcompanyid1 = ? order by showorder,id";
            rs.executeQuery(sql, key);
            while (rs.next()) {
                children.add(TreeSelect.builder().key(rs.getString("id")).title(rs.getString("departmentname")).canceled(rs.getString("canceled")).type(DEPARTMENT).showCanceled(showCanceled).build());
            }
        } else if (DEPARTMENT.equals(type)) {
            String sql = "select id,departmentname ,canceled from hrmdepartment where " + getCancelSqlStr() + " and supdepid  = ? order by showorder,id";
            rs.executeQuery(sql, key);
            while (rs.next()) {
                children.add(TreeSelect.builder().key(rs.getString("id")).title(rs.getString("departmentname")).canceled(rs.getString("canceled")).type(DEPARTMENT).showCanceled(showCanceled).build());
            }
        }
        return CollectionUtils.isEmpty(children) ? null : children;
    }

    public String getKey() {
        switch (type) {
            case COMPANY:
                return "c" + key;
            case SUB_COMPANY:
                return "s" + key;
            case DEPARTMENT:
                return "d" + key;
            default:
                break;
        }
        return key;
    }

    public String getId() {
        return getKey();
    }

    public String getCanceled() {
        if (StringUtils.isBlank(canceled)) {
            return "0";
        }
        return canceled;
    }

    public String getShowCanceled() {
        return null;
    }

    private String getCancelSqlStr() {
        if (StringUtils.isNotBlank(showCanceled) && "1".equals(showCanceled)) {
            return " 1=1 ";
        } else {
            return " (canceled is null or canceled != 1) ";
        }
    }
}
