<?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.ximai.mes.qt.mapper.QtFactoryQuoteMapper">

    <resultMap type="QtFactoryQuote" id="QtFactoryQuoteResult">
        <result property="id" column="id"/>
        <result property="clientId" column="client_id"/>
        <result property="clientCode" column="client_code"/>
        <result property="clientName" column="client_name"/>
        <result property="itemId" column="item_id"/>
        <result property="itemCode" column="item_code"/>
        <result property="itemName" column="item_name"/>
        <result property="price" column="price"/>
        <result property="validFrom" column="valid_from"/>
        <result property="validTo" column="valid_to"/>
        <result property="state" column="state"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectQtFactoryQuoteVo">
        select t1.id, t1.client_id, t1.item_id, t1.price, t1.valid_from, t1.valid_to, t1.state, t1.create_by,
               t1.create_time, t1.update_by, t1.update_time, t2.client_name, t2.client_code, t3.item_code, t3.item_name
        from qt_factory_quote t1
        left join md_client t2 on t1.client_id = t2.client_id
        left join md_item t3 on t3.item_id = t1.item_id
    </sql>

    <select id="selectQtFactoryQuoteList" parameterType="QtFactoryQuote" resultMap="QtFactoryQuoteResult">
        <include refid="selectQtFactoryQuoteVo"/>

    </select>

    <select id="selectOne" parameterType="Long" resultMap="QtFactoryQuoteResult">
        <include refid="selectQtFactoryQuoteVo"/>
        <where>
            <if test="id != null "> and t1.id = #{id}</if>
            <if test="clientId != null "> and t1.client_id = #{clientId}</if>
            <if test="itemId != null "> and t1.item_id = #{itemId}</if>
            <if test="validFrom != null and validTo != null">
                and not (t1.valid_from >= #{validTo} or t1.valid_to &lt;= #{validFrom})
            </if>
            <if test="validFrom != null and validFrom == null"> and #{validFrom} between t1.valid_from and t1.valid_to</if>
            <if test="state != null  and state != ''"> and t1.state = #{state}</if>
        </where>
    </select>

    <insert id="insertQtFactoryQuote" parameterType="QtFactoryQuote">
        insert into qt_factory_quote
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="clientId != null">client_id,</if>
            <if test="itemId != null">item_id,</if>
            <if test="price != null">price,</if>
            <if test="validFrom != null">valid_from,</if>
            <if test="validTo != null">valid_to,</if>
            <if test="state != null">state,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="clientId != null">#{clientId},</if>
            <if test="itemId != null">#{itemId},</if>
            <if test="price != null">#{price},</if>
            <if test="validFrom != null">#{validFrom},</if>
            <if test="validTo != null">#{validTo},</if>
            <if test="state != null">#{state},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateQtFactoryQuote" parameterType="QtFactoryQuote">
        update qt_factory_quote
        <trim prefix="SET" suffixOverrides=",">
            <if test="clientId != null">client_id = #{clientId},</if>
            <if test="itemId != null">item_id = #{itemId},</if>
            <if test="price != null">price = #{price},</if>
            <if test="validFrom != null">valid_from = #{validFrom},</if>
            <if test="validTo != null">valid_to = #{validTo},</if>
            <if test="state != null">state = #{state},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteQtFactoryQuoteById" parameterType="Long">
        delete from qt_factory_quote where id = #{id}
    </delete>

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