<?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.boco.nbd.wios.manage.mapper.extdb.MaterialsMapperEx" >

  <insert id="insertMaterials" >
    INSERT INTO t_materials (id, appkey, materials_name, materials_code, regular_model, status, unit, type, description,create_time,create_account,modify_account,modify_time)
    values
    <foreach collection="materialsList" item="item" index="index"
             separator=",">
      (#{item.id},#{item.appkey},#{item.materialsName},#{item.materialsCode},#{item.regularModel},#{item.status},#{item.unit},#{item.type},'',#{item.createTime},#{item.createAccount},#{item.modifyAccount},#{item.modifyTime})
    </foreach>
	ON DUPLICATE KEY UPDATE
        materials_name = values(materials_name),materials_code = values(materials_code),regular_model = values(regular_model),unit = values(unit),modify_account = values(modify_account),modify_time = now(),type = values(type)
  </insert>


    <select id="qryMaterialsList" resultType="com.boco.nbd.wios.manage.entity.bo.MaterialsEx">
        SELECT
            tm.id,
            tm.materials_name,
            tm.materials_code,
            tm.regular_model,
            tm.unit,
            tm.type,
            tm.description
    FROM
        t_materials tm
        LEFT JOIN t_warehouse_materials twm ON tm.id = twm.materials_id
        where tm.status = 1
        and tm.appkey = #{appkey}
        <if test="materialsName != null and materialsName != ''">
            and tm.materials_name like CONCAT('%',#{materialsName},'%')
        </if>
        <if test="materialsCode != null and materialsCode != ''">
            and tm.materials_code like CONCAT('%',#{materialsCode},'%')
        </if>
        <if test="regularModel != null and regularModel != '' ">
            and tm.regular_model like CONCAT('%',#{regularModel},'%')
        </if>
        <if test="type != null">
            and tm.type = #{type}
        </if>
        <if test="id != null and id != ''">
            and tm.id = #{id}
        </if>
        <if test="typesList != null and typesList.size() > 0">
            and tm.type in
            <foreach collection="typesList" item="item" index="index"
                     open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="warehouseIds != null and warehouseIds.size() > 0">
            and twm.warehouse_id in
            <foreach collection="warehouseIds" item="item" index="index"
                     open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        group by tm.id order by tm.create_time desc
    </select>



    <select id="qryMaterialsStockList" resultType="com.boco.nbd.wios.manage.entity.bo.MaterialsEx">
        SELECT
        tm.id,
        tm.materials_name,
        tm.materials_code,
        tm.regular_model,
        tm.unit,
        tm.type,
        tm.description,
        twm.warehouse_id,
        tw.name warehouseName,
        twm.total_number totalNumber,
        twm.has_number hasNumber,
        twm.remaining_number remainingNumber,
        twm.stay_with_number stayWithNumber
        FROM
        t_materials tm
        LEFT JOIN t_warehouse_materials twm ON tm.id = twm.materials_id
        LEFT JOIN t_warehouse tw on tw.id = twm.warehouse_id
        where tm.status = 1
        and tm.appkey = #{appkey}
        <if test="materialsName != null and materialsName != ''">
            and tm.materials_name like CONCAT('%',#{materialsName},'%')
        </if>
        <if test="materialsCode != null and materialsCode != ''">
            and tm.materials_code like CONCAT('%',#{materialsCode},'%')
        </if>
        <if test="regularModel != null and regularModel != '' ">
            and tm.regular_model like CONCAT('%',#{regularModel},'%')
        </if>
        <if test="type != null">
            and tm.type = #{type}
        </if>
        <if test="warehouseIds != null and warehouseIds.size() > 0">
            and twm.warehouse_id in
            <foreach collection="warehouseIds" item="item" index="index"
                     open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="id != null and id != ''">
            and tm.id = #{id}
        </if>
        <if test="typesList != null and typesList.size() > 0">
            and tm.type in
            <foreach collection="typesList" item="item" index="index"
                     open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="isBindWareHouse != null and isBindWareHouse == 1">
            and tw.name is not null
        </if>
        group by tw.id,tm.id order by tm.create_time desc
    </select>


    <select id="qryMaterialsCount" resultType="com.boco.nbd.wios.manage.entity.bo.MaterialsEx">
        SELECT
            tm.id,
            tm.materials_name,
            tm.materials_code,
            tm.regular_model,
            tm.unit,
            tm.type,
            tm.description
        FROM
            t_materials tm
        where tm.status = 1
            and tm.appkey = #{appkey}
        <if test="materialsName != null and materialsName != ''">
            and tm.materials_name = #{materialsName}
        </if>
        <if test="materialsCode != null and materialsCode != ''">
            and tm.materials_code = #{materialsCode}
        </if>
        <if test="regularModel != null and regularModel != '' ">
            and tm.regular_model = #{regularModel}
        </if>
        <if test="type != null">
            and tm.type = #{type}
        </if>
    </select>

  <update id="delMaterialsById">
    update
        t_materials
    set
        status = 0
    where
        id = #{materialsId}
  </update>

    <select id="getWarehouseMaterials" parameterType="string" resultType="com.boco.nbd.wios.manage.entity.bo.WarehouseMaterials">
        SELECT
            warehouse_id,
            materials_id,
            total_number,
            has_number,
            remaining_number,
            stay_with_number
        FROM
            t_warehouse_materials
        WHERE
            1=1
            <if test="materialsId != null and materialsId != ''">
                and warehouse_id = #{warehouseId}
            </if>
            <if test="warehouseId != null and warehouseId != ''">
                AND materials_id = #{materialsId}
            </if>
    </select>

    <update id="updateWarehouseMaterials">
        UPDATE t_warehouse_materials
        SET
            <if test="type != null and type == 3">
                stay_with_number = stay_with_number - #{quantity},
                has_number = has_number + #{quantity}
            </if>
            <if test="type != null and type != 3">
                remaining_number = remaining_number - #{quantity},
                has_number = has_number + #{quantity}
            </if>
        WHERE
            warehouse_id = #{warhouseId}
            AND materials_id = #{materialsId}
    </update>

    <update id="increaseInventoryQuantity">
        UPDATE t_warehouse_materials
        SET
            total_number = total_number + #{quantity},
            has_number = has_number - #{quantity},
            remaining_number = remaining_number + #{quantity},
            stay_with_number = stay_with_number + #{quantity}
        WHERE
        warehouse_id = #{warhouseId}
        AND materials_id = #{materialsId}
    </update>
    <update id="reduceInventoryQuantity">
        UPDATE t_warehouse_materials
        SET
            total_number = total_number - #{quantity},
            has_number = has_number + #{quantity},
            remaining_number = remaining_number - #{quantity},
            stay_with_number = stay_with_number - #{quantity}
        WHERE
            warehouse_id = #{warhouseId}
          AND materials_id = #{materialsId}
    </update>


    <insert id="insertWarehouseMaterials">
        insert into t_warehouse_materials
        ( warehouse_id,materials_id,total_number,has_number,remaining_number)
        values
            (#{warehouseId},#{materialsId},#{totalNumber},#{hasNumber},#{remainingNumber})
        ON DUPLICATE KEY UPDATE total_number = total_number + values(total_number),remaining_number = remaining_number + values(remaining_number)
    </insert>

    <select id="selectMaterialsCode" parameterType="String" resultType="String">
        SELECT
            t_materials.materials_code
        FROM
            t_materials
        WHERE
            t_materials.id=#{id}
    </select>


    <select id="selectIdForPrice" parameterType="String" resultType="String">
        SELECT
            tm.id
        FROM
           t_materials tm
        where tm.status = 1 AND
            tm.materials_code=#{materialsCode}
    </select>

    <select id="materialsByMaterialsCode" parameterType="string" resultType="com.boco.nbd.wios.manage.entity.bo.MaterialsEx">
            select id,materials_name,materials_code,type from t_materials where materials_code=#{materialsCode}
    </select>



</mapper>