<?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.unit.LargeProjectMapper">

    <resultMap type="com.project.zcustom.domain.addional.LargeProject" id="LargeProjectResult">
        <result property="id"    column="id"    />
        <result property="projectName"    column="project_name"    />
        <result property="appOrg"    column="app_org"    />
        <result property="appParentOrg"    column="app_parent_org"    />
        <result property="connectFlag"    column="connect_flag"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="subdivision"    column="subdivision"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectLargeProjectVo">
        select id, project_name, app_org, connect_flag, del_flag, create_by, create_time, update_by, update_time from large_plat_project
    </sql>

    <select id="selectLargeProjectList" parameterType="LargeProject" resultMap="LargeProjectResult">
        <include refid="selectLargeProjectVo"/>
        <where>
            <if test="projectName != null  and projectName != ''"> and project_name like concat('%', #{projectName}, '%')</if>
            <if test="appOrg != null "> and app_org = #{appOrg}</if>
            <if test="connectFlag != null  and connectFlag != ''"> and connect_flag = #{connectFlag}</if>
        </where>
    </select>

    <insert id="insertLargeProject" parameterType="LargeProject" useGeneratedKeys="true" keyProperty="id">
        insert into large_plat_project
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="projectName != null">project_name,</if>
            <if test="appOrg != null">app_org,</if>
            <if test="connectFlag != null">connect_flag,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="projectName != null">#{projectName},</if>
            <if test="appOrg != null">#{appOrg},</if>
            <if test="connectFlag != null">#{connectFlag},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateLargeProject" parameterType="LargeProject">
        update large_plat_project
        <trim prefix="SET" suffixOverrides=",">
            <if test="projectName != null">project_name = #{projectName},</if>
            <if test="appOrg != null">app_org = #{appOrg},</if>
            <if test="connectFlag != null">connect_flag = #{connectFlag},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteLargeProjectById" parameterType="Long">
        delete from large_plat_project where id = #{id}
    </delete>

    <delete id="deleteLargeProjectByIds" parameterType="String">
        delete from large_plat_project where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <delete id="reallyDeleteByWrapper">
        DELETE FROM large_plat_project ${ew.customSqlSegment}
    </delete>

    <select id="getProjectNum" parameterType="String" resultType="Long">
        SELECT
            count(*) AS num
        FROM
            large_plat_project a
                LEFT JOIN large_plat_app_org b ON a.app_org = b.app_org
        WHERE
            a.del_flag = '0'
          AND b.del_flag = '0'
        AND subdivision &lt;= 10
        <if test="appOrg != '0000'">AND ( b.app_parent_org = #{appOrg} OR b.app_org = #{appOrg} )</if>
    </select>

    <select id="getProjectNumX" parameterType="String" resultMap="LargeProjectResult">
        SELECT
            count(*) AS num,
            subdivision
        FROM
            large_plat_project a
            LEFT JOIN large_plat_app_org b ON a.app_org = b.app_org
        WHERE
            a.del_flag = '0'
          AND b.del_flag = '0'
        <if test="appOrg != '0000'">AND ( b.app_parent_org = #{appOrg} OR b.app_org = #{appOrg} )</if>
          AND subdivision &lt;= 10
        GROUP BY
            subdivision
    </select>

    <select id="getProjectNumY" parameterType="String" resultMap="LargeProjectResult">
        SELECT
            count(*) AS num,
            subdivision
        FROM
            large_plat_project a
                LEFT JOIN large_plat_app_org b ON a.app_org = b.app_org
        WHERE
            a.del_flag = '0'
          AND b.del_flag = '0'
        <if test="appOrg != '0000'">AND ( b.app_parent_org = #{appOrg} OR b.app_org = #{appOrg} )</if>
          AND subdivision > 10
        GROUP BY
            subdivision
    </select>

    <select id="getProjectByAppOrg" parameterType="String" resultMap="LargeProjectResult">
        SELECT
            a.project_name,
            a.abscissa,
            a.ordinate
        FROM
            large_plat_project a
                LEFT JOIN large_plat_app_org b ON a.app_org = b.app_org
        WHERE
            a.del_flag = '0'
          AND b.del_flag = '0'
          AND ( b.app_parent_org = #{appOrg} OR b.app_org = #{appOrg} )
    </select>

    <select id="getCameraNumList" parameterType="String" resultMap="LargeProjectResult">
        SELECT
            a.id,
            a.project_name,
            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' AND a.app_org = #{appOrg}
        GROUP BY a.id
    </select>

    <select id="getProjectNumThisWeek" parameterType="String" resultType="int">
        SELECT
            COUNT(a.id)
        FROM
            large_plat_plan 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
            a.del_flag = '0'
          AND b.del_flag = '0'
          AND c.del_flag = '0'
          AND ( c.app_parent_org = #{appOrg} OR c.app_org = #{appOrg} )
          AND (
            DATE( a.start_time ) BETWEEN DATE_SUB( CURDATE(), INTERVAL ( DAYOFWEEK( CURDATE()) - 2 ) DAY )
            AND DATE_ADD( CURDATE(), INTERVAL ( 8 - DAYOFWEEK( CURDATE())) DAY )
            OR DATE( a.end_time ) BETWEEN DATE_SUB( CURDATE(), INTERVAL ( DAYOFWEEK( CURDATE()) - 2 ) DAY )
            AND DATE_ADD( CURDATE(), INTERVAL ( 8 - DAYOFWEEK( CURDATE())) DAY ))
    </select>

    <select id="getProjectNumZ" parameterType="String" resultMap="LargeProjectResult">
        SELECT
            a.project_name,
            a.app_org,
            b.app_parent_org,
            CASE

                WHEN subdivision > 10 THEN
                    0 ELSE 1
                END AS subdivision
        FROM
            large_plat_project a
                LEFT JOIN large_plat_app_org b ON a.app_org = b.app_org
        WHERE
            a.del_flag = '0'
          AND b.del_flag = '0'
    </select>
</mapper>
