<?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.app.mapper.AppMapper">

    <resultMap type="AppVO" id="AppResult" autoMapping="true">
        <result property="config" column="config" typeHandler="com.ruoyi.bst.app.mapper.typehandler.AppConfigJsonTypeHandler"/>
        <result property="ownerIds" column="owner_ids" typeHandler="com.ruoyi.common.mybatis.typehandler.LongSplitListTypeHandler"/>
    </resultMap>

    <sql id="selectAppVo">
        select
            ba.id,
            ba.name,
            ba.type,
            ba.config,
            ba.create_time,
            ba.owner_ids
        from bst_app ba
    </sql>

    <sql id="searchTabele">
        bst_app ba
        left join bst_user_app bua on ba.id = bua.app_id
        left join sys_user su on bua.user_id = su.user_id
    </sql>

    <sql id="searchCondition">
        <if test="query.id != null "> and ba.id = #{query.id}</if>
        <if test="query.name != null  and query.name != ''"> and ba.name like concat('%', #{query.name}, '%')</if>
        <if test="query.type != null  and query.type != ''"> and ba.type = #{query.type}</if>
        <if test="query.ids != null and query.ids.size() > 0">
            and ba.id in
            <foreach item="item" collection="query.ids" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="query.userIdList != null and query.userIdList.size() > 0">
            and su.user_id in
            <foreach item="item" collection="query.userIdList" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        ${@com.ruoyi.framework.util.DataScopeUtil@create(query.scope)
        .appAlias("ba.id")
        .build()
        }
        ${query.params.dataScope}
    </sql>

    <select id="selectAppList" parameterType="AppQuery" resultMap="AppResult">
        <include refid="selectAppVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectAppById" parameterType="Long" resultMap="AppResult">
        <include refid="selectAppVo"/>
        where ba.id = #{id}
    </select>

    <insert id="insertApp" parameterType="App" useGeneratedKeys="true" keyProperty="id">
        insert into bst_app
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">name,</if>
            <if test="type != null">type,</if>
            <if test="createTime != null">create_time,</if>
            <if test="config != null">config,</if>
            <if test="ownerIds != null">owner_ids,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">#{name},</if>
            <if test="type != null">#{type},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="config != null">#{config,typeHandler=com.ruoyi.bst.app.mapper.typehandler.AppConfigJsonTypeHandler},</if>
            <if test="ownerIds != null">#{ownerIds,typeHandler=com.ruoyi.common.mybatis.typehandler.LongSplitListTypeHandler},</if>
         </trim>
    </insert>

    <update id="updateApp" parameterType="App">
        update bst_app
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where id = #{data.id}
    </update>

    <sql id="updateColumns">
        <if test="data.name != null and data.name != ''">`name` = #{data.name},</if>
        <if test="data.type != null">type = #{data.type},</if>
        <if test="data.config != null">config = #{data.config,typeHandler=com.ruoyi.bst.app.mapper.typehandler.AppConfigJsonTypeHandler},</if>
        <if test="data.createTime != null">create_time = #{data.createTime},</if>
        <if test="data.ownerIds != null">owner_ids = #{data.ownerIds,typeHandler=com.ruoyi.common.mybatis.typehandler.LongSplitListTypeHandler},</if>
    </sql>

    <delete id="deleteAppById" parameterType="Long">
        delete from bst_app where id = #{id}
    </delete>

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

    <select id="selectAllList" resultType="AppAllVO">
        select
            ba.id,
            ba.name,
            ba.type
        from bst_app ba
    </select>

</mapper>
