<?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.WzMaterialsManagementMapper">
    
    <resultMap type="WzMaterialsManagement" id="WzMaterialsManagementResult">
        <result property="materialManagementCode"    column="material_management_code"    />
        <result property="materialId"    column="material_id"    />
        <result property="materialName"    column="material_name"    />
        <result property="batchId"    column="batch_id"    />
        <result property="materialSpecification"    column="material_specification"    />
        <result property="materialUnit"    column="material_unit"    />
        <result property="currentStock"    column="current_stock"    />
        <result property="stockAlarm"    column="stock_alarm"    />
        <result property="productionDate"    column="production_date"    />
        <result property="expirationDate"    column="expiration_date"    />
        <result property="expirationAlarm"    column="expiration_alarm"    />
    </resultMap>

    <sql id="selectWzMaterialsManagementVo">
        select material_management_code, material_id, material_name, batch_id, material_specification, material_unit, current_stock, stock_alarm, production_date, expiration_date, expiration_alarm from wz_materials_management
    </sql>

    <select id="selectWzMaterialsManagementList" parameterType="WzMaterialsManagement" resultMap="WzMaterialsManagementResult">
        <include refid="selectWzMaterialsManagementVo"/>
        <where>  
            <if test="materialName != null  and materialName != ''"> and material_name like concat('%', #{materialName}, '%')</if>
            <if test="params.beginProductionDate != null and params.endProductionDate != null"> and production_date between #{params.beginProductionDate} and #{params.endProductionDate}</if>
            <if test="productionDate != null"> and production_date = #{productionDate}</if>
        </where>
    </select>
    
    <select id="selectWzMaterialsManagementByMaterialManagementCode" parameterType="Long" resultMap="WzMaterialsManagementResult">
        <include refid="selectWzMaterialsManagementVo"/>
        where material_management_code = #{materialManagementCode}
    </select>

    <insert id="insertWzMaterialsManagement" parameterType="WzMaterialsManagement" useGeneratedKeys="true" keyProperty="materialManagementCode">
        insert into wz_materials_management
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="materialId != null and materialId != ''">material_id,</if>
            <if test="materialName != null and materialName != ''">material_name,</if>
            <if test="batchId != null">batch_id,</if>
            <if test="materialSpecification != null">material_specification,</if>
            <if test="materialUnit != null">material_unit,</if>
            <if test="currentStock != null">current_stock,</if>
            <if test="stockAlarm != null">stock_alarm,</if>
            <if test="productionDate != null">production_date,</if>
            <if test="expirationDate != null">expiration_date,</if>
            <if test="expirationAlarm != null">expiration_alarm,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="materialId != null and materialId != ''">#{materialId},</if>
            <if test="materialName != null and materialName != ''">#{materialName},</if>
            <if test="batchId != null">#{batchId},</if>
            <if test="materialSpecification != null">#{materialSpecification},</if>
            <if test="materialUnit != null">#{materialUnit},</if>
            <if test="currentStock != null">#{currentStock},</if>
            <if test="stockAlarm != null">#{stockAlarm},</if>
            <if test="productionDate != null">#{productionDate},</if>
            <if test="expirationDate != null">#{expirationDate},</if>
            <if test="expirationAlarm != null">#{expirationAlarm},</if>
         </trim>
    </insert>

    <update id="updateWzMaterialsManagement" parameterType="WzMaterialsManagement">
        update wz_materials_management
        <trim prefix="SET" suffixOverrides=",">
            <if test="materialId != null and materialId != ''">material_id = #{materialId},</if>
            <if test="materialName != null and materialName != ''">material_name = #{materialName},</if>
            <if test="batchId != null">batch_id = #{batchId},</if>
            <if test="materialSpecification != null">material_specification = #{materialSpecification},</if>
            <if test="materialUnit != null">material_unit = #{materialUnit},</if>
            <if test="currentStock != null">current_stock = #{currentStock},</if>
            <if test="stockAlarm != null">stock_alarm = #{stockAlarm},</if>
            <if test="productionDate != null">production_date = #{productionDate},</if>
            <if test="expirationDate != null">expiration_date = #{expirationDate},</if>
            <if test="expirationAlarm != null">expiration_alarm = #{expirationAlarm},</if>
        </trim>
        where material_id = #{materialId}
    </update>

    <delete id="deleteWzMaterialsManagementByMaterialManagementCode" parameterType="Long">
        delete from wz_materials_management where material_management_code = #{materialManagementCode}
    </delete>

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

    <select id="selectWzMaterialsManagementByMaterialID" parameterType="String" resultMap="WzMaterialsManagementResult">
        SELECT * FROM wz_materials_management WHERE material_id = #{materialID}
    </select>
</mapper>