<?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.kiisoo.ic.domain.mapper.PrivilageDomainDOMapper">

    <select id="selectDomain" resultType="com.kiisoo.ic.domain.entity.PrivilageDomainDO">
      SELECT t1.id as id, t1.name as `name`, t1.status as `status`, t1.parent_id as parentId, t1.create_time as createTime, t1.update_time as updateTime
        FROM `privilage_domain` t1, privilage_domain_entity t2
        where t1.id = t2.domain_id
        and t2.type = #{type}
        and t1.status = #{status}
        and t2.entity_id = #{entityId}
        limit 1
    </select>

    <select id="selectEntityByDomainId" resultType="java.lang.Long">
        select entity_id from privilage_domain_entity
        where domain_id = #{domainId}
        and `type` = #{type}
    </select>

    <insert id="addDomainEntity" parameterType="java.util.List">
        INSERT INTO privilage_domain_entity (domain_id, entity_id, `type`, create_time) VALUES
        <foreach collection="list" item="item" index="index"
                 separator=",">
            (#{item.domainId},#{item.entityId},#{item.type}, #{item.createTime})
        </foreach>
    </insert>

    <delete id="removeDomainEntity" >
        delete from privilage_domain_entity where entity_id in
        <foreach item="item" collection="entityIds" open="(" separator="," close=")">
            #{item}
        </foreach>
        and `type` = #{type}
        and domain_id = #{domainId}
    </delete>

</mapper>
