<?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.platform.dao.ibatis.PlatformListenerDaoImpl">

    <!-- 字段和属性映射 -->
    <resultMap type="PlatformListenerEntity" id="PlatformListenerEntity-Map">
        <result property="id" column="id" />
        <result property="platformId" column="platformId" />
        <result property="platformSn" column="platformSn" />
        <result property="typeCode" column="typeCode" />
        <result property="typeName" column="typeName" />
        <result property="createTime" column="createTime" />
        <result property="createUserName" column="createUserName" />
    </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('platformId') or colPickMode == 1 and data.containsKey('platformId')))">
                    a.platformId as platformId,
                </if>
                <if test="(data == null) or (data != null and ( colPickMode == 0 and !data.containsKey('platformSn') or colPickMode == 1 and data.containsKey('platformSn')))">
                    a.platformSn as platformSn,
                </if>
                <if test="(data == null) or (data != null and ( colPickMode == 0 and !data.containsKey('typeCode') or colPickMode == 1 and data.containsKey('typeCode')))">
                    a.typeCode as typeCode,
                </if>
                <if test="(data == null) or (data != null and ( colPickMode == 0 and !data.containsKey('typeName') or colPickMode == 1 and data.containsKey('typeName')))">
                    a.typeName as typeName,
                </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('createUserName') or colPickMode == 1 and data.containsKey('createUserName')))">
                    a.createUserName as createUserName,
                </if>
        </trim>
    </sql>
    <!-- 新增 -->
        <insert id="insert" parameterType="PlatformListenerEntity" useGeneratedKeys="true" keyProperty="id">
            insert into mortals_xhx_platform_listener
            (platformId,platformSn,typeCode,typeName,createTime,createUserName)
            VALUES
            (#{platformId},#{platformSn},#{typeCode},#{typeName},#{createTime},#{createUserName})
        </insert>

        <!-- 批量新增 -->
        <insert id="insertBatch" parameterType="paramDto">
            insert into mortals_xhx_platform_listener
            (platformId,platformSn,typeCode,typeName,createTime,createUserName)
            VALUES
            <foreach collection="data.dataList" item="item" index="index" separator="," >
                (#{item.platformId},#{item.platformSn},#{item.typeCode},#{item.typeName},#{item.createTime},#{item.createUserName})
            </foreach>
        </insert>
    <!-- 根据ParamDto更新 -->
    <update id="update" parameterType="paramDto">
        update mortals_xhx_platform_listener as a
        set
        <trim suffixOverrides="," suffix="">
            <if test="(colPickMode==0 and data.containsKey('platformId')) or (colPickMode==1 and !data.containsKey('platformId'))">
                a.platformId=#{data.platformId},
            </if>
            <if test="(colPickMode==0 and data.containsKey('platformIdIncrement')) or (colPickMode==1 and !data.containsKey('platformIdIncrement'))">
                a.platformId=ifnull(a.platformId,0) + #{data.platformIdIncrement},
            </if>
            <if test="(colPickMode==0 and data.containsKey('platformSn')) or (colPickMode==1 and !data.containsKey('platformSn'))">
                a.platformSn=#{data.platformSn},
            </if>
            <if test="(colPickMode==0 and data.containsKey('typeCode')) or (colPickMode==1 and !data.containsKey('typeCode'))">
                a.typeCode=#{data.typeCode},
            </if>
            <if test="(colPickMode==0 and data.containsKey('typeName')) or (colPickMode==1 and !data.containsKey('typeName'))">
                a.typeName=#{data.typeName},
            </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('createUserName')) or (colPickMode==1 and !data.containsKey('createUserName'))">
                a.createUserName=#{data.createUserName},
            </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_platform_listener as a
        <trim prefix="set" suffixOverrides=",">


                        <trim prefix="platformId=(case" suffix="ELSE platformId end),">
                            <foreach collection="data.dataList" item="item" index="index" separator="" >
                                <choose>
                                    <when test="(colPickMode==0 and item.containsKey('platformId')) or (colPickMode==1 and !item.containsKey('platformId'))">
                                        when a.id=#{item.id} then #{item.platformId}
                                    </when>
                                    <when test="(colPickMode==0 and item.containsKey('platformIdIncrement')) or (colPickMode==1 and !item.containsKey('platformIdIncrement'))">
                                        when a.id=#{item.id} then ifnull(a.platformId,0) + #{item.platformIdIncrement}
                                    </when>
                                </choose>
                            </foreach>
                        </trim>

                <trim prefix="platformSn=(case" suffix="ELSE platformSn end),">
                    <foreach collection="data.dataList" item="item" index="index" separator="" >
                        <if test="(colPickMode==0 and item.containsKey('platformSn')) or (colPickMode==1 and !item.containsKey('platformSn'))">
                            when a.id=#{item.id} then #{item.platformSn}
                        </if>
                    </foreach>
                </trim>

                <trim prefix="typeCode=(case" suffix="ELSE typeCode end),">
                    <foreach collection="data.dataList" item="item" index="index" separator="" >
                        <if test="(colPickMode==0 and item.containsKey('typeCode')) or (colPickMode==1 and !item.containsKey('typeCode'))">
                            when a.id=#{item.id} then #{item.typeCode}
                        </if>
                    </foreach>
                </trim>

                <trim prefix="typeName=(case" suffix="ELSE typeName end),">
                    <foreach collection="data.dataList" item="item" index="index" separator="" >
                        <if test="(colPickMode==0 and item.containsKey('typeName')) or (colPickMode==1 and !item.containsKey('typeName'))">
                            when a.id=#{item.id} then #{item.typeName}
                        </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="createUserName=(case" suffix="ELSE createUserName end),">
                    <foreach collection="data.dataList" item="item" index="index" separator="" >
                        <if test="(colPickMode==0 and item.containsKey('createUserName')) or (colPickMode==1 and !item.containsKey('createUserName'))">
                            when a.id=#{item.id} then #{item.createUserName}
                        </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="PlatformListenerEntity-Map">
        select <include refid="_columns"/>
        from mortals_xhx_platform_listener as a
        where a.id=#{condition.id}
    </select>
    <!-- 根据主健删除 -->
    <delete id="deleteByKey" parameterType="paramDto">
        delete a.* from mortals_xhx_platform_listener as a where a.id=#{condition.id}
    </delete>
    <!-- 根据主健删除一批，针对单一主健有效 -->
    <delete id="deleteByKeys">
        delete from mortals_xhx_platform_listener 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_platform_listener as a
        <trim suffixOverrides="where" suffix="">
            where
            <trim prefixOverrides="and" prefix="">
                <include refid="_condition_"/>
            </trim>
        </trim>
    </delete>
    <!-- 获取列表 -->
    <select id="getList" parameterType="paramDto" resultMap="PlatformListenerEntity-Map">
        select <include refid="_columns"/>
        from mortals_xhx_platform_listener 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_platform_listener 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('platformId')">
                <if test="conditionParamRef.platformId != null ">
                    ${_conditionType_} a.platformId = #{${_conditionParam_}.platformId}
                </if>
                <if test="conditionParamRef.platformId == null">
                    ${_conditionType_} a.platformId is null
                </if>
            </if>
            <if test="conditionParamRef.containsKey('platformIdList')">
                ${_conditionType_} a.platformId in
                <foreach collection="conditionParamRef.platformIdList" open="(" close=")" index="index" item="item" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="conditionParamRef.containsKey('platformIdStart') and conditionParamRef.platformIdStart != null">
                ${_conditionType_} a.platformId <![CDATA[ >= ]]> #{${_conditionParam_}.platformIdStart}
            </if>
            <if test="conditionParamRef.containsKey('platformIdEnd') and conditionParamRef.platformIdEnd != null">
                ${_conditionType_} a.platformId <![CDATA[ <= ]]> #{${_conditionParam_}.platformIdEnd}
            </if>


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

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

            <if test="conditionParamRef.containsKey('typeName')">
                <if test="conditionParamRef.typeName != null and conditionParamRef.typeName != ''">
                    ${_conditionType_} a.typeName like #{${_conditionParam_}.typeName}
                </if>
                <if test="conditionParamRef.typeName == null">
                    ${_conditionType_} a.typeName is null
                </if>
            </if>
            <if test="conditionParamRef.containsKey('typeNameList')">
                ${_conditionType_} a.typeName in
                <foreach collection="conditionParamRef.typeNameList" 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('createUserName')">
                <if test="conditionParamRef.createUserName != null and conditionParamRef.createUserName != ''">
                    ${_conditionType_} a.createUserName like #{${_conditionParam_}.createUserName}
                </if>
                <if test="conditionParamRef.createUserName == null">
                    ${_conditionType_} a.createUserName is null
                </if>
            </if>
            <if test="conditionParamRef.containsKey('createUserNameList')">
                ${_conditionType_} a.createUserName in
                <foreach collection="conditionParamRef.createUserNameList" 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('platformId')">
                        a.platformId
                        <if test='orderCol.platformId != null and "DESC".equalsIgnoreCase(orderCol.platformId)'>DESC</if>
                        ,
                    </if>
                    <if test="orderCol.containsKey('platformSn')">
                        a.platformSn
                        <if test='orderCol.platformSn != null and "DESC".equalsIgnoreCase(orderCol.platformSn)'>DESC</if>
                        ,
                    </if>
                    <if test="orderCol.containsKey('typeCode')">
                        a.typeCode
                        <if test='orderCol.typeCode != null and "DESC".equalsIgnoreCase(orderCol.typeCode)'>DESC</if>
                        ,
                    </if>
                    <if test="orderCol.containsKey('typeName')">
                        a.typeName
                        <if test='orderCol.typeName != null and "DESC".equalsIgnoreCase(orderCol.typeName)'>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('createUserName')">
                        a.createUserName
                        <if test='orderCol.createUserName != null and "DESC".equalsIgnoreCase(orderCol.createUserName)'>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>