<?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.cclotus.cms.mapper.CmsTermRelationshipsMapper">

    <resultMap id="CmsTerm" type="com.cclotus.cms.domain.CmsTerm">
        <result property="termId" column="term_id"/>
        <result property="parentId" column="parent_id"/>
        <result property="ancestors" column="ancestors"/>
        <result property="name" column="name"/>
        <result property="alias" column="alias"/>
        <result property="description" column="description"/>
        <result property="image" column="image"/>
        <result property="taxonomy" column="taxonomy"/>
        <result property="type" column="type"/>
        <result property="number" column="number"/>
    </resultMap>

    <resultMap id="CmsTermRelationships" type="com.cclotus.cms.domain.CmsTermRelationships">
        <result property="relaId"    column="rela_id"/>
        <result property="termId"   column="term_id"/>
        <result property="objectId" column="object_id"/>
    </resultMap>

    <resultMap type="com.cclotus.cms.domain.CmsPost" id="CmsPost">
        <result property="postId" column="post_id"/>
        <result property="title" column="title"/>
        <result property="image" column="image"/>
        <result property="summary" column="summary"/>
        <result property="contentType" column="content_type"/>
        <result property="content" column="content"/>
        <result property="status" column="status"/>
        <result property="topStatus" column="top_status"/>
        <result property="visible" column="visible"/>
        <result property="alias" column="alias"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <select id="selectCatalogRelationsByPostId" parameterType="Int" resultMap="CmsTermRelationships">
        select r.* from cms_term_relationships r
        left join cms_term t on r.term_id = t.term_id and t.taxonomy = 'catalog'
        where r.object_id= #{postId}
    </select>

    <select id="selectCatalogTermsByPostId" parameterType="Int" resultMap="CmsTerm">
        select t.* from cms_term t
        left join cms_term_relationships r on r.term_id = t.term_id and t.taxonomy = 'catalog'
        where r.object_id = #{postId}
    </select>

    <select id="getBindingTermsByTermId" resultMap="CmsTerm">
        select * from cms_term where term_id in
        (
            select r.object_id from cms_term_relationships r
            where r.term_id = #{termId}
        )
    </select>

    <select id="getBindingPostsByTermId" resultMap="CmsPost">
        select * from cms_post where post_id in
        (
            select r.object_id from cms_term_relationships r where r.term_id = #{termId}
        )
    </select>

    <delete id="deleteObjectIds">
        delete from cms_term_relationships
        where object_id in
        <foreach collection="objectIds" item="objectId" open="(" separator="," close=")">
            #{objectId}
        </foreach>
    </delete>

    <delete id="deleteObjectIdsByTermId">
        delete from cms_term_relationships
        where term_id = #{termId} and object_id in
        <foreach collection="objectIds" item="objectId" open="(" separator="," close=")">
            #{objectId}
        </foreach>
    </delete>

    <delete id="deleteTermIdsByObjectId">
        delete from cms_term_relationships
        where object_id = #{ObjectId} and term_id in
        <foreach collection="termIds" item="termId" open="(" separator="," close=")">
            #{termId}
        </foreach>
    </delete>

</mapper>
