<?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.stock.mapper.StockDetailMapper">

    <select id="getMaterialInfoByQrCode" resultType="java.util.Map">
        select sd.id,
               m.Name  materialName,
               m.Code  materialCode,
               sd.Quantity,
               sh.Name houseName,
               sd.PlaceAreaId
        from stock_detail sd
                 left join stock_tag st on sd.QRCode = st.QRCode
                 left join material m on st.MaterialCode = m.code
                 left join stock_house sh on sh.ID = sd.StockHouseId
        where sd.QRCode = #{qrCode}
    </select>

    <select id="restrictedPicking" resultType="java.util.LinkedHashMap">
        select t.id,
               t.availableQuantity,
               t.type,
               t.planDate,
               m.Code  materialCode,
               m.Name  materialName,
               m.Width materialWidth
        from (
                 (
                     select ss.id,
                            ss.material_quantity availableQuantity,
                            ss.material_code     code,
                            1                    type,
                            ss.plan_date         planDate
                     from stock_scheduling ss
                     where ss.printer = #{jobNo}
                       and ss.material_status = 0
                       and ss.is_delete = 0
                 )
                 union all
                 (
                     select ss.id,
                            ss.paper_quantity availableQuantity,
                            ss.paper_code     code,
                            2                 type,
                            ss.plan_date      planDate
                     from stock_scheduling ss
                     where ss.paper_man = #{jobNo}
                       and ss.paper_status = 0
                       and ss.is_delete = 0
                 )
             ) t
                 left join material m on t.code = m.Code
        where t.planDate = #{date}
        order by t.availableQuantity
    </select>

    <select id="getMaterialQuantity" resultType="java.util.Map">
        select s.MaterialCode materialCode, s.Quantity totalQuantity, su.Quantity userQuantity
        from stock s
                 left join stockuser su on s.MaterialCode = su.MaterialCode
            ${ew.customSqlSegment}
    </select>

    <select id="selectRecommendMaterial" resultType="java.util.Map">
        select sd.RfidCode rfidCode, sd.Quantity quantity, sa.Name areaName
        from stock_detail sd
                 left join stock_area sa on sa.ID = sd.PlaceAreaId
        where sd.MaterialCode = #{materialCode}
          and sd.StockHouseId = 'c185883dba22478cb593d33f6b66cc53'
        order by sd.Quantity
    </select>

    <select id="getPlanList" resultType="java.util.Map">
        (select ss.material_code     code,
                ss.material_quantity quantity,
                ss.printer           jobNo
         from stock_scheduling ss
             ${ew.customSqlSegment})
        union all
        (select ss.paper_code     code,
                ss.paper_quantity quantity,
                ss.paper_man      jobNo
         from stock_scheduling ss
             ${ew.customSqlSegment})
    </select>

    <select id="selectUserByJobNoList" resultType="java.util.Map">
        select uu.JobNo    jobNo,
               uu.RealName userName
        from u_user uu
            ${ew.customSqlSegment}
    </select>

    <select id="getPlanListByUser" resultType="java.util.Map">
        (
        select
        ss.material_code code,
        ss.material_quantity quantity,
        ss.material_flag flag
        from stock_scheduling ss
        ${ew.customSqlSegment}
        <if test="jobNo neq null and jobNo neq ''">
            and ss.printer = #{jobNo}
        </if>
        )
        union all
        (
        select
        ss.paper_code code,
        ss.paper_quantity quantity,
        ss.paper_flag flag
        from stock_scheduling ss
        ${ew.customSqlSegment}
        <if test="jobNo neq null and jobNo neq ''">
            and ss.paper_man = #{jobNo}
        </if>
        )
    </select>

    <select id="getOutputList" resultType="java.util.Map">
        select swd.MaterialCode  materialCode,
               swd.ChangeNum     changeNum,
               swd.RfidCode      rfidCode,
               si.InOutStorageNo inOutStorageNo
        from stock_inoutbill si
                 left join stock_water sw on si.ID = sw.LinkId
                 left join stock_waterdetial swd on sw.id = swd.WaterId
            ${ew.customSqlSegment}
    </select>

    <select id="getMaterialWidthByCode" resultType="java.util.Map">
        select m.Code code, m.Width width, m.Name name
        from material m
            ${ew.customSqlSegment}
    </select>

    <select id="selectAdditionalMeterAndArea" resultType="java.util.Map">
        select sum(st.Quantity)                 appointMeter,
               sum(st.Quantity * m.Width / 100) appointArea
        from stock_tag st
                 left join material m on st.MaterialCode = m.Code
            ${ew.customSqlSegment}
    </select>

    <select id="selectClaimed" resultType="java.util.Map">
        select m.Name            materialName,
               m.Code            materialCode,
               sca.material_rfid materialRfid,
               u.RealName        userName,
               sca.quantity      quantity,
               m.Width           materialWidth
        from stock_scheduling_actual sca
                 left join material m on m.Code = sca.material_code
                 left join u_user u on u.JobNo = sca.job_no
                 right join (
            (
                select ss.plan_date     plan_date,
                       ss.material_flag flag
                from stock_scheduling ss
            )
            union all
            (
                select ss.plan_date  plan_date,
                       ss.paper_flag flag
                from stock_scheduling ss
            )
        ) ss on ss.flag = sca.flag
            ${ew.customSqlSegment}
    </select>

</mapper>
