<?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.microservices.zone.resource.mapper.ZoneResourceToTypeMapper">

    <resultMap type="com.microservices.zone.resource.domain.ZoneResourceToType" id="ZoneResourceToTypeResult">
        <result property="resourceId" column="resource_id"/>
        <result property="typeId" column="type_id"/>
    </resultMap>

    <delete id="deleteResourceToTypeByResourceId" parameterType="Long">
        delete
        from zone_resource_to_type
        where resource_id = #{resourceId}
    </delete>

    <select id="countResourceToTypeByTypeId" resultType="Integer">
        select count(1)
        from zone_resource_to_type
        where type_id = #{typeId}
    </select>
    <select id="selectResourceToTypeListByTypeId" resultMap="ZoneResourceToTypeResult">
        select resource_id, type_id
        from zone_resource_to_type
        where type_id = #{typeId}
    </select>
    <select id="selectResourceToTypeListByResourceId" resultMap="ZoneResourceToTypeResult">
        select resource_id, type_id
        from zone_resource_to_type
        where resource_id = #{resourceId}
    </select>

    <insert id="batchResourceToType">
        insert into zone_resource_to_type(resource_id, type_id) values
        <foreach item="item" index="index" collection="list" separator=",">
            (#{item.resourceId},#{item.typeId})
        </foreach>
    </insert>
    <insert id="insertResourceToType">
        insert into zone_resource_to_type(resource_id, type_id)
        values (#{resourceId}, #{typeId})
    </insert>

    <delete id="deleteResourceToTypes">
        delete from zone_resource_to_type where type_id=#{typeId} and resource_id in
        <foreach collection="resourceIds" item="resourceId" open="(" separator="," close=")">
            #{resourceId}
        </foreach>
    </delete>
    <delete id="deleteResourceToTypesByResourceIdAndTypeIds">
        delete from zone_resource_to_type where resource_id=#{resourceId} and type_id in
        <foreach collection="typeIds" item="typeId" open="(" separator="," close=")">
            #{typeId}
        </foreach>
    </delete>
    <delete id="deleteResourceToTypeByResourceIdAndTypeId">
        delete
        from zone_resource_to_type
        where resource_id = #{resourceId}
          and type_id = #{typeId}
    </delete>
</mapper> 