<?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.ChildrenDayReserveDao">
    <resultMap id="ChildrenDayReserve" type="com.ym.mec.biz.dal.entity.ChildrenDayReserve">
        <!--@mbg.generated-->
        <id column="id_" property="id"/>
        <result column="user_id_" property="userId"/>
        <result column="organ_id_" property="organId"/>
        <result column="cooperation_organ_id_" property="cooperationOrganId"/>
        <result column="music_group_id_" property="musicGroupId"/>
        <result column="is_reserve_" property="isReserve" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="is_send_msg_" property="isSendMsg" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
		<result column="tenant_id_" property="tenantId" />
    </resultMap>

    <select id="get" parameterType="java.lang.Integer" resultMap="ChildrenDayReserve">
        <!--@mbg.generated-->
        select *
        from children_day_reserve
        where id_ = #{id}
    </select>
    <delete id="delete" parameterType="java.lang.Integer">
        <!--@mbg.generated-->
        delete from children_day_reserve
        where id_ = #{id}
    </delete>
    <insert id="insert" keyColumn="id_" keyProperty="id" parameterType="com.ym.mec.biz.dal.entity.ChildrenDayReserve"
            useGeneratedKeys="true">
        <!--@mbg.generated-->
        insert into children_day_reserve (user_id_, organ_id_, cooperation_organ_id_, music_group_id_, is_reserve_,
        create_time_, update_time_,tenant_id_)
        values (#{userId}, #{organId}, #{cooperationOrganId}, #{musicGroupId},
        #{isReserve,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},NOW(), NOW(),#{tenantId})
    </insert>
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.ChildrenDayReserve">
        <!--@mbg.generated-->
        update children_day_reserve
        <set>
            <if test="userId != null">
                user_id_ = #{userId},
            </if>
            <if test="organId != null">
                organ_id_ = #{organId},
            </if>
            <if test="cooperationOrganId != null">
                cooperation_organ_id_ = #{cooperationOrganId},
            </if>
            <if test="musicGroupId != null">
                music_group_id_ = #{musicGroupId},
            </if>
            <if test="isReserve != null">
                is_reserve_ = #{isReserve,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="isSendMsg != null">
                is_send_msg_ = #{isSendMsg,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="createTime != null">
                create_time_ = #{createTime},
            </if>
            <if test="updateTime != null">
                update_time_ = #{updateTime},
            </if>
        </set>
        where id_ = #{id} and tenant_id_ = #{tenantId}
    </update>

    <!-- 分页查询 -->
    <select id="queryPage" resultMap="ChildrenDayReserve" parameterType="map">
        SELECT * FROM children_day_reserve where tenant_id_ = #{tenantId}
        <include refid="global.limit"/>
    </select>

    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int">
        SELECT COUNT(*)
        FROM children_day_reserve where tenant_id_ = #{tenantId}
    </select>
    <resultMap id="ChildrenStatisticsDtoMap" type="com.ym.mec.biz.dal.dto.ChildrenStatisticsDto">
        <result property="buyMusicTheoryAmount" column="buy_music_theory_amount_"/>
        <result property="buyMusicTheoryNum" column="buy_music_theory_num_"/>
        <result property="buyVip1Amount" column="buy_vip1_amount_"/>
        <result property="buyVip1Num" column="buy_vip1_num_"/>
        <result property="buyVip2Amount" column="buy_vip2_amount_"/>
        <result property="buyVip2Num" column="buy_vip2_num_"/>
        <result property="gradePrice" column="grade_price_"/>
        <result property="theoryPrice" column="theory_price_"/>
        <result property="totalAmount" column="total_amount_"/>
        <result property="cooperationOrganName" column="cooperation_organ_name_"/>
        <result property="instrumentalMusicNum" column="instrumental_music_num_"/>
        <result property="musicGroupName" column="music_group_name_"/>
        <result property="musicTheoryNum" column="music_theory_num_"/>
        <result property="noReserveNum" column="no_reserve_num_"/>
        <result property="normalNum" column="normal_num_"/>
        <result property="organName" column="organ_name_"/>
        <result property="reserveNum" column="reserve_num_"/>
        <result property="organId" column="organ_id_"/>
        <result property="cooperationOrganId" column="cooperation_organ_id_"/>
        <result property="musicGroupId" column="music_group_id_"/>
    </resultMap>
    <select id="queryChildrenStatistics" resultMap="ChildrenStatisticsDtoMap">
        SELECT o.name_ organ_name_,co.name_ cooperation_organ_name_,mg.name_ music_group_name_,cdr.organ_id_,
        cdr.cooperation_organ_id_,cdr.music_group_id_,
        COUNT(DISTINCT cdrr.user_id_) 'reserve_num_',
        COUNT(DISTINCT cdrn.user_id_) 'no_reserve_num_',
        COUNT(CASE WHEN cdd.grade_price_ > 0 THEN 1 ELSE NULL END) 'instrumental_music_num_',
        COUNT(CASE WHEN cdd.theory_price_ > 0 THEN 1 ELSE NULL END) 'music_theory_num_',
        COUNT(CASE WHEN cdd.vip_1v1_price_ > 0 THEN 1 ELSE NULL END) 'buy_vip1_num_',
        SUM(cdd.vip_1v1_price_) 'buy_vip1_amount_',
        COUNT(CASE WHEN cdd.vip_1v2_price_ > 0 THEN 1 ELSE NULL END) 'buy_vip2_num_',
        SUM(cdd.vip_1v2_price_) 'buy_vip2_amount_',
        COUNT(CASE WHEN cdd.theory_course_price_ > 0 THEN 1 ELSE NULL END) 'buy_music_theory_num_',
        SUM(cdd.theory_course_price_) 'buy_music_theory_amount_',
        SUM(cdd.grade_price_) 'grade_price_',
        SUM(cdd.theory_price_) 'theory_price_',
        SUM(cdd.total_amount_) 'total_amount_'
        FROM children_day_reserve cdr
        LEFT JOIN children_day_degree_info cdd ON cdr.id_ = cdd.reserve_id_
        LEFT JOIN music_group mg ON mg.id_ = cdr.music_group_id_
        LEFT JOIN cooperation_organ co ON co.id_ = cdr.cooperation_organ_id_
        LEFT JOIN organization o ON o.id_ = cdr.organ_id_
        LEFT JOIN (SELECT * FROM children_day_reserve cdr WHERE cdr.is_reserve_ = 1
            <include refid="queryChildrenStatisticsSql1"/>) cdrr ON cdrr.id_ = cdr.id_
        LEFT JOIN (SELECT * FROM children_day_reserve cdr WHERE cdr.is_reserve_ = 0
        <include refid="queryChildrenStatisticsSql1"/>) cdrn ON cdrn.id_ = cdr.id_
        <include refid="queryChildrenStatisticsSql"/>
        GROUP BY cdr.music_group_id_,cdr.cooperation_organ_id_,cdr.organ_id_
        ORDER BY cdr.id_ DESC
        <include refid="global.limit"/>
    </select>
    <select id="countChildrenStatistics" resultType="int">
        SELECT COUNT(c.id_) FROM (SELECT COUNT(cdr.id_) id_
        FROM children_day_reserve cdr
        <include refid="queryChildrenStatisticsSql"/>
        GROUP BY cdr.music_group_id_,cdr.cooperation_organ_id_,cdr.organ_id_) c
    </select>

    <select id="childrenStatistics" resultMap="ChildrenStatisticsDtoMap">
        SELECT SUM(CASE WHEN cdd.grade_price_ > 0 THEN 1 ELSE NULL END) 'instrumental_music_num_',
        SUM(CASE WHEN cdd.theory_price_ > 0 THEN 1 ELSE NULL END) 'music_theory_num_',
        SUM(CASE WHEN cdd.vip_1v1_price_ > 0 THEN 1 ELSE NULL END) 'buy_vip1_num_',
        SUM(cdd.vip_1v1_price_) 'buy_vip1_amount_',
        SUM(CASE WHEN cdd.vip_1v2_price_ > 0 THEN 1 ELSE NULL END) 'buy_vip2_num_',
        SUM(cdd.vip_1v2_price_) 'buy_vip2_amount_',
        SUM(CASE WHEN cdd.theory_course_price_ > 0 THEN 1 ELSE NULL END) 'buy_music_theory_num_',
        SUM(cdd.theory_course_price_) 'buy_music_theory_amount_',
        SUM(cdd.grade_price_) 'grade_price_',
        SUM(cdd.theory_price_) 'theory_price_',
        SUM(cdd.total_amount_) 'total_amount_'
        FROM children_day_reserve cdr
        LEFT JOIN children_day_degree_info cdd ON cdr.id_ = cdd.reserve_id_
        LEFT JOIN music_group mg ON mg.id_ = cdr.music_group_id_
        LEFT JOIN cooperation_organ co ON co.id_ = cdr.cooperation_organ_id_
        LEFT JOIN organization o ON o.id_ = cdr.organ_id_
        <include refid="queryChildrenStatisticsSql"/>
    </select>
    <sql id="queryChildrenStatisticsSql">
        <where>
            cdr.tenant_id_ = #{tenantId}
            <if test="cooperationId != null">
                AND cdr.cooperation_organ_id_ = #{cooperationId}
            </if>
            <if test="musicGroupId != null and musicGroupId != ''">
                AND cdr.music_group_id_ = #{musicGroupId}
            </if>
            <if test="organId != null">
                AND FIND_IN_SET(cdr.organ_id_,#{organId})
            </if>
        </where>
    </sql>
    <sql id="queryChildrenStatisticsSql1">
        <if test="cooperationId != null">
            AND cdr.cooperation_organ_id_ = #{cooperationId}
        </if>
        <if test="musicGroupId != null and musicGroupId != ''">
            AND cdr.music_group_id_ = #{musicGroupId}
        </if>
        <if test="organId != null">
            AND FIND_IN_SET(cdr.organ_id_,#{organId})
        </if>
    </sql>

    <select id="getByUserId" resultMap="ChildrenDayReserve">
        SELECT *
        FROM children_day_reserve
        WHERE user_id_ = #{userId}
    </select>

    <resultMap id="ChildrenStatisticsDetailDtoMap" type="com.ym.mec.biz.dal.dto.ChildrenStatisticsDetailDto">
        <result property="organName" column="organ_name_"/>
        <result property="cooperationOrganName" column="cooperation_organ_name_"/>
        <result property="musicGroupName" column="music_group_name_"/>
        <result property="currentClass" column="current_class_"/>
        <result property="realName" column="real_name_"/>
        <result property="username" column="username_"/>
        <result property="userId" column="user_id_"/>
        <result property="isReserve" column="is_reserve_"/>
        <result property="phone" column="phone_"/>
        <result property="subjectName" column="subject_name_"/>
        <result property="gradeLeave" column="grade_level_"/>
        <result property="theoryLevel" column="theory_level_"/>
        <result property="buyVipFlag" column="buy_vip_flag_"/>
        <result property="buyTheoryCourseFlag" column="buy_theory_course_flag_"/>
        <result property="totalAmount" column="total_amount_"/>
    </resultMap>
    <select id="countChildrenStatisticsDetail" resultType="int">
        SELECT COUNT(cdr.id_) FROM children_day_reserve cdr
        LEFT JOIN sys_user su ON su.id_ = cdr.user_id_
        LEFT JOIN children_day_degree_info cdd ON cdr.id_ = cdd.reserve_id_
        <include refid="queryChildrenStatisticsDetailSql"/>
    </select>
    <select id="queryChildrenStatisticsDetail" resultMap="ChildrenStatisticsDetailDtoMap">
        SELECT su.username_,su.phone_,cdr.user_id_,cdr.is_reserve_,s.name_
        subject_name_,cdd.grade_level_,cdd.theory_level_,
        CASE WHEN cdd.vip_1v1_price_ > 0 OR cdd.vip_1v2_price_ > 0 THEN 1 ELSE 0 END buy_vip_flag_,
        CASE WHEN cdd.theory_course_price_ > 0 THEN 1 ELSE 0 END buy_theory_course_flag_,cdd.total_amount_
        FROM children_day_reserve cdr
        LEFT JOIN children_day_degree_info cdd ON cdr.id_ = cdd.reserve_id_
        LEFT JOIN sys_user su ON su.id_ = cdr.user_id_
        LEFT JOIN `subject` s ON s.id_ = cdd.subject_id_
        <include refid="queryChildrenStatisticsDetailSql"/>
        ORDER BY cdr.create_time_ DESC
        <include refid="global.limit"/>
    </select>
    <select id="exportStatisticsDetail" resultMap="ChildrenStatisticsDetailDtoMap">
        SELECT o.name_ organ_name_,co.name_ cooperation_organ_name_,mg.name_ music_group_name_,cdr.user_id_,
        su.username_,su.phone_,s.name_
        subject_name_,st.current_class_,sut.real_name_,cdr.is_reserve_,cdd.grade_level_,cdd.theory_level_,
        CASE WHEN cdd.vip_1v1_price_ > 0 OR cdd.vip_1v2_price_ > 0 THEN 1 ELSE 0 END buy_vip_flag_,
        CASE WHEN cdd.theory_course_price_ > 0 THEN 1 ELSE 0 END buy_theory_course_flag_,cdd.total_amount_
        FROM children_day_reserve cdr
        LEFT JOIN children_day_degree_info cdd ON cdr.id_ = cdd.reserve_id_
        LEFT JOIN organization o ON o.id_ = cdr.organ_id_
        LEFT JOIN cooperation_organ co ON co.id_ = cdr.cooperation_organ_id_
        LEFT JOIN music_group mg ON mg.id_ = cdr.music_group_id_
        LEFT JOIN student st ON st.user_id_ = cdr.user_id_
        LEFT JOIN sys_user sut ON sut.id_ = st.teacher_id_
        LEFT JOIN sys_user su ON su.id_ = cdr.user_id_
        LEFT JOIN `subject` s ON s.id_ = cdd.subject_id_
        <where>
            <if test="cooperationId != null">
                AND cdr.cooperation_organ_id_ = #{cooperationId}
            </if>
            <if test="musicGroupId != null and musicGroupId != ''">
                AND cdr.music_group_id_ = #{musicGroupId}
            </if>
            <if test="organId != null">
                AND FIND_IN_SET(cdr.organ_id_,#{organId})
            </if>
        </where>
        ORDER BY cdr.organ_id_,cdr.create_time_ DESC
    </select>
    <sql id="queryChildrenStatisticsDetailSql">
        <where>
            <if test="search != null and search != ''">
                AND (cdr.user_id_ = #{search} OR su.username_ LIKE CONCAT('%',#{search},'%') OR su.phone_ LIKE
                CONCAT('%',#{search},'%'))
            </if>
            <if test="tenantId != null">
                AND cdr.tenant_id_ = #{tenantId}
            </if>
            <if test="isReserve != null">
                AND cdr.is_reserve_ = #{isReserve}
            </if>
            <if test="organId != null and organId != ''">
                AND cdr.organ_id_ = #{organId}
            </if>
            <if test="cooperationId != null">
                AND cdr.cooperation_organ_id_ = #{cooperationId}
            </if>
            <if test="cooperationId == null">
                AND cdr.cooperation_organ_id_ IS NULL
            </if>
            <if test="musicGroupId != null and musicGroupId != ''">
                AND cdr.music_group_id_ = #{musicGroupId}
            </if>
            <if test="musicGroupId == null">
                AND cdr.music_group_id_ IS NULL
            </if>
            <if test="subjectId != null">
                AND cdd.subject_id_ = #{subjectId}
            </if>
            <if test="gradeFlag != null">
                <if test="gradeFlag == 1">
                    AND cdd.grade_level_ IS NOT NULL
                </if>
                <if test="gradeFlag == 0">
                    AND cdd.grade_level_ IS NULL
                </if>
            </if>
            <if test="theoryFlag != null">
                <if test="theoryFlag == 1">
                    AND cdd.theory_level_ IS NOT NULL
                </if>
                <if test="theoryFlag == 0">
                    AND cdd.theory_level_ IS NULL
                </if>
            </if>
            <if test="vipFlag != null">
                <if test="vipFlag == 1">
                    AND (cdd.vip_1v1_price_ > 0 OR cdd.vip_1v2_price_ > 0)
                </if>
                <if test="vipFlag == 0">
                    AND cdd.vip_1v1_price_ &lt;= 0 AND cdd.vip_1v2_price_ &lt;= 0
                </if>
            </if>
            <if test="theoryPriceFlag != null">
                <if test="theoryPriceFlag == 1">
                    AND cdd.theory_course_price_ > 0
                </if>
                <if test="theoryPriceFlag == 0">
                    AND cdd.theory_course_price_ &lt;= 0
                </if>
            </if>
        </where>
    </sql>

    <select id="getSendMsgList" resultMap="ChildrenStatisticsDetailDtoMap">
        SELECT cdr.*,su.phone_ FROM children_day_reserve cdr
        LEFT JOIN sys_user su ON su.id_ = cdr.user_id_
        <where>
            (cdr.is_reserve_ = 1 AND is_send_msg_ = 0) AND
            <foreach collection="msgDtos" item="msgDto" open="(" close=")" separator="OR">
                <if test="msgDto.musicGroupId != null">
                    (cdr.organ_id_=#{msgDto.organId} AND cdr.music_group_id_ = #{msgDto.musicGroupId})
                </if>
                <if test="msgDto.musicGroupId == null">
                    (cdr.organ_id_=#{msgDto.organId} AND cdr.music_group_id_ IS NULL)
                </if>
            </foreach>
        </where>
    </select>

    <update id="updateSendMasStatus">
        UPDATE children_day_reserve SET is_send_msg_ = 1 WHERE user_id_ IN
        <foreach collection="userIds" item="userId" open="(" close=")" separator=",">
            #{userId}
        </foreach>
    </update>
</mapper>
