<?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.CourseScheduleRewardsRulesDao">

	<resultMap type="com.ym.mec.biz.dal.entity.CourseScheduleRewardsRules"
		id="CourseScheduleRewardsRules">
		<result column="id_" property="id" />
		<result column="name_" property="name" />
		<result column="course_schedule_type_" property="courseScheduleType"
			typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler" />
		<result column="reward_mode_" property="rewardMode"
			typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler" />
		<result column="organ_id_" property="organId" />
		<result column="rewards_rules_json_" property="rewardsRulesJson" />
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
		<result column="vip_group_category_id_list_" property="vipGroupCategoryIdList" />
		<result column="teacher_id_" property="teacherId" />
		<result column="subject_id_" property="subjectIds" />
		<result column="subject_name_" property="subjectName" />
		<result column="tenant_id_" property="tenantId" />
	</resultMap>

	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="CourseScheduleRewardsRules">
		SELECT * FROM course_schedule_rewards_rules WHERE id_ = #{id}
	</select>

	<!-- 全查询 -->
	<select id="findAll" resultMap="CourseScheduleRewardsRules">
		SELECT * FROM course_schedule_rewards_rules where tenant_id_ = #{tenantId} ORDER BY id_
	</select>

	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.CourseScheduleRewardsRules"
		useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		<!-- <selectKey resultClass="int" keyProperty="id" > SELECT SEQ_WSDEFINITION_ID.nextval 
			AS ID FROM DUAL </selectKey> -->
		INSERT INTO course_schedule_rewards_rules
		(id_,name_,course_schedule_type_,reward_mode_,organ_id_,rewards_rules_json_,create_time_,update_time_,vip_group_category_id_list_,teacher_id_,subject_id_,tenant_id_)
		VALUES(#{id},#{name},#{courseScheduleType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
		#{rewardMode,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{organId},#{rewardsRulesJson},now(),now(),#{vipGroupCategoryIdList},#{teacherId},#{subjectIds},#{tenantId})
	</insert>

	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.ym.mec.biz.dal.entity.CourseScheduleRewardsRules">
		UPDATE course_schedule_rewards_rules
		<set>
			<if test="rewardMode != null">
				reward_mode_ =
				#{rewardMode,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
			</if>
			<if test="courseScheduleType != null">
				course_schedule_type_ =
				#{courseScheduleType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
			</if>
			<if test="vipGroupCategoryIdList != null">
				vip_group_category_id_list_ = #{vipGroupCategoryIdList},
			</if>
			<if test="teacherId != null">
				teacher_id_ = #{teacherId},
			</if>
			<if test="subjectIds != null">
				subject_id_ = #{subjectIds},
			</if>
			<if test="updateTime != null">
				update_time_ = now(),
			</if>
			<if test="organId != null">
				organ_id_ = #{organId},
			</if>
			<if test="name != null">
				name_ = #{name},
			</if>
			<if test="rewardsRulesJson != null">
				rewards_rules_json_ = #{rewardsRulesJson},
			</if>
		</set>
		WHERE id_ = #{id} and tenant_id_ = #{tenantId}
	</update>

	<!-- 根据主键删除一条记录 -->
	<delete id="delete">
		DELETE FROM course_schedule_rewards_rules WHERE id_ = #{id}
	</delete>

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

	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM course_schedule_rewards_rules where tenant_id_ = #{tenantId}
	</select>
	
	<select id="query" resultType="map">
		SELECT * FROM course_schedule_rewards_rules WHERE find_in_set(#{organId},organ_id_)
		AND course_schedule_type_ = #{courseScheduleType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler} ORDER BY update_time_ DESC
	</select>
	
	<select id="queryOrganIdList" resultType="java.lang.String">
		SELECT organ_id_ FROM course_schedule_rewards_rules where tenant_id_ = #{tenantId}
	</select>
	<select id="findByTeacherAndCategory" resultMap="CourseScheduleRewardsRules">
		SELECT * FROM course_schedule_rewards_rules csrr
		WHERE INTE_ARRAY(csrr.vip_group_category_id_list_,#{vipGroupCategoryIdList})
		AND INTE_ARRAY(csrr.teacher_id_,#{teacherId})
		AND INTE_ARRAY(csrr.subject_id_,#{subjectIds})
	</select>

	<select id="queryDetailPage" resultMap="CourseScheduleRewardsRules">
		SELECT GROUP_CONCAT(s.name_) subject_name_,csrr.* FROM course_schedule_rewards_rules csrr
		LEFT JOIN `subject` s ON FIND_IN_SET(s.id_,csrr.subject_id_)
		<include refid="findDetailPageSql"/>
		GROUP BY csrr.id_ ORDER BY csrr.id_ DESC
		<include refid="global.limit" />
	</select>
	<select id="findDetailPage" resultType="java.lang.Integer">
		SELECT COUNT(csrr.id_) FROM course_schedule_rewards_rules csrr
		<include refid="findDetailPageSql"/>
	</select>
	<select id="findTeacherNames" resultType="java.util.Map">
		SELECT csrr.id_ 'key',GROUP_CONCAT(su.real_name_) 'value' FROM course_schedule_rewards_rules csrr
		LEFT JOIN sys_user su ON FIND_IN_SET(su.id_,csrr.teacher_id_)
		WHERE csrr.id_ IN
		<foreach collection="rewardsIds" separator="," item="item" open="(" close=")">
			#{item}
		</foreach>
		GROUP BY csrr.id_
	</select>
	<select id="findVipCategoryNames" resultType="java.util.Map">
		SELECT csrr.id_ 'key',GROUP_CONCAT(vgc.name_) 'value' FROM course_schedule_rewards_rules csrr
		LEFT JOIN vip_group_category vgc ON FIND_IN_SET(vgc.id_,csrr.vip_group_category_id_list_)
		WHERE vgc.del_flag_ != '1' AND csrr.id_ IN
		<foreach collection="rewardsIds" separator="," item="item" open="(" close=")">
			#{item}
		</foreach>
		GROUP BY csrr.id_
	</select>
	<resultMap id="TaskRewardsRulesDtoMap" type="com.ym.mec.biz.dal.dto.TaskRewardsRulesDto">
		<result property="teacherId" column="actual_teacher_id_"/>
		<result property="times" column="times_"/>
		<result property="activeId" column="active_id_"/>
		<result property="courseScheduleIds" column="courseScheduleIds"/>
		<result property="activeName" column="active_name_"/>
	</resultMap>
    <select id="queryVipGroupTeacherClassTimesByMonth" resultMap="TaskRewardsRulesDtoMap">
		SELECT COUNT(cs.id_) times_,cs.actual_teacher_id_,csrr.id_ active_id_,GROUP_CONCAT(cs.id_) courseScheduleIds,csrr.name_ active_name_
		FROM course_schedule_rewards_rules csrr
		LEFT JOIN vip_group vg ON FIND_IN_SET(vg.vip_group_category_id_,csrr.vip_group_category_id_list_)
		LEFT JOIN course_schedule cs ON vg.id_ = cs.music_group_id_
		LEFT JOIN class_group cg ON cg.id_ = cs.class_group_id_
		LEFT JOIN course_schedule_teacher_salary csts ON csts.course_schedule_id_ = cs.id_
		LEFT JOIN vip_group_activity vga ON vga.id_ = vg.vip_group_activity_id_
		WHERE cs.group_type_ = 'VIP' AND csts.settlement_time_ IS NOT NULL
		AND cs.class_date_ &gt;= #{firstDayOfMonth} AND cs.class_date_ &lt;= #{lastDayOfMonth}
		AND FIND_IN_SET(cs.actual_teacher_id_,csrr.teacher_id_)
		AND FIND_IN_SET(cg.subject_id_list_,csrr.subject_id_)
		AND csts.user_id_ = cs.actual_teacher_id_
		AND ((vga.offline_class_join_gradient_rewards_ = 1 AND cs.teach_mode_ = 'OFFLINE') OR (vga.online_class_join_gradient_rewards_ = 1 AND cs.teach_mode_ = 'ONLINE'))
		GROUP BY cs.actual_teacher_id_,csrr.id_
	</select>
	<select id="findByIds" resultMap="CourseScheduleRewardsRules">
		SELECT * FROM course_schedule_rewards_rules csrr WHERE csrr.id_ IN
		<foreach collection="activeIds" separator="," open="(" close=")" item="item">
			#{item}
		</foreach>
	</select>
	<sql id="findDetailPageSql">
		<where>
			<if test="teacherId">
				AND FIND_IN_SET(#{teacherId},csrr.teacher_id_)
			</if>
			<if test="subjectId">
				AND FIND_IN_SET(#{subjectId},csrr.subject_id_)
			</if>
			<if test="search">
				AND csrr.name_ LIKE CONCAT('%',#{search},'%')
			</if>
			<if test="rewardMode">
				AND csrr.reward_mode_ = #{rewardMode}
			</if>
			<if test="tenantId">
				AND csrr.tenant_id_ = #{tenantId}
			</if>
			<if test="vipGroupCategoryId">
				AND FIND_IN_SET(#{vipGroupCategoryId},csrr.vip_group_category_id_list_)
			</if>
		</where>
	</sql>
</mapper>
