<?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.apply.mapper.ApplyPurchaseMapper">

    <select id="technologyTypeStatistics" resultType="java.util.LinkedHashMap">
        select apd.PurchaseQty             purchaseQty,
               ifnull(m.TechnologyType, 4) technologyType,
               m.Width                     width,
               count(0)                    num
        from apply_purchase ap
                 inner join apply_purchasedetail apd on ap.ApplyBillNo = apd.ApplyBillNo
                 left join material m on apd.MaterialCode = m.Code
                 left join purchase_contract pc on pc.ApplyBillNo = ap.ApplyBillNo
                 left join stock_tag st on st.PurchaseBillNo = pc.PurchaseBillNo
            ${ew.customSqlSegment}
    </select>

    <select id="numStatistics" resultType="java.util.Map">
        select count(0) count, ap.ApproveBillState approveBillState
        from apply_purchase ap
                 inner join apply_purchasedetail apd on ap.ApplyBillNo = apd.ApplyBillNo
                 left join material m on apd.MaterialCode = m.Code
                 LEFT JOIN purchase_contract pc
                           ON pc.ApplyBillNo = apd.ApplyBillNo AND pc.MaterialCode = apd.MaterialCode
            ${ew.customSqlSegment}
    </select>

    <select id="totalNumStatistics" resultType="java.util.Map">
        SELECT sum(t.purchaseQuantity)                        AS purchaseQuantity,
               sum(t.purchaseQuantity - ifnull(t.arrived, 0)) AS onTheWayQuantity
        FROM (
                 SELECT apd.PurchaseQty                                                AS purchaseQuantity,
                        IF(pc.PurContractState = 3, apd.PurchaseQty, sum(st.Quantity)) AS arrived
                 FROM apply_purchase ap
                          INNER JOIN apply_purchasedetail apd ON ap.ApplyBillNo = apd.ApplyBillNo
                          LEFT JOIN material m on m.Code = apd.MaterialCode
                          LEFT JOIN purchase_contract pc
                                    ON pc.ApplyBillNo = apd.ApplyBillNo AND pc.MaterialCode = apd.MaterialCode
                          LEFT JOIN stock_tag st ON st.PurchaseBillNo = pc.PurchaseBillNo AND st.RfidCode IS NOT NULL
                     ${ew.customSqlSegment}
             ) t
    </select>

    <select id="getPage" resultType="java.util.LinkedHashMap">
        select apd.ApplyBillNo,
               m.Code                         materialCode,
               m.Name                         materialName,
               m.Spec                         spec,
               ap.CreatedTime                 applyTime,
               apd.PurchaseQty                purchaseQty,
               m.SafetyStock                  safetyQty,
               apd.Purpose                    purpose,
               (select pc.PurchaseQty - sum(st.Quantity)
                from stock_tag st
                where st.PurchaseBillNo = pc.PurchaseBillNo
                  and st.RfidCode is not null
                  and pc.PurContractState != 3
                  and st.IsDelete = 0
               )                              onWayQuantity,
               ifnull(pc.PurContractState, 0) purContractState,
               ap.ApplyAccount                applyAccount
        from apply_purchase ap
                 inner join apply_purchasedetail apd on ap.ApplyBillNo = apd.ApplyBillNo
                 left join material m on apd.MaterialCode = m.Code
                 left join purchase_contract pc
                           on pc.ApplyBillNo = apd.ApplyBillNo and pc.MaterialCode = apd.MaterialCode
            ${ew.customSqlSegment}
    </select>

    <select id="selectDelay" resultType="java.util.Map">
        select sd.MaterialCode                                          materialCode,
               sum(sd.Quantity)                                         stockQty,
               max(sd.CreatedTime)                                      maxDelayTime,
               sum(if(sd.CreatedTime &lt; #{dateTime}, sd.Quantity, 0)) delayQuantity
        from stock_detail sd
            ${ew.customSqlSegment}
    </select>

</mapper>
