<?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.njcn.gather.plan.mapper.PqDevCheckHistoryMapper">

    <select id="listItemDistributions" resultType="com.njcn.gather.plan.pojo.vo.PlanStatisticsItemVO">
        SELECT
            Item_Id AS itemId,
            Item_Name AS itemName,
            SUM(CASE WHEN Result = 0 THEN 1 ELSE 0 END) AS unqualifiedCount
        FROM pq_dev_check_history
        <where>
            State = 1
            AND Plan_Id = #{planId}
            <if test="manufacturer != null and manufacturer != ''">
                AND Manufacturer = #{manufacturer}
            </if>
            <if test="devType != null and devType != ''">
                AND Dev_Type = #{devType}
            </if>
        </where>
        GROUP BY Item_Id, Item_Name
        ORDER BY unqualifiedCount DESC, Item_Name ASC
    </select>
</mapper>
