<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="cn.edu.whu.boot.collection.mapper.BaseMapper">

    <insert id="insertSql" parameterType="java.lang.String">
        ${insertSql}
    </insert>

    <insert id="insertSqlEnity" parameterType="cn.edu.whu.boot.collection.dto.SqlEnity">

        <selectKey keyProperty="sqlEnity.id" resultType="java.lang.Long" order="AFTER">
            SELECT currval(${sqlEnity.primaryKeySeq}::regclass)
        </selectKey>
        ${sqlEnity.sql}
    </insert>

    <select id="getVersionByPolygonId" resultType="java.lang.Integer">
        SELECT version from ${tableName}
        where ${primaryKeyName} = ${primaryKeyValue}
    </select>

    <select id="selectData" parameterType="java.lang.String" resultType="java.util.Map">
        ${selectSql}
    </select>

    <select id="getUserWorkRole" resultType="java.lang.Integer">
        SELECT work_role from task_user_role where task_id = #{taskId} and user_id = #{userId}
    </select>

    <select id="getNotPassDataId" resultType="java.util.Map">
        SELECT ${primaryKey},reason,status from ${tableNameInDb}
        where (collector = #{userId} or reviewer = #{userId}) and (status = -1 OR status = 2)
    </select>
    <select id="getStatisticDataNum" resultType="java.lang.Long">
        SELECT count(*) FROM ${tableNameInDb}
        WHERE ST_Intersects(ST_GeomFromGeoJSON(${geometryName}),st_geomfromgeojson(${geoJson}));
    </select>

    <select id="getDataCount" resultType="java.lang.Long">
        SELECT count(*) FROM ${tableNameInDb}
    </select>

    <select id="getHasCollectAndPatrolDataCount" resultType="java.lang.Long">
        SELECT count(*) FROM ${tableNameInDb}
        WHERE status != 0;
    </select>

    <select id="getDataCountAndTimeByUser" resultType="java.util.Map">
        select count(*) as "dataCount", to_char(update_time,'yyyy-MM-dd') as "updateTime" from ${tableNameInDb}
        WHERE (collector = #{userId} or reviewer = #{userId}) and status != 0
        GROUP BY to_char(update_time,'yyyy-MM-dd') ORDER BY "updateTime"
    </select>

    <select id="getAllFuzzySearchData" resultType="java.util.Map">
        SELECT * FROM ${tableNameInDb} WHERE ${fieldNameInDB} LIKE ${searchKey};
    </select>

    <select id="getAllFuzzySearchDataCenterPoint" resultType="java.util.Map">
        SELECT *,ST_ASGEOJSON(ST_Centroid(ST_GeomFromGeoJSON(${geometryFieldNameInDB}))) AS geometry
        FROM ${tableNameInDb} WHERE ${fieldNameInDB} LIKE ${searchKey};
    </select>

    <select id="randomSelectDataIdNotInList" resultType="java.util.Map">
        SELECT * FROM ${tableNameInDb}
        <where>
            <if test="selectIdList != null ">
                ${primaryKey} NOT IN
                <foreach collection="selectIdList" index="index" item="item"
                         open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="whereClause != null and !whereClause.trim().isEmpty()">
                AND ${whereClause}
            </if>
        </where>
        order by random() limit ${selectDataNum}
    </select>

    <select id="getIdsByReviewer" resultType="java.lang.Integer">
        SELECT ${primaryKey}
        FROM ${tableNameInDb}
        WHERE reviewer = #{reviewerId}
    </select>

    <select id="getReviewerByDataId" resultType="java.lang.Integer">
        SELECT reviewer
        FROM ${tableNameInDb}
        WHERE ${primaryKey} = #{dataId}
    </select>
    <select id="selectSingleColumn" resultType="String">
        SELECT column_name
        FROM information_schema.columns
        WHERE table_name = #{tableName}
        AND table_schema = #{databaseName}
    </select>


</mapper>