<?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.jeeplus.modules.supply.materialLibrary.mapper.MaterialLibraryMapper">

    <sql id="MaterialLibraryColumnss">
        ml.id as "id",
        ml.material_code as "materialCode",
        ml.material_description as "materialDescription",
        ml.unit_of_measurement as "unitOfMeasurement"
    </sql>

    <select id="get" resultType="com.jeeplus.modules.supply.materialLibrary.entity.MaterialLibrary">
        SELECT
        <include refid="MaterialLibraryColumnss"/>
        FROM material_library ml
        WHERE ml.id = #{id}
    </select>

    <select id="findList" resultType="MaterialLibrary" >
        SELECT
        <include refid="MaterialLibraryColumnss"/>
        from material_library ml
        <where>
            ml.del_flag = #{DEL_FLAG_NORMAL}
            ${dataScope}
            <if test="materialCode != null and materialCode != ''">
                AND ml.material_code like concat('%',#{materialCode},'%')
            </if>
            <if test="materialDescription != null and materialDescription != ''">
                AND ml.material_description like concat('%',#{materialDescription},'%')
            </if>
            <if test="unitOfMeasurement != null and unitOfMeasurement != ''">
                AND ml.unit_of_measurement like concat('%',#{unitOfMeasurement},'%')
            </if>
        </where>
        GROUP BY ml.material_code
        <choose>
            <when test="page !=null and page.orderBy != null and page.orderBy != ''">
                ORDER BY ${page.orderBy}
            </when>
            <otherwise>
                ORDER BY ml.update_date DESC
            </otherwise>
        </choose>
    </select>

    <select id="getMaterialCodeByMaterialCodeList" resultType="java.lang.String">
        select material_code from material_library
        <where>
            <if test="materialCodeList!=null and materialCodeList.size!=0">
                material_code in
                <foreach collection="materialCodeList" item="materialCode" separator="," open="(" close=")">
                    #{materialCode}
                </foreach>
            </if>
        </where>
    </select>

    <select id="deleteMaterialLibraryByMaterialCodeList">
        delete from material_library ml
        <where>
            <if test="materialCodeList!=null and materialCodeList.size!=0">
                ml.material_code in
                <foreach collection="materialCodeList" item="materialCode" separator="," open="(" close=")">
                    #{materialCode}
                </foreach>
            </if>
        </where>
    </select>

    <select id="getCountByMaterialCodeList" resultType="integer">
        select count(distinct ml.material_code) from Material_library ml
        <where>
            <if test="materialCodeList!=null and materialCodeList.size!=0">
                ml.material_code in
                <foreach collection="materialCodeList" item="materialCode" separator="," open="(" close=")">
                    #{materialCode}
                </foreach>
            </if>
        </where>
    </select>

    <!--批量新增-->
    <insert id="insertList">
        insert into material_library (
        material_code,
        material_description,
        unit_of_measurement,
        id,
        create_by,
        create_date,
        update_by,
        update_date,
        remarks,
        del_flag
        )
        values
        <foreach collection="list" item="item" index="index" separator="," >
            (
            #{item.materialCode},
            #{item.materialDescription},
            #{item.unitOfMeasurement},
            #{item.id},
            #{item.createBy.id},
            #{item.createDate},
            #{item.updateBy.id},
            #{item.updateDate},
            #{item.remarks},
            #{item.delFlag}
            )
        </foreach>
    </insert>

    <delete id="deleteList">
        delete from material_library
        <where>
            material_code in
            <foreach collection="materialCodeList" item="materialCode" separator="," open="(" close=")">
                #{materialCode}
            </foreach>
        </where>
    </delete>
</mapper>
