<?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.collectfee.dao.ApplyInfoDao">

	<resultMap type="com.ym.mec.collectfee.entity.ApplyInfo" id="ApplyInfo">
		<result column="id_" property="id" />
		<result column="user_id_" property="userId" />
		<result column="name_" property="name" />
		<result column="sex_" property="sex" />
		<result column="birthday_" property="birthday" />
		<result column="city_" property="city" />
		<result column="school_id_" property="schoolId" />
		<result column="course_id_" property="courseId" />
		<result column="class_id_" property="classId" />
		<result column="branch_id_" property="branchId" />
		<result column="grade_" property="grade" />
		<result column="g_class_" property="gClass" />
		<result column="sub_id_" property="subId" />
		<result column="is_adjust_" property="isAdjust" />
		<result column="patriarch_phone_" property="patriarchPhone" />
		<result column="patriarch_name_" property="patriarchName" />
		<result column="patriarch_unit_" property="patriarchUnit" />
		<result column="status_" property="status" />
		<result column="push_status_" property="pushStatus" />
		<result column="update_time_" property="updateTime" />
		<result column="create_time_" property="createTime" />
	</resultMap>

	<resultMap type="com.ym.mec.collectfee.entity.ApplyInfoPage" id="ApplyInfoPage">
		<result column="id_" property="id" />
		<result column="user_id_" property="userId" />
		<result column="name_" property="name" />
		<result column="sex_" property="sex" />
		<result column="birthday_" property="birthday" />
		<result column="city_" property="city" />
		<result column="school_id_" property="schoolId" />
		<result column="course_id_" property="courseId" />
		<result column="class_id_" property="classId" />
		<result column="branch_id_" property="branchId" />
		<result column="grade_" property="grade" />
		<result column="g_class_" property="gClass" />
		<result column="sub_id_" property="subId" />
		<result column="is_adjust_" property="isAdjust" />
		<result column="patriarch_phone_" property="patriarchPhone" />
		<result column="patriarch_name_" property="patriarchName" />
		<result column="patriarch_unit_" property="patriarchUnit" />
		<result column="status_" property="status" />
		<result column="push_status_" property="pushStatus" />
		<result column="update_time_" property="updateTime" />
		<result column="sub_name_" property="subName" />
		<result column="amount_" property="amount" />
		<result column="create_time_" property="createTime" />
	</resultMap>

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

	<!-- 全查询 -->
	<select id="findAll" resultMap="ApplyInfo">
		SELECT * FROM apply_info ORDER BY id_
	</select>

	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.collectfee.entity.ApplyInfo" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO apply_info (course_id_,class_id_,user_id_,branch_id_,name_,sex_,birthday_,city_,school_id_,grade_,g_class_,sub_id_,is_adjust_,patriarch_phone_,patriarch_name_,patriarch_unit_,update_time_,create_time_) VALUES
		(#{courseId},#{classId},#{userId},#{branchId},#{name},#{sex},#{birthday},#{city},#{schoolId},#{grade},#{gClass},#{subId},#{isAdjust},#{patriarchPhone},#{patriarchName},#{patriarchUnit},#{updateTime},#{createTime})
	</insert>

	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.ym.mec.collectfee.entity.ApplyInfo">
		UPDATE apply_info
		<set>
			<if test="userId != null">
				user_id_ = #{userId},
			</if>
			<if test="branchId != null">
				branch_id_ = #{branchId},
			</if>
			<if test="pushStatus != null">
				push_status_ = #{pushStatus},
			</if>
			<if test="subId != null">
				sub_id_ = #{subId},
			</if>
			<if test="schoolId != null">
				school_id_ = #{schoolId},
			</if>
			<if test="isAdjust != null">
				is_adjust_ = #{isAdjust},
			</if>
			<if test="status != null">
				status_ = #{status},
			</if>
			<if test="gClass != null">
				g_class_ = #{gClass},
			</if>
			<if test="sex != null">
				sex_ = #{sex},
			</if>
			<if test="birthday != null">
				birthday_ = #{birthday},
			</if>
			<if test="name != null">
				name_ = #{name},
			</if>
			<if test="city != null">
				city_ = #{city},
			</if>
			<if test="patriarchPhone != null">
				patriarch_phone_ = #{patriarchPhone},
			</if>
			<if test="patriarchName != null">
				patriarch_name_ = #{patriarchName},
			</if>
			<if test="patriarchUnit != null">
				patriarch_unit_ = #{patriarchUnit},
			</if>
			<if test="updateTime != null">
				update_time_ = #{updateTime},
			</if>
			<if test="grade != null">
				grade_ = #{grade}
			</if>
		</set>
		WHERE id_ = #{id}
	</update>

    <update id="updateUserSub">
            UPDATE apply_info SET sub_id_ = #{subId},course_id_ = #{courseId},update_time_ = #{updateTime} WHERE id_ = ${userId} AND status_ != 1
    </update>

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

	<!-- 分页查询 -->
	<select id="queryPage" resultMap="ApplyInfo" parameterType="map">
		SELECT * FROM apply_info ai <include refid="studentsPage"/> ORDER BY id_ <include refid="global.limit"/>
	</select>

	<sql id="studentsPage">
		<where>
			<if test="musicTeamId != null">
				AND ai.class_id_ = #{musicTeamId}
			</if>
			<if test="name != null and name != ''">
				AND ai.name_ = #{name}
			</if>
			<if test="status != null">
				AND ai.status_ = #{status}
			</if>
			<if test="subId != null">
				AND ai.sub_id_ = #{subId}
			</if>
		</where>
	</sql>

	<!-- 查询当前表的总记录数 -->
	<select id="findCount" resultType="int">
		SELECT COUNT(*) FROM apply_info <include refid="studentsPage"/>
	</select>

    <select id="findUserByPhone" resultMap="ApplyInfo">
		SELECT * FROM apply_info WHERE patriarch_phone_ = #{phone}
		<if test="clazzId != null">
			AND class_id_ = #{clazzId}
		</if>
	</select>

	<select id="getUserRegisterVIewDetail" resultType="java.util.Map">
		SELECT ai.sub_id_,ai.is_adjust_,ai.status_,ai.sex_,ai.name_,ai.patriarch_phone_,ai.sub_id_,
		ai.grade_,ai.school_,ai.id_,ai.g_class_,ai.city_,ai.patriarch_name_,ai.patriarch_unit_,ai.course_id_,
		cgi.sub_name_,cgi.plan_num_,cgi.fee_amount_,cgi.fee_type_,s.status_ push_status_
		FROM apply_info ai LEFT JOIN course_group_info cgi ON ai.course_id_ = cgi.id_
		 LEFT JOIN school s ON ai.class_id_ = s.id_ WHERE ai.id_ = #{stuId}
	</select>

	<select id="queryUserPage" resultMap="ApplyInfoPage" parameterType="map">
		SELECT ao.*,cgi.sub_name_ sub_name_ FROM (SELECT ai.*,o.amount amount_ FROM apply_info ai
		LEFT JOIN (SELECT * FROM `order` WHERE id IN (SELECT DISTINCT(user_id) FROM `order` WHERE class_id = #{musicTeamId})) o
		ON ai.id_ = o.user_id <include refid="studentsPage"/>) ao
		LEFT JOIN course_group_info cgi ON ao.course_id_ = cgi.id_
		ORDER BY ao.update_time_ DESC
		<include refid="global.limit"/>
	</select>

	<select id="queryUserCount" resultType="java.lang.Integer">
		SELECT count(DISTINCT ai.id_) FROM apply_info ai
		LEFT JOIN course_group_info cgi ON ai.sub_id_ = cgi.sub_id_
		LEFT JOIN `order` o ON ai.id_ = o.user_id
		<include refid="studentsPage"/>
	</select>

	<select id="findByUserId" resultMap="ApplyInfo">
		SELECT * FROM apply_info WHERE user_id_ = #{userId}
	</select>
	<select id="findUserByClass" resultMap="ApplyInfo">
		SELECT * FROM apply_info
		<where>
			<if test="classId != null">
				class_id_ = #{classId}
			</if>
			<if test="status != null">
				status_ = #{status}
			</if>
		</where>
	</select>
</mapper>
