<?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.contract.ContractMapper">

    <select id="amountStatistics" resultType="java.util.Map">
        select ifnull(sum(c.amount_money), 0) amountMoney,
               m.technology_type              type
        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="getPage" resultType="java.util.Map">
        <include refid="select"/>
    </select>

    <select id="getList" resultType="java.util.Map">
        <include refid="select"/>
    </select>
    <select id="getAdjustInfo" resultType="com.fjhx.params.contract.AdjustInfo">
        select wb.contract_id,
               wb.contract_details_id,
               c.adjust_amount,
               cd.adjust_quantity,
               qt.adjust_rate,
               wb.tag_quantity_sum quantity,
               wb.price
        from quality_testing qt
                 left join water_batch wb on qt.water_batch_id = wb.id
                 left join contract_details cd on wb.contract_details_id = cd.id
                 left join contract c on cd.contract_id = c.id
        where qt.id = #{qualityTestingId}
    </select>

    <sql id="select">
        select cd.contract_id,
               cd.contract_code,
               cd.id                                          contractDetailsId,
               cd.code                                        contractDetailsCode,
               cd.apply_purchase_id,
               cd.apply_purchase_code,
               cd.apply_purchase_details_id,
               c.supplier_id,
               now()                                          signTime,
               adddate(now(), interval m.safety_warn_day day) deliverTime,
               m.id                                           materialId,
               m.code                                         materialCode,
               m.name                                         materialName,
               m.spec                                         materialSpec,
               m.unit                                         materialUnit,
               s.name                                         supplierName,
               st.quantity                                    stockQuantity,
               cd.quantity                                    buyQuantity,
               cd.afloat_quantity                             afloatQuantity,
               cd.price                                       materialPrice,
               (cd.quantity * cd.price)                       amountMoney,
               c.flow_status,
               c.processed,
               cd.shipment_quantity,
               (cd.shipment_quantity * cd.price)              shipmentAmount,
               c.status,
               c.shipment_status
        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
                 left join stock st on m.id = st.material_id
            ${ew.customSqlSegment}
    </sql>

</mapper>
