<?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.sv.mapper.MemberMoneyLogMapper">

	<resultMap id="MemberMoneyLogMap" type="com.sv.entity.MemberMoneyLog">
		<id column="id" property="id" jdbcType="INTEGER"/>
		<result column="type" property="type"  jdbcType="TINYINT" />
		<result column="money" property="money"  jdbcType="DECIMAL" />
		<result column="member_id" property="memberId"  jdbcType="INTEGER" />
		<result column="platform_id" property="platformId"  jdbcType="INTEGER" />
		<result column="pay_type" property="payType"  jdbcType="INTEGER" />
		<result column="card_type" property="cardType"  jdbcType="VARCHAR" />
		<result column="venue_id" property="venueId"  jdbcType="INTEGER" />
		<result column="venue_type" property="venueType"  jdbcType="INTEGER" />
		<result column="lesson_id" property="lessonId"  jdbcType="INTEGER" />
		<result column="created_id" property="createdId"  jdbcType="INTEGER" />
		<result column="modified_id" property="modifiedId"  jdbcType="INTEGER" />
		<result column="created_time" property="createdTime"  jdbcType="TIMESTAMP" />
		<result column="modified_time" property="modifiedTime"  jdbcType="TIMESTAMP" />
		<result column="deleted" property="deleted"  jdbcType="TINYINT" />
	</resultMap>

	<resultMap id="MemberMoneyLogDtoMap" type="com.sv.dto.api.MemberMoneyLogDTO">
		<id column="id" property="id" jdbcType="INTEGER"/>
		<result column="type" property="type" jdbcType="TINYINT" />
		<result column="money" property="money" jdbcType="DECIMAL" />
		<result column="member_id" property="memberId" jdbcType="INTEGER" />
		<result column="platform_id" property="platformId" jdbcType="INTEGER" />
		<result column="pay_type" property="payType"  jdbcType="INTEGER" />
		<result column="card_type" property="cardType"  jdbcType="VARCHAR" />
		<result column="venue_id" property="venueId"  jdbcType="INTEGER" />
		<result column="venue_type" property="venueType"  jdbcType="INTEGER" />
		<result column="lesson_id" property="lessonId"  jdbcType="INTEGER" />
		<result column="created_id" property="createdId" jdbcType="INTEGER" />
		<result column="modified_id" property="modifiedId" jdbcType="INTEGER" />
		<result column="created_time" property="createdTime" jdbcType="TIMESTAMP" />
		<result column="modified_time" property="modifiedTime" jdbcType="TIMESTAMP" />
		<result column="deleted" property="deleted" jdbcType="TINYINT" />
		<association property="venueLesson" select="com.sv.mapper.VenueLessonMapper.findMemberBillById" column="lesson_id"></association>
		<association property="venue" select="com.sv.mapper.VenueMapper.findAddress" column="venue_id"></association>
	</resultMap>

    <resultMap id="MemberMoneyLogOmdDtoMap" type="com.sv.dto.api.MemberMoneyLogDTO" extends="MemberMoneyLogMap">
        <result column="pay_type" property="payType" />
        <result column="nickname" property="nickname" />
    </resultMap>
	<!--表名 -->
	<sql id="tableName">
		sv_member_money_log
	</sql>
	
	<!-- 字段 -->
	<sql id="Field">
		id,
		type,
		money,
		member_id,
		platform_id,
		pay_type,
		card_type,
		venue_id,
		venue_type,
		lesson_id,
		created_id,
		modified_id,
		created_time,
		modified_time,
		deleted
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{id, jdbcType=INTEGER},
		#{type, jdbcType=TINYINT},
		#{money, jdbcType=DECIMAL},
		#{memberId, jdbcType=INTEGER},
		#{platformId, jdbcType=INTEGER},
		#{payType, jdbcType=INTEGER},
		#{cardType, jdbcType=VARCHAR},
		#{venueId, jdbcType=INTEGER},
		#{venueType, jdbcType=INTEGER},
		#{lessonId, jdbcType=INTEGER},
		#{createdId, jdbcType=INTEGER},
		#{modifiedId, jdbcType=INTEGER},
		#{createdTime, jdbcType=TIMESTAMP},
		#{modifiedTime, jdbcType=TIMESTAMP},
		#{deleted, jdbcType=TINYINT}
	</sql>

	<!-- 查询全部记录 -->
	<select id="findAll" resultMap="MemberMoneyLogMap">
		SELECT
		<include refid="Field"></include>
		FROM
		<include refid="tableName"></include>
		WHERE
		deleted = 0
	</select>

	<!-- 查询全部记录 -->
	<select id="findAlls" resultMap="MemberMoneyLogOmdDtoMap">
		SELECT
		smml.id,
		smml.type,
		smml.money,
		smml.member_id,
		smml.platform_id,
		smml.pay_type,
		smml.card_type,
		smml.venue_id,
		smml.venue_type,
		smml.lesson_id,
		smml.created_id,
		smml.modified_id,
		smml.created_time,
		smml.modified_time,
		smml.deleted,
		sm.nickname
		FROM
		sv_member_money_log AS smml LEFT JOIN sv_member AS sm ON smml.member_id = sm.id
		WHERE
		smml.deleted = 0 AND
		smml.pay_type != 3 AND
		smml.deleted = 0
		<if test="memberId != null">
			AND smml.member_id  = #{memberId}
		</if>
	</select>

	<!-- 新增-->
	<insert id="insert" parameterType="com.sv.dto.api.MemberMoneyLogDTO" useGeneratedKeys="true" keyProperty="id">
		INSERT INTO
		<include refid="tableName"></include>
		<trim prefix="(" suffix=")" suffixOverrides=",">
			<if test="id != null">
				id,
			</if>
			<if test="type != null">
				type,
			</if>
			<if test="money != null">
				money,
			</if>
			<if test="memberId != null">
				member_id,
			</if>
			<if test="platformId != null">
				platform_id,
			</if>
			<if test="payType != null">
				pay_type,
			</if>
			<if test="cardType != null">
				card_type,
			</if>
			<if test="venueId != null">
				venue_id,
			</if>
			<if test="venueType != null">
				venue_type,
			</if>
			<if test="lessonId != null">
				lesson_id,
			</if>
			<if test="createdId != null">
				created_id,
			</if>
			<if test="modifiedId != null">
				modified_id,
			</if>
			<if test="createdTime != null">
				created_time,
			</if>
			<if test="modifiedTime != null">
				modified_time,
			</if>
			<if test="deleted != null">
				deleted,
			</if>
		</trim>
		<trim prefix="values (" suffix=")" suffixOverrides=",">
			<if test="id != null">
				#{id},
			</if>
			<if test="type != null">
				#{type},
			</if>
			<if test="money != null">
				#{money},
			</if>
			<if test="memberId != null">
				#{memberId},
			</if>
			<if test="platformId != null">
				#{platformId},
			</if>
			<if test="payType != null">
				#{payType},
			</if>
			<if test="cardType != null">
				#{cardType},
			</if>
			<if test="venueId != null">
				#{venueId},
			</if>
			<if test="venueType != null">
				#{venueType},
			</if>
			<if test="lessonId != null">
				#{lessonId},
			</if>
			<if test="createdId != null">
				#{createdId},
			</if>
			<if test="modifiedId != null">
				#{modifiedId},
			</if>
			<if test="createdTime != null">
				#{createdTime},
			</if>
			<if test="modifiedTime != null">
				#{modifiedTime},
			</if>
			<if test="deleted != null">
				#{deleted},
			</if>
		</trim>
	</insert>

	<!-- 删除-->
	<delete id="delete" parameterType="java.lang.Integer">
		UPDATE
		<include refid="tableName"></include>
		SET
		deleted = 1
		WHERE
		id = #{id}
	</delete>

	<!-- 批量删除 -->
	<delete id="deleteByIds">
		UPDATE
		<include refid="tableName"></include>
		SET
		deleted = 1
		WHERE
		id IN
		<foreach item="item" index="index" collection="array" open="(" separator="," close=")">
			#{item}
		</foreach>
	</delete>

	<!-- 更新记录 -->
	<update id="update" parameterType="com.sv.entity.MemberMoneyLog">
		UPDATE
		<include refid="tableName"></include>
		<set>
			<if test="type != null">
				type = #{type},
			</if>
			<if test="money != null">
				money = #{money},
			</if>
			<if test="memberId != null">
				member_id = #{memberId},
			</if>
			<if test="platformId != null">
				platform_id = #{platformId},
			</if>
			<if test="payType != null">
				pay_type = #{payType},
			</if>
			<if test="cardType != null">
				card_type = #{cardType},
			</if>
			<if test="venueId != null">
				venue_id = #{venueId},
			</if>
			<if test="venueType != null">
				venue_type = #{venueType},
			</if>
			<if test="lessonId != null">
				lesson_id = #{lessonId},
			</if>
			<if test="createdId != null">
				created_id = #{createdId},
			</if>
			<if test="modifiedId != null">
				modified_id = #{modifiedId},
			</if>
			<if test="createdTime != null">
				created_time = #{createdTime},
			</if>
			<if test="modifiedTime != null">
				modified_time = #{modifiedTime},
			</if>
			<if test="deleted != null">
				deleted = #{deleted},
			</if>
		</set>
		WHERE id = #{id}
	</update>

	<!-- 通过编号查询 -->
	<select id="findById" parameterType="java.lang.Integer" resultMap="MemberMoneyLogMap">
		SELECT
		<include refid="Field"></include>
		FROM
		<include refid="tableName"></include>
		WHERE id = #{id}
	</select>

	<!-- api 查询用户账单 -->
	<select id="findAllBill" resultMap="MemberMoneyLogDtoMap">
		SELECT
			id,
			`type`,
			money,
			member_id,
			pay_type,
			card_type,
			venue_id,
			venue_type,
			lesson_id,
			created_time
		FROM
			sv_member_money_log
		WHERE
			member_id = #{memberId}
		AND deleted = 0
		ORDER BY
			created_time DESC
	</select>
</mapper>