<?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.yunfeiyun.agmp.generator.mapper.GenTableColumnMapper">

    <resultMap type="GenTableColumn" id="GenTableColumnResult">
        <id     property="cmnId"       column="cmnId"      />
        <result property="tblId"        column="tblId"       />
        <result property="cmnName"     column="cmnName"    />
        <result property="cmnComment"  column="cmnComment" />
        <result property="cmnType"     column="cmnType"    />
        <result property="cmnJavatype"       column="cmnJavatype"      />
        <result property="cmnJavafield"      column="cmnJavafield"     />
        <result property="cmnIspk"           column="cmnIspk"          />
        <result property="cmnIsincrement"    column="cmnIsincrement"   />
        <result property="cmnIsrequired"     column="cmnIsrequired"    />
        <result property="cmnIsinsert"       column="cmnIsinsert"      />
        <result property="cmnIsedit"         column="cmnIsedit"        />
        <result property="cmnIslist"         column="cmnIslist"        />
        <result property="cmnIsquery"        column="cmnIsquery"       />
        <result property="cmnQuerytype"      column="cmnQuerytype"     />
        <result property="cmnHtmltype"       column="cmnHtmltype"      />
        <result property="cmnDicttype"       column="cmnDicttype"      />
        <result property="cmnSeq"           column="cmnSeq"           />
        <result property="cmnCreator"       column="cmnCreator"      />
        <result property="cmnCreateddate"     column="cmnCreateddate"    />
        <result property="cmnModifier"       column="cmnModifier"      />
        <result property="cmnModifieddate"     column="cmnModifieddate"    />
    </resultMap>

	<sql id="selectGenTableColumnVo">
        select cmnId, tblId, cmnName, cmnComment, cmnType, cmnJavatype,
        cmnJavafield, cmnIspk, cmnIsincrement, cmnIsrequired, cmnIsinsert, cmnIsedit,
         cmnIslist, cmnIsquery, cmnQuerytype, cmnHtmltype, cmnQuerytype, cmnSeq, cmnCreator, cmnCreateddate,
          cmnModifier, cmnModifieddate from ToolsGenTablecolumn
    </sql>

    <select id="selectGenTableColumnListByTableId" parameterType="String" resultMap="GenTableColumnResult">
        <include refid="selectGenTableColumnVo"/>
        where tblId = #{tableId}
        order by cmnSeq
    </select>

    <select id="selectDbTableColumnsByName" parameterType="String" resultMap="GenTableColumnResult">
		select column_name as cmnName, (case when (is_nullable = 'no' <![CDATA[ && ]]> column_key != 'PRI') then '1' else null end) as cmnIsrequired, (case when column_key = 'PRI' then '1' else '0' end) as cmnIspk, ordinal_position as cmnSeq,
		column_comment as cmnComment, (case when extra = 'auto_increment' then '1' else '0' end) as cmnIsincrement,
		column_type as cmnType
		from information_schema.columns where table_schema = (select database()) and table_name = (#{tableName})
		order by ordinal_position
	</select>

    <insert id="insertGenTableColumn" parameterType="GenTableColumn" useGeneratedKeys="false" keyProperty="cmnId">
        insert into ToolsGenTablecolumn (
            <if test="cmnId != null and cmnId != ''">cmnId,</if>
			<if test="tblId != null and tblId != ''">tblId,</if>
			<if test="cmnName != null and cmnName != ''">cmnName,</if>
			<if test="cmnComment != null and cmnComment != ''">cmnComment,</if>
			<if test="cmnType != null and cmnType != ''">cmnType,</if>
			<if test="cmnJavatype != null and cmnJavatype != ''">cmnJavatype,</if>
			<if test="cmnJavafield != null  and cmnJavafield != ''">cmnJavafield,</if>
			<if test="cmnIspk != null and cmnIspk != ''">cmnIspk,</if>
			<if test="cmnIsincrement != null and cmnIsincrement != ''">cmnIsincrement,</if>
			<if test="cmnIsrequired != null and cmnIsrequired != ''">cmnIsrequired,</if>
			<if test="cmnIsinsert != null and cmnIsinsert != ''">cmnIsinsert,</if>
			<if test="cmnIsedit != null and cmnIsedit != ''">cmnIsedit,</if>
			<if test="cmnIslist != null and cmnIslist != ''">cmnIslist,</if>
			<if test="cmnIsquery != null and cmnIsquery != ''">cmnIsquery,</if>
			<if test="cmnQuerytype != null and cmnQuerytype != ''">cmnQuerytype,</if>
			<if test="cmnHtmltype != null and cmnHtmltype != ''">cmnHtmltype,</if>
			<if test="cmnDicttype != null and cmnDicttype != ''">cmnDicttype,</if>
			<if test="cmnSeq != null">cmnSeq,</if>
			<if test="cmnCreator != null and cmnCreator != ''">cmnCreator,</if>
        cmnCreateddate
         )values(
            <if test="cmnId != null and cmnId != ''">#{cmnId},</if>
			<if test="tblId != null and tblId != ''">#{tblId},</if>
			<if test="cmnName != null and cmnName != ''">#{cmnName},</if>
			<if test="cmnComment != null and cmnComment != ''">#{cmnComment},</if>
			<if test="cmnType != null and cmnType != ''">#{cmnType},</if>
			<if test="cmnJavatype != null and cmnJavatype != ''">#{cmnJavatype},</if>
			<if test="cmnJavafield != null and cmnJavafield != ''">#{cmnJavafield},</if>
			<if test="cmnIspk != null and cmnIspk != ''">#{cmnIspk},</if>
			<if test="cmnIsincrement != null and cmnIsincrement != ''">#{cmnIsincrement},</if>
			<if test="cmnIsrequired != null and cmnIsrequired != ''">#{cmnIsrequired},</if>
			<if test="cmnIsinsert != null and cmnIsinsert != ''">#{cmnIsinsert},</if>
			<if test="cmnIsedit != null and cmnIsedit != ''">#{cmnIsedit},</if>
			<if test="cmnIslist != null and cmnIslist != ''">#{cmnIslist},</if>
			<if test="cmnIsquery != null and cmnIsquery != ''">#{cmnIsquery},</if>
			<if test="cmnQuerytype != null and cmnQuerytype != ''">#{cmnQuerytype},</if>
			<if test="cmnHtmltype != null and cmnHtmltype != ''">#{cmnHtmltype},</if>
			<if test="cmnDicttype != null and cmnDicttype != ''">#{cmnDicttype},</if>
			<if test="cmnSeq != null">#{cmnSeq},</if>
			<if test="cmnCreator != null and cmnCreator != ''">#{cmnCreator},</if>
        date_format(sysdate(),'%Y-%m-%d %H:%i:%s')
         )
    </insert>

    <update id="updateGenTableColumn" parameterType="GenTableColumn">
        update ToolsGenTablecolumn
        <set>
            <if test="cmnComment != null">cmnComment = #{cmnComment},</if>
            <if test="cmnJavatype != null">cmnJavatype = #{cmnJavatype},</if>
            <if test="cmnJavafield != null">cmnJavafield = #{cmnJavafield},</if>
            <if test="cmnIsinsert != null">cmnIsinsert = #{cmnIsinsert},</if>
            <if test="cmnIsedit != null">cmnIsedit = #{cmnIsedit},</if>
            <if test="cmnIslist != null">cmnIslist = #{cmnIslist},</if>
            <if test="cmnIsquery != null">cmnIsquery = #{cmnIsquery},</if>
            <if test="cmnIsrequired != null">cmnIsrequired = #{cmnIsrequired},</if>
            <if test="cmnQuerytype != null">cmnQuerytype = #{cmnQuerytype},</if>
            <if test="cmnHtmltype != null">cmnHtmltype = #{cmnHtmltype},</if>
            <if test="cmnDicttype != null">cmnDicttype = #{cmnDicttype},</if>
            <if test="cmnSeq != null">cmnSeq = #{cmnSeq},</if>
            <if test="cmnCreator != null">cmnCreator = #{cmnCreator},</if>
            cmnModifieddate = date_format(sysdate(),'%Y-%m-%d %H:%i:%s')
        </set>
        where cmnId = #{columnId}
    </update>

    <delete id="deleteGenTableColumnByIds" parameterType="Long">
        delete from ToolsGenTablecolumn where tblId in
        <foreach collection="array" item="tableId" open="(" separator="," close=")">
            #{tableId}
        </foreach>
    </delete>

    <delete id="deleteGenTableColumns">
        delete from ToolsGenTablecolumn where cmnId in
        <foreach collection="list" item="item" open="(" separator="," close=")">
            #{item.columnId}
        </foreach>
    </delete>

</mapper>