package com.keyware.shandan.dynacmicform.core.basic.entity;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.*;
import com.keyware.shandan.common.entity.BaseEntity;
import com.keyware.shandan.common.util.StreamUtil;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.*;

/**
 * 表单信息表
 */
@Data
@EqualsAndHashCode(callSuper = false)
@TableName(value = "FORM_INFO")
public class FormInfoPo extends BaseEntity implements Serializable {

    private static final long serialVersionUID = 3181477069579982429L;

    @TableId(type = IdType.AUTO)
    @TableField("ID")
    private Integer id;

    /**
     * 表单唯一标识
     */
    @TableField("FORM_IDENTITY")
    private String formId;

    /**
     * 表单名称
     */
    @TableField(value = "FORM_NAME", condition = SqlCondition.LIKE)
    private String formName;

    /**
     * 字段配置
     */
    @TableField("FIELD_CONFIG")
    private String fieldConfig;

    /**
     * 查询列表配置
     */
    @TableField("QUERY_CONFIG")
    private String queryConfig;

    /**
     * 是否删除
     */
    @TableField(value = "IS_DELETE")
    @TableLogic(delval = "1", value = "0")
    private Boolean deleted;

    /**
     * 是否禁用
     */
    @TableField("IS_DISABLED")
    private Boolean disabled;


    /**
     * @param param 查询参数
     * @return 返回统计总行数Sql
     */
    public String getCountSql(QueryParam param) {
        return "select count(1) from \"" + formId + "\" where ";
    }

    /**
     * @param param 查询参数
     * @return 返回查询语句sql
     */
    public String getSelectSql(QueryParam param) {
        return "select * from \"" + formId + "\" where ";
    }

    /**
     * @return 返回表单查询列表配置中fieldId的集合, 如果没有查询列表配置，则返回表单字段配置
     */
    public List<String> getQueryColumns() {
        if (StringUtils.hasText(queryConfig)) {
            JSONArray jsonArray = JSONArray.parseArray(queryConfig);
            List<String> columns = StreamUtil.as(jsonArray).map(json -> "\"" + ((JSONObject) json).getString("fieldId") + "\"").toList();
            if (!columns.contains("id") && !columns.contains("ID")) {
                columns.add("\"ID\"");
            }
            return columns;
        } else {
            return getColumns(true);
        }
    }

    /**
     * @return 返回表单配置中fieldId的集合
     */
    public List<String> getColumns(boolean containsId) {
        if (StringUtils.hasText(fieldConfig)) {
            JSONArray jsonArray = JSONArray.parseArray(fieldConfig);
            List<String> columns = StreamUtil.as(jsonArray).map(json -> "\"" + ((JSONObject) json).getString("fieldId") + "\"").toList();
            if (containsId) {
                columns.add("\"ID\"");
            }
            return columns;
        }
        return new ArrayList<>();
    }


    /**
     * @return 以逗号拼接的列名称字符串
     */
    public String getQueryColumnsString() {
        return String.join(",", getQueryColumns());
    }

    /**
     * @return 返回基于查询列表配置字段的查询sql
     */
    public String getBasicSelectSql() {
        return "select " + getQueryColumnsString() + " from \"" + formId + "\" where ";
    }
}
