<?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="cn.axzo.msg.center.dal.mapper.MessageModuleMapper">

    <resultMap id="MessageModuleMap" type="cn.axzo.msg.center.domain.entity.MessageModule">
        <result column="id" jdbcType="INTEGER" property="id"/>
        <result column="module_name" jdbcType="VARCHAR" property="moduleName"/>
        <result column="icon" jdbcType="VARCHAR" property="icon"/>
        <result column="remark" jdbcType="VARCHAR" property="remark"/>
        <result column="owned_type" jdbcType="VARCHAR" property="ownedType"/>
        <result column="is_delete" jdbcType="INTEGER" property="isDelete"/>
        <result column="create_at" jdbcType="TIMESTAMP" property="createAt"/>
        <result column="update_at" jdbcType="TIMESTAMP" property="updateAt"/>
    </resultMap>

    <select id="listModuleByRelationIdsOrTypeIds" resultType="cn.axzo.msg.center.domain.dto.MsgModuleDTO">
        select
        modu.owned_type as ownedType,
        relation.id as relationId,
        relation.type_id as typeId,
        modu.id as msgModuleId,
        modu.icon as msgModuleIcon,
        modu.module_name as msgModuleName
        from message_module modu
            join message_relation relation
        on modu.id = relation.module_id
        where modu.is_delete = 0
        <if test=" oldTypeIds.size()>0 and relationIds.size()>0">
            and (relation.type_id in
            <foreach collection="oldTypeIds" index="index" item="typeId" open="(" separator="," close=")">
                #{typeId}
            </foreach>
            or relation.id in
            <foreach collection="relationIds" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
            )
        </if>
        <if test=" relationIds.size == 0">
            and relation.type_id in
            <foreach collection="oldTypeIds" index="index" item="typeId" open="(" separator="," close=")">
                #{typeId}
            </foreach>
        </if>
        <if test="oldTypeIds.size() == 0 ">
            and relation.id in
            <foreach collection="relationIds" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>

    </select>

</mapper>

