<?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.fizz.business.mapper.CrmPdoExcoilMapper">

    <select id="getReportSummary" resultType="com.fizz.business.vo.ReportSummaryVO">
        SELECT
        -- 总计
        SUM(exit_width)       AS totalExitWidth,
        SUM(exit_length)      AS totalExitLength,
        SUM(theory_weight)    AS totalTheoryWeight,
        SUM(actual_weight)    AS totalActualWeight,
        SUM(exit_thickness)   AS totalExitThickness,

        -- 平均
        AVG(exit_width)       AS avgExitWidth,
        AVG(exit_length)      AS avgExitLength,
        AVG(theory_weight)    AS avgTheoryWeight,
        AVG(actual_weight)    AS avgActualWeight,
        AVG(exit_thickness)   AS avgExitThickness,

        -- 总数
        COUNT(DISTINCT exit_mat_id) AS coilCount,

        -- 原料总重（去重 entry_mat_id）
        (SELECT SUM(t.entry_weight)
        FROM (
        SELECT entry_mat_id, MAX(entry_weight) AS entry_weight
        FROM crm_pdo_excoil
        WHERE del_flag = 0
        <if test="groupNo != null and groupNo != ''">
            AND group_no = #{groupNo}
        </if>
        <if test="shiftNo != null and shiftNo != ''">
            AND shift_no = #{shiftNo}
        </if>
        <if test="startTime != null">
            AND end_time <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null">
            AND end_time <![CDATA[ <= ]]> #{endTime}
        </if>
        GROUP BY entry_mat_id
        ) t
        ) AS totalEntryWeight,

        -- 成材率
        CASE
        WHEN (SELECT SUM(tt.entry_weight)
        FROM (
        SELECT entry_mat_id, MAX(entry_weight) AS entry_weight
        FROM crm_pdo_excoil
        WHERE del_flag = 0
        <if test="groupNo != null and groupNo != ''">
            AND group_no = #{groupNo}
        </if>
        <if test="shiftNo != null and shiftNo != ''">
            AND shift_no = #{shiftNo}
        </if>
        <if test="startTime != null">
            AND end_time <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null">
            AND end_time <![CDATA[ <= ]]> #{endTime}
        </if>
        GROUP BY entry_mat_id
        ) tt
        ) > 0
        THEN SUM(actual_weight) /
        (SELECT SUM(tt.entry_weight)
        FROM (
        SELECT entry_mat_id, MAX(entry_weight) AS entry_weight
        FROM crm_pdo_excoil
        WHERE del_flag = 0
        <if test="groupNo != null and groupNo != ''">
            AND group_no = #{groupNo}
        </if>
        <if test="shiftNo != null and shiftNo != ''">
            AND shift_no = #{shiftNo}
        </if>
        <if test="startTime != null">
            AND end_time <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null">
            AND end_time <![CDATA[ <= ]]> #{endTime}
        </if>
        GROUP BY entry_mat_id
        ) tt
        )
        ELSE 0
        END AS yieldRate

        FROM crm_pdo_excoil
        WHERE del_flag = 0
        <if test="groupNo != null and groupNo != ''">
            AND group_no = #{groupNo}
        </if>
        <if test="shiftNo != null and shiftNo != ''">
            AND shift_no = #{shiftNo}
        </if>
        <if test="startTime != null">
            AND end_time <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null">
            AND end_time <![CDATA[ <= ]]> #{endTime}
        </if>
    </select>

    <select id="getReportDetails" resultType="com.fizz.business.vo.ReportDetailVO">
        SELECT
        exit_mat_id   AS exitMatId,
        entry_mat_id  AS entryMatId,
        group_no      AS groupNo,
        shift_no      AS shiftNo,
        steel_grade   AS steelGrade,
        exit_width    AS exitWidth,
        exit_length   AS exitLength,
        theory_weight AS theoryWeight,
        actual_weight AS actualWeight,
        exit_thickness AS exitThickness,
        online_time   AS onlineTime,
        end_time      AS endTime
        FROM crm_pdo_excoil
        WHERE del_flag = 0
        <if test="groupNo != null and groupNo != ''">
            AND group_no = #{groupNo}
        </if>
        <if test="shiftNo != null and shiftNo != ''">
            AND shift_no = #{shiftNo}
        </if>
        <if test="startTime != null">
            AND end_time <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null">
            AND end_time <![CDATA[ <= ]]> #{endTime}
        </if>
        ORDER BY end_time ASC
    </select>

</mapper>