<?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.process.mapper.PmsTerminalDetectionMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.njcn.process.pojo.po.PmsTerminalDetection">
        <id column="Id" property="id"/>
        <result column="Create_Time" property="createTime"/>
        <result column="Update_Time" property="updateTime"/>
        <result column="Name" property="name"/>
        <result column="Org_Name" property="orgName"/>
        <result column="Org_No" property="orgNo"/>
        <result column="Manufacture" property="manufacture"/>
        <result column="Install_place" property="installPlace"/>
        <result column="Inspection_Name" property="inspectionName"/>
        <result column="Test_results" property="testResults"/>
        <result column="Inspection_Time" property="inspectionTime"/>
        <result column="Next_inspection_Time" property="nextInspectionTime"/>
        <result column="Inspection_Report" property="inspectionReport"/>
        <result column="Original_Report" property="originalReport"/>
        <result column="Status" property="status"/>
        <result column="Create_By" property="createBy"/>
        <result column="Update_By" property="updateBy"/>
    </resultMap>
    <select id="selectStatistics" resultType="com.njcn.process.pojo.vo.TerminalVO$OrgStatistics">
        SELECT
        Org_No,
        count( id ) AS count
        FROM
        pms_terminal_detection
        <where>
            <if test="ids != null and ids.size > 0">
                AND org_no IN
                <foreach collection='ids' item='item' index="index" open='(' separator=',' close=')'>
                    #{item}
                </foreach>
            </if>
            <if test="param!=null and param.startTime != null and param.startTime !=''">
                AND Create_Time >= #{param.startTime}
            </if>
            <if test="param!=null and param.endTime != null and param.endTime != ''">
                AND Create_Time &lt;= #{param.endTime}
            </if>
        </where>
        GROUP BY
        Org_No
    </select>
    <select id="selectCycleStatistics" resultType="com.njcn.process.pojo.vo.TerminalVO$OrgStatistics">
        SELECT
        Org_No,
        count( id ) AS count
        FROM
        pms_terminal_detection
        <where>
            Next_inspection_Time &lt;= DATE_FORMAT( now(), '%Y-%m-%d' )
            <if test="ids != null and ids.size > 0">
                AND org_no IN
                <foreach collection='ids' item='item' index="index" open='(' separator=',' close=')'>
                    #{item}
                </foreach>
            </if>
            <if test="param!=null and param.startTime != null and param.startTime !=''">
                AND Create_Time >= #{param.startTime}
            </if>
            <if test="param!=null and param.endTime != null and param.endTime != ''">
                AND Create_Time &lt;= #{param.endTime}
            </if>
        </where>
        GROUP BY
        Org_No
    </select>
    <select id="selectDateStatistics" resultType="com.njcn.process.pojo.vo.TerminalVO$DateStatistics">
        SELECT
        DATE_FORMAT(Create_Time,'%Y-%m-%d') as statisticsDate,
        count( id ) AS count
        FROM
        pms_terminal_detection
        <where>
            <if test="ids != null and ids.size > 0">
                AND org_no IN
                <foreach collection='ids' item='item' index="index" open='(' separator=',' close=')'>
                    #{item}
                </foreach>
            </if>
            <if test="param!=null and param.startTime != null and param.startTime !=''">
                AND Create_Time >= #{param.startTime}
            </if>
            <if test="param!=null and param.endTime != null and param.endTime != ''">
                AND Create_Time &lt;= #{param.endTime}
            </if>
        </where>
        GROUP BY
        Create_Time
    </select>
    <select id="selectResultsCycleStatistics" resultType="com.njcn.process.pojo.vo.TerminalVO$ResultsStatistics">
        SELECT
        Org_Name,
        ifnull( GROUP_CONCAT( CASE WHEN Test_results = '0' THEN count END SEPARATOR '' ), 0 ) as notExpanded,
        ifnull( GROUP_CONCAT( CASE WHEN Test_results = '1' THEN count END SEPARATOR '' ), 0 ) as expanded
        FROM
        (
        SELECT
            Org_Name,
            Test_results,
            count( id ) AS count
        FROM
            pms_terminal_detection
        <where>
            Next_inspection_Time &lt;= DATE_FORMAT( now(), '%Y-%m-%d' )
            <if test="ids != null and ids.size > 0">
                AND org_no IN
                <foreach collection='ids' item='item' index="index" open='(' separator=',' close=')'>
                    #{item}
                </foreach>
            </if>
            <if test="param!=null and param.startTime != null and param.startTime !=''">
                AND Create_Time >= #{param.startTime}
            </if>
            <if test="param!=null and param.endTime != null and param.endTime != ''">
                AND Create_Time &lt;= #{param.endTime}
            </if>
        </where>
        GROUP BY
            Org_Name,
            Test_results
        ) a
        GROUP BY
            Org_Name
    </select>

</mapper>
