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

    <select id="getTypeStatisticsList" resultType="java.util.Map">
        select m.technology_type type,
               m.width,
               apd.quantity
        from apply_purchase ap
                 left join apply_purchase_details apd on ap.id = apd.apply_purchase_id
                 left join material m on m.id = apd.material_id
            ${ew.customSqlSegment}
    </select>

    <select id="flowStatistics" resultType="java.util.Map">
        select count(0) count,
               ap.flow_status
        from apply_purchase ap
                 inner join apply_purchase_details apd on ap.id = apd.apply_purchase_id
                 left join material m on apd.material_id = m.id
            ${ew.customSqlSegment}
    </select>

    <select id="getPage" resultType="java.util.LinkedHashMap">
        select m.id                          materialId,
               m.code                        materialCode,
               m.name                        materialName,
               m.spec                        materialSpec,
               apd.create_time               applyTime,
               apd.quantity                  applyQuantity,
               ifnull(s.quantity, 0)         stockQuantity,
               ifnull(cd.afloat_quantity, 0) afloatQuantity,
               m.safety_stock                safetyStock,
               apd.purpose                   purpose,
               ap.remark                     remark,
               c.flow_status                 flowStatus
        from apply_purchase_details apd
                 left join apply_purchase ap on apd.apply_purchase_id = ap.id
                 left join material m on apd.material_id = m.id
                 left join stock s on s.material_id = m.id
                 left join contract_details cd on apd.id = cd.apply_purchase_details_id
                 left join contract c on c.id = cd.contract_id
            ${ew.customSqlSegment}
    </select>

    <select id="getApplyPurchaseDetailsList" resultType="java.util.Map">
        select m.name materialName,
               m.code materialCode,
               m.spec materialSpec,
               m.unit materialUnit,
               apd.quantity,
               apd.purpose
        from apply_purchase_details apd
                 left join material m on apd.material_id = m.id
        where apd.apply_purchase_id = #{applyPurchaseId}
    </select>

    <select id="getAmountStatisticsList" resultType="java.util.Map">
        select ifnull(m.technology_type, 4)                      materialType,
               if(c.status = 3, c.adjust_amount, c.amount_money) money,
               c.account_date                                    accountDate,
               c.supplier_id                                     supplierId,
               s.Name                                            supplierName
        from contract c
                 left join contract_details cd on c.id = cd.contract_id
                 left join material m on cd.material_id = m.id
                 left join supplier s on c.supplier_id = s.id
            ${ew.customSqlSegment}
    </select>

    <select id="amountPage" resultType="java.util.LinkedHashMap">
        select s.Name          supplierName,
               c.code          contractCode,
               m.name          materialName,
               cd.quantity     quantity,
               cd.price        price,
               c.amount_money  amountMoney,
               c.adjust_amount adjustAmount,
               c.approval_time approvalTime
        from contract c
                 left join contract_details cd on c.id = cd.contract_id
                 left join material m on cd.material_id = m.id
                 left join supplier s on c.supplier_id = s.id
            ${ew.customSqlSegment}
    </select>

</mapper>
