<?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.ehsMaterial.mapper.EhsMaterialMapper">

    <resultMap type="EhsMaterial" id="EhsMaterialResult">
        <result property="materialId"    column="material_id"    />
        <result property="materialType"    column="material_type"    />
        <result property="classFirst"    column="class_first"    />
        <result property="classSecond"    column="class_second"    />
        <result property="materialName"    column="material_name"    />
        <result property="materialSpecs"    column="material_specs"    />
        <result property="materialQuantity"    column="material_quantity"    />
        <result property="reserveLocation"    column="reserve_location"    />
        <result property="managerPerson"    column="Manager_person"    />
        <result property="tel"    column="tel"    />
        <result property="sendQuantity"    column="send_quantity"    />
        <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="updateUserId"    column="update_user_id"    />
    </resultMap>

    <sql id="selectEhsMaterialVo">
        select m.* , d.dept_name,a.send_quantity   from ehs_material m
             left join sys_dept d on m.dept_id = d.dept_id
             left join (select GROUP_CONCAT(send_quantity ) send_quantity,m.material_id
                    from  ehs_material m left join  ehs_material_detail dt
                    on dt.material_id= m.material_id and dt.outbound_user_id is null group by dt.material_id ) a
		on a.material_id= m.material_id
    </sql>

    <select id="selectEhsMaterialList" parameterType="EhsMaterial" resultMap="EhsMaterialResult">
        <include refid="selectEhsMaterialVo"/>
        <where>
            <if test="deptId != null "> and m.dept_Id = #{deptId}</if>
            <if test="materialType != null "> and material_type = #{materialType}</if>
            <if test="classFirst != null "> and class_first = #{classFirst}</if>
            <if test="classSecond != null  and classSecond != ''"> and class_second = #{classSecond}</if>
            <if test="materialName != null  and materialName != ''"> and material_name like concat('%', #{materialName}, '%')</if>
            <if test="materialSpecs != null  and materialSpecs != ''"> and material_specs = #{materialSpecs}</if>
            <if test="materialQuantity != null  and materialQuantity != ''"> and material_quantity = #{materialQuantity}</if>
            <if test="reserveLocation != null  and reserveLocation != ''"> and reserve_location like concat('%', #{reserveLocation}, '%')</if>
            <if test="managerPerson != null  and managerPerson != ''"> and Manager_person like concat('%', #{managerPerson}, '%')</if>
            <if test="tel != null  and tel != ''"> and tel like concat('%', #{tel}, '%')</if>
        </where>
    </select>

    <select id="selectEhsMaterialByMaterialId" parameterType="Long" resultMap="EhsMaterialResult">
        <include refid="selectEhsMaterialVo"/>
        where m.material_id = #{materialId}
    </select>




</mapper>