<?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.purchase.mapper.subscribe.SubscribeDetailMapper">
    <select id="getPage" resultType="com.fjhx.purchase.entity.subscribe.vo.SubscribeDetailVo">
        SELECT *
        FROM ((SELECT t1.*,
                      t2.`code`                AS subscribeCode,
                      t2.subcribe_content      AS subscribeContent,
                      t2.subcribe_time         AS subcribeTime,
                      t2.subcribe_name         AS subcribeName,
                      t2.id                    AS subcribeId,
                      t2.required_arrival_time AS requiredArrivalTime,
                      t2.put_warehouse_id      AS putWarehouseId,
                      t2.flow_id,
                      t2.company_id            AS corporationId,
                      null                     AS prodTag,
                      NULL                     AS prodRemark
               FROM subscribe_detail t1
                        LEFT JOIN subscribe t2 ON t1.subscribe_id = t2.id
                   AND t1.data_type = 0
               WHERE t1.data_type = 0)
              UNION ALL
              (SELECT t1.*,
                      t2.`code`      AS subscribeCode,
                      null           AS subscribeContent,
                      t1.create_time AS subcribeTime,
                      null           AS subcribeName,
                      t2.id          AS subcribeId,
                      null           AS requiredArrivalTime,
                      null           AS putWarehouseId,
                      t2.flow_id,
                      null           AS corporationId,
                      t2.prod_tag    AS prodTag,
                      t2.prod_remark AS prodRemark
               FROM subscribe_detail t1
                        LEFT JOIN contract t2 ON t1.contract_id = t2.id
               WHERE t1.data_type = 1)) t2
                 LEFT JOIN product_info pi ON t2.product_id = pi.id
            ${ew.customSqlSegment}
    </select>

    <select id="getDetail" resultType="com.fjhx.purchase.entity.subscribe.vo.SubscribeDetailVo">
        SELECT
        t1.*,
        IFNULL( SUM( t2.quantity ), 0 ) AS purchaseCount
        FROM
        subscribe_detail t1
        LEFT JOIN ehsd_purchase_product t2 ON t1.id = t2.subscribe_detail_id
        <where>
            <if test="ids neq null and ids.size() > 0">
                <foreach collection="ids" item="id" open="t1.id IN (" separator="," close=")">
                    #{id}
                </foreach>
            </if>
        </where>
        GROUP BY
            t1.id
    </select>
    <select id="subscribeStatistics" resultType="com.fjhx.purchase.entity.subscribe.vo.SubscribeDetailVo">
        SELECT count(DISTINCT (pi.id)) typeCount,
               count(DISTINCT (su.id)) counts,
               ifnull(pi.type, -1)     type
        FROM subscribe su
                 LEFT JOIN subscribe_detail sd ON su.id = sd.subscribe_id
                 LEFT JOIN product_info pi ON sd.bussiness_id = pi.id
            ${ew.customSqlSegment}
    </select>
    <select id="getPurchaseCountByIds" resultType="com.fjhx.purchase.entity.subscribe.po.SubscribeDetail">
        SELECT
        pp.subscribe_detail_id AS id,
        sum( pp.quantity ) AS `Count`
        FROM
        ehsd_purchase_product pp
        JOIN ehsd_purchase p ON pp.purchase_id = p.id
        <where>
            p.`status` IN ( 10, 30, 99 )
            <foreach collection="ids" item="id" open="and pp.subscribe_detail_id IN (" separator="," close=")">
                #{id}
            </foreach>
        </where>
        GROUP BY
        pp.subscribe_detail_id
    </select>
    <select id="getContractCountByContractIds"
            resultType="com.fjhx.purchase.entity.subscribe.vo.SubscribeDetailVo">
        SELECT
        cpb.contract_id,
        cpb.material_id AS productId,
        sum( cpb.quantity * cp.quantity ) AS count
        FROM
        contract_product_bom cpb
        LEFT JOIN contract_product cp ON cpb.contract_product_id = cp.id
        <where>
            <foreach collection="ids" item="id" open="cpb.contract_id IN (" separator="," close=")">
                #{id}
            </foreach>
        </where>
        GROUP BY
        cpb.contract_id,
        cpb.material_id
    </select>
</mapper>
