<?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.OdSalesOrderMapper">

    <resultMap type="OdSalesOrder" id="OdSalesOrderResult">
        <result property="id" column="id"/>
        <result property="purchaseNo" column="purchase_no"/>
        <result property="purchaseDate" column="purchase_date"/>
        <result property="clientId" column="client_id"/>
        <result property="clientNo" column="client_no"/>
        <result property="clientName" column="client_name"/>
        <result property="sapClientCode" column="sap_client_code"/>
        <result property="orderNo" column="order_no"/>
        <result property="sapOrderNo" column="sap_order_no"/>
        <result property="typeNo" column="type_no"/>
        <result property="amount" column="amount"/>
        <result property="merchandiser" column="merchandiser"/>
        <result property="custom" column="custom"/>
        <result property="orgNo" column="org_no"/>
        <result property="channelNo" column="channel_no"/>
        <result property="reasonNo" column="reason_no"/>
        <result property="agencyNo" column="agency_no"/>
        <result property="groupNo" column="group_no"/>
        <result property="currencyNo" column="currency_no"/>
        <result property="deliveryDate" column="delivery_date"/>
        <result property="clauseNo" column="clause_no"/>
        <result property="clauseAddr" column="clause_addr"/>
        <result property="paymentClause" column="payment_clause"/>
        <result property="itemGroup" column="item_group"/>
        <result property="poPurchaseNo" column="po_purchase_no"/>
        <result property="netValue" column="net_value"/>
        <result property="despatchType" column="despatch_type"/>
        <result property="barCode" column="bar_code"/>
        <result property="urgent" column="urgent"/>
        <result property="ospNo" column="osp_no"/>
        <result property="remark" column="remark"/>
        <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="selectOdSalesOrderVo">
        select t1.id, t1.purchase_no, t1.purchase_date, t1.client_id, t1.client_no, t2.client_name, t2.sap_client_code, t1.order_no, t1.sap_order_no, t1.type_no, t1.amount,
               t1.merchandiser, t1.custom, t1.org_no, t1.channel_no, t1.reason_no, t1.agency_no, t1.group_no, t1.currency_no,
               t1.delivery_date, t1.clause_no, t1.clause_addr, t1.payment_clause, t1.item_group, t1.po_purchase_no, t1.net_value, t1.despatch_type, t1.bar_code, t1.urgent, t1.osp_no,
               t1.remark, t1.state, t1.create_by, t1.create_time, t1.update_by, t1.update_time
        from od_sales_order t1
        left join md_client t2 on t1.client_id = t2.client_id
    </sql>

    <select id="selectOdSalesOrderList" parameterType="OdSalesOrder" resultMap="OdSalesOrderResult">
        <include refid="selectOdSalesOrderVo"/>
        <where>
            <if test="orderNo != null  and orderNo != ''"> and t1.order_no = #{orderNo}</if>
            <if test="clientId != null  and clientId != ''"> and t1.client_id = #{clientId}</if>
            <if test="clientName != null  and clientName != ''"> and t2.client_name like concat('%', #{clientName}, '%')</if>
            <if test="deliveryDate != null "> and t1.delivery_date = #{deliveryDate}</if>
            <if test="amount != null "> and t1.amount = #{amount}</if>
            <if test="merchandiser != null  and merchandiser != ''"> and t1.merchandiser = #{merchandiser}</if>
            <if test="state != null  and state != ''"> and t1.state = #{state}</if>
        </where>
    </select>

    <select id="selectOdSalesOrderById" parameterType="Long" resultMap="OdSalesOrderResult">
        <include refid="selectOdSalesOrderVo"/>
        <where>
            <if test="id != null">and t1.id = #{id}</if>
        </where>
    </select>

    <insert id="insertOdSalesOrder" parameterType="OdSalesOrder" useGeneratedKeys="true" keyProperty="id">
        insert into od_sales_order
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="purchaseNo != null">purchase_no,</if>
            <if test="orderNo != null">order_no,</if>
            <if test="sapOrderNo != null">sap_order_no,</if>
            <if test="deliveryDate != null">delivery_date,</if>
            <if test="amount != null">amount,</if>
            <if test="merchandiser != null">merchandiser,</if>
            <if test="remark != null">remark,</if>
            <if test="state != null">state,</if>
            <if test="purchaseDate != null">purchase_date,</if>
            <if test="clientId != null">client_id,</if>
            <if test="clientNo != null">client_no,</if>
            <if test="typeNo != null">type_no,</if>
            <if test="custom != null">custom,</if>
            <if test="orgNo != null">org_no,</if>
            <if test="channelNo != null">channel_no,</if>
            <if test="reasonNo != null">reason_no,</if>
            <if test="agencyNo != null">agency_no,</if>
            <if test="groupNo != null">group_no,</if>
            <if test="currencyNo != null">currency_no,</if>
            <if test="clauseNo != null">clause_no,</if>
            <if test="clauseAddr != null">clause_addr,</if>
            <if test="netValue != null">net_value,</if>
            <if test="paymentClause != null">payment_clause,</if>
            <if test="itemGroup != null">item_group,</if>
            <if test="despatchType != null">despatch_type,</if>
            <if test="poPurchaseNo != null">po_purchase_no,</if>
            <if test="barCode != null">bar_code,</if>
            <if test="urgent != null">urgent,</if>
            <if test="ospNo != null">osp_no,</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="purchaseNo != null">#{purchaseNo},</if>
            <if test="orderNo != null">#{orderNo},</if>
            <if test="sapOrderNo != null">#{sapOrderNo},</if>
            <if test="deliveryDate != null">#{deliveryDate},</if>
            <if test="amount != null">#{amount},</if>
            <if test="merchandiser != null">#{merchandiser},</if>
            <if test="remark != null">#{remark},</if>
            <if test="state != null">#{state},</if>
            <if test="purchaseDate != null">#{purchaseDate},</if>
            <if test="clientId != null">#{clientId},</if>
            <if test="clientNo != null">#{clientNo},</if>
            <if test="typeNo != null">#{typeNo},</if>
            <if test="custom != null">#{custom},</if>
            <if test="orgNo != null">#{orgNo},</if>
            <if test="channelNo != null">#{channelNo},</if>
            <if test="reasonNo != null">#{reasonNo},</if>
            <if test="agencyNo != null">#{agencyNo},</if>
            <if test="groupNo != null">#{groupNo},</if>
            <if test="currencyNo != null">#{currencyNo},</if>
            <if test="clauseNo != null">#{clauseNo},</if>
            <if test="clauseAddr != null">#{clauseAddr},</if>
            <if test="netValue != null">#{netValue},</if>
            <if test="paymentClause != null">#{paymentClause},</if>
            <if test="itemGroup != null">#{itemGroup},</if>
            <if test="despatchType != null">#{despatchType},</if>
            <if test="poPurchaseNo != null">#{poPurchaseNo},</if>
            <if test="barCode != null">#{barCode},</if>
            <if test="urgent != null">#{urgent},</if>
            <if test="ospNo != null">#{ospNo},</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="updateOdSalesOrder" parameterType="OdSalesOrder">
        update od_sales_order
        <trim prefix="SET" suffixOverrides=",">
            <if test="purchaseNo != null">purchase_no = #{purchaseNo},</if>
            <if test="orderNo != null">order_no = #{orderNo},</if>
            <if test="sapOrderNo != null">sap_order_no = #{sapOrderNo},</if>
            <if test="deliveryDate != null">delivery_date = #{deliveryDate},</if>
            <if test="amount != null">amount = #{amount},</if>
            <if test="merchandiser != null">merchandiser = #{merchandiser},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="state != null">state = #{state},</if>
            <if test="purchaseDate != null">purchase_date = #{purchaseDate},</if>
            <if test="clientId != null">client_id = #{clientId},</if>
            <if test="clientNo != null">client_no = #{clientNo},</if>
            <if test="typeNo != null">type_no = #{typeNo},</if>
            <if test="custom != null">custom = #{custom},</if>
            <if test="orgNo != null">org_no = #{orgNo},</if>
            <if test="channelNo != null">channel_no = #{channelNo},</if>
            <if test="reasonNo != null">reason_no = #{reasonNo},</if>
            <if test="agencyNo != null">agency_no = #{agencyNo},</if>
            <if test="groupNo != null">group_no = #{groupNo},</if>
            <if test="currencyNo != null">currency_no = #{currencyNo},</if>
            <if test="clauseNo != null">clause_no = #{clauseNo},</if>
            <if test="clauseAddr != null">clause_addr = #{clauseAddr},</if>
            <if test="netValue != null">net_value = #{netValue},</if>
            <if test="paymentClause != null">payment_clause = #{paymentClause},</if>
            <if test="itemGroup != null">item_group = #{itemGroup},</if>
            <if test="despatchType != null">despatch_type = #{despatchType},</if>
            <if test="poPurchaseNo != null">po_purchase_no = #{poPurchaseNo},</if>
            <if test="barCode != null">bar_code = #{barCode},</if>
            <if test="urgent != null">urgent = #{urgent},</if>
            <if test="ospNo != null">osp_no = #{ospNo},</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="deleteOdSalesOrderById" parameterType="String">
        delete from od_sales_order where id = #{id}
    </delete>

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