<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"mybatis-3-mapper.dtd">
<mapper namespace="com.mortals.xhx.module.doc.form.dao.ibatis.DocFormDaoImpl">
    <!-- 字段和属性映射 -->
    <resultMap type="DocFormEntity" id="DocFormEntity-Map">
            <id  property="id" column="id" />
            <result property="templateId" column="templateId" />
            <result property="formName" column="formName" />
            <result property="formContent" column="formContent" />
            <result property="remark" column="remark" />
            <result property="updateTime" column="updateTime" />
            <result property="updateUser" column="updateUser" />
            <result property="createTime" column="createTime" />
            <result property="createUser" column="createUser" />
            
    </resultMap>


    <!-- 表所有列 -->
    <sql id="_columns">
        <trim suffixOverrides="," suffix="">
                <if test="(data == null) or (data != null and ( colPickMode == 0 and !data.containsKey('id') or colPickMode == 1 and data.containsKey('id')))">
                    a.id as id,
                </if>
                <if test="(data == null) or (data != null and ( colPickMode == 0 and !data.containsKey('templateId') or colPickMode == 1 and data.containsKey('templateId')))">
                    a.templateId as templateId,
                </if>
                <if test="(data == null) or (data != null and ( colPickMode == 0 and !data.containsKey('formName') or colPickMode == 1 and data.containsKey('formName')))">
                    a.formName as formName,
                </if>
                <if test="(data == null) or (data != null and ( colPickMode == 0 and !data.containsKey('formContent') or colPickMode == 1 and data.containsKey('formContent')))">
                    a.formContent as formContent,
                </if>
                <if test="(data == null) or (data != null and ( colPickMode == 0 and !data.containsKey('remark') or colPickMode == 1 and data.containsKey('remark')))">
                    a.remark as remark,
                </if>
                <if test="(data == null) or (data != null and ( colPickMode == 0 and !data.containsKey('updateTime') or colPickMode == 1 and data.containsKey('updateTime')))">
                    a.updateTime as updateTime,
                </if>
                <if test="(data == null) or (data != null and ( colPickMode == 0 and !data.containsKey('updateUser') or colPickMode == 1 and data.containsKey('updateUser')))">
                    a.updateUser as updateUser,
                </if>
                <if test="(data == null) or (data != null and ( colPickMode == 0 and !data.containsKey('createTime') or colPickMode == 1 and data.containsKey('createTime')))">
                    a.createTime as createTime,
                </if>
                <if test="(data == null) or (data != null and ( colPickMode == 0 and !data.containsKey('createUser') or colPickMode == 1 and data.containsKey('createUser')))">
                    a.createUser as createUser,
                </if>
        </trim>
    </sql>
    <!-- 新增 区分主键自增加还是业务插入 -->
    <insert id="insert" parameterType="DocFormEntity"  useGeneratedKeys="true" keyProperty="id">
        insert into mortals_xhx_doc_form
        (templateId,formName,formContent,remark,updateTime,updateUser,createTime,createUser)
        VALUES
        (#{templateId},#{formName},#{formContent},#{remark},#{updateTime},#{updateUser},#{createTime},#{createUser})
    </insert>

    <!-- 批量新增 -->
    <insert id="insertBatch" parameterType="paramDto">
        insert into mortals_xhx_doc_form
        (templateId,formName,formContent,remark,updateTime,updateUser,createTime,createUser)
        VALUES
        <foreach collection="data.dataList" item="item" index="index" separator="," >
            (#{item.templateId},#{item.formName},#{item.formContent},#{item.remark},#{item.updateTime},#{item.updateUser},#{item.createTime},#{item.createUser})
        </foreach>
    </insert>


    <!-- 根据ParamDto更新 -->
    <update id="update" parameterType="paramDto">
        update mortals_xhx_doc_form as a
        set
        <trim suffixOverrides="," suffix="">
            <if test="(colPickMode==0 and data.containsKey('templateId')) or (colPickMode==1 and !data.containsKey('templateId'))">
                a.templateId=#{data.templateId},
            </if>
            <if test="(colPickMode==0 and data.containsKey('templateIdIncrement')) or (colPickMode==1 and !data.containsKey('templateIdIncrement'))">
                a.templateId=ifnull(a.templateId,0) + #{data.templateIdIncrement},
            </if>
            <if test="(colPickMode==0 and data.containsKey('formName')) or (colPickMode==1 and !data.containsKey('formName'))">
                a.formName=#{data.formName},
            </if>
            <if test="(colPickMode==0 and data.containsKey('formContent')) or (colPickMode==1 and !data.containsKey('formContent'))">
                a.formContent=#{data.formContent},
            </if>
            <if test="(colPickMode==0 and data.containsKey('remark')) or (colPickMode==1 and !data.containsKey('remark'))">
                a.remark=#{data.remark},
            </if>
            <if test="(colPickMode==0 and data.containsKey('updateTime')) or (colPickMode==1 and !data.containsKey('updateTime'))">
                a.updateTime=#{data.updateTime},
            </if>
            <if test="(colPickMode==0 and data.containsKey('updateUser')) or (colPickMode==1 and !data.containsKey('updateUser'))">
                a.updateUser=#{data.updateUser},
            </if>
            <if test="(colPickMode==0 and data.containsKey('createTime')) or (colPickMode==1 and !data.containsKey('createTime'))">
                a.createTime=#{data.createTime},
            </if>
            <if test="(colPickMode==0 and data.containsKey('createUser')) or (colPickMode==1 and !data.containsKey('createUser'))">
                a.createUser=#{data.createUser},
            </if>
        </trim>
        <trim suffixOverrides="where" suffix="">
            where
            <trim prefixOverrides="and" prefix="">
                <include refid="_condition_"/>
            </trim>
        </trim>
    </update>
    <!-- 批量更新 -->
    <update id="updateBatch" parameterType="paramDto">
        update mortals_xhx_doc_form as a
        <trim prefix="set" suffixOverrides=",">
                        <trim prefix="templateId=(case" suffix="ELSE templateId end),">
                            <foreach collection="data.dataList" item="item" index="index" separator="" >
                                <choose>
                                    <when test="(colPickMode==0 and item.containsKey('templateId')) or (colPickMode==1 and !item.containsKey('templateId'))">
                                        when a.id=#{item.id} then #{item.templateId}
                                    </when>
                                    <when test="(colPickMode==0 and item.containsKey('templateIdIncrement')) or (colPickMode==1 and !item.containsKey('templateIdIncrement'))">
                                        when a.id=#{item.id} then ifnull(a.templateId,0) + #{item.templateIdIncrement}
                                    </when>
                                </choose>
                            </foreach>
                        </trim>
                <trim prefix="formName=(case" suffix="ELSE formName end),">
                    <foreach collection="data.dataList" item="item" index="index" separator="" >
                        <if test="(colPickMode==0 and item.containsKey('formName')) or (colPickMode==1 and !item.containsKey('formName'))">
                            when a.id=#{item.id} then #{item.formName}
                        </if>
                    </foreach>
                </trim>
                <trim prefix="formContent=(case" suffix="ELSE formContent end),">
                    <foreach collection="data.dataList" item="item" index="index" separator="" >
                        <if test="(colPickMode==0 and item.containsKey('formContent')) or (colPickMode==1 and !item.containsKey('formContent'))">
                            when a.id=#{item.id} then #{item.formContent}
                        </if>
                    </foreach>
                </trim>
                <trim prefix="remark=(case" suffix="ELSE remark end),">
                    <foreach collection="data.dataList" item="item" index="index" separator="" >
                        <if test="(colPickMode==0 and item.containsKey('remark')) or (colPickMode==1 and !item.containsKey('remark'))">
                            when a.id=#{item.id} then #{item.remark}
                        </if>
                    </foreach>
                </trim>
                <trim prefix="updateTime=(case" suffix="ELSE updateTime end),">
                    <foreach collection="data.dataList" item="item" index="index" separator="" >
                        <if test="(colPickMode==0 and item.containsKey('updateTime')) or (colPickMode==1 and !item.containsKey('updateTime'))">
                            when a.id=#{item.id} then #{item.updateTime}
                        </if>
                    </foreach>
                </trim>
                <trim prefix="updateUser=(case" suffix="ELSE updateUser end),">
                    <foreach collection="data.dataList" item="item" index="index" separator="" >
                        <if test="(colPickMode==0 and item.containsKey('updateUser')) or (colPickMode==1 and !item.containsKey('updateUser'))">
                            when a.id=#{item.id} then #{item.updateUser}
                        </if>
                    </foreach>
                </trim>
                <trim prefix="createTime=(case" suffix="ELSE createTime end),">
                    <foreach collection="data.dataList" item="item" index="index" separator="" >
                        <if test="(colPickMode==0 and item.containsKey('createTime')) or (colPickMode==1 and !item.containsKey('createTime'))">
                            when a.id=#{item.id} then #{item.createTime}
                        </if>
                    </foreach>
                </trim>
                <trim prefix="createUser=(case" suffix="ELSE createUser end),">
                    <foreach collection="data.dataList" item="item" index="index" separator="" >
                        <if test="(colPickMode==0 and item.containsKey('createUser')) or (colPickMode==1 and !item.containsKey('createUser'))">
                            when a.id=#{item.id} then #{item.createUser}
                        </if>
                    </foreach>
                </trim>
        </trim>
        where id in
        <foreach collection="data.dataList" item="item" index="index" open="(" separator="," close=")">
            #{item.id}
        </foreach>
    </update>
    <!-- 根据主健查询 -->
    <select id="getByKey" parameterType="paramDto" resultMap="DocFormEntity-Map">
        select <include refid="_columns"/>
        from mortals_xhx_doc_form as a
        where a.id=#{condition.id}
    </select>
    <!-- 根据主健删除 -->
    <delete id="deleteByKey" parameterType="paramDto">
        delete a.* from mortals_xhx_doc_form as a where a.id=#{condition.id}
    </delete>
    <!-- 根据主健删除一批，针对单一主健有效 -->
    <delete id="deleteByKeys">
        delete from mortals_xhx_doc_form where id in
        <foreach collection="array" item="item" index="index" open="(" separator="," close=")">
            #{item}
        </foreach>
    </delete>
    <!-- 根据paramDto删除一批 -->
    <delete id="deleteByMap" parameterType="paramDto">
        delete a.* from mortals_xhx_doc_form as a
        <trim suffixOverrides="where" suffix="">
            where
            <trim prefixOverrides="and" prefix="">
                <include refid="_condition_"/>
            </trim>
        </trim>
    </delete>
    <!-- 获取列表 -->
    <select id="getList" parameterType="paramDto" resultMap="DocFormEntity-Map">
        select <include refid="_columns"/>
        from mortals_xhx_doc_form as a
        <trim suffixOverrides="where" suffix="">
            where
            <trim prefixOverrides="and" prefix="">
                <include refid="_condition_"/>
            </trim>
        </trim>
        <include refid="_orderCols_"/>
    </select>

    <!-- 获取 -->
    <select id="getListCount" parameterType="paramDto" resultType="int">
        select count(1)
        from mortals_xhx_doc_form as a
        <trim suffixOverrides="where" suffix="">
            where
            <trim prefixOverrides="and" prefix="">
                <include refid="_condition_"/>
            </trim>
        </trim>
    </select>
    <!-- 条件映射 -->
    <sql id="_condition_">
        <if test="condition != null and !condition.isEmpty()">
            <!-- 条件映射-普通条件 -->
            <include refid="_condition_param_">
                <property name="_conditionParam_" value="condition"/>
                <property name="_conditionType_" value="and"/>
            </include>
            <!-- 条件映射-集合之间使用AND，集合中元素使用OR-(list[0].1 or list[0].2) and (list[1].3 or list[1].4) -->
            <if test="condition.containsKey('andConditionList') and !condition.andConditionList.isEmpty()">
                and
                <foreach collection="condition.andConditionList" open="(" close=")" index="index" item="andCondition" separator=" and ">
                    <trim prefixOverrides="or" prefix="(" suffix=")">
                        <include refid="_condition_param_">
                            <property name="_conditionParam_" value="andCondition"/>
                            <property name="_conditionType_" value="or"/>
                        </include>
                    </trim>
                </foreach>
            </if>
            <!-- 条件映射-集合之间使用OR，集合中元素使用AND-(list[0].1 and list[0].2) or (list[1].3 and list[1].4) -->
            <if test="condition.containsKey('orConditionList') and !condition.orConditionList.isEmpty()">
                and
                <foreach collection="condition.orConditionList" open="(" close=")" index="index" item="orCondition" separator=" or ">
                    <trim prefixOverrides="and" prefix="(" suffix=")">
                        <include refid="_condition_param_">
                            <property name="_conditionParam_" value="orCondition"/>
                            <property name="_conditionType_" value="and"/>
                        </include>
                    </trim>
                </foreach>
            </if>
        </if>
    </sql>
    <!-- 条件映射-代参数 -->
    <sql id="_condition_param_">
        <bind name="conditionParamRef" value="${_conditionParam_}"/>

        <if test="conditionParamRef.containsKey('id')">
            <if test="conditionParamRef.id != null">
                ${_conditionType_} a.id=#{${_conditionParam_}.id}
            </if>
        </if>
            <if test="conditionParamRef.containsKey('id')">
                <if test="conditionParamRef.id != null ">
                    ${_conditionType_} a.id = #{${_conditionParam_}.id}
                </if>
                <if test="conditionParamRef.id == null">
                    ${_conditionType_} a.id is null
                </if>
            </if>
            <if test="conditionParamRef.containsKey('idList')">
                ${_conditionType_} a.id in
                <foreach collection="conditionParamRef.idList" open="(" close=")" index="index" item="item" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="conditionParamRef.containsKey('idStart') and conditionParamRef.idStart != null">
                ${_conditionType_} a.id <![CDATA[ >= ]]> #{${_conditionParam_}.idStart}
            </if>
            <if test="conditionParamRef.containsKey('idEnd') and conditionParamRef.idEnd != null">
                ${_conditionType_} a.id <![CDATA[ <= ]]> #{${_conditionParam_}.idEnd}
            </if>

            <if test="conditionParamRef.containsKey('templateId')">
                <if test="conditionParamRef.templateId != null ">
                    ${_conditionType_} a.templateId = #{${_conditionParam_}.templateId}
                </if>
                <if test="conditionParamRef.templateId == null">
                    ${_conditionType_} a.templateId is null
                </if>
            </if>
            <if test="conditionParamRef.containsKey('templateIdList')">
                ${_conditionType_} a.templateId in
                <foreach collection="conditionParamRef.templateIdList" open="(" close=")" index="index" item="item" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="conditionParamRef.containsKey('templateIdStart') and conditionParamRef.templateIdStart != null">
                ${_conditionType_} a.templateId <![CDATA[ >= ]]> #{${_conditionParam_}.templateIdStart}
            </if>
            <if test="conditionParamRef.containsKey('templateIdEnd') and conditionParamRef.templateIdEnd != null">
                ${_conditionType_} a.templateId <![CDATA[ <= ]]> #{${_conditionParam_}.templateIdEnd}
            </if>


            <if test="conditionParamRef.containsKey('formName')">
                <if test="conditionParamRef.formName != null and conditionParamRef.formName != ''">
                    ${_conditionType_} a.formName like #{${_conditionParam_}.formName}
                </if>
                <if test="conditionParamRef.formName == null">
                    ${_conditionType_} a.formName is null
                </if>
            </if>
            <if test="conditionParamRef.containsKey('formNameList')">
                ${_conditionType_} a.formName in
                <foreach collection="conditionParamRef.formNameList" open="(" close=")" index="index" item="item" separator=",">
                    #{item}
                </foreach>
            </if>

            <if test="conditionParamRef.containsKey('formContent')">
                <if test="conditionParamRef.formContent != null and conditionParamRef.formContent != ''">
                    ${_conditionType_} a.formContent like #{${_conditionParam_}.formContent}
                </if>
                <if test="conditionParamRef.formContent == null">
                    ${_conditionType_} a.formContent is null
                </if>
            </if>
            <if test="conditionParamRef.containsKey('formContentList')">
                ${_conditionType_} a.formContent in
                <foreach collection="conditionParamRef.formContentList" open="(" close=")" index="index" item="item" separator=",">
                    #{item}
                </foreach>
            </if>

            <if test="conditionParamRef.containsKey('remark')">
                <if test="conditionParamRef.remark != null and conditionParamRef.remark != ''">
                    ${_conditionType_} a.remark like #{${_conditionParam_}.remark}
                </if>
                <if test="conditionParamRef.remark == null">
                    ${_conditionType_} a.remark is null
                </if>
            </if>
            <if test="conditionParamRef.containsKey('remarkList')">
                ${_conditionType_} a.remark in
                <foreach collection="conditionParamRef.remarkList" open="(" close=")" index="index" item="item" separator=",">
                    #{item}
                </foreach>
            </if>

            <if test="conditionParamRef.containsKey('updateTime')">
                <if test="conditionParamRef.updateTime != null ">
                    ${_conditionType_} a.updateTime = #{${_conditionParam_}.updateTime}
                </if>
                <if test="conditionParamRef.updateTime == null">
                    ${_conditionType_} a.updateTime is null
                </if>
            </if>
            <if test="conditionParamRef.containsKey('updateTimeStart') and conditionParamRef.updateTimeStart != null and conditionParamRef.updateTimeStart!=''">
                ${_conditionType_} a.updateTime <![CDATA[ >= ]]> STR_TO_DATE(left(concat(#{${_conditionParam_}.updateTimeStart},' 00:00:00'),19),'%Y-%m-%d %k:%i:%s')
            </if>
            <if test="conditionParamRef.containsKey('updateTimeEnd') and conditionParamRef.updateTimeEnd != null and conditionParamRef.updateTimeEnd!=''">
                ${_conditionType_} a.updateTime <![CDATA[ <= ]]> STR_TO_DATE(left(concat(#{${_conditionParam_}.updateTimeEnd},' 23:59:59'),19),'%Y-%m-%d %k:%i:%s')
            </if>

            <if test="conditionParamRef.containsKey('updateUser')">
                <if test="conditionParamRef.updateUser != null and conditionParamRef.updateUser != ''">
                    ${_conditionType_} a.updateUser like #{${_conditionParam_}.updateUser}
                </if>
                <if test="conditionParamRef.updateUser == null">
                    ${_conditionType_} a.updateUser is null
                </if>
            </if>
            <if test="conditionParamRef.containsKey('updateUserList')">
                ${_conditionType_} a.updateUser in
                <foreach collection="conditionParamRef.updateUserList" open="(" close=")" index="index" item="item" separator=",">
                    #{item}
                </foreach>
            </if>

            <if test="conditionParamRef.containsKey('createTime')">
                <if test="conditionParamRef.createTime != null ">
                    ${_conditionType_} a.createTime = #{${_conditionParam_}.createTime}
                </if>
                <if test="conditionParamRef.createTime == null">
                    ${_conditionType_} a.createTime is null
                </if>
            </if>
            <if test="conditionParamRef.containsKey('createTimeStart') and conditionParamRef.createTimeStart != null and conditionParamRef.createTimeStart!=''">
                ${_conditionType_} a.createTime <![CDATA[ >= ]]> STR_TO_DATE(left(concat(#{${_conditionParam_}.createTimeStart},' 00:00:00'),19),'%Y-%m-%d %k:%i:%s')
            </if>
            <if test="conditionParamRef.containsKey('createTimeEnd') and conditionParamRef.createTimeEnd != null and conditionParamRef.createTimeEnd!=''">
                ${_conditionType_} a.createTime <![CDATA[ <= ]]> STR_TO_DATE(left(concat(#{${_conditionParam_}.createTimeEnd},' 23:59:59'),19),'%Y-%m-%d %k:%i:%s')
            </if>

            <if test="conditionParamRef.containsKey('createUser')">
                <if test="conditionParamRef.createUser != null and conditionParamRef.createUser != ''">
                    ${_conditionType_} a.createUser like #{${_conditionParam_}.createUser}
                </if>
                <if test="conditionParamRef.createUser == null">
                    ${_conditionType_} a.createUser is null
                </if>
            </if>
            <if test="conditionParamRef.containsKey('createUserList')">
                ${_conditionType_} a.createUser in
                <foreach collection="conditionParamRef.createUserList" open="(" close=")" index="index" item="item" separator=",">
                    #{item}
                </foreach>
            </if>
    </sql>
    <sql id="_orderCols_">
        <if test="orderColList != null and !orderColList.isEmpty()">
            order by
            <trim suffixOverrides=","  suffix="">
                <foreach collection="orderColList" open="" close="" index="index" item="item" separator=",">
                     ${item.colName} ${item.sortKind}
                </foreach>
            </trim>
        </if>
        <if test="(orderColList == null or orderColList.isEmpty()) and orderCol != null and !orderCol.isEmpty()">
            order by
            <trim suffixOverrides=","  suffix="">
                    <if test="orderCol.containsKey('id')">
                        a.id
                        <if test='orderCol.id != null and "DESC".equalsIgnoreCase(orderCol.id)'>DESC</if>
                        ,
                    </if>
                    <if test="orderCol.containsKey('templateId')">
                        a.templateId
                        <if test='orderCol.templateId != null and "DESC".equalsIgnoreCase(orderCol.templateId)'>DESC</if>
                        ,
                    </if>
                    <if test="orderCol.containsKey('formName')">
                        a.formName
                        <if test='orderCol.formName != null and "DESC".equalsIgnoreCase(orderCol.formName)'>DESC</if>
                        ,
                    </if>
                    <if test="orderCol.containsKey('formContent')">
                        a.formContent
                        <if test='orderCol.formContent != null and "DESC".equalsIgnoreCase(orderCol.formContent)'>DESC</if>
                        ,
                    </if>
                    <if test="orderCol.containsKey('remark')">
                        a.remark
                        <if test='orderCol.remark != null and "DESC".equalsIgnoreCase(orderCol.remark)'>DESC</if>
                        ,
                    </if>
                    <if test="orderCol.containsKey('updateTime')">
                        a.updateTime
                        <if test='orderCol.updateTime != null and "DESC".equalsIgnoreCase(orderCol.updateTime)'>DESC</if>
                        ,
                    </if>
                    <if test="orderCol.containsKey('updateUser')">
                        a.updateUser
                        <if test='orderCol.updateUser != null and "DESC".equalsIgnoreCase(orderCol.updateUser)'>DESC</if>
                        ,
                    </if>
                    <if test="orderCol.containsKey('createTime')">
                        a.createTime
                        <if test='orderCol.createTime != null and "DESC".equalsIgnoreCase(orderCol.createTime)'>DESC</if>
                        ,
                    </if>
                    <if test="orderCol.containsKey('createUser')">
                        a.createUser
                        <if test='orderCol.createUser != null and "DESC".equalsIgnoreCase(orderCol.createUser)'>DESC</if>
                        ,
                    </if>
            </trim>
        </if>
    </sql>
    <sql id="_group_by_">
        <if test="groupList != null and !groupList.isEmpty()">
            GROUP BY
            <trim suffixOverrides=","  suffix="">
                <foreach collection="groupList" open="" close="" index="index" item="item" separator=",">
                    ${item}
                </foreach>
            </trim>
        </if>
    </sql>
</mapper>