<?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 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.victoriatourist_json,
               t2.flow_id,
               pi.code                     productCode,
               pi.name                     productName,
               pi.type                     productType,
               pc.name                     productCategory,
               pi.unit                     productUnit,
               pi.definition               productDefinition,
               pi.custom_code              productCustomCode,
               pi.spec                     productSpec,
               de.dept_id                  deptId
        FROM subscribe_detail t1
                 LEFT JOIN subscribe t2 ON t1.subscribe_id = t2.id
                 left JOIN bytesailing_item.product_info pi on t1.bussiness_id = pi.id
                 left join bytesailing_item.product_classify pc on pi.product_classify_id = pc.id
                 LEFT JOIN bytesailing_base.sys_dept de
                           ON CAST(json_unquote(pi.victoriatourist_json -> '$.deptId') AS UNSIGNED) = de.dept_id
            ${ew.customSqlSegment}
    </select>

    <select id="getDetail" resultType="com.fjhx.purchase.entity.subscribe.vo.SubscribeDetailVo">
        SELECT
            t1.*,
            IFNULL( SUM( t2.count ), 0 ) AS purchaseCount
        FROM
            subscribe_detail t1
        LEFT JOIN purchase_detail 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 bytesailing_item.product_info pi ON sd.bussiness_id = pi.id
        ${ew.customSqlSegment}
    </select>
</mapper>
