package cn.edu.whu.boot.common.domain;

/**
 * 存放 SQL 的模板常量类! 该类中的常量主要供 MessageFormat 使用，所以存在 {0} {1} 等样式!!!
 *
 * 为什么会存在这个类呢？系统用于自定义项目，也就是或表名、字段名你都不知道，那么你能通过
 * Task 来获取数据库表相关信息，然后基于这些信息组装 SQL ! 下面提供常用的模板，只需要确定参数即可!!!
 */
public class SqlStringConstant{
    /**
     * PostgreSQL 数据库 schema 相关的 SQL
     */
    public static final String DROP_SCHEMA = "DROP SCHEMA IF EXISTS {0} CASCADE;";
    public static final String CREATE_SCHEMA = "CREATE SCHEMA {0};";

    /**
     * PostgreSQL 数据库 table 相关的 SQL !
     * 注意 CREATE_Table 并没有带 ";" ，因为后面还需要追加字段的信息!!!
     */
    public static final String DROP_TABLE = "DROP TABLE IF EXISTS {0};";
    public static final String CREATE_TABLE_PREFIX = "CREATE TABLE {0}( ";
    public static final String CREATE_TABLE_COMMENT = "COMMENT on TABLE {0} is {1};";
    public static final String CREATE_COLUMN_COMMENT = "COMMENT on COLUMN {0} is {1};";

    /**
     * 创建多媒体表、轨迹表、草图表相关的 SQL !
     */
    public static final String CREATE_MEDIA_TABLE_SQL = "DROP TABLE IF EXISTS {0};" +
            "CREATE TABLE {0}( media_id serial, id integer NOT NULL, " +
            "file VARCHAR(255) NOT NULL ,media_path VARCHAR(255), azimuth decimal(13,8)," +
            "satellites INTEGER, longitude decimal(13,10), latitude decimal(13,10), " +
            "altitude decimal(13,8), accuracy decimal(13,8), camera_angle decimal(13,10), " +
            "camera_pitch decimal(13,10), camera_roll decimal(13,10), method VARCHAR(10), " +
            "time VARCHAR(50), focal_length decimal(13,8), type INTEGER , version smallint default 0, ";

    public static final String CREATE_ROUTE_TABLE_SQL = "DROP TABLE IF EXISTS {0}; " +
            "CREATE TABLE {0}(id serial PRIMARY key,collector varchar(255), collector_id integer, route text ," +
            " begin_time timestamp without time zone, stop_time timestamp without time zone);";

    public static final String CREATE_DRAFT_TABLE_SQL = "DROP TABLE IF EXISTS {0}; " +
            "CREATE TABLE {0}(id serial PRIMARY key,username varchar(255), collector_id integer," +
            "shape text , draw_time timestamp without time zone);";

    /**
     * PostgreSQL 数据库设置主键相关的 SQL !
     */
    public static final String CREATE_PRIMARY_KEY = "PRIMARY KEY ({0}));";
    public static final String CREATE_UNION_PRIMARY_KEY = "PRIMARY KEY ({0},{1}));";
    public static final String CREATE_MEDIA_PRIMARY_KEY = "PRIMARY KEY (media_id));";
    public static final String CREATE_MEDIA_UNION_PRIMARY_KEY = "PRIMARY KEY (media_id, version));";

    // 数据库查询中使用到的 SQL 模板

    /**
     * 抽稀获取 GeoHash 的边界
     */
    public static String SELECT_SQL_BY_GEOHASH = " select count(*) as datacount,geometry from ( "
            + " select ST_ASGEOJSON(st_geomfromgeohash(ST_GeoHash(ST_GeomFromGeoJSON({1}), {2}),{2})) as geometry,{1} "
            + " from {0} {7} ) AS geohash where ST_Intersects(ST_MakeEnvelope({3},{4},{5},{6},4490), st_setsrid(ST_GeomFromGeoJSON({1}), 4490)) "
            + " group by geometry ";

    /**
     * 网页端进行抽稀查询的固定SELECT_SQL_BY_GEOHASH,获取的是geohash的中心点
     */
    public static String SELECT_CENTROID_SQL_BY_GEOHASH = " select count(*) as datacount,geometry from ( "
            + " select ST_ASGEOJSON(ST_Centroid(st_geomfromgeohash(ST_GeoHash(ST_GeomFromGeoJSON({1}), {2}),{2}))) as geometry,{1} "
            + " from {0} ) AS geohash where ST_Intersects(ST_MakeEnvelope({3},{4},{5},{6},4490), st_setsrid(ST_GeomFromGeoJSON({1}), 4490)) "
            + " group by geometry ";

    /**
     * postgis中标准的点的GeoJson格式，主要是 SELECT_SQL_BY_DISTANCE 中用到
     */
    public static String POINT_GEOGSON = "\"coordinates\":[{0},{1}],\"type\":\"Point\"";

    /**
     * 网页端查询与中心点距离小于 30 m 的数据点
     */
    public static String SELECT_SQL_BY_DISTANCE = " select * from {0} where ST_distance(ST_GeomFromGeoJSON({1})::geography, ST_GeomFromGeoJSON({2})::geography) < 30";

    /**
     * 获取全部数据固定Sql
     */
    public static String SELECT_ALLDATA_SQL = "select * from {0}";

    /**
     *  网页端获取某范围全部数据
     */
    public static String SELECT_SQL_BY_RANGE_INTERSECTION = " select {1},{2},{3}, reviewer FROM {0}" +
            " where ST_Intersects(ST_MakeEnvelope({4},{5},{6},{7},4490), st_setsrid(ST_GeomFromGeoJSON({2}), 4490)) ";

    /**
     * 网页端获取某范围全部数据的中心点
     */
    public static String SELECTCENTER_SQL_BY_RANGE_INTERSECTION = " select {1},ST_ASGEOJSON(ST_GeomFromGeoJSON({2})) as geometry "
            + " FROM {0} where ST_Intersects(ST_MakeEnvelope({3},{4},{5},{6},4490), "
            + " st_setsrid(ST_GeomFromGeoJSON({2}), 4490)) ";
}
