<?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.ruoyi.bst.userApp.mapper.UserAppMapper">

    <resultMap type="UserAppVO" id="UserAppResult" autoMapping="true"/>

    <sql id="selectUserAppVo">
        select
            bua.id,
            bua.user_id,
            bua.app_id,
            bua.open_id,
            bua.create_time,
            su.nick_name as user_name,
            ba.name as app_name
        from bst_user_app bua
            left join bst_app ba on bua.app_id = ba.id
            left join sys_user su on bua.user_id = su.user_id
    </sql>

    <sql id="searchCondition">
        <if test="query.id != null "> and bua.id = #{query.id}</if>
        <if test="query.userId != null "> and bua.user_id = #{query.userId}</if>
        <if test="query.appId != null "> and bua.app_id = #{query.appId}</if>
        <if test="query.openId != null  and query.openId != ''"> and bua.open_id like concat('%', #{query.openId}, '%')</if>
        <if test="query.userName != null  and query.userName != ''"> and su.nick_name like concat('%', #{query.userName}, '%')</if>
        <if test="query.appName != null  and query.appName != ''"> and ba.name like concat('%', #{query.appName}, '%')</if>
        <if test="query.eqOpenId != null  and query.eqOpenId != ''"> and bua.open_id = #{query.eqOpenId}</if>
        ${query.params.dataScope}
    </sql>

    <select id="selectUserAppList" parameterType="UserAppQuery" resultMap="UserAppResult">
        <include refid="selectUserAppVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectUserAppById" parameterType="Long" resultMap="UserAppResult">
        <include refid="selectUserAppVo"/>
        where bua.id = #{id}
    </select>

    <insert id="insertUserApp" parameterType="UserApp" useGeneratedKeys="true" keyProperty="id">
        insert into bst_user_app
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="appId != null">app_id,</if>
            <if test="openId != null and openId != ''">open_id,</if>
            <if test="createTime != null">create_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="appId != null">#{appId},</if>
            <if test="openId != null and openId != ''">#{openId},</if>
            <if test="createTime != null">#{createTime},</if>
         </trim>
    </insert>

    <update id="updateUserApp" parameterType="UserApp">
        update bst_user_app
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where id = #{data.id}
    </update>

    <sql id="updateColumns">
        <if test="data.userId != null">user_id = #{data.userId},</if>
        <if test="data.appId != null">app_id = #{data.appId},</if>
        <if test="data.openId != null and data.openId != ''">open_id = #{data.openId},</if>
        <if test="data.createTime != null">create_time = #{data.createTime},</if>
    </sql>

    <delete id="deleteUserAppById" parameterType="Long">
        delete from bst_user_app where id = #{id}
    </delete>

    <delete id="deleteUserAppByIds" parameterType="String">
        delete from bst_user_app where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <!-- selectCount --> 

    <select id="selectCount" resultType="int">
        select count(bua.id) 
        from bst_user_app  bua
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>
</mapper>
