<?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.ss.deviceSuit.mapper.DeviceSuitMapper">

    <resultMap type="DeviceSuitVO" id="DeviceSuitResult" autoMapping="true">
        <result property="deviceId" column="device_id"/>
        <result property="suitId" column="suit_id"/>
        <association property="device" autoMapping="true" select="com.ruoyi.ss.device.mapper.DeviceMapper.selectSmDeviceByDeviceId" column="device_id"/>
        <association property="suit" autoMapping="true" select="com.ruoyi.ss.suit.mapper.SuitMapper.selectSuitBySuitId" column="suit_id"/>
    </resultMap>

    <sql id="selectDeviceSuitVo">
        select
            sds.id,
            sds.device_id,
            sds.suit_id
        from ss_device_suit sds
    </sql>

    <sql id="searchCondition">
        <if test="query.deviceId != null "> and sds.device_id = #{query.deviceId}</if>
        <if test="query.suitId != null "> and sds.suit_id = #{query.suitId}</if>
        <if test="query.suitIds != null and query.suitIds.size() > 0">
            and sds.suit_id in
            <foreach collection="query.suitIds" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="query.deviceIds != null and query.deviceIds.size() > 0">
            and sds.device_id in
            <foreach collection="query.deviceIds" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
    </sql>

    <select id="selectDeviceSuitList" parameterType="DeviceSuitQuery" resultMap="DeviceSuitResult">
        <include refid="selectDeviceSuitVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectDeviceSuitById" parameterType="Long" resultMap="DeviceSuitResult">
        <include refid="selectDeviceSuitVo"/>
        where sds.id = #{id}
    </select>

    <insert id="insertDeviceSuit" parameterType="DeviceSuit" useGeneratedKeys="true" keyProperty="id">
        insert into ss_device_suit
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deviceId != null">device_id,</if>
            <if test="suitId != null">suit_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="deviceId != null">#{deviceId},</if>
            <if test="suitId != null">#{suitId},</if>
         </trim>
    </insert>

    <insert id="batchInsert">
        insert into ss_device_suit(device_id, suit_id)
        values <foreach collection="list" item="item" separator=",">
            (#{item.deviceId}, #{item.suitId})
        </foreach>
    </insert>

    <update id="updateDeviceSuit" parameterType="DeviceSuit">
        update ss_device_suit
        <trim prefix="SET" suffixOverrides=",">
            <if test="data.deviceId != null">device_id = #{data.deviceId},</if>
            <if test="data.suitId != null">suit_id = #{data.suitId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteDeviceSuitById" parameterType="Long">
        delete from ss_device_suit where id = #{id}
    </delete>

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

    <delete id="deleteBySuitIds">
        delete from ss_device_suit
        where suit_id in
        <foreach item="id" collection="list" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <delete id="deleteByDeviceIds">
        delete from ss_device_suit
        where device_id in
        <foreach item="id" collection="list" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>
