<?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.pcloud.book.group.mapper.MpServiceMappingMapper">

    <resultMap type="com.pcloud.book.group.entity.MpServiceMapping" id="MpServiceMappingMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="name" column="name" jdbcType="VARCHAR"/>
        <result property="serverType" column="server_type" jdbcType="VARCHAR"/>
        <result property="typeCode" column="type_code" jdbcType="VARCHAR"/>
        <result property="mapping" column="mapping" jdbcType="INTEGER"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
    </resultMap>

    <!--查询单个-->
    <select id="queryById" resultMap="MpServiceMappingMap">
        select id,
               name,
               server_type,
               type_code,
               mapping,
               create_time,
               update_time
        from book.mp_service_mapping
        where id = #{id}
    </select>

    <!--查询指定行数据-->
    <select id="queryAllByLimit" resultMap="MpServiceMappingMap">
        select id,
               name,
               server_type,
               type_code,
               mapping,
               create_time,
               update_time
        from book.mp_service_mapping limit #{offset}, #{limit}
    </select>

    <!--通过实体作为筛选条件查询-->
    <select id="queryAll" resultMap="MpServiceMappingMap">
        select
        id, name, server_type, type_code, mapping, create_time, update_time
        from book.mp_service_mapping
        <where>
            <if test="id != null">
                and id = #{id}
            </if>
            <if test="name != null and name != ''">
                and name = #{name}
            </if>
            <if test="serverType != null and serverType != ''">
                and server_type = #{serverType}
            </if>
            <if test="typeCode != null and typeCode != ''">
                and type_code = #{typeCode}
            </if>
            <if test="mapping != null">
                and mapping = #{mapping}
            </if>
            <if test="createTime != null">
                and create_time = #{createTime}
            </if>
            <if test="updateTime != null">
                and update_time = #{updateTime}
            </if>
        </where>
    </select>

    <!--新增所有列-->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into book.mp_service_mapping(name, server_type, type_code, mapping, create_time, update_time)
        values (#{name}, #{serverType}, #{typeCode}, #{mapping}, #{createTime}, #{updateTime})
    </insert>

    <insert id="insertBatch" keyProperty="id" useGeneratedKeys="true">
        insert into book.mp_service_mapping(name, server_type, type_code, mapping, create_time, update_time)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.name}, #{entity.serverType}, #{entity.typeCode}, #{entity.mapping}, #{entity.createTime},
            #{entity.updateTime})
        </foreach>
    </insert>

    <insert id="insertOrUpdateBatch" keyProperty="id" useGeneratedKeys="true">
        insert into book.mp_service_mapping(name, server_type, type_code, mapping, create_time, update_time)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.name}, #{entity.serverType}, #{entity.typeCode}, #{entity.mapping}, #{entity.createTime},
            #{entity.updateTime})
        </foreach>
        on duplicate key update
        name = values(name) , server_type = values(server_type) , type_code = values(type_code) , mapping =
        values(mapping) , create_time = values(create_time) , update_time = values(update_time)
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        update book.mp_service_mapping
        <set>
            <if test="name != null and name != ''">
                name = #{name},
            </if>
            <if test="serverType != null and serverType != ''">
                server_type = #{serverType},
            </if>
            <if test="typeCode != null and typeCode != ''">
                type_code = #{typeCode},
            </if>
            <if test="mapping != null">
                mapping = #{mapping},
            </if>
            <if test="createTime != null">
                create_time = #{createTime},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
        </set>
        where id = #{id}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        delete
        from book.mp_service_mapping
        where id = #{id}
    </delete>

    <select id="selectMapping" resultType="java.lang.Integer">
        SELECT DISTINCT mapping FROM mp_service_mapping
        <where>
            <if test="app !=null and app.size()>0">
            ( server_type = "APP" AND type_code IN
                <foreach collection="app" separator="," open="(" close=")" item="item">
                #{item}
            </foreach>
                 )
        </if>
        <if test="product !=null and product.size()>0">OR (
            server_type = "PRODUCT" AND type_code IN
            <foreach collection="product" separator="," open="(" close=")" item="item">
                #{item}
            </foreach>
            )
        </if>
        </where>
    </select>


</mapper>