<?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.ym.mec.biz.dal.dao.GroupDao">

    <resultMap type="com.ym.mec.biz.dal.entity.Group" id="Group">
        <result column="organ_id_" property="organId"/>
        <result column="organ_name_" property="organName"/>
        <result column="id_" property="id"/>
        <result column="group_name_" property="groupName"/>
        <result column="group_type_" property="groupType" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
    </resultMap>

    <sql id="searchMusicGroupsCondition">
        <where>
            <if test="search!=null">
                AND (g.id_ = #{search} OR g.name_ LIKE CONCAT( '%', #{search}, '%' ))
            </if>
            <if test="organIdList!=null">
                AND FIND_IN_SET(g.organ_id_,#{organIdList})
            </if>
            <if test="musicGroupStatus!=null">
                AND g.status_=#{musicGroupStatus, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
        </where>
    </sql>

    <sql id="searchVipGroupsCondition">
        <where>
            <if test="search!=null">
                AND (g.id_ = #{search} OR g.name_ LIKE CONCAT( '%', #{search}, '%' ))
            </if>
            <if test="organIdList!=null">
                AND FIND_IN_SET(g.organ_id_,#{organIdList})
            </if>
            <if test="vipGroupStatus!=null">
                AND g.group_status_=#{vipGroupStatus, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
        </where>
    </sql>

    <sql id="searchPracticeGroupsCondition">
        <where>
            <if test="search!=null">
                AND (g.id_ = #{search} OR g.name_ LIKE CONCAT( '%', #{search}, '%' ))
            </if>
            <if test="organIdList!=null">
                AND FIND_IN_SET(g.organ_id_,#{organIdList})
            </if>
        </where>
    </sql>

    <delete id="deleteGroup">
        DELETE FROM
        <if test="groupType!=null and groupType=='MUSIC'.toString()">
            music_group
        </if>
        <if test="groupType!=null and groupType=='VIP'.toString()">
            vip_group
        </if>
        <if test="groupType!=null and groupType=='PRACTICE'.toString()">
            practice_group
        </if>
        WHERE id_=#{groupId}
    </delete>

    <select id="searchGroups" resultMap="Group">
        ( SELECT o.id_ organ_id_,o.name_ organ_name_,g.id_, g.name_ group_name_, 'MUSIC' group_type_
        FROM music_group g
        LEFT JOIN organization o ON g.organ_id_ = o.id_
        <include refid="searchMusicGroupsCondition"/> )
        UNION
        ( SELECT o.id_ organ_id_,o.name_ organ_name_,g.id_, g.name_ group_name_, 'VIP' group_type_
        FROM vip_group g
        LEFT JOIN organization o ON g.organ_id_ = o.id_
        <include refid="searchVipGroupsCondition"/> )
        UNION
        ( SELECT o.id_ organ_id_,o.name_ organ_name_,g.id_, g.name_ group_name_, 'PRACTICE' group_type_
        FROM practice_group g
        LEFT JOIN organization o ON g.organ_id_ = o.id_
        <include refid="searchPracticeGroupsCondition"/> );
    </select>
    <select id="findByGroupIds" resultMap="Group">
      ( SELECT o.id_ organ_id_,o.name_ organ_name_,g.id_, g.name_ group_name_, 'MUSIC' group_type_
        FROM music_group g
        LEFT JOIN organization o ON g.organ_id_ = o.id_
        WHERE g.id_ IN
        <foreach collection="groups" item="group" open="(" close=")" separator=",">
            <if test="group != null and group.groupType == @com.ym.mec.biz.dal.enums.GroupType@MUSIC">
                #{group.id}
            </if>
            <if test="group == null">
                #{group}
            </if>
        </foreach>
      )
      UNION
      ( SELECT o.id_ organ_id_,o.name_ organ_name_,g.id_, g.name_ group_name_, 'VIP' group_type_
      FROM vip_group g LEFT JOIN organization o ON g.organ_id_ = o.id_
      WHERE g.id_ IN
        <foreach collection="groups" item="group" open="(" close=")" separator=",">
            <if test="group != null and group.groupType == @com.ym.mec.biz.dal.enums.GroupType@VIP">
                #{group.id}
            </if>
            <if test="group == null">
                #{group}
            </if>
        </foreach>
      )
        UNION
        ( SELECT o.id_ organ_id_,o.name_ organ_name_,g.id_, g.name_ group_name_, 'PRACTICE' group_type_
        FROM practice_group g LEFT JOIN organization o ON g.organ_id_ = o.id_
        WHERE g.id_ IN
        <foreach collection="groups" item="group" open="(" close=")" separator=",">
            <if test="group != null and group.groupType == @com.ym.mec.biz.dal.enums.GroupType@PRACTICE">
                #{group.id}
            </if>
            <if test="group == null">
                #{group}
            </if>
        </foreach>
        )
    </select>

</mapper>
