<?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.zhyc.module.stock.mapper.WzStockOutMapper">
    
    <resultMap type="WzStockOut" id="WzStockOutResult">
        <result property="stockOutCode"    column="stock_out_code"    />
        <result property="docDate"    column="doc_date"    />
        <result property="createDate"    column="create_date"    />
        <result property="docId"    column="doc_id"    />
        <result property="businessType"    column="business_type"    />
        <result property="repositoryId"    column="repository_id"    />
        <result property="repositoryName"    column="repository_name"    />
        <result property="projectClassification"    column="project_classification"    />
        <result property="projectId"    column="project_id"    />
        <result property="projectName"    column="project_name"    />
        <result property="departmentId"    column="department_id"    />
        <result property="departmentName"    column="department_name"    />
        <result property="receiverId"    column="receiver_id"    />
        <result property="receiver"    column="receiver"    />
        <result property="single"    column="single"    />
        <result property="reviewer"    column="reviewer"    />
        <result property="reviewDate"    column="review_date"    />
        <result property="modifier"    column="modifier"    />
        <result property="materialClassId"    column="material_class_id"    />
        <result property="materialClassName"    column="material_class_name"    />
        <result property="materialId"    column="material_id"    />
        <result property="materialName"    column="material_name"    />
        <result property="materialSpecification"    column="material_specification"    />
        <result property="materialUnit"    column="material_unit"    />
        <result property="count"    column="count"    />
        <result property="batchId"    column="batch_id"    />
        <result property="productionDate"    column="production_date"    />
        <result property="shelfLife"    column="shelf_life"    />
        <result property="shelfLifeUnit"    column="shelf_life_unit"    />
        <result property="expirationDate"    column="expiration_date"    />
        <result property="agent"    column="agent"    />
    </resultMap>

    <sql id="selectWzStockOutVo">
        select stock_out_code, doc_date, create_date, doc_id, business_type, repository_id, repository_name, project_classification, project_id, project_name, department_id, department_name,receiver_id, receiver, single, reviewer, review_date, modifier, material_class_id, material_class_name, material_id,material_name, material_specification, material_unit, count, batch_id, production_date, shelf_life, shelf_life_unit, expiration_date, agent from wz_stock_out
    </sql>

    <select id="selectWzStockOutList" parameterType="WzStockOut" resultMap="WzStockOutResult">
        <include refid="selectWzStockOutVo"/>
        <where>  
            <if test="businessType != null  and businessType != ''"> and business_type = #{businessType}</if>
            <if test="repositoryName != null  and repositoryName != ''"> and repository_name like concat('%', #{repositoryName}, '%')</if>
            <if test="projectName != null  and projectName != ''"> and project_name like concat('%', #{projectName}, '%')</if>
            <if test="departmentName != null  and departmentName != ''"> and department_name like concat('%', #{departmentName}, '%')</if>
            <if test="materialName != null  and materialName != ''"> and material_name like concat('%', #{materialName}, '%')</if>
        </where>
    </select>
    
    <select id="selectWzStockOutByStockOutCode" parameterType="Long" resultMap="WzStockOutResult">
        <include refid="selectWzStockOutVo"/>
        where stock_out_code = #{stockOutCode}
    </select>

    <insert id="insertWzStockOut" parameterType="WzStockOut" useGeneratedKeys="true" keyProperty="stockOutCode">
        insert into wz_stock_out
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="docDate != null">doc_date,</if>
            <if test="createDate != null">create_date,</if>
            <if test="docId != null">doc_id,</if>
            <if test="businessType != null">business_type,</if>
            <if test="repositoryId != null">repository_id,</if>
            <if test="repositoryName != null">repository_name,</if>
            <if test="projectClassification != null">project_classification,</if>
            <if test="projectId != null">project_id,</if>
            <if test="projectName != null">project_name,</if>
            <if test="departmentId != null">department_id,</if>
            <if test="departmentName != null">department_name,</if>
            <if test="receiverId != null">receiver_id,</if>
            <if test="receiver != null">receiver,</if>
            <if test="single != null">single,</if>
            <if test="reviewer != null">reviewer,</if>
            <if test="reviewDate != null">review_date,</if>
            <if test="modifier != null">modifier,</if>
            <if test="materialClassId != null">material_class_id,</if>
            <if test="materialClassName != null">material_class_name,</if>
            <if test="materialId != null">material_id,</if>
            <if test="materialName != null">material_name,</if>
            <if test="materialSpecification != null">material_specification,</if>
            <if test="materialUnit != null">material_unit,</if>
            <if test="count != null">count,</if>
            <if test="batchId != null">batch_id,</if>
            <if test="productionDate != null">production_date,</if>
            <if test="shelfLife != null">shelf_life,</if>
            <if test="shelfLifeUnit != null">shelf_life_unit,</if>
            <if test="expirationDate != null">expiration_date,</if>
            <if test="agent != null">agent,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="docDate != null">#{docDate},</if>
            <if test="createDate != null">#{createDate},</if>
            <if test="docId != null">#{docId},</if>
            <if test="businessType != null">#{businessType},</if>
            <if test="repositoryId != null">#{repositoryId},</if>
            <if test="repositoryName != null">#{repositoryName},</if>
            <if test="projectClassification != null">#{projectClassification},</if>
            <if test="projectId != null">#{projectId},</if>
            <if test="projectName != null">#{projectName},</if>
            <if test="departmentId != null">#{departmentId},</if>
            <if test="departmentName != null">#{departmentName},</if>
            <if test="receiverId != null">#{receiverId},</if>
            <if test="receiver != null">#{receiver},</if>
            <if test="single != null">#{single},</if>
            <if test="reviewer != null">#{reviewer},</if>
            <if test="reviewDate != null">#{reviewDate},</if>
            <if test="modifier != null">#{modifier},</if>
            <if test="materialClassId != null">#{materialClassId},</if>
            <if test="materialClassName != null">#{materialClassName},</if>
            <if test="materialId != null">#{materialId},</if>
            <if test="materialName != null">#{materialName},</if>
            <if test="materialSpecification != null">#{materialSpecification},</if>
            <if test="materialUnit != null">#{materialUnit},</if>
            <if test="count != null">#{count},</if>
            <if test="batchId != null">#{batchId},</if>
            <if test="productionDate != null">#{productionDate},</if>
            <if test="shelfLife != null">#{shelfLife},</if>
            <if test="shelfLifeUnit != null">#{shelfLifeUnit},</if>
            <if test="expirationDate != null">#{expirationDate},</if>
            <if test="agent != null">#{agent},</if>
         </trim>
    </insert>

    <update id="updateWzStockOut" parameterType="WzStockOut">
        update wz_stock_out
        <trim prefix="SET" suffixOverrides=",">
            <if test="docDate != null">doc_date = #{docDate},</if>
            <if test="createDate != null">create_date = #{createDate},</if>
            <if test="docId != null">doc_id = #{docId},</if>
            <if test="businessType != null">business_type = #{businessType},</if>
            <if test="repositoryId != null">repository_id = #{repositoryId},</if>
            <if test="repositoryName != null">repository_name = #{repositoryName},</if>
            <if test="projectClassification != null">project_classification = #{projectClassification},</if>
            <if test="projectId != null">project_id = #{projectId},</if>
            <if test="projectName != null">project_name = #{projectName},</if>
            <if test="departmentId != null">department_id = #{departmentId},</if>
            <if test="departmentName != null">department_name = #{departmentName},</if>
            <if test="receiverId != null">receiver_id = #{receiverId},</if>
            <if test="receiver != null">receiver = #{receiver},</if>
            <if test="single != null">single = #{single},</if>
            <if test="reviewer != null">reviewer = #{reviewer},</if>
            <if test="reviewDate != null">review_date = #{reviewDate},</if>
            <if test="modifier != null">modifier = #{modifier},</if>
            <if test="materialClassId != null">material_class_id = #{materialClassId},</if>
            <if test="materialClassName != null">material_class_name = #{materialClassName},</if>
            <if test="materialId != null">material_id = #{materialId},</if>
            <if test="materialName != null">material_name = #{materialName},</if>
            <if test="materialSpecification != null">material_specification = #{materialSpecification},</if>
            <if test="materialUnit != null">material_unit = #{materialUnit},</if>
            <if test="count != null">count = #{count},</if>
            <if test="batchId != null">batch_id = #{batchId},</if>
            <if test="productionDate != null">production_date = #{productionDate},</if>
            <if test="shelfLife != null">shelf_life = #{shelfLife},</if>
            <if test="shelfLifeUnit != null">shelf_life_unit = #{shelfLifeUnit},</if>
            <if test="expirationDate != null">expiration_date = #{expirationDate},</if>
            <if test="agent != null">agent = #{agent},</if>
        </trim>
        where stock_out_code = #{stockOutCode}
    </update>

    <delete id="deleteWzStockOutByStockOutCode" parameterType="Long">
        delete from wz_stock_out where stock_out_code = #{stockOutCode}
    </delete>

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

    <select id="getEarliestStockOut" resultMap="WzStockOutResult">
        SELECT * FROM wz_stock_out ORDER BY create_date DESC LIMIT 1
    </select>
</mapper>