<?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.mmc.iuav.user.dao.UserSubInfoDao">

    <resultMap id="userSubInfoResultMap" type="com.mmc.iuav.user.entity.UserSubInfoDO">
        <id property="id" column="id"/>
        <result property="openId" column="open_id"/>
        <result property="unionId" column="union_id"/>
        <result property="nickName" column="nick_name"/>
        <result property="regStatus" column="reg_status"/>
        <result property="deleted" column="is_deleted"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <select id="getUserSubInfo" resultMap="userSubInfoResultMap" parameterType="String">
		select u.id,u.open_id,u.union_id,u.nick_name,u.reg_status,
		u.is_deleted,u.create_time,u.update_time
		from user_sub_info u
		where u.open_id = #{openId}
	</select>

    <select id="getUserSubInfoById" resultMap="userSubInfoResultMap" parameterType="Integer">
		select usi.id,usi.open_id,usi.union_id,usi.nick_name,usi.is_deleted
		from user_account ua
		inner join user_sub_info usi on usi.union_id=ua.union_id
		where usi.is_deleted=0 and ua.is_deleted=0
		and ua.user_account_id = #{userAccountId}
	</select>

    <select id="getUserSubInfoByUnionId" resultMap="userSubInfoResultMap" parameterType="String">
		select usi.id,usi.open_id,usi.union_id,usi.nick_name,usi.is_deleted,usi.reg_status
		from  user_sub_info usi
		where usi.union_id = #{unionId} and usi.is_deleted=0
	</select>

    <select id="countUserSubInfo" resultType="Integer" parameterType="String">
		select count(*)
		from user_sub_info u
		where u.open_id = #{openId} and is_deleted = 0
	</select>

    <insert id="insertUserSubInfo" useGeneratedKeys="true"
            keyProperty="id" parameterType="com.mmc.iuav.user.entity.UserSubInfoDO">
		insert into user_sub_info
		(
		open_id,union_id,nick_name,reg_status,is_deleted,create_time,update_time
		)
		values
		(
		#{openId},#{unionId},#{nickName},#{regStatus},#{deleted},#{createTime},#{updateTime}
		)
	</insert>

    <update id="removeUserSubInfo" parameterType="String">
		update user_sub_info
		set is_deleted = 1
		where open_id = #{openId}
	</update>

    <update id="updateUserSubInfo"
            parameterType="com.mmc.iuav.user.entity.UserSubInfoDO">
        update user_sub_info
        <set>
            <if test="unionId != null and unionId != '' ">
                union_id = #{unionId},
            </if>
            <if test="nickName != null">
                nick_name = #{nickName},
            </if>
            <if test="regStatus != null">
                reg_status = #{regStatus},
            </if>
        </set>
        where open_id = #{openId}
    </update>

</mapper>