<?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.jsh.erp.datasource.mappers.MaterialBatchMapper">

    <select id="getMaterialBatchByMaterialId" resultType="com.jsh.erp.datasource.entities.MaterialBatch">
        SELECT id,material_id,depot_id,inventory FROM material_batch
        WHERE material_id = #{mid} AND depot_id = #{depotID} AND inventory > 0
        ORDER BY production_date ASC
    </select>

    <select id="getInventorySumByDepotAndMid"  resultType="java.math.BigDecimal">
        select ifnull(sum(inventory),0) inventory from material_batch
        where material_id = #{mid}
        <if test="depotList.size()>0">
            and depot_id in
            <foreach collection="depotList" item="item" index="index" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
        AND ifnull(delete_Flag,'0') !='1'
    </select>

    <select id="findBySelectWithBarCode" parameterType="com.jsh.erp.datasource.entities.MaterialExample" resultType="com.jsh.erp.datasource.entities.MaterialVo4Unit">
        select m.*,u.name unit_name,mc.name categoryName,me.bar_code,me.id meId,me.commodity_unit,me.sku,
        me.production_date,me.expiry_num,me.supplier_id,me.batch_number,me.depot_id,me.position,d.`name` depotName,s.supplier supplierName,me.inventory
        from jsh_material m
        left join material_batch me on m.id=me.material_id and ifnull(me.delete_Flag,'0') !='1'
        left join jsh_unit u on m.unit_id=u.id and ifnull(u.delete_Flag,'0') !='1'
        left JOIN jsh_material_category mc on m.category_id = mc.id and ifnull(mc.delete_Flag,'0') !='1'
        left JOIN jsh_depot d on me.depot_id = d.id and ifnull(d.delete_Flag,'0') !='1'
        left JOIN jsh_supplier s on me.supplier_id = s.id and ifnull(s.delete_Flag,'0') !='1'
        where m.enabled=1 and me.id is not null
        <if test="q != null and q !=''">
            <bind name="bindKey" value="'%'+q+'%'"/>
            and (me.bar_code like #{bindKey} or me.batch_number like #{bindKey} or m.name like #{bindKey} or m.mnemonic like #{bindKey} or m.standard like #{bindKey}
            or m.model like #{bindKey} or m.color like #{bindKey} or m.brand like #{bindKey} )
        </if>
        <if test="standardOrModel != null and standardOrModel !=''">
            <bind name="bindStandardOrModel" value="'%'+standardOrModel+'%'"/>
            and (m.standard like #{bindStandardOrModel} or m.model like #{bindStandardOrModel})
        </if>
        <if test="color != null and color !=''">
            <bind name="bindColor" value="'%'+color+'%'"/>
            and m.color like #{bindColor}
        </if>
        <if test="brand != null and brand !=''">
            <bind name="bindBrand" value="'%'+brand+'%'"/>
            and m.brand like #{bindBrand}
        </if>
        <if test="enableSerialNumber != null and enableSerialNumber !=''">
            and m.enable_serial_number = #{enableSerialNumber}
        </if>
        <if test="enableBatchNumber != null and enableBatchNumber !=''">
            and m.enable_batch_number = #{enableBatchNumber}
        </if>
        <if test="depotId != null and depotId !=''">
            and me.depot_id = #{depotId}
        </if>
        and ifnull(m.delete_flag,'0') !='1'
        ORDER BY m.id desc, me.default_flag desc, me.id asc
        <if test="offset != null and rows != null">
            limit #{offset},#{rows}
        </if>
    </select>

    <select id="getMaterialByBatchNumber" parameterType="com.jsh.erp.datasource.entities.MaterialExample" resultType="com.jsh.erp.datasource.vo.TaskStocktakingItemVO">
        select
        mc.`name` AS category_name,
        m.`name` AS material_name,
        m.standard AS standard,
        m.model AS model,
        m.color AS color,
        m.brand AS brand,
        me.position AS position,
        me.batch_number AS batch_number,
        m.system_sku AS system_sku,
        me.sku AS sku,
        me.commodity_unit AS commodity_unit,
        me.production_date AS production_date,
        s.supplier AS supplier_name,
        me.bar_code AS bar_code,
        me.inventory AS inventory,
        me.expiry_num AS expiry_num,
        d.`name` AS depot_name
        from jsh_material m
        left join material_batch me on m.id=me.material_id and ifnull(me.delete_Flag,'0') !='1'
        left join jsh_unit u on m.unit_id=u.id and ifnull(u.delete_Flag,'0') !='1'
        LEFT JOIN jsh_material_category mc ON mc.id = m.category_id
        left JOIN jsh_depot d on me.depot_id = d.id and ifnull(d.delete_Flag,'0') !='1'
        left JOIN jsh_supplier s on me.supplier_id = s.id and ifnull(s.delete_Flag,'0') !='1'
        where
        me.batch_number in (
        <foreach collection="batchNumberArray" item="batchNumber" separator=",">
            #{batchNumber}
        </foreach>
        )
        and ifnull(m.delete_flag,'0') !='1'
        order by m.id desc, me.default_flag desc, me.id asc
    </select>

</mapper>