<?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.cordys.crm.form.mapper.ExtCustomFormDataMapper">
    <select id="list" resultType="cn.cordys.crm.form.dto.response.CustomFormDataListResponse">
        select d.id, d.custom_form_id as customFormId, d.name, d.owner, d.create_user as createUser, d.update_user as updateUser,
               d.create_time as createTime, d.update_time as updateTime
        from custom_form_data d

        <include refid="fieldConditionJoin">
            <property name="conditions" value="request.filters"/>
            <property name="tablePrefix" value="filter"/>
        </include>
        <include refid="fieldConditionJoin">
            <property name="conditions" value="request.combineSearch.conditions"/>
            <property name="tablePrefix" value="combine"/>
        </include>
        <include refid="fieldConditionJoin">
            <property name="conditions" value="request.viewCombineSearch.conditions"/>
            <property name="tablePrefix" value="viewCombine"/>
        </include>
        <include refid="fieldSortJoin">
            <property name="sort" value="request.sort"/>
        </include>

        where d.custom_form_id = #{request.customFormId}

        <if test="manageOwn">
            and d.owner = #{userId}
        </if>

        <if test="request.keyword != null and request.keyword != ''">
            and d.name like concat('%', #{request.keyword},'%')
        </if>

        <include refid="filters">
            <property name="filters" value="request.filters"/>
        </include>

        <include refid="combine">
            <property name="combineSearch" value="request.combineSearch"/>
        </include>
        <include refid="viewCombine">
            <property name="combineSearch" value="request.viewCombineSearch"/>
        </include>

        <include refid="sort">
            <property name="sort" value="request.sort"/>
        </include>
    </select>

    <select id="listForExport" resultType="cn.cordys.crm.form.dto.response.CustomFormDataListResponse">
        select d.id, d.custom_form_id as customFormId, d.name, d.owner, d.create_user as createUser, d.update_user as updateUser,
               d.create_time as createTime, d.update_time as updateTime
        from custom_form_data d

        <include refid="fieldConditionJoin">
            <property name="conditions" value="request.filters"/>
            <property name="tablePrefix" value="filter"/>
        </include>
        <include refid="fieldConditionJoin">
            <property name="conditions" value="request.combineSearch.conditions"/>
            <property name="tablePrefix" value="combine"/>
        </include>
        <include refid="fieldConditionJoin">
            <property name="conditions" value="request.viewCombineSearch.conditions"/>
            <property name="tablePrefix" value="viewCombine"/>
        </include>
        <include refid="fieldSortJoin">
            <property name="sort" value="request.sort"/>
        </include>

        where d.custom_form_id = #{request.customFormId}

        <if test="manageOwn">
            and d.owner = #{userId}
        </if>

        <if test="request.keyword != null and request.keyword != ''">
            and d.name like concat('%', #{request.keyword},'%')
        </if>

        <include refid="filters">
            <property name="filters" value="request.filters"/>
        </include>

        <include refid="combine">
            <property name="combineSearch" value="request.combineSearch"/>
        </include>
        <include refid="viewCombine">
            <property name="combineSearch" value="request.viewCombineSearch"/>
        </include>

        <include refid="sort">
            <property name="sort" value="request.sort"/>
        </include>
        LIMIT #{limit} OFFSET #{offset}
    </select>

    <sql id="sort">
        <choose>
            <when test="${sort} != null and ${sort}.valid()">
                <bind name="sortType" value="${sort}.type"/>
                <bind name="sortName" value="${sort}.name"/>
                order by
                <choose>
                    <when test="${sort}.name in {'name', 'owner', 'create_time', 'update_time', 'create_user', 'update_user'}">
                        d.${sortName} ${sortType}, d.id ASC
                    </when>
                    <otherwise>
                        CASE
                        WHEN module_table.type = 'INPUT_NUMBER' OR module_table.type = 'FORMULA'
                        THEN sort_table.field_value+0
                        END ${sortType},
                        sort_table.field_value ${sortType},
                        d.id ${sortType}
                    </otherwise>
                </choose>
            </when>
            <otherwise>
                order by d.create_time desc, d.id asc
            </otherwise>
        </choose>
    </sql>

    <sql id="fieldConditionJoin">
        <if test="${conditions} != null and ${conditions}.size() > 0">
            <foreach collection="${conditions}" item="condition" index="i">
                <choose>
                    <when test="condition.refFiled">
                        <include refid="cn.cordys.common.mapper.CommonMapper.refFieldConditionJoin">
                            <property name="condition" value="condition"/>
                            <property name="mainTable" value="custom_form_data"/>
                            <property name="mainTableAlias" value="d"/>
                            <property name="fieldTable" value="${tablePrefix}_${i}"/>
                        </include>
                    </when>
                    <otherwise>
                        <if test="condition.name not in {'name', 'owner', 'createTime', 'updateTime', 'createUser', 'updateUser'}">
                            <if test="condition.blob">
                                left join custom_form_data_field_blob ${tablePrefix}_${i}
                                on d.id = ${tablePrefix}_${i}.resource_id and ${tablePrefix}_${i}.field_id = #{condition.name}
                            </if>
                            <if test="!condition.blob">
                                left join custom_form_data_field ${tablePrefix}_${i}
                                on d.id = ${tablePrefix}_${i}.resource_id and ${tablePrefix}_${i}.field_id = #{condition.name}
                            </if>
                        </if>
                    </otherwise>
                </choose>
            </foreach>
        </if>
    </sql>

    <sql id="fieldSortJoin">
        <if test="${sort} != null and ${sort}.valid()">
            <bind name="sortName" value="${sort}.name"/>
            <choose>
                <when test="${sort}.name not in {'name', 'owner', 'create_time', 'update_time', 'create_user', 'update_user'}">
                    left join custom_form_data_field sort_table
                    on d.id = sort_table.resource_id and sort_table.field_id = #{sortName}
                    left join sys_module_field module_table on sort_table.field_id = module_table.id
                </when>
            </choose>
        </if>
    </sql>

    <sql id="filters">
        <if test="${filters} != null and ${filters}.size() > 0">
            <trim prefix="and">
                <foreach collection="${filters}" item="condition" open="(" close=")" index="i" separator="and">
                    <include refid="condition">
                        <property name="condition" value="condition"/>
                        <property name="fieldTable" value="filter_${i}.field_value"/>
                        <property name="table" value="filter_${i}"/>
                    </include>
                </foreach>
            </trim>
        </if>
    </sql>

    <sql id="combine">
        <if test="${combineSearch} != null and ${combineSearch}.conditions != null and ${combineSearch}.conditions.size() > 0">
            <trim prefix="and">
                <trim prefix="(" suffix=")" suffixOverrides="and|or">
                    <foreach collection="${combineSearch}.conditions" index="i" item="condition">
                        <include refid="condition">
                            <property name="condition" value="condition"/>
                            <property name="fieldTable" value="combine_${i}.field_value"/>
                            <property name="table" value="combine_${i}"/>
                        </include>
                        <include refid="cn.cordys.common.mapper.CommonMapper.searchMode">
                            <property name="searchMode" value="${combineSearch}.searchMode"/>
                        </include>
                    </foreach>
                </trim>
            </trim>
        </if>
    </sql>

    <sql id="viewCombine">
        <if test="${combineSearch} != null and ${combineSearch}.conditions != null and ${combineSearch}.conditions.size() > 0">
            <trim prefix="and">
                <trim prefix="(" suffix=")" suffixOverrides="and|or">
                    <foreach collection="${combineSearch}.conditions" index="i" item="condition">
                        <include refid="condition">
                            <property name="condition" value="condition"/>
                            <property name="fieldTable" value="viewCombine_${i}.field_value"/>
                            <property name="table" value="viewCombine_${i}"/>
                        </include>
                        <include refid="cn.cordys.common.mapper.CommonMapper.searchMode">
                            <property name="searchMode" value="${combineSearch}.searchMode"/>
                        </include>
                    </foreach>
                </trim>
            </trim>
        </if>
    </sql>

    <sql id="condition">
        <choose>
            <when test="condition.refFiled">
                <choose>
                    <when test="!condition.customField">
                        <include refid="cn.cordys.common.mapper.CommonMapper.moduleFieldCondition">
                            <property name="condition" value="condition"/>
                            <property name="column" value="${table}.${condition.name}"/>
                        </include>
                    </when>
                    <otherwise>
                        <include refid="cn.cordys.common.mapper.CommonMapper.moduleFieldCondition">
                            <property name="condition" value="condition"/>
                            <property name="column" value="${fieldTable}"/>
                        </include>
                    </otherwise>
                </choose>
            </when>
            <otherwise>
                <choose>
                    <when test="condition.name == 'name'">
                        <include refid="cn.cordys.common.mapper.CommonMapper.condition">
                            <property name="condition" value="condition"/>
                            <property name="column" value="d.name"/>
                        </include>
                    </when>
                    <when test="condition.name == 'owner'">
                        <include refid="cn.cordys.common.mapper.CommonMapper.condition">
                            <property name="condition" value="condition"/>
                            <property name="column" value="d.owner"/>
                        </include>
                    </when>
                    <when test="condition.name == 'createTime'">
                        <include refid="cn.cordys.common.mapper.CommonMapper.condition">
                            <property name="condition" value="condition"/>
                            <property name="column" value="d.create_time"/>
                        </include>
                    </when>
                    <when test="condition.name == 'updateTime'">
                        <include refid="cn.cordys.common.mapper.CommonMapper.condition">
                            <property name="condition" value="condition"/>
                            <property name="column" value="d.update_time"/>
                        </include>
                    </when>
                    <when test="condition.name == 'createUser'">
                        <include refid="cn.cordys.common.mapper.CommonMapper.condition">
                            <property name="condition" value="condition"/>
                            <property name="column" value="d.create_user"/>
                        </include>
                    </when>
                    <when test="condition.name == 'updateUser'">
                        <include refid="cn.cordys.common.mapper.CommonMapper.condition">
                            <property name="condition" value="condition"/>
                            <property name="column" value="d.update_user"/>
                        </include>
                    </when>
                    <otherwise>
                        <include refid="cn.cordys.common.mapper.CommonMapper.moduleFieldCondition">
                            <property name="condition" value="condition"/>
                            <property name="column" value="${fieldTable}"/>
                        </include>
                    </otherwise>
                </choose>
            </otherwise>
        </choose>
    </sql>

    <update id="batchUpdate">
        UPDATE custom_form_data
        <set>
            <if test="request.fieldName == 'owner'">
                owner = #{request.fieldValue},
            </if>
            <if test="request.fieldName == 'name'">
                name = #{request.fieldValue},
            </if>
            update_time = #{request.updateTime},
            update_user = #{request.updateUser}
        </set>
        WHERE id IN
        <foreach collection="request.ids" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </update>

    <delete id="deleteFormDataByCustomFormId">
        DELETE d, f, b
        FROM custom_form_data d
        LEFT JOIN custom_form_data_field f ON f.resource_id = d.id
        LEFT JOIN custom_form_data_field_blob b ON b.resource_id = d.id
        WHERE d.custom_form_id = #{formId}
    </delete>

    <select id="getListByIds" resultType="cn.cordys.crm.form.dto.response.CustomFormDataListResponse">
        select d.id, d.custom_form_id as customFormId, d.name, d.owner, d.create_user as createUser, d.update_user as updateUser,
               d.create_time as createTime, d.update_time as updateTime
        from custom_form_data d
        where d.id in
        <foreach collection="ids" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </select>

</mapper>
