<?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.ruoyi.ehsMaterialDetail.mapper.EhsMaterialDetailMapper">

    <resultMap type="EhsMaterialDetail" id="EhsMaterialDetailResult">
            <result property="materialDetailId"    column="material_detail_id"    />
            <result property="materialId"    column="material_id"    />
            <result property="sendUserId"    column="send_user_id"    />
            <result property="sendUserName"    column="send_user_name"    />
            <result property="sendQuantity"    column="send_quantity"    />
            <result property="sendTime"    column="send_time"    />
            <result property="outboundUserId"    column="outbound_user_id"    />
            <result property="outboundUserName"    column="outbound_user_Name"    />
            <result property="outboundQuantity"    column="outbound_quantity"    />
            <result property="outboundTime"    column="outbound_time"    />
            <result property="createBy"    column="create_by"    />
            <result property="createTime"    column="create_time"    />
            <result property="updateBy"    column="update_by"    />
            <result property="updateTime"    column="update_time"    />
            <result property="remark"    column="remark"    />
            <result property="deptId"    column="dept_id"    />
            <result property="createUserId"    column="create_user_id"    />
        <result property="inventory"    column="inventory"    />
        <result property="operateType"    column="operate_type"    />
    </resultMap>

    <sql id="selectEhsMaterialDetailVo">
        select mt.*,u1.nick_name send_user_name,u2.nick_name outbound_user_Name
        from ehs_material_detail mt left join sys_user u1 on mt.send_user_id=u1.user_id
        left join sys_user u2 on mt.outbound_user_id=u2.user_id
    </sql>

    <select id="selectEhsMaterialDetailList" parameterType="EhsMaterialDetail" resultMap="EhsMaterialDetailResult">
        <include refid="selectEhsMaterialDetailVo"/>
        <where>
            <if test="materialId != null "> and material_id = #{materialId}</if>
            <if test="sendUserId != null "> and send_user_id = #{sendUserId}</if>
            <if test="sendQuantity != null  and sendQuantity != ''"> and send_quantity = #{sendQuantity}</if>
            <if test="sendTime != null "> and send_time = #{sendTime}</if>
            <if test="outboundUserId != null "> and outbound_user_id = #{outboundUserId}</if>
            <if test="outboundQuantity != null  and outboundQuantity != ''"> and outbound_quantity = #{outboundQuantity}</if>
            <if test="outboundTime != null "> and outbound_time = #{outboundTime}</if>
            <if test="deptId != null "> and dept_id = #{deptId}</if>
            <if test="inventory != null "> and inventory = #{inventory}</if>
            <if test="operateType != null "> and operate_type = #{operateType}</if>
            <if test="getOut != null "> and (outbound_user_id  is null or outbound_user_id ='')</if>
        </where>
    </select>

    <select id="selectEhsMaterialDetailByMaterialDetailId" parameterType="Long" resultMap="EhsMaterialDetailResult">
            <include refid="selectEhsMaterialDetailVo"/>
            where material_detail_id = #{materialDetailId}
    </select>

    <delete id="deleteEhsMaterialDetailByMaterialId" parameterType="Long">
        delete from ehs_material_detail where material_id = #{materialId} and outbound_user_id is null
    </delete>


</mapper>