<?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.fs.oms.mapper.OmsPurchaseOrderMapper">

    <resultMap type="OmsPurchaseOrderVO" id="OmsPurchaseOrderResult">
        <result property="id"    column="id"    />
        <result property="code"    column="code"    />
        <result property="applyId"    column="apply_id"    />
        <result property="applyCode"    column="apply_code"    />
        <result property="supplierId"    column="supplier_id"    />
        <result property="supplierCode"    column="supplier_code"    />
        <result property="supplierName"    column="supplier_name"    />
        <result property="warehouseId"    column="warehouse_id"    />
        <result property="warehouseName"    column="warehouse_Name"    />
        <result property="expectedTime"    column="expected_time"    />
        <result property="type"    column="type"    />
        <result property="purchaseUserId"    column="purchase_user_id"    />
        <result property="overfillRate"    column="overfill_rate"    />
        <result property="products"    column="products"    />
        <result property="amount"    column="amount"    />
        <result property="invoiceType"    column="invoice_type"    />
        <result property="packagingFlag"    column="packaging_flag"    />
        <result property="shippingFlag"    column="shipping_flag"    />
        <result property="alreadyNum"    column="already_num"    />
        <result property="waitNum"    column="wait_num"    />
        <result property="planNum"    column="plan_num"    />
        <result property="auditUserId"    column="audit_user_id"    />
        <result property="auditTime"    column="audit_time"    />
        <result property="finishTime"    column="finish_time"    />
        <result property="status"    column="status"    />
        <result property="stockinStatus"    column="stockin_status"    />
        <result property="cancelNum"    column="cancel_num"    />
        <result property="logisticsId"    column="logistics_id"    />
        <result property="logisticsNo"    column="logistics_no"    />
        <result property="logisticsBn"    column="logistics_business_name"    />
        <result property="logisticsAmount"    column="logistics_amount"    />
        <result property="paymentType"    column="payment_type"    />
        <result property="createUserId"    column="create_user_id"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateUserId"    column="update_user_id"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="createBy"    column="nick_name"    />
    </resultMap>

    <sql id="selectOmsPurchaseOrderVo">
        select s.nick_name,opo.id,opo.code,opo.`type`,os.code as supplier_code,os.name as supplier_name
        ,ow.warehouse_name,opo.`status`,opo.stockin_status,opo.create_time,opo.expected_time,
        opo.payment_type,opo.plan_num,opo.amount,opo.already_num,opo.wait_num,opo.cancel_num,
        opo.packaging_flag,opo.shipping_flag,opo.overfill_rate,ol.logistics_no,ol.logistics_business_name,
        ol.logistics_amount,opo.remark,opo.finish_time,opo.audit_time,op.code as apply_code from oms_purchase_order opo left join oms_supplier os
        on opo.supplier_id = os.id left join oms_warehouse ow on opo.warehouse_id = ow.warehouse_id left join oms_logistics ol
        on opo.logistics_id = ol.id left join oms_purchase_apply op on opo.apply_id = op.id left join sys_user s on opo.create_user_id = s.user_id   </sql>

    <select id="selectOmsPurchaseOrderList" parameterType="OmsPurchaseOrderVO" resultMap="OmsPurchaseOrderResult">
        <include refid="selectOmsPurchaseOrderVo"/>
        <where>
            <if test="code != null  and code != ''"> and code = #{code}</if>
            <if test="supplierName != null  and supplierName != ''"> and supplier_name like concat('%', #{supplierName}, '%')</if>
            <if test="warehouseName != null  and warehouseName != ''"> and warehouse_name like concat('%', #{warehouseName}, '%')</if>
            <if test="expectedTime != null "> and expected_time = #{expectedTime}</if>
            <if test="type != null "> and type = #{type}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="stockinStatus != null "> and stockin_status = #{stockinStatus}</if>
            <if test="finishTime != null "> and finish_time = #{finishTime}</if>
            <if test="createTime != null "> and create_time = #{createTime}</if>
        </where>
    </select>

    <select id="selectOmsPurchaseOrderById" parameterType="Long" resultMap="OmsPurchaseOrderResult">
        <include refid="selectOmsPurchaseOrderVo"/>
        where opo.id = #{id}
    </select>

    <insert id="insertOmsPurchaseOrder" parameterType="OmsPurchaseOrder" useGeneratedKeys="true" keyProperty="id">
        insert into oms_purchase_order
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="code != null">code,</if>
            <if test="applyId != null">apply_id,</if>
            <if test="supplierId != null">supplier_id,</if>
            <if test="warehouseId != null">warehouse_id,</if>
            <if test="expectedTime != null">expected_time,</if>
            <if test="type != null">type,</if>
            <if test="purchaseUserId != null">purchase_user_id,</if>
            <if test="overfillRate != null">overfill_rate,</if>
            <if test="products != null">products,</if>
            <if test="amount != null">amount,</if>
            <if test="invoiceType != null">invoice_type,</if>
            <if test="packagingFlag != null">packaging_flag,</if>
            <if test="shippingFlag != null">shipping_flag,</if>
            <if test="alreadyNum != null">already_num,</if>
            <if test="waitNum != null">wait_num,</if>
            <if test="planNum != null">plan_num,</if>
            <if test="auditUserId != null">audit_user_id,</if>
            <if test="auditTime != null">audit_time,</if>
            <if test="finishTime != null">finish_time,</if>
            <if test="status != null">status,</if>
            <if test="stockinStatus != null">stockin_status,</if>
            <if test="cancelNum != null">cancel_num,</if>
            <if test="logisticsId != null">logistics_id,</if>
            <if test="paymentType != null">payment_type,</if>
            <if test="createUserId != null">create_user_id,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateUserId != null">update_user_id,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="code != null">#{code},</if>
            <if test="applyId != null">#{applyId},</if>
            <if test="supplierId != null">#{supplierId},</if>
            <if test="warehouseId != null">#{warehouseId},</if>
            <if test="expectedTime != null">#{expectedTime},</if>
            <if test="type != null">#{type},</if>
            <if test="purchaseUserId != null">#{purchaseUserId},</if>
            <if test="overfillRate != null">#{overfillRate},</if>
            <if test="products != null">#{products},</if>
            <if test="amount != null">#{amount},</if>
            <if test="invoiceType != null">#{invoiceType},</if>
            <if test="packagingFlag != null">#{packagingFlag},</if>
            <if test="shippingFlag != null">#{shippingFlag},</if>
            <if test="alreadyNum != null">#{alreadyNum},</if>
            <if test="waitNum != null">#{waitNum},</if>
            <if test="planNum != null">#{planNum},</if>
            <if test="auditUserId != null">#{auditUserId},</if>
            <if test="auditTime != null">#{auditTime},</if>
            <if test="finishTime != null">#{finishTime},</if>
            <if test="status != null">#{status},</if>
            <if test="stockinStatus != null">#{stockinStatus},</if>
            <if test="cancelNum != null">#{cancelNum},</if>
            <if test="logisticsId != null">#{logisticsId},</if>
            <if test="paymentType != null">#{paymentType},</if>
            <if test="createUserId != null">#{createUserId},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateUserId != null">#{updateUserId},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateOmsPurchaseOrder" parameterType="OmsPurchaseOrder">
        update oms_purchase_order
        <trim prefix="SET" suffixOverrides=",">
            <if test="code != null">code = #{code},</if>
            <if test="applyId != null">apply_id = #{applyId},</if>
            <if test="supplierId != null">supplier_id = #{supplierId},</if>
            <if test="warehouseId != null">warehouse_id = #{warehouseId},</if>
            <if test="expectedTime != null">expected_time = #{expectedTime},</if>
            <if test="type != null">type = #{type},</if>
            <if test="purchaseUserId != null">purchase_user_id = #{purchaseUserId},</if>
            <if test="overfillRate != null">overfill_rate = #{overfillRate},</if>
            <if test="products != null">products = #{products},</if>
            <if test="amount != null">amount = #{amount},</if>
            <if test="invoiceType != null">invoice_type = #{invoiceType},</if>
            <if test="packagingFlag != null">packaging_flag = #{packagingFlag},</if>
            <if test="shippingFlag != null">shipping_flag = #{shippingFlag},</if>
            <if test="alreadyNum != null">already_num = #{alreadyNum},</if>
            <if test="waitNum != null">wait_num = #{waitNum},</if>
            <if test="planNum != null">plan_num = #{planNum},</if>
            <if test="auditUserId != null">audit_user_id = #{auditUserId},</if>
            <if test="auditTime != null">audit_time = #{auditTime},</if>
            <if test="finishTime != null">finish_time = #{finishTime},</if>
            <if test="status != null">status = #{status},</if>
            <if test="stockinStatus != null">stockin_status = #{stockinStatus},</if>
            <if test="cancelNum != null">cancel_num = #{cancelNum},</if>
            <if test="logisticsId != null">logistics_id = #{logisticsId},</if>
            <if test="paymentType != null">payment_type = #{paymentType},</if>
            <if test="createUserId != null">create_user_id = #{createUserId},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateUserId != null">update_user_id = #{updateUserId},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where opo.id = #{id}
    </update>

    <delete id="deleteOmsPurchaseOrderById" parameterType="Long">
        delete from oms_purchase_order where id = #{id}
    </delete>

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

</mapper>
