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

    <resultMap type="OmsPurchaseApply" id="OmsPurchaseApplyResult">
        <result property="id"    column="id"    />
        <result property="code"    column="code"    />
        <result property="warehouseId"    column="warehouse_id"    />
        <result property="warehouseName"    column="warehouse_name"    />
        <result property="expectedTime"    column="expected_time"    />
        <result property="auditTime"    column="audit_time"    />
        <result property="status"    column="status"    />
        <result property="actuallyNum"    column="actually_num"    />
        <result property="products"    column="products"    />
        <result property="auditUserId"    column="audit_user_id"    />
        <result property="planNum"    column="plan_num"    />
        <result property="alreadyNum"    column="already_num"    />
        <result property="waitNum"    column="wait_num"    />
        <result property="createUserId"    column="create_user_id"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="createBy"    column="nick_name"    />
    </resultMap>

    <sql id="selectOmsPurchaseApplyVo">
        select op.id, op.code, op.warehouse_id, ow.warehouse_name,op.update_time,s.nick_name,
        op.expected_time, op.audit_time, op.status, op.actually_num, op.products, op.audit_user_id, op.plan_num, op.already_num, op.wait_num, op.create_user_id, op.create_time, op.update_time,
        op.remark from oms_purchase_apply op left join oms_warehouse ow
        on op.warehouse_id = ow.warehouse_id left join sys_user s on op.create_user_id = s.user_id
    </sql>

    <select id="selectOmsPurchaseApplyList" parameterType="OmsPurchaseApply" resultMap="OmsPurchaseApplyResult">
        <include refid="selectOmsPurchaseApplyVo"/>
        <where>
            <if test="code != null  and code != ''"> and code = #{code}</if>
            <if test="warehouseId != null "> and warehouse_id = #{warehouseId}</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="auditTime != null "> and audit_time = #{auditTime}</if>
            <if test="status != null "> and op.status = #{status}</if>
            <if test="actuallyNum != null "> and actually_num = #{actuallyNum}</if>
            <if test="products != null  and products != ''"> and products = #{products}</if>
            <if test="auditUserId != null "> and audit_user_id = #{auditUserId}</if>
            <if test="planNum != null "> and plan_num = #{planNum}</if>
            <if test="alreadyNum != null "> and already_num = #{alreadyNum}</if>
            <if test="waitNum != null "> and wait_num = #{waitNum}</if>
            <if test="createUserId != null "> and create_user_id = #{createUserId}</if>
        </where>
    </select>

    <select id="selectOmsPurchaseApplyById" parameterType="Long" resultMap="OmsPurchaseApplyResult">
        <include refid="selectOmsPurchaseApplyVo"/>
        where op.id = #{id}
    </select>

    <insert id="insertOmsPurchaseApply" parameterType="OmsPurchaseApply" useGeneratedKeys="true" keyProperty="id">
        insert into oms_purchase_apply
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="code != null">code,</if>
            <if test="warehouseId != null">warehouse_id,</if>
            <if test="expectedTime != null">expected_time,</if>
            <if test="auditTime != null">audit_time,</if>
            <if test="status != null">status,</if>
            <if test="actuallyNum != null">actually_num,</if>
            <if test="products != null">products,</if>
            <if test="auditUserId != null">audit_user_id,</if>
            <if test="planNum != null">plan_num,</if>
            <if test="alreadyNum != null">already_num,</if>
            <if test="waitNum != null">wait_num,</if>
            <if test="createUserId != null">create_user_id,</if>
            <if test="createTime != null">create_time,</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="warehouseId != null">#{warehouseId},</if>
            <if test="expectedTime != null">#{expectedTime},</if>
            <if test="auditTime != null">#{auditTime},</if>
            <if test="status != null">#{status},</if>
            <if test="actuallyNum != null">#{actuallyNum},</if>
            <if test="products != null">#{products},</if>
            <if test="auditUserId != null">#{auditUserId},</if>
            <if test="planNum != null">#{planNum},</if>
            <if test="alreadyNum != null">#{alreadyNum},</if>
            <if test="waitNum != null">#{waitNum},</if>
            <if test="createUserId != null">#{createUserId},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateOmsPurchaseApply" parameterType="OmsPurchaseApply">
        update oms_purchase_apply
        <trim prefix="SET" suffixOverrides=",">
            <if test="code != null">code = #{code},</if>
            <if test="warehouseId != null">warehouse_id = #{warehouseId},</if>
            <if test="expectedTime != null">expected_time = #{expectedTime},</if>
            <if test="auditTime != null">audit_time = #{auditTime},</if>
            <if test="status != null">status = #{status},</if>
            <if test="actuallyNum != null">actually_num = #{actuallyNum},</if>
            <if test="products != null">products = #{products},</if>
            <if test="auditUserId != null">audit_user_id = #{auditUserId},</if>
            <if test="planNum != null">plan_num = #{planNum},</if>
            <if test="alreadyNum != null">already_num = #{alreadyNum},</if>
            <if test="waitNum != null">wait_num = #{waitNum},</if>
            <if test="createUserId != null">create_user_id = #{createUserId},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="updateApplyStatus" parameterType="OmsPurchaseApply">
        update oms_purchase_apply
        <trim prefix="SET" suffixOverrides=",">
            <if test="auditTime != null">audit_time = #{auditTime},</if>
            <if test="status != null">status = #{status},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteOmsPurchaseApplyById" parameterType="Long">
        delete from oms_purchase_apply where id = #{id}
    </delete>

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

</mapper>
