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

    <resultMap id="MemberAuthMap" type="com.sv.entity.MemberAuth">
		<id column="id" property="id" jdbcType="INTEGER"/>
		<result column="auth_id" property="authId"  jdbcType="VARCHAR" />
		<result column="mobile" property="mobile"  jdbcType="VARCHAR" />
		<result column="member_id" property="memberId"  jdbcType="INTEGER" />
		<result column="type" property="type"  jdbcType="TINYINT" />
		<result column="created_time" property="createdTime"  jdbcType="TIMESTAMP" />
		<result column="modified_time" property="modifiedTime"  jdbcType="TIMESTAMP" />
		<result column="created_id" property="createdId"  jdbcType="INTEGER" />
		<result column="modified_id" property="modifiedId"  jdbcType="INTEGER" />
		<result column="deleted" property="deleted"  jdbcType="TINYINT" />
		<result column="platform_id" property="platformId"  jdbcType="INTEGER" />
    </resultMap>

    <resultMap id="MemberAuthDtoMap" type="com.sv.dto.api.MemberAuthDTO">
		<id column="id" property="id" jdbcType="INTEGER"/>
		<result column="auth_id" property="authId" jdbcType="VARCHAR" />
		<result column="mobile" property="mobile" jdbcType="VARCHAR" />
		<result column="member_id" property="memberId" jdbcType="INTEGER" />
		<result column="type" property="type" jdbcType="TINYINT" />
		<result column="created_time" property="createdTime" jdbcType="TIMESTAMP" />
		<result column="modified_time" property="modifiedTime" jdbcType="TIMESTAMP" />
		<result column="created_id" property="createdId" jdbcType="INTEGER" />
		<result column="modified_id" property="modifiedId" jdbcType="INTEGER" />
		<result column="deleted" property="deleted" jdbcType="TINYINT" />
    </resultMap>
	
	<!--表名 -->
	<sql id="tableName">
		sv_member_auth
	</sql>
	
	<!-- 字段 -->
	<sql id="Field">
		id,
		auth_id,
		mobile,
		member_id,
		type,
		created_time,
		modified_time,
		created_id,
		modified_id,
		deleted,
		platform_id
	</sql>
	
	<!-- 字段值 -->
	<sql id="FieldValue">
		#{id, jdbcType=INTEGER},
		#{authId, jdbcType=VARCHAR},
		#{mobile, jdbcType=VARCHAR},
		#{memberId, jdbcType=INTEGER},
		#{type, jdbcType=TINYINT},
		#{createdTime, jdbcType=TIMESTAMP},
		#{modifiedTime, jdbcType=TIMESTAMP},
		#{createdId, jdbcType=INTEGER},
		#{modifiedId, jdbcType=INTEGER},
		#{deleted, jdbcType=TINYINT},
		#{platform_id, jdbcType=INTEGER}
	</sql>

	<!-- 查询全部记录 -->
    <select id="findAll" resultMap="MemberAuthMap">
        SELECT
        	<include refid="Field"></include>
        FROM
        	<include refid="tableName"></include>
		WHERE
			deleted = 0
    </select>
	
	<!-- 新增-->
	<insert id="insert" parameterType="com.sv.entity.MemberAuth" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO
			<include refid="tableName"></include>
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
				id,
            </if>
            <if test="authId != null">
				auth_id,
            </if>
            <if test="mobile != null">
				mobile,
            </if>
            <if test="memberId != null">
				member_id,
            </if>
            <if test="type != null">
				type,
            </if>
            <if test="createdTime != null">
				created_time,
            </if>
            <if test="modifiedTime != null">
				modified_time,
            </if>
            <if test="createdId != null">
				created_id,
            </if>
            <if test="modifiedId != null">
				modified_id,
            </if>
            <if test="deleted != null">
				deleted,
            </if>
            <if test="platformId != null">
				platform_id,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
				#{id},
            </if>
            <if test="authId != null">
				#{authId},
            </if>
            <if test="mobile != null">
				#{mobile},
            </if>
            <if test="memberId != null">
				#{memberId},
            </if>
            <if test="type != null">
				#{type},
            </if>
            <if test="createdTime != null">
				#{createdTime},
            </if>
            <if test="modifiedTime != null">
				#{modifiedTime},
            </if>
            <if test="createdId != null">
				#{createdId},
            </if>
            <if test="modifiedId != null">
				#{modifiedId},
            </if>
            <if test="deleted != null">
				#{deleted},
            </if>
            <if test="platformId != null">
				#{platformId},
            </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.MemberAuth">
		UPDATE
			<include refid="tableName"></include>
        <set>
			<if test="authId != null">
				auth_id = #{authId},
			</if>
			<if test="mobile != null">
				mobile = #{mobile},
			</if>
			<if test="memberId != null">
				member_id = #{memberId},
			</if>
			<if test="type != null">
				type = #{type},
			</if>
			<if test="createdTime != null">
				created_time = #{createdTime},
			</if>
			<if test="modifiedTime != null">
				modified_time = #{modifiedTime},
			</if>
			<if test="createdId != null">
				created_id = #{createdId},
			</if>
			<if test="modifiedId != null">
				modified_id = #{modifiedId},
			</if>
			<if test="deleted != null">
				deleted = #{deleted},
			</if>
			<if test="platformId != null">
				platform_id = #{platformId},
			</if>
        </set>
        WHERE id = #{id}
	</update>
	
	<!-- 通过编号查询 -->
	<select id="findById" parameterType="java.lang.Integer" resultMap="MemberAuthMap">
		SELECT
			<include refid="Field"></include>
		FROM
			<include refid="tableName"></include>
		WHERE id = #{id}
	</select>

	<!-- 查询登录认证 -->
	<select id="findByMobile" resultMap="MemberAuthMap">
		SELECT
		<include refid="Field"></include>
		FROM
		<include refid="tableName"></include>
		WHERE mobile = #{mobile, jdbcType=VARCHAR} AND `type` = 2 AND deleted = 0
	</select>

	<!-- 微信登录判断用户是否注册过 -->
	<select id="findByAuthId" parameterType="java.lang.String" resultMap="MemberAuthMap">
		select
			member_id,
			mobile,
			auth_id
		FROM
			<include refid="tableName"></include>
		WHERE auth_id = #{openId} AND `type` = 1 AND deleted = 0
	</select>

	<!-- 查询用户的登录方式 -->
	<select id="checkLoginWay" parameterType="java.lang.Integer" resultMap="MemberAuthMap">
		select
			auth_id,
			mobile,
			`type`,
			member_id
		FROM
			<include refid="tableName"></include>
		WHERE member_id = #{memberId} AND `type` = #{type} AND deleted = 0
	</select>

	<!--根据手机号码统计数量-->
	<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>

	<!-- api 通话用户Id查找用户auth_id -->
	<select id="findByMemberId" resultMap="MemberAuthMap">
		select auth_id
		FROM
			<include refid="tableName"></include>
		WHERE
			member_id = #{memberId}
		and deleted = 0
		and `type` = 1
	</select>

	<!-- api 判断用户是否用手机登陆过 -->
	<select id="findByType" resultMap="MemberAuthMap">
		SELECT
			mobile,
			`type`,
			member_id
		FROM
			sv_member_auth
		WHERE
			member_id = #{memberId}
		AND `type` = 2
		AND deleted = 0
	</select>

	<!-- api 修改手机号 -->
	<update id="updateMobile">
		UPDATE sv_member_auth
		SET mobile = #{mobile}
		WHERE
			member_id = #{memberId}
		AND mobile is not null
	</update>
</mapper>