<?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.od.mapper.OdPurchaseOrderMapper">

    <resultMap type="OdPurchaseOrder" id="OdPurchaseOrderResult">
        <result property="id" column="id"/>
        <result property="orderNo" column="order_no"/>
        <result property="clientId" column="client_id"/>
        <result property="clientNo" column="client_code"/>
        <result property="clientName" column="client_name"/>
        <result property="deliveryDate" column="delivery_date"/>
        <result property="remark" column="remark"/>
        <result property="state" column="state"/>
        <result property="source" column="source"/>
        <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="selectOdPurchaseOrderVo">
        select t1.id, t1.order_no, t1.client_id, t1.delivery_date, t1.remark, t1.state, t1.source, t1.create_by, t1.create_time,
               t1.update_by, t1.update_time, t2.client_name, t2.client_code
        from od_purchase_order t1
        left join md_client t2 on t1.client_id = t2.client_id
    </sql>
    <select id="selectOdPurchaseOrderList" parameterType="OdPurchaseOrder" resultMap="OdPurchaseOrderResult">
        <include refid="selectOdPurchaseOrderVo"/>
        <where>
            <if test="orderNo != null  and orderNo != ''"> and t1.order_no = #{orderNo}</if>
            <if test="clientId != null "> and t1.client_id = #{clientId}</if>
            <if test="deliveryDate != null "> and t1.delivery_date = #{deliveryDate}</if>
            <if test="state != null "> and t1.state = #{state}</if>
            <if test="source != null  and source != ''"> and t1.source = #{source}</if>
        </where>
    </select>

    <select id="selectOdPurchaseOrderById" resultMap="OdPurchaseOrderResult">
        <include refid="selectOdPurchaseOrderVo"/>
        where t1.id = #{id}
    </select>
    <select id="getByNo" resultType="com.ximai.mes.od.domain.OdPurchaseOrder">
        <include refid="selectOdPurchaseOrderVo"/>
        where t1.order_no = #{purchaseNo}
    </select>

    <insert id="insertOdPurchaseOrder" parameterType="OdPurchaseOrder" useGeneratedKeys="true" keyProperty="id">
        insert into od_purchase_order
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="orderNo != null">order_no,</if>
            <if test="clientId != null">client_id,</if>
            <if test="deliveryDate != null">delivery_date,</if>
            <if test="remark != null">remark,</if>
            <if test="state != null">state,</if>
            <if test="source != null">source,</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="orderNo != null">#{orderNo},</if>
            <if test="clientId != null">#{clientId},</if>
            <if test="deliveryDate != null">#{deliveryDate},</if>
            <if test="remark != null">#{remark},</if>
            <if test="state != null">#{state},</if>
            <if test="source != null">#{source},</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="updateOdPurchaseOrder" parameterType="OdPurchaseOrder">
        update od_purchase_order
        <trim prefix="SET" suffixOverrides=",">
            <if test="orderNo != null">order_no = #{orderNo},</if>
            <if test="clientId != null">client_id = #{clientId},</if>
            <if test="deliveryDate != null">delivery_date = #{deliveryDate},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="state != null">state = #{state},</if>
            <if test="source != null">source = #{source},</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="deleteOdPurchaseOrderById" parameterType="String">
        delete from od_purchase_order where id = #{id}
    </delete>

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

</mapper>
