<?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.hw.mes.mapper.MesBaseMaterialInfoMapper">

    <resultMap type="MesBaseMaterialInfo" id="MesBaseMaterialInfoResult">
        <result property="materialId" column="material_id"/>
        <result property="erpId" column="erp_id"/>
        <result property="materialCode" column="material_code"/>
        <result property="oldMaterialCode" column="old_material_code"/>
        <result property="materialName" column="material_name"/>
        <result property="materialCategories" column="material_categories"/>
        <result property="materialSubclass" column="material_subclass"/>
        <result property="materialTypeId" column="material_type_id"/>
        <result property="batchFlag" column="batch_flag"/>
        <result property="materialUnitId" column="material_unit_id"/>
        <result property="materialUnit" column="material_unit"/>
        <result property="materialMatkl" column="material_matkl"/>
        <result property="materialSpec" column="material_spec"/>
        <result property="netWeight" column="net_weight"/>
        <result property="grossWeight" column="gross_weight"/>
        <result property="factoryId" column="factory_id"/>
        <result property="createOrgId" column="create_org_id"/>
        <result property="useOrgId" column="use_org_id"/>
        <result property="prodlineId" column="prodline_id"/>
        <result property="activeFlag" column="active_flag"/>
        <result property="deletedFlag" column="deleted_flag"/>
        <result property="remark" column="remark"/>
        <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="approveDate" column="approve_date"/>
        <result property="erpModifyDate" column="erp_modify_date"/>
        <result property="warehouseId" column="warehouse_id"/>
    </resultMap>

    <sql id="selectMesBaseMaterialInfoVo">
        select material_id,
               erp_id,
               material_code,
               old_material_code,
               material_name,
               material_categories,
               material_subclass,
               material_type_id,
               batch_flag,
               material_unit_id,
               material_unit,
               material_matkl,
               material_spec,
               net_weight,
               gross_weight,
               factory_id,
               create_org_id,
               use_org_id,
               prodline_id,
               active_flag,
               deleted_flag,
               remark,
               create_by,
               create_time,
               update_by,
               update_time,
               approve_date,
               erp_modify_date
        from mes_base_material_info
    </sql>

    <select id="selectMesBaseMaterialInfoList" parameterType="MesBaseMaterialInfo"
            resultMap="MesBaseMaterialInfoResult">
        <include refid="selectMesBaseMaterialInfoVo"/>
        <where>
            <if test="erpId != null ">and erp_id = #{erpId}</if>
            <if test="materialCode != null  and materialCode != ''">and material_code = #{materialCode}</if>
            <if test="oldMaterialCode != null  and oldMaterialCode != ''">and old_material_code = #{oldMaterialCode}
            </if>
            <if test="materialName != null  and materialName != ''">and material_name like concat('%', #{materialName},
                '%')
            </if>
            <if test="materialCategories != null  and materialCategories != ''">and material_categories =
                #{materialCategories}
            </if>
            <if test="materialSubclass != null  and materialSubclass != ''">and material_subclass =
                #{materialSubclass}
            </if>
            <if test="materialTypeId != null ">and material_type_id = #{materialTypeId}</if>
            <if test="batchFlag != null  and batchFlag != ''">and batch_flag = #{batchFlag}</if>
            <if test="materialUnitId != null ">and material_unit_id = #{materialUnitId}</if>
            <if test="materialUnit != null  and materialUnit != ''">and material_unit = #{materialUnit}</if>
            <if test="materialMatkl != null  and materialMatkl != ''">and material_matkl = #{materialMatkl}</if>
            <if test="materialSpec != null  and materialSpec != ''">and material_spec = #{materialSpec}</if>
            <if test="netWeight != null ">and net_weight = #{netWeight}</if>
            <if test="grossWeight != null ">and gross_weight = #{grossWeight}</if>
            <if test="factoryId != null ">and factory_id = #{factoryId}</if>
            <if test="createOrgId != null ">and create_org_id = #{createOrgId}</if>
            <if test="useOrgId != null ">and use_org_id = #{useOrgId}</if>
            <if test="prodlineId != null  and prodlineId != ''">and prodline_id = #{prodlineId}</if>
            <if test="activeFlag != null  and activeFlag != ''">and active_flag = #{activeFlag}</if>
            <if test="deletedFlag != null  and deletedFlag != ''">and deleted_flag = #{deletedFlag}</if>
            <if test="approveDate != null ">and approve_date = #{approveDate}</if>
            <if test="erpModifyDate != null ">and erp_modify_date = #{erpModifyDate}</if>
        </where>
    </select>

    <select id="selectMesBaseMaterialInfoByMaterialId" parameterType="Long" resultMap="MesBaseMaterialInfoResult">
        <include refid="selectMesBaseMaterialInfoVo"/>
        where material_id = #{materialId}
    </select>

    <insert id="insertMesBaseMaterialInfo" parameterType="MesBaseMaterialInfo" useGeneratedKeys="true"
            keyProperty="materialId">
        insert into mes_base_material_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="erpId != null">erp_id,</if>
            <if test="materialCode != null and materialCode != ''">material_code,</if>
            <if test="oldMaterialCode != null">old_material_code,</if>
            <if test="materialName != null and materialName != ''">material_name,</if>
            <if test="materialCategories != null and materialCategories != ''">material_categories,</if>
            <if test="materialSubclass != null">material_subclass,</if>
            <if test="materialTypeId != null">material_type_id,</if>
            <if test="batchFlag != null">batch_flag,</if>
            <if test="materialUnitId != null">material_unit_id,</if>
            <if test="materialUnit != null">material_unit,</if>
            <if test="materialMatkl != null">material_matkl,</if>
            <if test="materialSpec != null">material_spec,</if>
            <if test="netWeight != null">net_weight,</if>
            <if test="grossWeight != null">gross_weight,</if>
            <if test="factoryId != null">factory_id,</if>
            <if test="createOrgId != null">create_org_id,</if>
            <if test="useOrgId != null">use_org_id,</if>
            <if test="prodlineId != null">prodline_id,</if>
            <if test="activeFlag != null">active_flag,</if>
            <if test="deletedFlag != null">deleted_flag,</if>
            <if test="remark != null">remark,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="approveDate != null">approve_date,</if>
            <if test="erpModifyDate != null">erp_modify_date,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="erpId != null">#{erpId},</if>
            <if test="materialCode != null and materialCode != ''">#{materialCode},</if>
            <if test="oldMaterialCode != null">#{oldMaterialCode},</if>
            <if test="materialName != null and materialName != ''">#{materialName},</if>
            <if test="materialCategories != null and materialCategories != ''">#{materialCategories},</if>
            <if test="materialSubclass != null">#{materialSubclass},</if>
            <if test="materialTypeId != null">#{materialTypeId},</if>
            <if test="batchFlag != null">#{batchFlag},</if>
            <if test="materialUnitId != null">#{materialUnitId},</if>
            <if test="materialUnit != null">#{materialUnit},</if>
            <if test="materialMatkl != null">#{materialMatkl},</if>
            <if test="materialSpec != null">#{materialSpec},</if>
            <if test="netWeight != null">#{netWeight},</if>
            <if test="grossWeight != null">#{grossWeight},</if>
            <if test="factoryId != null">#{factoryId},</if>
            <if test="createOrgId != null">#{createOrgId},</if>
            <if test="useOrgId != null">#{useOrgId},</if>
            <if test="prodlineId != null">#{prodlineId},</if>
            <if test="activeFlag != null">#{activeFlag},</if>
            <if test="deletedFlag != null">#{deletedFlag},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="approveDate != null">#{approveDate},</if>
            <if test="erpModifyDate != null">#{erpModifyDate},</if>
        </trim>
    </insert>

    <update id="updateMesBaseMaterialInfo" parameterType="MesBaseMaterialInfo">
        update mes_base_material_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="erpId != null">erp_id = #{erpId},</if>
            <if test="materialCode != null and materialCode != ''">material_code = #{materialCode},</if>
            <if test="oldMaterialCode != null">old_material_code = #{oldMaterialCode},</if>
            <if test="materialName != null and materialName != ''">material_name = #{materialName},</if>
            <if test="materialCategories != null and materialCategories != ''">material_categories =
                #{materialCategories},
            </if>
            <if test="materialSubclass != null">material_subclass = #{materialSubclass},</if>
            <if test="materialTypeId != null">material_type_id = #{materialTypeId},</if>
            <if test="batchFlag != null">batch_flag = #{batchFlag},</if>
            <if test="materialUnitId != null">material_unit_id = #{materialUnitId},</if>
            <if test="materialUnit != null">material_unit = #{materialUnit},</if>
            <if test="materialMatkl != null">material_matkl = #{materialMatkl},</if>
            <if test="materialSpec != null">material_spec = #{materialSpec},</if>
            <if test="netWeight != null">net_weight = #{netWeight},</if>
            <if test="grossWeight != null">gross_weight = #{grossWeight},</if>
            <if test="factoryId != null">factory_id = #{factoryId},</if>
            <if test="createOrgId != null">create_org_id = #{createOrgId},</if>
            <if test="useOrgId != null">use_org_id = #{useOrgId},</if>
            <if test="prodlineId != null">prodline_id = #{prodlineId},</if>
            <if test="activeFlag != null">active_flag = #{activeFlag},</if>
            <if test="deletedFlag != null">deleted_flag = #{deletedFlag},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="approveDate != null">approve_date = #{approveDate},</if>
            <if test="erpModifyDate != null">erp_modify_date = #{erpModifyDate},</if>
        </trim>
        where material_id = #{materialId}
    </update>

    <delete id="deleteMesBaseMaterialInfoByMaterialId" parameterType="Long">
        delete
        from mes_base_material_info
        where material_id = #{materialId}
    </delete>

    <delete id="deleteMesBaseMaterialInfoByMaterialIds" parameterType="String">
        delete from mes_base_material_info where material_id in
        <foreach item="materialId" collection="array" open="(" separator="," close=")">
            #{materialId}
        </foreach>
    </delete>




    <!--新增以下-->

    <select id="selectMesBaseMaterialInfoListByMaterialIds" parameterType="String"
            resultMap="MesBaseMaterialInfoResult">
        <include refid="selectMesBaseMaterialInfoVo"/>
        where material_id in
        <foreach item="materialId" collection="array" open="(" separator="," close=")">
            #{materialId}
        </foreach>
    </select>





    <select id="selectMaterialInfos4AllocationWarehouse" parameterType="MesBaseMaterialInfo"
            resultMap="MesBaseMaterialInfoResult">
        select material_id,
        erp_id,
        material_code,
        old_material_code,
        material_name,
        material_categories,
        material_subclass,
        material_type_id,
        batch_flag,
        material_unit_id,
        material_unit,
        material_matkl,
        material_spec,
        net_weight,
        gross_weight,
        factory_id
        from mes_base_material_info mbmi
        <where>
        and not exists (select 1 from wms_warehouse_material wwm where mbmi.material_id = wwm.storage_id and wwm.storage_type='1' and wwm.warehouse_id=#{warehouseId})
            <if test="erpId != null ">and erp_id = #{erpId}</if>
            <if test="materialCode != null  and materialCode != ''">and material_code = #{materialCode}</if>
            <if test="oldMaterialCode != null  and oldMaterialCode != ''">and old_material_code = #{oldMaterialCode}
            </if>
            <if test="materialName != null  and materialName != ''">and material_name like concat('%', #{materialName},
                '%')
            </if>
            <if test="materialCategories != null  and materialCategories != ''">and material_categories =
                #{materialCategories}
            </if>
            <if test="materialSubclass != null  and materialSubclass != ''">and material_subclass =
                #{materialSubclass}
            </if>
            <if test="materialTypeId != null ">and material_type_id = #{materialTypeId}</if>
            <if test="batchFlag != null  and batchFlag != ''">and batch_flag = #{batchFlag}</if>
            <if test="materialUnitId != null ">and material_unit_id = #{materialUnitId}</if>
            <if test="materialUnit != null  and materialUnit != ''">and material_unit = #{materialUnit}</if>
            <if test="materialMatkl != null  and materialMatkl != ''">and material_matkl = #{materialMatkl}</if>
            <if test="materialSpec != null  and materialSpec != ''">and material_spec = #{materialSpec}</if>
            <if test="netWeight != null ">and net_weight = #{netWeight}</if>
            <if test="grossWeight != null ">and gross_weight = #{grossWeight}</if>
            <if test="factoryId != null ">and factory_id = #{factoryId}</if>
            <if test="createOrgId != null ">and create_org_id = #{createOrgId}</if>
            <if test="useOrgId != null ">and use_org_id = #{useOrgId}</if>
            <if test="prodlineId != null  and prodlineId != ''">and prodline_id = #{prodlineId}</if>
            <if test="activeFlag != null  and activeFlag != ''">and active_flag = #{activeFlag}</if>
            <if test="deletedFlag != null  and deletedFlag != ''">and deleted_flag = #{deletedFlag}</if>
            <if test="approveDate != null ">and approve_date = #{approveDate}</if>
            <if test="erpModifyDate != null ">and erp_modify_date = #{erpModifyDate}</if>
        </where>
    </select>


</mapper>
