<?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.cm.common.plugin.dao.dynamic.IDynamicDataDao">

    <!-- 新增动态数据 -->
    <insert id="saveDynamicData" parameterType="map">
        INSERT INTO ${tableName}(
            ${uuidField},
        <foreach collection="insertFieldList" item="insertField" open="" separator="," close=",">
            ${insertField}
        </foreach>
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        ) VALUES(
            #{uuidValue},
        <foreach collection="insertValueList" item="insertValue" open="" separator="," close=",">
            #{insertValue}
        </foreach>
            #{creator},
            #{gmtCreate},
            #{modifier},
            #{gmtModified},
            #{isDelete}
        )
    </insert>

    <!-- 删除动态数据 -->
    <update id="removeDynamicData" parameterType="map">
        UPDATE
            ${tableName}
        SET
            is_delete = 1,
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            ${uuidField} IN
        <foreach collection="idArray" index="index" open="(" separator="," close=")">
            #{idArray[${index}]}
        </foreach>
    </update>

    <!-- 修改动态数据 -->
    <update id="updateDynamicData" parameterType="map">
        UPDATE
            ${tableName}
        SET
        <foreach collection="updateFieldValueList" item="item">
        <if test="item.value != null">
            ${item.key} = #{item.value},
        </if>
        </foreach>
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            ${uuidField} = #{uuidValue}
    </update>

    <!-- 修改动态数据（链接） -->
    <update id="updateDynamicJoinData">
        UPDATE
            ${tableName}
        SET
        <foreach collection="updateFieldValueList" item="item">
        <if test="item.value != null">
            ${item.key} = #{item.value},
        </if>
        </foreach>
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            ${joinKey} = #{joinId}
    </update>

    <!-- 动态数据列表 -->
    <select id="listDynamicData" parameterType="map" resultType="map">
        SELECT
        <foreach collection="listShowFieldList" index="index" item="item" open="" separator="" close="">
            t1.${item.fieldName},
            <!-- 主表字典表字段 start -->
            <if test="item.fieldType == 'radio' or item.fieldType == 'select'">
            dt${index + 1}.dictionary_name ${item.fieldName}_dictionary_name,
            </if>
            <if test="item.fieldType == 'checkbox'">
            GROUP_CONCAT(DISTINCT dt${index + 1}.dictionary_name) ${item.fieldName}_dictionary_name,
            </if>
            <!-- 主表字典表字段 end -->
        </foreach>
        <!-- 联表字段 start -->
        <if test="listJoinTableField != null and listJoinTableField.size > 0">
            <foreach collection="listJoinTableField" index="index" item="item" open="" separator="" close="">
                <if test="item.joinTableListShow != null and item.joinTableListShow.size > 0" >
                    <foreach collection="item.joinTableListShow" index="joinIndex" item="joinItem" open="" separator="" close="">
            jt${index + 1}.${joinItem} ${joinItem}_jt${index + 1},
                    </foreach>
                </if>
            </foreach>
        </if>
        <!-- 联表字段 end -->
            t1.${uuidField}
        FROM
            ${tableName} t1
        <!-- 主表字典表 start -->
        <foreach collection="listShowFieldList" index="index" item="item" open="" separator="" close="">
            <if test="item.fieldType == 'radio'  or item.fieldType == 'select'">
        LEFT JOIN
            data_dictionary dt${index + 1}
        ON
            dt${index + 1}.dictionary_id = t1.${item.fieldName}
        AND
            dt${index + 1}.is_delete = 0
            </if>
            <if test="item.fieldType == 'checkbox'">
        LEFT JOIN
            data_dictionary dt${index + 1}
        ON
            FIND_IN_SET(dt${index + 1}.dictionary_id, t1.${item.fieldName})
        AND
            dt${index + 1}.is_delete = 0
            </if>
        </foreach>
        <!-- 主表字典表 end -->
        <!-- 联表字段 start -->
        <if test="listJoinTableField != null and listJoinTableField.size > 0">
            <foreach collection="listJoinTableField" index="index" item="item" open="" separator="" close="">
                <if test="item.fieldType == 'leftJoin'">
        LEFT JOIN
                </if>
                <if test="item.fieldType == 'innerJoin'">
        INNER JOIN
                </if>
                <if test="item.fieldType == 'rightJoin'">
        RIGHT JOIN
                </if>
            ${item.joinTable} jt${index + 1}
        ON
            t1.${item.fieldName} = jt${index + 1}.${item.joinTableField}
        AND
            jt${index + 1}.is_delete = 0
            </foreach>
        </if>
        <!-- 联表字段 end -->
        WHERE
            t1.is_delete = 0
        <if test="isCheckboxExist != null and isCheckboxExist == 'exist'">
        GROUP BY
            t1.${uuidField}
            <foreach collection="listShowFieldList" item="item" index="index" open="," separator="," close="">
                <if test="item.fieldType == 'radio' or item.fieldType == 'select'">
            dt${index + 1}.dictionary_name,
                </if>
            t1.${item.fieldName}
            </foreach>
            <if test="listJoinTableField != null and listJoinTableField.size > 0">
                <foreach collection="listJoinTableField" index="index" item="item" open="" separator="" close="">
                    <if test="item.joinTableListShow != null and item.joinTableListShow.size > 0" >
                        <foreach collection="item.joinTableListShow" index="joinIndex" item="item" open="," separator="," close="">
            jt${index + 1}.${item}
                        </foreach>
                    </if>
                </foreach>
            </if>
        </if>
    </select>

    <!-- 动态数据详情 -->
    <select id="getDynamicData" parameterType="map" resultType="map">
        SELECT
            ${uuidField}
        <foreach collection="formShowFieldList" item="item" open="," separator="," close="">
            ${item}
        </foreach>
        FROM
            ${tableName}
        WHERE
            is_delete = 0
        AND
            ${uuidField} = #{uuidValue}
    </select>

    <!-- 动态数据详情 -->
    <select id="getDynamicJoinData" parameterType="map" resultType="map">
        SELECT
            ${uuidField}
        <foreach collection="formShowFieldList" item="item" open="," separator="," close="">
            ${item}
        </foreach>
        FROM
            ${tableName}
        WHERE
            is_delete = 0
        AND
            ${joinKey} = #{joinId}
    </select>

    <!-- 动态数据详情 -->
    <select id="listDynamicJoinData" parameterType="map" resultType="map">
        SELECT
            ${uuidField}
        <foreach collection="formShowFieldList" item="item" open="," separator="," close="">
            ${item}
        </foreach>
        FROM
            ${tableName}
        WHERE
            is_delete = 0
        AND
            ${joinKey} IN
        <foreach collection="joinIds" index="index" open="(" separator="," close=")">
            #{joinIds[${index}]}
        </foreach>
    </select>

</mapper>