<?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.engine.organization.mapper.extend.ExtendInfoMapper">
    <resultMap id="BaseResultMap" type="com.engine.organization.entity.extend.po.ExtendInfoPO">
        <result column="id" property="id"/>
        <result column="extend_type" property="extendType"/>
        <result column="table_name" property="tableName"/>
        <result column="field_name" property="fieldName"/>
        <result column="field_name_desc" property="fieldNameDesc"/>
        <result column="field_type" property="fieldType"/>
        <result column="control_type" property="controlType"/>
        <result column="browser_type" property="browserType"/>
        <result column="extend_group_id" property="extendGroupId"/>
        <result column="isenable" property="isenable"/>
        <result column="isrequired" property="isrequired"/>
        <result column="list_show" property="listShow"/>
        <result column="search_show" property="searchShow"/>
        <result column="edit_show" property="editShow"/>
        <result column="add_show" property="addShow"/>
        <result column="browser_show" property="browserShow"/>
        <result column="custom_value" property="customValue"/>
        <result column="show_order" property="showOrder"/>
        <result column="is_system_default" property="isSystemDefault"/>
        <result column="creator" property="creator"/>
        <result column="delete_type" property="deleteType"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
    </resultMap>

    <!-- 表字段 -->
    <sql id="baseColumns">
        t
        .
        id
        , t.extend_type
        , t.table_name
        , t.field_name
        , t.field_name_desc
        , t.field_type
        , t.control_type
        , t.browser_type
        , t.extend_group_id
        , t.isenable
        , t.isrequired
        , t.list_show
        , t.search_show
        , t.edit_show
        , t.add_show
        , t.browser_show
        , t.custom_value
        , t.show_order
        , t.is_system_default
        , t.creator
        , t.delete_type
        , t.create_time
        , t.update_time
    </sql>
    <insert id="insertExtendInfo" parameterType="com.engine.organization.entity.extend.po.ExtendInfoPO"
            keyProperty="id"
            keyColumn="id" useGeneratedKeys="true">
        INSERT INTO jcl_field_extendinfo
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="creator != null">
                creator,
            </if>
            <if test="deleteType != null">
                delete_type,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
            <if test="extendType != null ">
                extend_type,
            </if>
            <if test="tableName != null ">
                table_name,
            </if>
            <if test="fieldName != null ">
                field_name,
            </if>
            <if test="fieldNameDesc != null ">
                field_name_desc,
            </if>
            <if test="fieldType != null ">
                field_type,
            </if>
            <if test="controlType != null ">
                control_type,
            </if>
            <if test="browserType != null ">
                browser_type,
            </if>
            <if test="extendGroupId != null ">
                extend_group_id,
            </if>
            <if test="isenable != null ">
                isenable,
            </if>
            <if test="isrequired != null ">
                isrequired,
            </if>
            <if test="listShow != null ">
                list_show,
            </if>
            <if test="searchShow != null ">
                search_show,
            </if>
            <if test="editShow != null ">
                edit_show,
            </if>
            <if test="addShow != null ">
                add_show,
            </if>
            <if test="browserShow != null ">
                browser_show,
            </if>
            <if test="customValue != null ">
                custom_value,
            </if>
            <if test="showOrder != null ">
                show_order,
            </if>
            <if test="isSystemDefault != null ">
                is_system_default,
            </if>
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            <if test="creator != null">
                #{creator},
            </if>
            <if test="deleteType != null">
                #{deleteType},
            </if>
            <if test="createTime != null">
                #{createTime},
            </if>
            <if test="updateTime != null">
                #{updateTime},
            </if>
            <if test="extendType != null ">
                #{extendType},
            </if>
            <if test="tableName != null ">
                #{tableName},
            </if>
            <if test="fieldName != null ">
                #{fieldName},
            </if>
            <if test="fieldNameDesc != null ">
                #{fieldNameDesc},
            </if>
            <if test="fieldType != null ">
                #{fieldType},
            </if>
            <if test="controlType != null ">
                #{controlType},
            </if>
            <if test="browserType != null ">
                #{browserType},
            </if>
            <if test="extendGroupId != null ">
                #{extendGroupId},
            </if>
            <if test="isenable != null ">
                #{isenable},
            </if>
            <if test="isrequired != null ">
                #{isrequired},
            </if>
            <if test="listShow != null ">
                #{listShow},
            </if>
            <if test="searchShow != null ">
                #{searchShow},
            </if>
            <if test="editShow != null ">
                #{editShow},
            </if>
            <if test="addShow != null ">
                #{addShow},
            </if>
            <if test="browserShow != null ">
                #{browserShow},
            </if>
            <if test="customValue != null ">
                #{customValue},
            </if>
            <if test="showOrder != null ">
                #{showOrder},
            </if>
            <if test="isSystemDefault != null ">
                #{isSystemDefault},
            </if>
        </trim>
    </insert>
    <update id="addTableColumn">
        ALTER TABLE ${tableName}
            ADD ${fieldName} ${dbType} NULL;
    </update>
    <update id="updateExtendInfo" parameterType="com.engine.organization.entity.extend.po.ExtendInfoPO">
        update jcl_field_extendinfo
        <set>
            update_time=#{updateTime},
            field_name_desc=#{fieldNameDesc},
            isenable=#{isenable},
            isrequired=#{isrequired},
            edit_show=#{editShow},
            show_order=#{showOrder},
        </set>
        WHERE id = #{id} AND delete_type = 0
    </update>

    <update id="deleteExtendInfoByIds">
        UPDATE jcl_field_extendinfo
        SET delete_type = 1
        WHERE delete_type = 0
        AND id IN
        <foreach collection="ids" open="(" item="id" separator="," close=")">
            #{id}
        </foreach>
    </update>
    <update id="deleteTableColumn">
        ALTER TABLE ${tableName}
        DROP
        COLUMN
        ${fieldName};
    </update>
    <update id="updateExtendGroupId">
        UPDATE jcl_field_extendinfo
        SET extend_group_id = #{groupId}
        WHERE delete_type = 0
        AND id IN
        <foreach collection="ids" open="(" item="id" separator="," close=")">
            #{id}
        </foreach>
    </update>


    <select id="listFields" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumns"/>
        FROM
        jcl_field_extendinfo t
        WHERE t.delete_type = 0
        <if test=" extendType != null and extendType != '' ">
            and extend_type = #{extendType}
        </if>
        <if test=" extendGroupId != null and extendGroupId != '' ">
            and extend_group_id = #{extendGroupId}
        </if>
        <if test=" tableName != null and tableName != '' ">
            and table_name = #{tableName}
        </if>
        <if test=" operateType != null and operateType !='' ">
            and ${operateType}_show = 1
        </if>
        order by show_order
    </select>
    <select id="countFieldsByGroupId" resultType="java.lang.Integer">
        select count(1)
        from jcl_field_extendinfo t
        where t.delete_type = 0
          and extend_group_id = #{groupId}
          and table_name = #{tableName}
    </select>
    <select id="getMaxId" resultType="java.lang.Long">
        select max(id)
        from jcl_field_extendinfo
    </select>
    <select id="listFieldsByTableName" resultType="java.lang.Object">
        select ${fieldName}
        from ${tableName}
    </select>
    <select id="getExtendInfosByIds" resultMap="BaseResultMap">
        select
        <include refid="baseColumns"/>
        from jcl_field_extendinfo t
        where t.delete_type = 0
        AND id IN
        <foreach collection="ids" open="(" item="id" separator="," close=")">
            #{id}
        </foreach>
    </select>


</mapper>