<?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.modelSuit.mapper.ModelSuitMapper">

    <resultMap type="ModelSuitVO" id="ModelSuitResult" autoMapping="true"/>

    <sql id="selectModelSuitVo">
        select
            bms.id,
            bms.suit_id,
            bms.model_id,
            bm.name as model_name,
            bs.name as suit_name
        from bst_model_suit bms
            left join bst_model bm on bms.model_id = bm.id
            left join bst_suit bs on bms.suit_id = bs.id
    </sql>

    <sql id="searchCondition">
        <if test="query.id != null "> and bms.id = #{query.id}</if>
        <if test="query.suitId != null "> and bms.suit_id = #{query.suitId}</if>
        <if test="query.modelId != null "> and bms.model_id = #{query.modelId}</if>
        <if test="query.modelIds != null and query.modelIds.size() > 0">
            and bms.model_id in
            <foreach item="item" collection="query.modelIds" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="query.suitIds != null and query.suitIds.size() > 0">
            and bms.suit_id in
            <foreach item="item" collection="query.suitIds" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        ${query.params.dataScope}
    </sql>

    <select id="selectModelSuitList" parameterType="ModelSuitQuery" resultMap="ModelSuitResult">
        <include refid="selectModelSuitVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectModelSuitById" parameterType="Long" resultMap="ModelSuitResult">
        <include refid="selectModelSuitVo"/>
        where bms.id = #{id}
    </select>

    <insert id="insertModelSuit" parameterType="ModelSuit" useGeneratedKeys="true" keyProperty="id">
        insert into bst_model_suit
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="suitId != null">suit_id,</if>
            <if test="modelId != null">model_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="suitId != null">#{suitId},</if>
            <if test="modelId != null">#{modelId},</if>
         </trim>
    </insert>

    <insert id="batchInsert" parameterType="ModelSuit" useGeneratedKeys="true" keyProperty="id">
        insert into bst_model_suit
        <trim prefix="(" suffix=")" suffixOverrides=",">
            suit_id,
            model_id,
        </trim>
        values
        <foreach collection="list" item="i" separator=",">
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="i.suitId != null ">#{i.suitId},</if>
                <if test="i.suitId == null ">default,</if>
                <if test="i.modelId != null ">#{i.modelId},</if>
                <if test="i.modelId == null ">default,</if>
            </trim>
        </foreach>
    </insert>

    <update id="updateModelSuit" parameterType="ModelSuit">
        update bst_model_suit
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where id = #{data.id}
    </update>

    <sql id="updateColumns">
        <if test="data.suitId != null">suit_id = #{data.suitId},</if>
        <if test="data.modelId != null">model_id = #{data.modelId},</if>
    </sql>

    <delete id="deleteModelSuitById" parameterType="Long">
        delete from bst_model_suit where id = #{id}
    </delete>

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

    <!-- deleteModelSuitBySuitIds --> 

    <delete id="deleteModelSuitBySuitIds">
        delete from bst_model_suit 
        where suit_id in
        <foreach item="suitId" collection="suitIds" open="(" separator="," close=")">
            #{suitId}
        </foreach>
    </delete>

    <!-- deleteModelSuitByModelIds --> 

    <delete id="deleteModelSuitByModelIds">
        delete from bst_model_suit 
        where model_id in
        <foreach item="modelId" collection="modelIds" open="(" separator="," close=")">
            #{modelId}
        </foreach>
    </delete>    
</mapper>
