<?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.VipGroupActivityDao">
	
	<resultMap type="com.ym.mec.biz.dal.entity.VipGroupActivity" id="VipGroupActivity">
		<result column="id_" property="id" />
		<result column="name_" property="name" />
		<result column="description_" property="description" />
		<result column="vip_group_category_id_list_" property="vipGroupCategoryIdList" />
		<result column="start_time_" property="startTime" />
		<result column="end_time_" property="endTime" />
		<result column="organ_id_" property="organId" />
		<result column="courses_start_time_" property="coursesStartTime" />
		<result column="courses_end_time_" property="coursesEndTime" />
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
		<result column="salary_settlement_json_" property="salarySettlementJson" />
		<result column="vip_group_category_names_" property="vipGroupCategoryNames"/>
		<result property="delFlag" column="del_flag_"/>
		<result property="onlineClassJoinGradientRewards" column="online_class_join_gradient_rewards_"/>
		<result property="offlineClassJoinGradientRewards" column="offline_class_join_gradient_rewards_"/>
		<result property="minCourseNum" column="min_course_num_"/>
		<result property="maxCourseNum" column="max_course_num_"/>
		<result property="studentMaxUsedTimes" column="student_max_used_times_"/>
		<result property="applyToStudentType" column="apply_to_student_type_"/>
		<result property="allowOnlineToOffline" column="allow_online_to_offline_"/>
		<result property="giveCategoryId" column="give_category_id_"/>
		<result property="singleCourseTime" column="single_course_time_"/>
		<result property="discount" column="discount_"/>
		<result property="fullMinusCourseTimes" column="full_minus_course_times_"/>
		<result property="giveCourseType" column="give_course_type_"/>
		<result property="giveCourseNum" column="give_course_num_"/>
		<result property="giveMemberRankId" column="give_member_rank_id_"/>
		<result property="giveMemberTime" column="give_member_time_"/>
		<result property="givePrizeNum" column="give_prize_num_"/>
		<result property="giveAllowOnlineToOffline" column="give_allow_online_to_offline_"/>
		<result property="giveCouponId" column="give_coupon_id_"/>
		<result property="giveCouponNum" column="give_coupon_num_"/>
		<result property="memberRankId" column="member_rank_id_"/>
		<result property="memberTime" column="member_time_"/>
		<result property="courseType" column="course_type_"/>
		<result property="activityType" column="activity_type_"/>
		<result property="giveSingleCourseTime" column="give_sign_course_time_"/>
		<result property="payToBalance" column="is_pay_to_balance_"/>
		<result property="teachMode" column="teach_mode_"/>
		<result property="giveTeachMode" column="give_teach_mode_"/>
		<result property="periodEnum" column="period_" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
		<result property="givePeriodEnum" column="give_period_" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
		<result property="status" column="status_"/>
		<result property="marketPrice" column="market_price_"/>
		<result property="enable" column="enable_"/>
		<result property="code" column="code_"/>
		<result property="activityChannel" column="activity_channel_"/>
		<result property="originalPrice" column="original_price_"/>
        <result column="tenant_id_" property="tenantId"/>
	</resultMap>
	
	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="VipGroupActivity" >
		SELECT * FROM vip_group_activity WHERE id_ = #{id}
	</select>
	
	<!-- 全查询 -->
	<select id="findAll" resultMap="VipGroupActivity">
		SELECT * FROM vip_group_activity WHERE del_flag_=0 and tenant_id_ = #{tenantId} ORDER BY id_
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.VipGroupActivity" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO vip_group_activity (name_,description_,vip_group_category_id_list_,start_time_,end_time_,organ_id_,courses_start_time_,courses_end_time_,
		                                create_time_,update_time_,salary_settlement_json_,del_flag_,
		                                online_class_join_gradient_rewards_,offline_class_join_gradient_rewards_,min_course_num_,max_course_num_,
										student_max_used_times_, apply_to_student_type_,allow_online_to_offline_,
										single_course_time_,discount_,full_minus_course_times_,give_course_type_,give_category_id_,give_course_num_
										,member_rank_id_,member_time_,give_member_rank_id_,give_member_time_,course_type_,
		                                activity_type_,give_sign_course_time_,is_pay_to_balance_,teach_mode_,give_teach_mode_,
		                                period_,give_period_,status_,market_price_,give_allow_online_to_offline_,give_prize_num_,
		                                activity_channel_,original_price_,tenant_id_,give_coupon_id_,give_coupon_num_)
		VALUES(#{name},#{description},#{vipGroupCategoryIdList},#{startTime},#{endTime},#{organId},#{coursesStartTime},#{coursesEndTime},
		       now(),now(),#{salarySettlementJson},#{delFlag},#{onlineClassJoinGradientRewards},
		       #{offlineClassJoinGradientRewards},#{minCourseNum},#{maxCourseNum},#{studentMaxUsedTimes},#{applyToStudentType},#{allowOnlineToOffline},
			   #{singleCourseTime},#{discount},#{fullMinusCourseTimes},#{giveCourseType},#{giveCategoryId},#{giveCourseNum}
				  ,#{memberRankId},#{memberTime},#{giveMemberRankId},#{giveMemberTime},#{courseType},#{activityType},#{giveSingleCourseTime},
		       #{isPayToBalance},#{teachMode},#{giveTeachMode},#{periodEnum,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{givePeriodEnum,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{status},
		       #{marketPrice},#{giveAllowOnlineToOffline},#{givePrizeNum},#{activityChannel},#{originalPrice},#{tenantId},#{giveCouponId},#{giveCouponNum})
	</insert>
	
	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.ym.mec.biz.dal.entity.VipGroupActivity">
		UPDATE vip_group_activity SET status_ = #{status},give_period_ = #{givePeriodEnum,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
		period_ = #{periodEnum,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},give_teach_mode_ = #{giveTeachMode},
		teach_mode_ = #{teachMode},is_pay_to_balance_ = #{isPayToBalance},give_sign_course_time_ = #{giveSingleCourseTime},
		single_course_time_ = #{singleCourseTime},discount_ = #{discount},full_minus_course_times_ = #{fullMinusCourseTimes},
		give_course_type_ = #{giveCourseType},give_category_id_ = #{giveCategoryId},give_course_num_ = #{giveCourseNum},
		activity_type_ = #{activityType},course_type_ = #{courseType},member_time_ = #{memberTime},
		member_rank_id_ = #{memberRankId},give_member_time_ = #{giveMemberTime},give_member_rank_id_ = #{giveMemberRankId},
		organ_id_ = #{organId},
		courses_end_time_ = #{coursesEndTime},
		name_ = #{name},start_time_ = #{startTime},vip_group_category_id_list_ = #{vipGroupCategoryIdList},
		description_ = #{description},salary_settlement_json_ = #{salarySettlementJson},courses_start_time_ = #{coursesStartTime},
		online_class_join_gradient_rewards_=#{onlineClassJoinGradientRewards},
		offline_class_join_gradient_rewards_=#{offlineClassJoinGradientRewards},min_course_num_=#{minCourseNum},
		max_course_num_=#{maxCourseNum},student_max_used_times_=#{studentMaxUsedTimes},apply_to_student_type_=#{applyToStudentType},
		allow_online_to_offline_=#{allowOnlineToOffline},end_time_ = #{endTime},market_price_ = #{marketPrice},update_time_ = NOW(),
		give_allow_online_to_offline_ = #{giveAllowOnlineToOffline},
		give_prize_num_ = #{givePrizeNum},activity_channel_ = #{activityChannel},original_price_ = #{originalPrice} ,
									  give_coupon_id_ = #{giveCouponId} ,give_coupon_num_ = #{giveCouponNum}
		WHERE id_ = #{id} and tenant_id_ = #{tenantId}
	</update>

	<!-- 根据主键删除一条记录 -->
	<delete id="delete" >
		UPDATE vip_group_activity SET del_flag_=1 WHERE id_ = #{id}
	</delete>

	<sql id="queryCondition">
		<where>
			vga.del_flag_ = 0 and vga.tenant_id_ = #{tenantId}
			<if test="activityChannel != null">
				AND vga.activity_channel_ = #{activityChannel}
			</if>
			<if test="courseType != null and courseType != ''">
				AND vga.course_type_ = #{courseType}
			</if>
			<if test="giveCourseType != null and giveCourseType != ''">
				AND vga.give_course_type_ = #{giveCourseType}
			</if>
			<if test="organId != null">
				AND INTE_ARRAY(vga.organ_id_,#{organId})
			</if>
			<if test="enable != null and enable == true">
				AND ((NOW() &lt; vga.end_time_ AND NOW() > vga.start_time_) OR vga.end_time_ IS NULL)
			</if>
			<if test="enable != null and enable == false">
				AND (NOW() >= vga.end_time_ OR NOW() &lt;= vga.start_time_)
			</if>
			<if test="giveFlag != null">
				<if test="giveFlag == 'true'">
					AND (vga.give_course_num_ > 0 OR vga.member_time_ > 0)
				</if>
				<if test="giveFlag == 'false'">
					AND vga.give_course_num_ = 0 AND vga.member_time_ = 0
				</if>
			</if>
			<if test="giveType != null">
				<if test="giveType == 'VIP' or giveType == 'PRACTICE'">
					AND vga.give_course_type_ = #{giveType} AND vga.give_course_num_ > 0
				</if>
				<if test="giveType == 'MEMBER'">
					AND vga.give_course_type_ = #{status} AND vga.give_member_time_ > 0
				</if>
			</if>
			<if test="status != null">
				AND vga.status_ = #{status}
			</if>
			<if test="allowOnlineToOffline != null">
				AND vga.allow_online_to_offline_ = #{allowOnlineToOffline}
			</if>
			<if test="search != null and search != ''">
				AND (vga.id_ = #{search} OR vga.name_ LIKE CONCAT('%', #{search}, '%'))
			</if>
			<if test="newStudent != null">
				AND (aps.new_student_ = #{newStudent} OR aps.new_student_ = -1)
			</if>
			<if test="memberFlag != null">
				AND (aps.member_flag_ = #{memberFlag} OR aps.member_flag_ = -1)
			</if>
		</where>
	</sql>

	<!-- 分页查询 -->
	<select id="queryPage" resultMap="VipGroupActivity" parameterType="map">
		SELECT
		vga.*,((NOW() &lt; end_time_ AND NOW() > start_time_) OR end_time_ IS NULL) enable_,GROUP_CONCAT(vgc.name_) vip_group_category_names_
		FROM vip_group_activity vga
		LEFT JOIN activity_apply_student_type aps ON vga.id_ = aps.activity_id_
		LEFT JOIN vip_group_category vgc ON FIND_IN_SET(vgc.id_,vga.vip_group_category_id_list_)
		<include refid="queryCondition"/>
		GROUP BY vga.id_
		ORDER BY id_ DESC
		<include refid="global.limit"/>
	</select>
	
	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(vga.id_) FROM vip_group_activity vga
		LEFT JOIN activity_apply_student_type aps ON vga.id_ = aps.activity_id_
		<include refid="queryCondition"/>
	</select>
    <select id="findByCategory" resultMap="VipGroupActivity">
		SELECT vga.* FROM
			vip_group_activity vga
			LEFT JOIN activity_apply_student_type ast ON vga.id_ = ast.activity_id_
		WHERE vga.status_ = 'PROGRESS'
		AND vga.del_flag_ = 0 AND vga.course_type_ = #{activityQueryDto.courseType}
		AND FIND_IN_SET(#{activityQueryDto.organId},vga.organ_id_)
		<if test="activityQueryDto.enable != null and activityQueryDto.enable == true">
			AND ((NOW() &lt; vga.end_time_ AND NOW() > vga.start_time_) OR vga.end_time_ IS NULL)
		</if>
		<if test="activityQueryDto.enable != null and activityQueryDto.enable == false">
			AND (NOW() >= vga.end_time_ OR NOW() &lt;= vga.start_time_)
		</if>
		<if test="activityQueryDto.categoryId != null">
		  AND FIND_IN_SET(#{activityQueryDto.categoryId}, vga.vip_group_category_id_list_ )
		</if>
		<if test="activityQueryDto.includeClosed == null">
			AND (vga.end_time_ IS NULL OR NOW() BETWEEN vga.start_time_ AND vga.end_time_)
		</if>
		<if test="activityQueryDto.newStudentNum != null">
			AND ast.new_student_ != #{activityQueryDto.newStudentNum}
		</if>
		<if test="activityQueryDto.newStudentNum == null">
			AND ast.new_student_ = -1
		</if>
		<if test="activityQueryDto.memberNum != null">
			AND ast.member_flag_ != #{activityQueryDto.memberNum}
		</if>
		<if test="activityQueryDto.memberNum == null">
			AND ast.member_flag_ = -1
		</if>
		<if test="activityQueryDto.activityChannel != null">
			AND vga.activity_channel_ = #{activityQueryDto.activityChannel}
		</if>
		ORDER BY vga.create_time_ DESC
	</select>
    <select id="queryNamesById" resultType="java.util.Map">
		SELECT vga.id_ `key`,vga.name_ `value` FROM vip_group_activity vga
		WHERE FIND_IN_SET(vga.id_,#{activityIds}) AND vga.del_flag_=0
	</select>
	
    <select id="queryByIds" resultMap="VipGroupActivity">
		SELECT * FROM vip_group_activity vga WHERE FIND_IN_SET(vga.id_,#{activityIds})
	</select>
	<resultMap type="com.ym.mec.biz.dal.dto.ExportVipGroupActivityDto" id="exportVipGroupActivityMap">
		<result column="organ_name_" property="organName" />
		<result column="id_" property="id" />
		<result column="name_" property="name" />
		<result column="description_" property="description" />
		<result column="vip_group_category_id_list_" property="vipGroupCategoryIdList" />
		<result column="open_flag_" property="openFlag" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
		<result column="time_" property="time" />
		<result column="course_time_" property="courseTime" />
		<result column="type_" property="type" />
		<result column="dis_count_" property="disCount" />
		<result column="online_salary_type_" property="onlineSalaryType" />
		<result column="online_salary_settlement_" property="onlineSalarySettlement" />
		<result column="offline_salary_type_" property="offlineSalaryType" />
		<result column="offline_salary_settlement_" property="offlineSalarySettlement" />

	</resultMap>
	<select id="exportVipGroupActivity" resultMap="exportVipGroupActivityMap">
		SELECT GROUP_CONCAT(distinct o.`name_`) organ_name_,vga.id_ , vga.`name_`,vga.`description_`,GROUP_CONCAT(distinct vgc.`name_`) vip_group_category_id_list_ ,
		((NOW() &lt; vga.end_time_ AND NOW() > vga.start_time_) OR vga.end_time_ IS NULL) open_flag_,concat(vga.`start_time_`,' - ',vga.`end_time_` ) time_,
		concat(vga.courses_start_time_,' - ',vga.courses_end_time_) course_time_,
		case when vga.`type_` = 'BASE_ACTIVITY' then '基础活动' when vga.`type_` = 'DISCOUNT' then '折扣' else '赠送课时' end type_ ,
		case when vga.`type_` = 'DISCOUNT' then concat(vga.`attribute1_`,'%')  end dis_count_ ,
		case when JSON_EXTRACT(vga.`salary_settlement_json_`,'$.onlineSalarySettlement.salarySettlementType') = 'RATIO_DISCOUNT' then '课程单价比例折扣'
		when JSON_EXTRACT(vga.`salary_settlement_json_`,'$.onlineSalarySettlement.salarySettlementType') = 'FIXED_SALARY' then '固定课酬'
		else '老师默认课酬' end online_salary_type_,
		JSON_EXTRACT(vga.`salary_settlement_json_`,'$.onlineSalarySettlement.settlementValue') online_salary_settlement_,
		case when JSON_EXTRACT(vga.`salary_settlement_json_`,'$.offlineSalarySettlement.salarySettlementType') = 'RATIO_DISCOUNT' then '课程单价比例折扣'
		when JSON_EXTRACT(vga.`salary_settlement_json_`,'$.offlineSalarySettlement.salarySettlementType') = 'FIXED_SALARY' then '固定课酬'
		else '老师默认课酬' end  offline_salary_type_,
		JSON_EXTRACT(vga.`salary_settlement_json_`,'$.offlineSalarySettlement.settlementValue') offline_salary_settlement_
		FROM `vip_group_activity` vga
		LEFT JOIN `organization` o on find_in_set(o.`id_`,vga.`organ_id_` )
		LEFT JOIN `vip_group_category` vgc on find_in_set(vgc.`id_`,vga.`vip_group_category_id_list_` )
		<where>
			vga.del_flag_=0 and vga.tenant_id_ = #{tenantId}
			<if test="organId != null">
				AND INTE_ARRAY(vga.organ_id_,#{organId})
			</if>
			<if test="enable != null and enable == true">
				AND ((NOW() &lt; vga.end_time_ AND NOW() > vga.start_time_) OR end_time_ IS NULL)
			</if>
			<if test="enable != null and enable == false">
				AND (NOW() >= vga.end_time_ OR NOW() &lt;= vga.start_time_)
			</if>
			<if test="search != null and search != ''">
				AND (vga.id_ = #{search} OR vga.name_ LIKE CONCAT('%', #{search}, '%'))
			</if>
		</where>
		GROUP BY vga.`id_`
	</select>

    <select id="findByName" resultMap="VipGroupActivity">
		SELECT * FROM vip_group_activity vga WHERE vga.name_ = #{name} and vga.tenant_id_ = #{tenantId}
	</select>
	<resultMap id="WaitCourseActivityDto" type="com.ym.mec.biz.dal.dto.WaitCourseActivityDto">
		<result property="activityId" column="activity_id_"/>
		<result property="activityName" column="activity_name_"/>
		<result property="studentNum" column="student_num_"/>
	</resultMap>
	<select id="queryWaitCourseActivity" resultMap="WaitCourseActivityDto">
		SELECT aum.id_,aum.activity_id_,vga.name_ activity_name_,COUNT(DISTINCT aum.user_id_) student_num_ FROM activity_user_mapper aum
		LEFT JOIN vip_group_activity vga ON aum.activity_id_ = vga.id_
		WHERE aum.return_fee_ = 0 and aum.tenant_id_ = #{tenantId}
		<if test="teacherId != null">
			AND aum.teacher_id_ = #{teacherId}
		</if>
		<if test="organId != null and organId != ''">
			AND INTE_ARRAY(#{organId},vga.organ_id_)
		</if>
		AND (aum.vip_flag_ = 1 OR aum.practice_flag_ = 1 OR aum.give_vip_flag_ = 1 OR aum.give_practice_flag_ = 1)
		GROUP BY aum.activity_id_
	</select>
	<resultMap id="ActivityWaitCourseStudentNumDto" type="com.ym.mec.biz.dal.dto.ActivityWaitCourseStudentNumDto">
		<result property="vipNum" column="vip_num_"/>
		<result property="giveVipNum" column="give_vip_num_"/>
		<result property="practiceNum" column="practice_num_"/>
		<result property="givePracticeNum" column="give_practice_num_"/>
		<result property="username" column="username_"/>
		<result property="avatar" column="avatar_"/>
		<result property="userId" column="user_id_"/>
		<result property="phone" column="phone_"/>
	</resultMap>
	<select id="getActivityWaitCourseStudentNum" resultMap="ActivityWaitCourseStudentNumDto">
		SELECT COUNT(CASE WHEN c.vip_num_ > 0 THEN 1 ELSE NULL END) vip_num_,
		COUNT(CASE WHEN c.practice_num_ > 0 THEN 1 ELSE NULL END) practice_num_,
		COUNT(CASE WHEN c.give_vip_num_ > 0 THEN 1 ELSE NULL END)give_vip_num_,
		COUNT(CASE WHEN c.give_practice_num_ > 0 THEN 1 ELSE NULL END)give_practice_num_
		FROM (SELECT
		COUNT(CASE WHEN vip_flag_ = 1 THEN 1 ELSE NULL END) vip_num_,
		COUNT(CASE WHEN practice_flag_ = 1 THEN 1 ELSE NULL END) practice_num_,
		COUNT(CASE WHEN give_vip_flag_ = 1 THEN 1 ELSE NULL END) give_vip_num_,
		COUNT(CASE WHEN give_practice_flag_ = 1 THEN 1 ELSE NULL END) give_practice_num_
		FROM activity_user_mapper aum
		WHERE aum.return_fee_ = 0 AND aum.activity_id_ = #{activityId}
		<if test="userId != null">
			AND aum.teacher_id_ = #{userId}
		</if>
		GROUP BY aum.user_id_)c
	</select>
	<select id="getActivityStudentCanCourseNum" resultMap="ActivityWaitCourseStudentNumDto">
		SELECT * FROM (
		SELECT COUNT(CASE WHEN aum.vip_flag_ = 1 THEN 1 ELSE NULL END) vip_num_,
			   COUNT(CASE WHEN aum.practice_flag_ = 1 THEN 1 ELSE NULL END) practice_num_,
			   COUNT(CASE WHEN aum.give_vip_flag_ = 1 THEN 1 ELSE NULL END) give_vip_num_,
			   COUNT(CASE WHEN aum.give_practice_flag_ = 1 THEN 1 ELSE NULL END) give_practice_num_,
				su.username_,su.avatar_,aum.user_id_,su.phone_
		FROM activity_user_mapper aum
		LEFT JOIN sys_user su ON su.id_ = aum.user_id_
		WHERE aum.return_fee_ = 0 AND aum.activity_id_ = #{activityId}
		<if test="userId != null">
			AND aum.teacher_id_ = #{userId}
		</if>
		<if test="search != null and search != ''">
			AND (aum.user_id_ = #{search} OR su.username_ LIKE CONCAT('%',#{search},'%') OR su.phone_ LIKE CONCAT('%',#{search},'%'))
		</if>
		GROUP BY aum.user_id_) c
		<where>
			<if test="giveFlag != null and giveFlag == 1">
				AND c.give_vip_num_ > 0 OR c.give_practice_num_ > 0
			</if>
			<if test="giveFlag != null and giveFlag == 0">
				AND c.vip_num_ > 0 OR c.practice_num_ > 0
			</if>
		</where>
		<include refid="global.limit"/>
	</select>
	<select id="countActivityStudentCanCourseNum" resultType="int">
		SELECT COUNT(c.user_id_) FROM (
		SELECT COUNT(CASE WHEN aum.vip_flag_ = 1 THEN 1 ELSE NULL END) vip_num_,
		COUNT(CASE WHEN aum.practice_flag_ = 1 THEN 1 ELSE NULL END) practice_num_,
		COUNT(CASE WHEN aum.give_vip_flag_ = 1 THEN 1 ELSE NULL END) give_vip_num_,
		COUNT(CASE WHEN aum.give_practice_flag_ = 1 THEN 1 ELSE NULL END) give_practice_num_,aum.user_id_
		FROM activity_user_mapper aum
		LEFT JOIN sys_user su ON su.id_ = aum.user_id_
		WHERE aum.return_fee_ = 0 AND aum.activity_id_ = #{activityId}
		<if test="userId != null">
			AND aum.teacher_id_ = #{userId}
		</if>
		<if test="search != null and search != ''">
			AND (aum.user_id_ = #{search} OR su.username_ LIKE CONCAT('%',#{search},'%') OR su.phone_ LIKE CONCAT('%',#{search},'%'))
		</if>
		GROUP BY aum.user_id_) c
		<where>
			<if test="giveFlag != null and giveFlag == 1">
				AND c.give_vip_num_ > 0 OR c.give_practice_num_ > 0
			</if>
			<if test="giveFlag != null and giveFlag == 0">
				AND c.vip_num_ > 0 OR c.practice_num_ > 0
			</if>
		</where>
	</select>
	<resultMap id="DoubleEleven2021ActivityDto" type="com.ym.mec.biz.dal.dto.DoubleEleven2021ActivityDto">
		<result property="buyNum" column="buy_num_"/>
		<result property="buyAmount" column="buy_amount_"/>
		<result property="type" column="type_"/>
		<result property="organId" column="organ_id_"/>
	</resultMap>
	<select id="countActivityStatis" resultMap="DoubleEleven2021ActivityDto">
		SELECT SUM(c.buy_amount_) buy_amount_,SUM(c.buy_num_) buy_num_,c.type_ FROM (
		SELECT SUM(aum.actual_price_) buy_amount_,COUNT(DISTINCT aum.user_id_) buy_num_,vga.code_ type_
		FROM student_payment_order spo
				 LEFT JOIN activity_user_mapper aum ON spo.id_ = aum.payment_order_id_ AND spo.user_id_ = aum.user_id_
				 LEFT JOIN vip_group_activity vga ON vga.id_ = aum.activity_id_
		WHERE spo.type_ = 'DOUBLE_ELEVEN2021' AND spo.status_ = 'SUCCESS'
		  AND spo.organ_id_ NOT IN (36,38,39,41,42,43,44,45,46,47,48,49,50,52,54,55,56,2,27,22,24)
		GROUP BY aum.activity_id_,aum.user_id_) c GROUP BY c.type_
	</select>
	<resultMap id="DoubleEleven2021Dto" type="com.ym.mec.biz.dal.dto.DoubleEleven2021Dto">
		<result property="totalBuyAmount" column="total_buy_amount_"/>
		<result property="totalBuyNum" column="total_buy_num_"/>
		<result property="organName" column="organ_name_"/>
		<result property="organId" column="organ_id_"/>
	</resultMap>
	<select id="countDoubleEleven2021Statis" resultMap="DoubleEleven2021Dto">
		SELECT SUM(expect_amount_) total_buy_amount_,COUNT(DISTINCT user_id_) total_buy_num_ FROM student_payment_order
		WHERE type_ = 'DOUBLE_ELEVEN2021' AND status_ = 'SUCCESS'
		AND organ_id_ NOT IN (36,38,39,41,42,43,44,45,46,47,48,49,50,52,54,55,56,2,27,22,24)
	</select>
	<select id="countGiveMemberNum" resultType="java.lang.Integer">
		SELECT COUNT(DISTINCT student_id_) FROM cloud_teacher_order WHERE remark_ = '双十一活动赠送会员'
		AND organ_id_ NOT IN (36,38,39,41,42,43,44,45,46,47,48,49,50,52,54,55,56,2,27,22,24)
	</select>
	<select id="countOrganGiveMemberNum" resultType="map">
		SELECT organ_id_ 'key',COUNT(DISTINCT student_id_) 'value'
		FROM cloud_teacher_order WHERE remark_ = '双十一活动赠送会员'
		AND organ_id_ IN
		<foreach collection="organIdList" open="(" close=")" item="organId" separator=",">
			#{organId}
		</foreach>
		GROUP BY organ_id_
	</select>
	<select id="countOrganActivityStatis" resultMap="DoubleEleven2021ActivityDto">
		SELECT SUM(c.buy_amount_) buy_amount_,SUM(c.buy_num_) buy_num_,c.type_,c.organ_id_ FROM (
		SELECT SUM(aum.actual_price_) buy_amount_,COUNT(DISTINCT aum.user_id_) buy_num_,vga.code_ type_,spo.organ_id_
		FROM student_payment_order spo
		LEFT JOIN activity_user_mapper aum ON spo.id_ = aum.payment_order_id_ AND spo.user_id_ = aum.user_id_
		LEFT JOIN vip_group_activity vga ON vga.id_ = aum.activity_id_
		WHERE spo.type_ = 'DOUBLE_ELEVEN2021' AND spo.status_ = 'SUCCESS'
		AND spo.organ_id_ IN
		<foreach collection="organIdList" open="(" close=")" item="organId" separator=",">
			#{organId}
		</foreach>
		GROUP BY spo.organ_id_,aum.activity_id_,aum.user_id_)c GROUP BY c.organ_id_,c.type_
	</select>
	<select id="countOrganDoubleEleven2021Statis" resultMap="DoubleEleven2021Dto">
		SELECT SUM(spo.expect_amount_) total_buy_amount_,COUNT(DISTINCT spo.user_id_) total_buy_num_,o.name_ organ_name_,o.id_ organ_id_
		FROM organization o
		LEFT JOIN student_payment_order spo ON o.id_ = spo.organ_id_ AND spo.type_ = 'DOUBLE_ELEVEN2021' AND spo.status_ = 'SUCCESS'
		WHERE o.id_ NOT IN (36,38,39,41,42,43,44,45,46,47,48,49,50,52,54,55,56,2,27,22,24)
		<if test="organId != null">
			AND o.id_ = #{organId}
		</if>
		GROUP BY o.id_
	</select>
	<resultMap id="DoubleEleven2021OrderDetailDto" type="com.ym.mec.biz.dal.dto.DoubleEleven2021OrderDetailDto">
		<result property="userId" column="user_id_"/>
		<result property="username" column="username_"/>
		<result property="amount" column="amount_"/>
		<result property="createTime" column="create_time_"/>
		<result property="giveMember" column="give_member_"/>
		<result property="orderNo" column="order_no_"/>
		<result property="payTime" column="pay_time_"/>
		<result property="type" column="type_"/>
	</resultMap>
	<sql id="queryDoubleEleven2021OrderDetailSql">
		WHERE spo.status_ = 'SUCCESS' AND spo.type_ = 'DOUBLE_ELEVEN2021' and spo.tenant_id_ = #{tenantId}
		<if test="search != null and search != ''">
			AND su.id_ = #{search} OR su.username_ LIKE CONCAT('%',#{search},'%') OR spo.order_no_ LIKE CONCAT('%',#{search},'%')
		</if>
		<if test="organId != null and organId != ''">
			AND spo.organ_id_ = #{organId}
		</if>
		<if test="startTime != null">
			AND spo.pay_time_ >= #{startTime}
		</if>
		<if test="endTime != null">
			AND spo.pay_time_ &lt;= #{endTime}
		</if>
		<if test="type != null">
			AND vga.code_ = #{type}
		</if>
		<if test="giveMemberFlag != null and giveMemberFlag == 1">
			AND cto.id_ IS NOT NULL
		</if>
		<if test="giveMemberFlag != null and giveMemberFlag == 0">
			AND cto.id_ IS NULL
		</if>
	</sql>
	<select id="queryDoubleEleven2021OrderDetail" resultMap="DoubleEleven2021OrderDetailDto">
		SELECT spo.user_id_,su.username_,spo.order_no_,SUM(aum.actual_price_) amount_,spo.create_time_,spo.pay_time_,GROUP_CONCAT(vga.attribute1_) type_,
			   CASE WHEN cto.id_ IS NULL THEN 0 ELSE 1 END give_member_
		FROM student_payment_order spo
				 LEFT JOIN activity_user_mapper aum ON spo.id_ = aum.payment_order_id_
				 LEFT JOIN vip_group_activity vga ON vga.id_ = aum.activity_id_
				 LEFT JOIN sys_user su ON su.id_ = spo.user_id_
				 LEFT JOIN cloud_teacher_order cto ON cto.order_id_ = spo.id_
		<include refid="queryDoubleEleven2021OrderDetailSql"/>
		GROUP BY spo.id_
		ORDER BY spo.pay_time_ DESC
		<include refid="global.limit"/>
	</select>
	<select id="countDoubleEleven2021OrderDetail" resultType="java.lang.Integer">
		SELECT COUNT(DISTINCT spo.id_)
		FROM student_payment_order spo
		LEFT JOIN sys_user su ON su.id_ = spo.user_id_
		LEFT JOIN activity_user_mapper aum ON spo.id_ = aum.payment_order_id_
		LEFT JOIN vip_group_activity vga ON vga.id_ = aum.activity_id_
		LEFT JOIN cloud_teacher_order cto ON cto.order_id_ = spo.id_
		<include refid="queryDoubleEleven2021OrderDetailSql"/>
	</select>
</mapper>
