<?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="com.datai.generator.mapper.GenColumnMapper">
    
    <resultMap type="GenColumn" id="GenTableColumnResult">
        <id     property="columnId"       column="column_id"      />
        <result property="tableId"        column="table_id"       />
        <result property="columnName"     column="column_name"    />
        <result property="columnComment"  column="column_comment" />
        <result property="columnType"     column="column_type"    />
        <result property="javaType"       column="java_type"      />
        <result property="javaField"      column="java_field"     />
        <result property="isPk"           column="is_pk"          />
        <result property="isIncrement"    column="is_increment"   />
        <result property="isRequired"     column="is_required"    />
        <result property="isInsert"       column="is_insert"      />
        <result property="isEdit"         column="is_edit"        />
        <result property="isList"         column="is_list"        />
        <result property="isDetail"       column="is_detail"      />
        <result property="isQuery"        column="is_query"       />
        <result property="queryType"      column="query_type"     />
        <result property="htmlType"       column="html_type"      />
        <result property="dictType"       column="dict_type"      />
        <result property="sort"           column="sort"           />
        <result property="createBy"       column="create_by"      />
        <result property="createTime"     column="create_time"    />
        <result property="updateBy"       column="update_by"      />
        <result property="updateTime"     column="update_time"    />
        <result property="subColumnTableName"     column="sub_column_table_name"   />
        <result property="subColumnFkName"        column="sub_column_fk_name"      />
        <result property="subColumnName"          column="sub_column_name"         />
        <result property="subColumnJavaField"     column="sub_column_java_field"   />
        <result property="subColumnJavaType"      column="sub_column_java_type"    />
    </resultMap>
	
	<sql id="selectGenTableColumnVo">
        select column_id, table_id, column_name, column_comment, column_type, java_type, java_field, is_pk, is_increment, is_required, is_insert, is_edit, is_list, is_detail, is_query, query_type, html_type, dict_type, sort,sub_column_table_name,sub_column_fk_name,sub_column_name,sub_column_java_type,sub_column_java_field, create_by, create_time, update_by, update_time from gen_table_column
    </sql>
	
    <select id="selectGenTableColumnListByTableId" parameterType="Long" resultMap="GenTableColumnResult">
        <include refid="selectGenTableColumnVo"/>
        where table_id = #{tableId}
        order by sort
    </select>
    
    <select id="selectDbTableColumnsByName" parameterType="String" resultMap="GenTableColumnResult">
        <choose>
            <!-- PostgreSQL/openGauss适配：修复column_key和column_comment不存在问题 -->
            <when test="_databaseId == 'postgresql' or _databaseId == 'openGauss'">
                select 
                    c.column_name, 
                    c.udt_name as column_type,
                    coalesce(pd.description, '') as column_comment,
                    c.ordinal_position as sort, 
                    (case when (c.is_nullable = 'no' AND pk.column_name IS NULL) then '1' else '0' end) as is_required,
                    (case when pk.column_name IS NOT NULL then '1' else '0' end) as is_pk, 
                    (case when c.column_default like '%nextval%' then '1' else '0' end) as is_increment
                from information_schema.columns c 
                left join pg_catalog.pg_class pc on pc.relname = c.table_name
                left join pg_catalog.pg_description pd on pc.oid = pd.objoid and pd.objsubid = c.ordinal_position
                left join (
                    select kcu.column_name
                    from information_schema.key_column_usage kcu
                    inner join information_schema.table_constraints tc on kcu.constraint_name = tc.constraint_name
                    where tc.table_schema = 'public'
                    and tc.table_name = #{tableName}
                    and tc.constraint_type = 'PRIMARY KEY'
                ) pk on c.column_name = pk.column_name
            </when>
            <!-- 其他数据库（如MySQL）保留原逻辑 -->
            <otherwise>
                select 
                    column_name,
                    column_type,
                    column_comment,
                    ordinal_position as sort,
                    (case when (is_nullable = 'no' <![CDATA[ && ]]> column_key != 'PRI') then '1' else '0' end) as is_required,
                    (case when column_key = 'PRI' then '1' else '0' end) as is_pk, 
                    (case when extra = 'auto_increment' then '1' else '0' end) as is_increment
                from information_schema.columns
            </otherwise>
        </choose>

        where table_schema = (
            <choose>
                <when test="_databaseId == 'postgresql' or _databaseId == 'openGauss'">
                    'public'
                </when>
                <otherwise>
                    select database()
                </otherwise>
            </choose>
        ) and table_name = (#{tableName})
        order by ordinal_position
    </select>
    
    <insert id="insertGenTableColumn" parameterType="GenColumn" useGeneratedKeys="true" keyProperty="columnId">
        insert into gen_table_column (
			<if test="tableId != null and tableId != ''">table_id,</if>
			<if test="columnName != null and columnName != ''">column_name,</if>
			<if test="columnComment != null and columnComment != ''">column_comment,</if>
			<if test="columnType != null and columnType != ''">column_type,</if>
			<if test="javaType != null and javaType != ''">java_type,</if>
			<if test="javaField != null  and javaField != ''">java_field,</if>
			<if test="isPk != null and isPk != ''">is_pk,</if>
			<if test="isIncrement != null and isIncrement != ''">is_increment,</if>
			<if test="isRequired != null and isRequired != ''">is_required,</if>
			<if test="isInsert != null and isInsert != ''">is_insert,</if>
			<if test="isEdit != null and isEdit != ''">is_edit,</if>
			<if test="isList != null and isList != ''">is_list,</if>
			<if test="isDetail != null and isDetail != ''">is_detail,</if>
			<if test="isQuery != null and isQuery != ''">is_query,</if>
			<if test="queryType != null and queryType != ''">query_type,</if>
			<if test="htmlType != null and htmlType != ''">html_type,</if>
			<if test="dictType != null and dictType != ''">dict_type,</if>
			<if test="sort != null">sort,</if>
            <if test="subColumnTableName != null and subColumnTableName != ''">sub_column_table_name,</if>
            <if test="subColumnFkName != null and subColumnFkName != ''">sub_column_fk_name,</if>
            <if test="subColumnName != null and subColumnName != ''">sub_column_name,</if>
            <if test="subColumnJavaField != null and subColumnJavaField != ''">sub_column_java_field,</if>
            <if test="subColumnJavaType != null and subColumnJavaType != ''">sub_column_java_type,</if>
			<if test="createBy != null and createBy != ''">create_by,</if>
			create_time
         )values(
			<if test="tableId != null and tableId != ''">#{tableId},</if>
			<if test="columnName != null and columnName != ''">#{columnName},</if>
			<if test="columnComment != null and columnComment != ''">#{columnComment},</if>
			<if test="columnType != null and columnType != ''">#{columnType},</if>
			<if test="javaType != null and javaType != ''">#{javaType},</if>
			<if test="javaField != null and javaField != ''">#{javaField},</if>
			<if test="isPk != null and isPk != ''">#{isPk},</if>
			<if test="isIncrement != null and isIncrement != ''">#{isIncrement},</if>
			<if test="isRequired != null and isRequired != ''">#{isRequired},</if>
			<if test="isInsert != null and isInsert != ''">#{isInsert},</if>
			<if test="isEdit != null and isEdit != ''">#{isEdit},</if>
			<if test="isList != null and isList != ''">#{isList},</if>
			<if test="isDetail != null and isDetail != ''">#{isDetail},</if>
			<if test="isQuery != null and isQuery != ''">#{isQuery},</if>
			<if test="queryType != null and queryType != ''">#{queryType},</if>
			<if test="htmlType != null and htmlType != ''">#{htmlType},</if>
			<if test="dictType != null and dictType != ''">#{dictType},</if>
			<if test="sort != null">#{sort},</if>
            <if test="subColumnTableName != null and subColumnTableName != ''">#{subColumnTableName},</if>
            <if test="subColumnFkName != null and subColumnFkName != ''">#{subColumnFkName},</if>
            <if test="subColumnName != null and subColumnName != ''">#{subColumnName},</if>
            <if test="subColumnJavaField != null and subColumnJavaField != ''">#{subColumnJavaField},</if>
            <if test="subColumnJavaType != null and subColumnJavaType != ''">#{subColumnJavaType},</if>
			<if test="createBy != null and createBy != ''">#{createBy},</if>
			<choose>
                <when test="_databaseId == 'postgresql' or _databaseId == 'openGauss'">
                    CURRENT_TIMESTAMP
                </when>
                <otherwise>
                    sysdate()
                </otherwise>
            </choose>
         )
    </insert>
	 
    <update id="updateGenTableColumn" parameterType="GenColumn">
        update gen_table_column
        <set>
            <if test="columnComment != null">column_comment = #{columnComment},</if>
            <if test="javaType != null">java_type = #{javaType},</if>
            <if test="javaField != null">java_field = #{javaField},</if>
            <if test="isInsert != null">is_insert = #{isInsert},</if>
            <if test="isEdit != null">is_edit = #{isEdit},</if>
            <if test="isList != null">is_list = #{isList},</if>
			<if test="isDetail != null">is_detail = #{isDetail},</if>
			<if test="isQuery != null">is_query = #{isQuery},</if>
            <if test="isRequired != null">is_required = #{isRequired},</if>
            <if test="queryType != null">query_type = #{queryType},</if>
            <if test="htmlType != null">html_type = #{htmlType},</if>
            <if test="dictType != null">dict_type = #{dictType},</if>
            <if test="sort != null">sort = #{sort},</if>
            sub_column_table_name = #{subColumnTableName},
            sub_column_java_type = #{subColumnJavaType},
            sub_column_fk_name = #{subColumnFkName},
            sub_column_name = #{subColumnName},
            sub_column_java_field = #{subColumnJavaField},
            <if test="updateBy != null">update_by = #{updateBy},</if>
            update_time = <choose>
                <when test="_databaseId == 'postgresql' or _databaseId == 'openGauss'">
                    CURRENT_TIMESTAMP
                </when>
                <otherwise>
                    sysdate()
                </otherwise>
            </choose>
        </set>
        where column_id = #{columnId}
    </update>

    <delete id="deleteGenTableColumnByIds" parameterType="Long">
        delete from gen_table_column where table_id in 
        <foreach collection="array" item="tableId" open="(" separator="," close=")">
            #{tableId}
        </foreach>
    </delete>
    
    <delete id="deleteGenTableColumns">
        delete from gen_table_column where column_id in 
        <foreach collection="list" item="item" open="(" separator="," close=")">
            #{item.columnId}
        </foreach>
    </delete>

</mapper>