<?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.LargeUnitMapper">

    <resultMap type="com.project.zcustom.domain.addional.LargeUnit" 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="unitCode"    column="unit_code"    />
        <result property="unitName"    column="unit_name"    />
        <result property="parentUnitCode"    column="parent_unit_code"    />
    </resultMap>

    <sql id="selectLargeUnitVo">
        select id, del_flag, connect_flag, create_by, create_time, update_by, update_time, unit_name, unit_code, parent_unit_code from large_unit
    </sql>

    <select id="selectLargeUnitList" parameterType="LargeUnit" resultMap="LargeUnitResult">
        <include refid="selectLargeUnitVo"/>
        <where>
            <if test="unitCode != null  and unitCode != ''"> and unit_code = #{unitCode}</if>
            <if test="unitName != null  and unitName != ''"> and unit_name like concat('%', #{unitName}, '%')</if>
        </where>
    </select>

    <select id="selectLargeUnitById" parameterType="Long" resultMap="LargeUnitResult">
        <include refid="selectLargeUnitVo"/>
        where id = #{id}
    </select>

    <insert id="insertLargeUnit" parameterType="LargeUnit" useGeneratedKeys="true" keyProperty="id">
        insert into large_unit
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <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>
            <if test="appOrg != null">app_org,</if>
            <if test="appName != null">app_name,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <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>
            <if test="appOrg != null">#{appOrg},</if>
            <if test="appName != null">#{appName},</if>
         </trim>
    </insert>

    <update id="updateLargeUnit" parameterType="LargeUnit">
        update large_unit
        <trim prefix="SET" suffixOverrides=",">
            <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>
            <if test="appOrg != null">app_org = #{appOrg},</if>
            <if test="appName != null">app_name = #{appName},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteLargeUnitById" parameterType="Long">
        delete from large_unit where id = #{id}
    </delete>

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

    <select id="getProjectCameraNumList" resultMap="LargeUnitResult">
        SELECT
            c.unit_name,
            COUNT(*) AS projectNum,
            SUM(t.cameraNum) AS cameraNum
        FROM
            (
                SELECT
                    a.unit_code,
                    COUNT(
                            IF
                                ( a.del_flag = 0 AND b.del_flag = 0, 1, NULL )) AS cameraNum
                FROM
                    large_project a
                        LEFT JOIN large_camera b ON a.id = b.project_id
                WHERE a.del_flag = 0
                GROUP BY a.id
            ) t
                LEFT JOIN large_unit c ON t.unit_code = c.unit_code
        WHERE c.del_flag = 0
        GROUP BY
            c.id
    </select>


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

    <select id="getAllCameraCondition" resultMap="LargeUnitResult">
        SELECT
            c.unit_code,
            c.unit_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_camera` a
                LEFT JOIN large_project b ON a.project_id = b.id
                LEFT JOIN large_unit c ON b.unit_code = c.unit_code
        GROUP BY
            c.unit_code
    </select>

    <select id="getCameraConditionByAppOrg" parameterType="String" resultMap="LargeUnitResult">
        SELECT
            c.unit_code,
            c.unit_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_camera` a
                LEFT JOIN large_project b ON a.project_id = b.id
                LEFT JOIN large_unit c ON b.unit_code = c.unit_code
        WHERE
            c.unit_code = #{unitCode}
    </select>
</mapper>
