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

    <resultMap id="MemberMap" type="com.sv.dto.oms.MemberDTO">
		<id column="id" property="id" jdbcType="INTEGER"/>
		<result column="nickname" property="nickname"  jdbcType="VARCHAR" />
		<result column="avatar" property="avatar"  jdbcType="VARCHAR" />
		<result column="status" property="status"  jdbcType="TINYINT" />
		<result column="ban_type" property="banType"></result>
		<result column="invite_code" property="inviteCode"  jdbcType="VARCHAR" />
		<result column="created_id" property="createdId"  jdbcType="INTEGER" />
		<result column="modified_id" property="modifiedId"  jdbcType="INTEGER" />
		<result column="mobile" property="mobile"  jdbcType="VARCHAR" />
		<result column="name" property="name"  jdbcType="VARCHAR" />
		<result column="sex" property="sex"  jdbcType="TINYINT" />
		<result column="age" property="age"  jdbcType="INTEGER" />
		<result column="address" property="address"  jdbcType="VARCHAR" />
		<result column="money" property="money"  jdbcType="DECIMAL" />
		<result column="platform_id" property="platformId"  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" />
		<result column="face_url" property="faceUrl"  jdbcType="VARCHAR" />
    </resultMap>

    <resultMap id="MemberDtoMap" type="com.sv.dto.api.MemberDTO">
		<id column="id" property="id" jdbcType="INTEGER"/>
		<result column="nickname" property="nickname" jdbcType="VARCHAR" />
		<result column="avatar" property="avatar" jdbcType="VARCHAR" />
		<result column="status" property="status" jdbcType="TINYINT" />
		<result column="invite_code" property="inviteCode" jdbcType="VARCHAR" />
		<result column="created_id" property="createdId" jdbcType="INTEGER" />
		<result column="modified_id" property="modifiedId" jdbcType="INTEGER" />
		<result column="mobile" property="mobile" jdbcType="VARCHAR" />
		<result column="name" property="name" jdbcType="VARCHAR" />
		<result column="sex" property="sex" jdbcType="TINYINT" />
		<result column="age" property="age" jdbcType="INTEGER" />
		<result column="address" property="address" jdbcType="VARCHAR" />
		<result column="money" property="money" jdbcType="DECIMAL" />
		<result column="platform_id" property="platformId" 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="MemberFaceInfoMap" type="com.sv.entity.MemberFaceInfo">
		<id column="id" property="id" jdbcType="INTEGER"/>
		<result column="face_id" property="faceId"></result>
		<result column="face_url" property="faceUrl"></result>
		<result column="member_id" property="memberId"></result>
		<result column="created_time" property="createdTime" jdbcType="TIMESTAMP" />
		<result column="modified_time" property="modifiedTime" jdbcType="TIMESTAMP" />
		<result column="deleted" property="deleted" jdbcType="TINYINT" />
		<result column="created_id" property="createdId" jdbcType="INTEGER" />
		<result column="modified_id" property="modifiedId" jdbcType="INTEGER" />
	</resultMap>
	<!--表名 -->
	<sql id="tableName">
		sv_member
	</sql>
	
	<!-- 字段 -->
	<sql id="Field">
		id,
		nickname,
		avatar,
		status,
		ban_type,
		invite_code,
		created_id,
		modified_id,
		mobile,
		name,
		sex,
		age,
		address,
		money,
		platform_id,
		created_time,
		modified_time,
		deleted
	</sql>
	
	<!-- 字段值 -->
	<sql id="FieldValue">
		#{id, jdbcType=INTEGER},
		#{nickname, jdbcType=VARCHAR},
		#{avatar, jdbcType=VARCHAR},
		#{status, jdbcType=TINYINT},
		#{inviteCode, jdbcType=VARCHAR},
		#{createdId, jdbcType=INTEGER},
		#{modifiedId, jdbcType=INTEGER},
		#{mobile, jdbcType=VARCHAR},
		#{name, jdbcType=VARCHAR},
		#{sex, jdbcType=TINYINT},
		#{age, jdbcType=INTEGER},
		#{address, jdbcType=VARCHAR},
		#{money, jdbcType=DECIMAL},
		#{platformId, jdbcType=INTEGER},
		#{createdTime, jdbcType=TIMESTAMP},
		#{modifiedTime, jdbcType=TIMESTAMP},
		#{deleted, jdbcType=TINYINT}
	</sql>

	<!-- 查询全部记录 -->
    <select id="findAll" resultMap="MemberMap">
        SELECT
        	sm.id,
			sm.nickname,
			sm.avatar,
			sm.status,
			sm.invite_code,
			sm.created_id,
			sm.modified_id,
			sm.mobile,
			sm.name,
			sm.sex,
			sm.age,
			sm.address,
			sm.money,
			sm.platform_id,
			sm.created_time,
			sm.modified_time,
			sm.deleted,
			smfi.face_url
        FROM
			sv_member AS sm
		LEFT JOIN sv_member_face_info AS smfi ON sm.id = smfi.member_id
		WHERE
			sm.deleted = 0
    </select>
	
	<!-- 新增-->
	<insert id="insert" parameterType="com.sv.entity.Member" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO
			<include refid="tableName"></include>
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
				id,
            </if>
            <if test="nickname != null">
				nickname,
            </if>
            <if test="avatar != null">
				avatar,
            </if>
            <if test="status != null">
				status,
            </if>
            <if test="inviteCode != null">
				invite_code,
            </if>
            <if test="createdId != null">
				created_id,
            </if>
            <if test="modifiedId != null">
				modified_id,
            </if>
            <if test="mobile != null">
				mobile,
            </if>
            <if test="name != null">
                name,
            </if>
            <if test="sex != null">
				sex,
            </if>
            <if test="age != null">
				age,
            </if>
            <if test="address != null">
				address,
            </if>
            <if test="money != null">
				money,
            </if>
            <if test="platformId != null">
				platform_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="nickname != null">
				#{nickname},
            </if>
            <if test="avatar != null">
				#{avatar},
            </if>
            <if test="status != null">
				#{status},
            </if>
            <if test="inviteCode != null">
				#{inviteCode},
            </if>
            <if test="createdId != null">
				#{createdId},
            </if>
            <if test="modifiedId != null">
				#{modifiedId},
            </if>
            <if test="mobile != null">
				#{mobile},
            </if>
            <if test="name != null">
				#{name},
            </if>
            <if test="sex != null">
				#{sex},
            </if>
            <if test="age != null">
				#{age},
            </if>
            <if test="address != null">
				#{address},
            </if>
            <if test="money != null">
				#{money},
            </if>
            <if test="platformId != null">
				#{platformId},
            </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.Member">
		UPDATE
			<include refid="tableName"></include>
        <set>
			<if test="nickname != null">
				nickname = #{nickname},
			</if>
			<if test="avatar != null">
				avatar = #{avatar},
			</if>
			<if test="status != null">
				status = #{status},
			</if>
			<if test="inviteCode != null">
				invite_code = #{inviteCode},
			</if>
			<if test="createdId != null">
				created_id = #{createdId},
			</if>
			<if test="modifiedId != null">
				modified_id = #{modifiedId},
			</if>
			<if test="mobile != null">
				mobile = #{mobile},
			</if>
			<if test="name != null">
                name = #{name},
			</if>
			<if test="sex != null">
				sex = #{sex},
			</if>
            age = #{age},
			<if test="address != null">
				address = #{address},
			</if>
			<if test="money != null">
				money = #{money},
			</if>
			<if test="platformId != null">
				platform_id = #{platformId},
			</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>

	<update id="updateBanStatus">
		UPDATE <include refid="tableName"></include>
		set ban_status = #{banStatus}  WHERE id = #{id}
	</update>
	<!-- 通过编号查询 -->
	<select id="findByIds" parameterType="java.lang.Integer" resultMap="MemberMap">
		SELECT
		sm.id,
		sm.nickname,
		sm.avatar,
		sm.status,
		sm.invite_code,
		sm.created_id,
		sm.modified_id,
		sm.mobile,
		sm.name,
		sm.sex,
		sm.age,
		sm.address,
		sm.money,
		sm.platform_id,
		sm.created_time,
		sm.modified_time,
		sm.deleted,
		smfi.face_url
		FROM
		sv_member AS sm
		LEFT JOIN sv_member_face_info AS smfi ON sm.id = smfi.member_id
		WHERE sm.id = #{id}
	</select>
	
	<!--api 通过编号查询 -->
	<select id="findById" parameterType="java.lang.Integer" resultMap="MemberMap">
		SELECT
		<include refid="Field"></include>
		FROM
		<include refid="tableName"></include>
		WHERE id = #{id}
	</select>

	<!-- api -->
	<select id="findByMember" parameterType="java.lang.Integer" resultMap="MemberMap">
		SELECT
			id,
			money,
			nickname,
			avatar,
			status,
			deleted
		FROM
			<include refid="tableName"></include>
		WHERE id = #{id}
	</select>

    <!--更新用户状态-->
    <update id="updateStatus">
        UPDATE
        	<include refid="tableName"></include>
        SET
        	status = #{status, jdbcType=TINYINT}
        WHERE
        	id
        IN
        <foreach collection="ids" item="id" index="index" open="(" close=")" separator=",">
			#{id}
        </foreach>
    </update>

	<!--根据手机号码统计数量-->
	<select id="countByMobile" resultType="Integer">
		SELECT
		COUNT(1)
		FROM
		<include refid="tableName"></include>
		WHERE
		deleted = 0 AND mobile = #{mobile, jdbcType=VARCHAR}
		<if test="excludeId != null">
			AND id != #{excludeId, jdbcType=INTEGER}
		</if>
	</select>

	<!-- 校验邀请码是否存在-->
	<select id="checkInviteCode" parameterType="java.lang.String" resultType="java.lang.Integer">
		SELECT
			COUNT(0)
		FROM
			<include refid="tableName"></include>
		WHERE
			invite_code = #{inviteCode, jdbcType=VARCHAR}
		AND deleted = 0
	</select>

	<!-- 根据手机号码查找用户 -->
	<select id="findByMobile" parameterType="java.lang.String" resultMap="MemberMap">
		select
			nickname,
			avatar,
			id,
			status,
			platform_id
		FROM
		  sv_member
		where
			mobile = #{mobile}
		AND deleted = 0
	</select>

    <!-- 修改用户余额 -->
    <update id="updateMoney">
        UPDATE
          <include refid="tableName"></include>
        SET
          money = money + #{price}
        where id = #{id}
    </update>

	<select id="findMemberFaceInfo" resultMap="MemberFaceInfoMap">
		SELECT * from sv_member_face_info
		where member_id = #{memberId} and deleted = 0
	</select>

	<insert id="insertMemberFaceInfo">
		INSERT INTO sv_member_face_info
		(member_id,face_url,face_id,platform_id)
		VALUES (#{memberId},#{faceUrl},#{faceId},#{platformId})
	</insert>

	<update id="updateMemberFaceInfo">
		UPDATE sv_member_face_info
		SET face_url = #{faceUrl},face_id = #{faceId},modified_time = #{modifiedTime}
		where member_id = #{memberId} and deleted = 0
	</update>

	<select id="findByFaceId" resultMap="MemberMap">
		select m.* from sv_member m
		LEFT JOIN sv_member_face_info f
		on f.member_id = m.id
		where f.face_id = #{faceId} and m.deleted = 0
	</select>

	<select id="countMoneyGtPrice" resultType="java.lang.Integer">
		SELECT count(0) from sv_member
		WHERE money >= #{price} and id = #{memberId} and deleted = 0
	</select>
</mapper>