<?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.fjhx.material.mapper.MaterialSMapper">

    <select id="selectMaList" resultType="com.fjhx.entity.material.Material">
        SELECT
        t1.*
        FROM
        material t1
        LEFT JOIN (
        SELECT
        materialcode,
        sum( swd.ChangeNum ) AS sum
        FROM
        stock_waterdetial swd
        WHERE
        swd.StockChangeType IN ( 20, 23 )
        GROUP BY MaterialCode
        ) AS t2 ON t1.`Code` = t2.materialcode
        <include refid="list_condition"/>
        ORDER BY t2.sum DESC
        <include refid="com.fjhx.supplier.mapper.SupplierMapper.sql_limit"/>
    </select>

    <select id="getMaterialPrice" resultType="java.util.Map">
        SELECT s.`Name`      name,
               s.ID          id,
               sp.Price      price,
               max(sp.Price) maxPrice,
               min(sp.Price) minPrice
        FROM supplier_price sp
                 LEFT JOIN supplier s ON sp.FactoryId = s.ID
        WHERE sp.IsDelete != 1
          AND sp.FactoryId != ''
          AND sp.FactoryId IS NOT NULL
          AND sp.MaterialCode = #{materialCode}
          AND s.`Name` IS NOT NULL
        GROUP BY sp.FactoryId,
                 sp.MaterialCode
        ORDER BY sp.CreatedTime
    </select>


    <select id="getInRfidData" resultType="java.util.Map">
        SELECT m.ID        materialId,
               m.CODE      materialCode,
               m.NAME      materialName,
               st.Quantity quantity,
               st.RfidCode rfid
        FROM stock_tag st
                 LEFT JOIN stock_detail sd ON st.rfidCode = sd.rfidCode AND sd.IsDelete = 0
                 LEFT JOIN material m ON m.CODE = st.MaterialCode
                 LEFT JOIN stock_waterdetial swd
                           ON swd.RfidCode = st.RfidCode AND swd.IsDelete = 0 AND swd.StockChangeType IN (20, 23)
            ${ew.customSqlSegment}
    </select>

    <select id="getOutRfidData" resultType="java.util.Map">
        SELECT m.ID        materialId,
               m.CODE      materialCode,
               m.NAME      materialName,
               sd.Quantity quantity,
               sd.RfidCode rfid
        FROM stock_detail sd
                 INNER JOIN material m ON m.CODE = sd.MaterialCode
            ${ew.customSqlSegment}
    </select>


    <sql id="list_condition">
        <where>
            t1.IsDelete = 0
            <if test="search neq null and search neq '' ">
                AND (INSTR(t1.`Name`, #{search}) > 0)
            </if>
        </where>
    </sql>
</mapper>
