<?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.StudentCloudCoachPaymentDetailsDao">
	
	<resultMap type="com.ym.mec.biz.dal.entity.StudentCloudCoachPaymentDetails" id="StudentCloudCoachPaymentDetails">
		<result column="id_" property="id" />
		<result column="cloud_coach_payment_program_id_" property="cloudCoachPaymentProgramId" />
		<result column="user_id_" property="userId" />
		<result column="username_" property="username" />
		<result column="phone_" property="phone" />
		<result column="payment_order_id_" property="paymentOrderId" />
		<result column="payment_status_" property="paymentStatus" />
		<result column="pay_time_" property="payTime" />
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
		<result column="tenant_id_" property="tenantId" />
	</resultMap>
	
	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="StudentCloudCoachPaymentDetails" >
		SELECT * FROM student_cloud_coach_payment_details WHERE id_ = #{id} 
	</select>
	
	<!-- 全查询 -->
	<select id="findAll" resultMap="StudentCloudCoachPaymentDetails">
		SELECT * FROM student_cloud_coach_payment_details ORDER BY id_
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.StudentCloudCoachPaymentDetails" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO student_cloud_coach_payment_details (user_id_,payment_order_id_,
		payment_status_,create_time_,update_time_,tenant_id_,cloud_coach_payment_program_id_)
		VALUES(#{userId},#{paymentOrderId},#{paymentStatus},NOW(),NOW(),#{tenantId},#{cloudCoachPaymentProgramId})
	</insert>
	<insert id="batchInsert">
		INSERT INTO student_cloud_coach_payment_details (user_id_,payment_status_,create_time_,update_time_,tenant_id_,cloud_coach_payment_program_id_)VALUES
		<foreach collection="userIds" separator="," item="userId">
			(#{userId},0,NOW(),NOW(),#{tenantId},#{cloudCoachPaymentProgramId})
		</foreach>
	</insert>

	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.ym.mec.biz.dal.entity.StudentCloudCoachPaymentDetails">
		UPDATE student_cloud_coach_payment_details <set>
		<if test="userId != null">
		user_id_ = #{userId},
		</if>
		<if test="paymentStatus != null">
		payment_status_ = #{paymentStatus},
		</if>
		<if test="tenantId != null">
		tenant_id_ = #{tenantId},
		</if>
		<if test="updateTime != null">
		update_time_ = #{updateTime},
		</if>
		<if test="paymentOrderId != null">
		payment_order_id_ = #{paymentOrderId},
		</if>
		update_time_ = NOW()
		</set> WHERE id_ = #{id}
	</update>
	<update id="updatePaymentStatus">
		UPDATE student_cloud_coach_payment_details SET payment_status_ = 1,update_time_ = NOW()
		WHERE cloud_coach_payment_program_id_ = #{cloudCoachPaymentProgramId}
	</update>

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

	<sql id="queryPageSql">
		<where>
			<if test="tenantId != null">
				AND sccpd.tenant_id_ = #{tenantId}
			</if>
			<if test="paymentId != null">
				AND sccpd.cloud_coach_payment_program_id_ = #{paymentId}
			</if>
		</where>
	</sql>
	<!-- 分页查询 -->
	<select id="queryPage" resultMap="StudentCloudCoachPaymentDetails" parameterType="map">
		SELECT sccpd.*,su.username_,su.phone_,spo.pay_time_ FROM student_cloud_coach_payment_details sccpd
		LEFT JOIN sys_user su ON  su.id_ = sccpd.user_id_
		LEFT JOIN student_payment_order spo ON spo.music_group_id_ = sccpd.id_
		AND spo.user_id_ = sccpd.user_id_ AND spo.type_ = 'MEMBER' AND spo.status_ = 'SUCCESS'
		<include refid="queryPageSql"/>
		GROUP BY sccpd.id_
		ORDER BY sccpd.id_
		<include refid="global.limit"/>
	</select>
	
	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(DISTINCT sccpd.id_) FROM student_cloud_coach_payment_details sccpd
		<include refid="queryPageSql"/>
	</select>
	<select id="findByUserId" resultMap="StudentCloudCoachPaymentDetails">
		SELECT * FROM student_cloud_coach_payment_details WHERE cloud_coach_payment_program_id_ = #{paymentId} AND user_id_ = #{userId} LIMIT 1
	</select>
	<select id="findUserByProgramId" resultType="java.lang.Integer">
		SELECT user_id_ FROM student_cloud_coach_payment_details WHERE cloud_coach_payment_program_id_ = #{programId}
	</select>
</mapper>
