<?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.project.zcustom.mapper.basics.PlatAppOrgMapper">

    <resultMap type="com.project.zcustom.domain.basics.PlatAppOrg" id="LargeUnitResult">
        <result property="id"    column="id"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="connectFlag"    column="connect_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="appOrg"    column="app_org"    />
        <result property="appName"    column="app_name"    />
        <result property="appParentOrg"    column="app_parent_org"    />
    </resultMap>

    <select id="getProjectCameraNumList" parameterType="String" resultMap="LargeUnitResult">
        SELECT
            c.app_name,
            c.app_org,
            COUNT(*) AS projectNum,
            SUM( t.cameraNum ) AS cameraNum
        FROM
            (
                SELECT
                    a.app_org,
                    COUNT(
                            IF
                                ( a.del_flag = 0 AND b.del_flag = 0, 1, NULL )) AS cameraNum
                FROM
                    large_plat_project a
                        LEFT JOIN large_plat_camera b ON a.id = b.project_id
                WHERE
                    a.del_flag = 0
                GROUP BY
                    a.id
            ) t
                LEFT JOIN large_plat_app_org c ON t.app_org = c.app_org
        WHERE
            c.del_flag = 0
        <if test="appOrg != '0000'">AND ( c.app_parent_org = #{appOrg} OR c.app_org = #{appOrg} )</if>
        GROUP BY
            c.id
    </select>

    <select id="getConnectUnitNum" resultType="int">
        select count(*) from large_plat_app_org
        where connect_flag = '0' and del_flag = '0'
    </select>

    <select id="getAllCameraCondition" resultMap="LargeUnitResult">
        SELECT
            c.app_org,
            c.app_name,
            COUNT(
                    IF
                        ( a.del_flag = 0 AND a.connect_flag = 0, 1, NULL )) AS cameraNum,
            COUNT(
                    IF
                        ( a.del_flag = 0 AND a.connect_flag = 0 AND a.online_flag = 0, 1, NULL )) AS onlineCameraNum
        FROM
            `large_plat_camera` a
                LEFT JOIN large_plat_project b ON a.project_id = b.id
                LEFT JOIN large_plat_app_org c ON b.app_org = c.app_org
        GROUP BY
            c.app_org
    </select>

    <select id="getCameraConditionByAppOrg" parameterType="String" resultMap="LargeUnitResult">
        SELECT
            c.app_org,
            c.app_name,
            COUNT(
                    IF
                        ( a.del_flag = 0 AND a.connect_flag = 0, 1, NULL )) AS cameraNum,
            COUNT(
                    IF
                        ( a.del_flag = 0 AND a.connect_flag = 0 AND a.online_flag = 0, 1, NULL )) AS onlineCameraNum
        FROM
            `large_plat_camera` a
                LEFT JOIN large_plat_project b ON a.project_id = b.id
                LEFT JOIN large_plat_app_org c ON b.app_org = c.app_org
        WHERE
            c.app_org = #{appOrg}
    </select>
</mapper>
