<?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.MemberEnterVenueLogMapper">
    <resultMap id="MemberEnterVeneuLogMap" type="com.sv.entity.MemberEnterVenueLog">
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="member_id" property="memberId"/>
        <result column="veneu_type" property="veneuType"/>
        <result column="venue_id" property="venueId"/>
        <result column="order_sn" property="orderSn"/>
        <result column="member_card_id" property="memberCardId"/>
        <result column="pay_type" property="payType"/>
        <result column="platform_id" property="platformId"/>
        <result column="created_id" property="createdId"/>
        <result column="modified_id" property="modifiedId"/>
        <result column="created_time" property="createdTime"/>
        <result column="modified_time" property="modifiedTime"/>
        <result column="deleted" property="deleted"/>
    </resultMap>

    <resultMap id="MemberEnterVeneuLogOmsDTOMap" type="com.sv.dto.oms.MemberEnterVeneuLogOmsDTO"
               extends="MemberEnterVeneuLogMap">
        <result column="price" property="price"/>
        <result column="nickname" property="nickname"/>
        <result column="name" property="name"/>
        <result column="trade_sn" property="tradeSn"/>
        <result column="pay_status" property="payStatus"/>
        <result column="mobile" property="mobile"/>
        <result column="face_url" property="faceUrl"></result>
    </resultMap>

    <!--表名 -->
    <sql id="tableName">
		sv_member_enter_veneu_log
	</sql>

    <!-- 字段 -->
    <sql id="Field">
		id,
		member_id,
		veneu_type,
		venue_id,
		order_sn,
		member_card_id,
		pay_type,
		platform_id,
		created_id,
		modified_id,
		created_time,
		modified_time,
		deleted,type
	</sql>

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

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

    <!-- 查询全部记录(后台方法) -->

    <select id="findAlls" resultMap="MemberEnterVeneuLogOmsDTOMap">
        SELECT
        smevl.id,
        smevl.member_id,
        smevl.veneu_type,
        smevl.venue_id,
        smevl.order_sn,
        smevl.member_card_id,
        smevl.pay_type,
        smevl.platform_id,
        smevl.created_id,
        smevl.modified_id,
        smevl.created_time,
        smevl.modified_time,
        smevl.deleted,
        smevl.type,
        so.price,
        sm.nickname,
        sm.mobile,
        face.face_url,
        sv.name,
        so.trade_sn,
        so.pay_status
        FROM
        sv_member_enter_veneu_log AS smevl
        LEFT JOIN sv_order AS so ON smevl.order_sn = so.order_sn
        LEFT JOIN sv_member AS sm ON smevl.member_id = sm.id
        LEFT JOIN sv_member_face_info as face on face.member_id = sm.id
        LEFT JOIN sv_venue AS sv ON smevl.venue_id = sv.id
        WHERE
        smevl.deleted = 0
        AND sm.deleted = 0
        AND sv.deleted = 0
        <if test="payStatus != null">
            AND so.pay_status = #{payStatus}
        </if>
        <if test="memberId != null">
            AND sm.id = #{memberId}
        </if>
        <if test="orderSn != ''">
            AND smevl.order_sn = #{orderSn}
        </if>
        <if test="starTime != ''">
            AND so.created_time &gt;= #{starTime}
        </if>
        <if test="endTime != ''">
            AND so.created_time &lt;= #{endTime}
        </if>
        <if test="payType != null">
            AND smevl.pay_type = #{payType}
        </if>
        <if test="venueId != null">
            AND smevl.venue_id = #{venueId}
        </if>
    </select>

    <!-- 新增-->
    <insert id="insert" parameterType="com.sv.entity.MemberEnterVenueLog" useGeneratedKeys="true" keyProperty="id">

        INSERT INTO
        <include refid="tableName"></include>
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="memberId != null">
                member_id,
            </if>
            <if test="veneuType != null">
                veneu_type,
            </if>
            <if test="venueId != null">
                venue_id,
            </if>
            <if test="orderSn != null">
                order_sn,
            </if>
            <if test="memberCardId != null">
                member_card_id,
            </if>
            <if test="payType != null">
                pay_type,
            </if>
            <if test="platformId != null">
                platform_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>
            <if test="type != null">
                type,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id},
            </if>
            <if test="memberId != null">
                #{memberId},
            </if>
            <if test="veneuType != null">
                #{veneuType},
            </if>
            <if test="venueId != null">
                #{venueId},
            </if>
            <if test="orderSn != null">
                #{orderSn},
            </if>
            <if test="memberCardId != null">
                #{memberCardId},
            </if>
            <if test="payType != null">
                #{payType},
            </if>
            <if test="platformId != null">
                #{platformId},
            </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>
            <if test="type != null">
                #{type},
            </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.MemberEnterVenueLog">
        UPDATE
        <include refid="tableName"></include>
        <set>
            <if test="memberId != null">
                member_id = #{memberId},
            </if>
            <if test="veneuType != null">
                veneu_type = #{veneuType},
            </if>
            <if test="venueId != null">
                venue_id = #{venueId},
            </if>
            <if test="orderSn != null">
                order_sn = #{orderSn},
            </if>
            <if test="memberCardId != null">
                member_card_id = #{memberCardId},
            </if>
            <if test="payType != null">
                pay_type = #{payType},
            </if>
            <if test="platformId != null">
                platform_id = #{platformId},
            </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="MemberEnterVeneuLogOmsDTOMap">
		SELECT
			smevl.id,
			smevl.member_id,
			smevl.veneu_type,
			smevl.venue_id,
			smevl.order_sn,
			smevl.member_card_id,
			smevl.pay_type,
			smevl.platform_id,
			smevl.created_id,
			smevl.modified_id,
			so.created_time,
			smevl.modified_time,
			smevl.deleted,
			so.price,
			sm.nickname,
			sv.name,
			so.trade_sn,
			so.pay_status
		FROM
			sv_member_enter_veneu_log AS smevl
		LEFT JOIN sv_order AS so ON smevl.order_sn = so.order_sn
		LEFT JOIN sv_member AS sm ON smevl.member_id = sm.id
		LEFT JOIN sv_venue AS sv ON smevl.venue_id = sv.id
		WHERE smevl.id = #{id}
	</select>

    <select id="findLastByMember" resultMap="MemberEnterVeneuLogMap">
        SELECT id,member_id,venue_id,created_time,member_card_id,type
        FROM sv_member_enter_veneu_log
        WHERE member_id = #{memberId} and type = 0 and  venue_id = #{venueId} and deleted = 0 order by created_time desc LIMIT 1
    </select>

    <select id="findLastNoTypeByMember" resultMap="MemberEnterVeneuLogMap">
        SELECT id,member_id,venue_id,created_time,member_card_id,type
        FROM sv_member_enter_veneu_log
        WHERE member_id = #{memberId} and  venue_id = #{venueId} and deleted = 0 order by created_time desc LIMIT 1
    </select>

    <select id="findMemberEnterLogByDate" resultMap="MemberEnterVeneuLogMap">
        SELECT id,member_id,venue_id,created_time,member_card_id,type
        FROM sv_member_enter_veneu_log
        WHERE member_id = #{memberId} and venue_id = #{venueId} and DATE(created_time) = #{date} and deleted = 0 order by created_time desc
    </select>

    <select id="countByMemberCardId" resultType="java.lang.Integer">
        SELECT count(0)
        FROM sv_member_enter_veneu_log where member_card_id = #{memberCardId}
        and DATE_FORMAT(created_time,'%X-%m-%d') = #{today} and deleted = 0
    </select>

</mapper>