<?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.system.mapper.QuoteMaterialMapper">
    
    <resultMap type="QuoteMaterial" id="QuoteMaterialResult">
        <result property="id"    column="id"    />
        <result property="goodId"    column="good_id"    />
        <result property="typeId"    column="type_id"    />
        <result property="title"    column="title"    />
        <result property="price"    column="price"    />
        <result property="unit"    column="unit"    />
        <result property="createdAt"    column="created_at"    />
        <result property="updatedAt"    column="updated_at"    />
    </resultMap>

    <sql id="selectQuoteMaterialVo">
        select id, good_id, type_id, title, price, unit, created_at, updated_at from quote_material
    </sql>

    <select id="selectQuoteMaterialList" parameterType="QuoteMaterial" resultMap="QuoteMaterialResult">
        <include refid="selectQuoteMaterialVo"/>
        <where>
            <if test="goodId != null  and goodId != ''"> and good_id like concat('%', #{goodId}, '%')</if>

            <if test="typeId != null  and typeId != ''">
                and type_id  in (select b.id  from  quote_material_type  b  where  b.title like  concat('%', #{typeId}, '%') )   </if>

            <if test="title != null  and title != ''"> and title like concat('%', #{title}, '%')</if>
            <if test="priceMin != null and priceMax != null">
                and price BETWEEN #{priceMin} AND #{priceMax}
            </if>
            <if test="priceMin != null and priceMax == null">
                and price >= #{priceMin}
            </if>
            <if test="priceMax != null and priceMin == null">
                and price  &lt;=  #{priceMax}
            </if>
            <if test="unit != null  and unit != ''"> and unit = #{unit}</if>

        </where>
        order by id desc
    </select>
    
    <select id="selectQuoteMaterialById" parameterType="Long" resultMap="QuoteMaterialResult">
        <include refid="selectQuoteMaterialVo"/>
        where id = #{id}
    </select>

    <insert id="insertQuoteMaterial" parameterType="QuoteMaterial" useGeneratedKeys="true" keyProperty="id">
        insert into quote_material
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="goodId != null and goodId != ''">good_id,</if>
            <if test="typeId != null and typeId != ''">type_id,</if>
            <if test="title != null and title != ''">title,</if>
            <if test="price != null">price,</if>
            <if test="unit != null and unit != ''">unit,</if>
            <if test="createdAt != null">created_at,</if>
            <if test="updatedAt != null">updated_at,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="goodId != null and goodId != ''">#{goodId},</if>
            <if test="typeId != null and typeId != ''">#{typeId},</if>
            <if test="title != null and title != ''">#{title},</if>
            <if test="price != null">#{price},</if>
            <if test="unit != null and unit != ''">#{unit},</if>
            <if test="createdAt != null">#{createdAt},</if>
            <if test="updatedAt != null">#{updatedAt},</if>
         </trim>
    </insert>

    <update id="updateQuoteMaterial" parameterType="QuoteMaterial">
        update quote_material
        <trim prefix="SET" suffixOverrides=",">
            <if test="goodId != null and goodId != ''">good_id = #{goodId},</if>
            <if test="typeId != null and typeId != ''">type_id = #{typeId},</if>
            <if test="title != null and title != ''">title = #{title},</if>
            <if test="price != null">price = #{price},</if>
            <if test="unit != null and unit != ''">unit = #{unit},</if>
            <if test="createdAt != null">created_at = #{createdAt},</if>
            <if test="updatedAt != null">updated_at = #{updatedAt},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteQuoteMaterialById" parameterType="Long">
        delete from quote_material where id = #{id}
    </delete>

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