<?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.sale.mapper.purchase.EhsdPurchaseProductMapper">
    <select id="getPage" resultType="com.fjhx.sale.entity.purchase.vo.EhsdPurchaseProductVo">
        select epp.id,
               epp.purchase_id,
               epp.product_id,
               epp.product_name,
               epp.product_model,
               epp.quantity,
               epp.price,
               epp.amount,
               epp.remark,
               epp.json,
               epp.create_user,
               epp.create_time,
               epp.update_user,
               epp.update_time
        from ehsd_purchase_product epp
            ${ew.customSqlSegment}
    </select>

    <select id="getGroupSumAmount" resultType="com.fjhx.sale.entity.purchase.vo.EhsdPurchaseProductVo">
        SELECT
            CONCAT(
            IFNULL(
            (SELECT IFNULL( SUM( amount ), 0 ) FROM ehsd_purchase_product_mountings WHERE FIND_IN_SET(purchase_product_id,GROUP_CONCAT(t1.id))),
            0
            ),
            "-",
            IFNULL( t2.currency, "¥" ),
            "-",
            IFNULL( t2.rate, 1 )) AS currencyAmount,
            t1.contract_id AS contractId
        FROM
            ehsd_purchase_product t1
        LEFT JOIN ehsd_purchase t2 ON t1.purchase_id = t2.id
        WHERE
        t2.`status` in(10,30)
        <if test="contractIds neq null and contractIds.size() > 0">
            <foreach collection="contractIds" item="contractId" open="AND t1.contract_id IN (" separator="," close=")">
                #{contractId}
            </foreach>
        </if>
        GROUP BY t1.contract_id
    </select>

    <select id="getPurchaseByContractId" resultType="com.fjhx.sale.entity.purchase.vo.EhsdPurchaseProductVo">
        SELECT
            t2.data_resource_id,
            IFNULL(t2.amount,0) AS amount ,
            t2.currency,
            IFNULL(t2.rate,0) AS rate,
            t1.product_id
        FROM
            ehsd_purchase_product t1
                LEFT JOIN ehsd_purchase t2 ON t1.purchase_id = t2.id
        WHERE
            t2.`status` IN ( 10, 30 )
        <if test="contractIds neq null and contractIds.size() > 0">
            <foreach collection="contractIds" item="contractId" open="AND t2.data_resource_id IN (" separator="," close=")">
                #{contractId}
            </foreach>
        </if>
    </select>
    <select id="getListByContractId" resultType="com.fjhx.sale.entity.purchase.vo.EhsdPurchaseProductVo">
        SELECT
            t1.*,
            t2.currency,
            t2.rate
        FROM
            ehsd_purchase_product t1
                LEFT JOIN ehsd_purchase t2 ON t1.purchase_id = t2.id
        WHERE
            t1.contract_id = #{contractId}
    </select>
    <select id="getPurchaseProductByContractProductIds" resultType="com.fjhx.sale.entity.purchase.po.EhsdPurchaseProduct">
        SELECT
        pp.*
        FROM
        ehsd_purchase_product pp
        LEFT JOIN ehsd_purchase p ON pp.purchase_id = p.id
        WHERE
        (p.`status` = 30
        OR p.`status` = 10)
        <foreach collection="dataResourceIds" item="dataResourceId" open="AND pp.data_resource_id IN (" separator=","
                 close=")">
            #{dataResourceId}
        </foreach>
    </select>
    <select id="getPurchaseQuantitySumByContractId"
            resultType="com.fjhx.sale.entity.purchase.vo.EhsdPurchaseProductVo">
        SELECT epp.product_id,
               sum(epp.quantity) AS quantity
        FROM ehsd_purchase_product epp
                 LEFT JOIN ehsd_purchase ep ON epp.purchase_id = ep.id
        WHERE ep.`status` IN (10, 30, 99)
          AND ep.data_resource_id = #{contractId}
        GROUP BY epp.product_id
    </select>

    <select id="getPurchaseQuantitySumBySubscribeDetailId" resultType="java.math.BigDecimal">
        SELECT IFNULL(sum(epp.quantity), 0)
        FROM ehsd_purchase_product epp
                 LEFT JOIN ehsd_purchase ep ON epp.purchase_id = ep.id
        WHERE ep.`status` IN (10, 30, 99)
          AND epp.subscribe_detailId = #{subscribeDetailId}
        GROUP BY epp.product_id
    </select>
    <select id="getList" resultType="com.fjhx.sale.entity.purchase.vo.EhsdPurchaseProductVo">
        SELECT *
        FROM (SELECT ep.`code`                               AS purchaseCode,
                     ep.create_time,
                     epp.id,
                     epp.tenant_id,
                     epp.purchase_id,
                     epp.product_id,
                     epp.product_name,
                     epp.product_model,
                     epp.quantity,
                     epp.exceed_quantity,
                     epp.price,
                     epp.amount,
                     epp.remark,
                     epp.create_user,
                     epp.update_user,
                     epp.update_time,
                     epp.data_resource,
                     epp.data_resource_id,
                     epp.contract_id,
                     epp.subscribe_detail_id,
                     epp.old_purchase_product_id,
                     (SELECT IFNULL(sum(swd.receipt_quantity), 0)
                      FROM stock_wait_details swd
                      WHERE swd.purchase_detail_id = epp.id) AS arrivalQuantity
              FROM ehsd_purchase_product epp
                       LEFT JOIN ehsd_purchase ep ON epp.purchase_id = ep.id) epp
            ${ew.customSqlSegment}
    </select>
</mapper>
