<?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.car.PlatCarMapper">
    <resultMap type="com.project.zcustom.domain.addional.LargeCar" id="LargePlatPublicCarResult">
        <result property="appEncode"    column="app_encode"    />
        <result property="appOrg"    column="app_org"    />
        <result property="totalCarNum"    column="total_car_num"    />
        <result property="carNumOne"    column="car_num_one"    />
        <result property="carNumTwo"    column="car_num_two"    />
        <result property="carNumThree"    column="car_num_three"    />
        <result property="carNumFour"    column="car_num_four"    />
        <result property="carNumFive"    column="car_num_five"    />
        <result property="enterpriseCarNum"    column="enterprise_car_num"    />
        <result property="headCarNum"    column="head_car_num"    />
        <result property="oilCarNum"    column="oil_car_num"    />
        <result property="newPowerCarNum"    column="new_power_car_num"    />
        <result property="establishmentsCarNum"    column="establishments_car_num"    />
        <result property="yearAddNum"    column="year_add_num"    />
        <result property="yearReduceNum"    column="year_reduce_num"    />
        <result property="notArchivedNum"    column="not_archived_num"    />
        <result property="noPidNum"    column="no_pid_num"    />
        <result property="overSpeedNum"    column="over_speed_num"    />
        <result property="holidayNum"    column="holiday_num"    />
        <result property="pcdNum"    column="pcd_num"    />
        <result property="avgPcdNum"    column="avg_pcd_num"    />
        <result property="mileCount"    column="mile_count"    />
        <result property="avgMile"    column="avg_mile"    />
    </resultMap>

    <resultMap type="com.project.zcustom.domain.addional.LargeCarDetail" id="LargePlatPublicCarChangeDetailResult">
        <result property="appName"    column="app_name"    />
        <result property="appEncode"    column="app_encode"    />
        <result property="vehicleNum"    column="vehicle_num"    />
        <result property="useProperty"    column="use_property"    />
        <result property="assetsCard"    column="assets_card"    />
        <result property="orgDate"    column="org_date"    />
        <result property="handleDate"    column="handle_date"    />
        <result property="handleReason"    column="handle_reason"    />
        <result property="flag"    column="flag"    />
    </resultMap>

    <resultMap type="com.project.zcustom.domain.addional.LargeWarnDetail" id="LargePlatPublicCarWarnDetailResult">
        <result property="appName"    column="app_name"    />
        <result property="appEncode"    column="app_encode"    />
        <result property="vehicleNum"    column="vehicle_num"    />
        <result property="alertType"    column="alert_type"    />
        <result property="startTime"    column="start_time"    />
        <result property="endTime"    column="end_time"    />
    </resultMap>

    <resultMap type="com.project.zcustom.domain.addional.LargeCarRank" id="LargeCarRankResult">
        <result property="orgName"    column="org_name"    />
        <result property="appEncode"    column="app_encode"    />
        <result property="pcdCount"    column="pcd_count"    />
        <result property="vehicleNum"    column="vehicle_num"    />
    </resultMap>

    <resultMap type="com.project.zcustom.domain.addional.LargeUnitRank" id="LargeUnitRankResult">
        <result property="id"    column="id"    />
        <result property="appName"    column="app_name"    />
        <result property="pcdNum"    column="pcd_num"    />
        <result property="totalCarNum"    column="total_car_num"    />
        <result property="appEncode"    column="app_encode"    />
    </resultMap>


    <select id="getResourceNumber" parameterType="String" resultMap="LargePlatPublicCarResult">
        SELECT
            SUM(total_car_num) AS total_car_num,
            SUM(car_num_one) AS car_num_one,
            SUM(car_num_two) AS car_num_two,
            SUM(car_num_three) AS car_num_three,
            SUM(car_num_four) AS car_num_four,
            SUM(car_num_five) AS car_num_five,
            SUM(enterprise_car_num) AS enterprise_car_num,
            SUM(head_car_num) AS head_car_num,
            SUM(oil_car_num) AS oil_car_num,
            SUM(new_power_car_num) AS new_power_car_num,
            SUM(establishments_car_num) AS establishments_car_num,
            SUM(year_add_num) AS year_add_num,
            SUM(year_reduce_num) AS year_reduce_num
        FROM
            large_plat_public_car a
                LEFT JOIN large_plat_app_org b ON a.app_encode = b.app_encode
        <if test="appOrg != '10001'">
        WHERE
            b.app_org = #{appOrg}
        </if>
    </select>

    <select id="getCarDetail" resultMap="LargePlatPublicCarChangeDetailResult">
        SELECT
        app_name,
        vehicle_num,
        use_property,
        assets_card,
        org_date,
        handle_date,
        handle_reason
        FROM
        large_plat_public_car_change_detail a
        LEFT JOIN large_plat_app_org b ON a.app_encode = b.app_encode
        WHERE
        flag = #{flag}
        <if test="appOrg != '10001'">AND b.app_org = #{appOrg}</if>
        <if test="flag == 0">ORDER BY org_date DESC</if>
        <if test="flag == 1">ORDER BY handle_date DESC</if>
        LIMIT #{pageSize} OFFSET #{page}
    </select>

    <select id="getCarDetailNum" resultType="int">
        SELECT
        count(1)
        FROM
        large_plat_public_car_change_detail a
        LEFT JOIN large_plat_app_org b ON a.app_encode = b.app_encode
        WHERE
        flag = #{flag}
        <if test="appOrg != '10001'">AND b.app_org = #{appOrg}</if>
    </select>

    <select id="getMonitorNumber" parameterType="String" resultMap="LargePlatPublicCarResult">
        SELECT
            SUM(not_archived_num) AS not_archived_num,
            SUM(no_pid_num) AS no_pid_num,
            SUM(over_speed_num) AS over_speed_num,
            SUM(holiday_num) AS holiday_num,
            SUM(terminal_total) AS terminal_total,
            SUM(terminal_install) AS terminal_install,
            SUM(terminal_online) AS terminal_online
        FROM
            large_plat_public_car a
                LEFT JOIN large_plat_app_org b ON a.app_encode = b.app_encode
        <if test="appOrg != '10001'">
        WHERE
            b.app_org = #{appOrg}
        </if>
    </select>

    <select id="getWarnDetail" resultMap="LargePlatPublicCarWarnDetailResult">
        SELECT
        app_name,
        vehicle_num,
        alert_type,
        start_time,
        end_time
        FROM
        large_plat_public_car_warn_detail a
        LEFT JOIN large_plat_app_org b ON a.app_encode = b.app_encode
        WHERE
        alert_type = #{alertType}
        <if test="appOrg != '10001'">AND b.app_org = #{appOrg}</if>
        ORDER BY start_time DESC
        LIMIT #{pageSize} OFFSET #{page}
    </select>

    <select id="getWarnDetailNum" resultType="int">
        SELECT
        count(1)
        FROM
        large_plat_public_car_warn_detail a
        LEFT JOIN large_plat_app_org b ON a.app_encode = b.app_encode
        WHERE
        alert_type = #{alertType}
        <if test="appOrg != '10001'">AND b.app_org = #{appOrg}</if>
    </select>

    <select id="getOperationManageTopHalf" parameterType="String" resultMap="LargePlatPublicCarResult">
        SELECT
            AVG(avg_mile) AS avg_mile,
            SUM(mile_count) AS mile_count,
            AVG(avg_pcd_num) AS avg_pcd_num,
            SUM(pcd_num) AS pcd_num
        FROM
            large_plat_public_car a
                LEFT JOIN large_plat_app_org b ON a.app_encode = b.app_encode
        <if test="appOrg != '10001'">
        WHERE
            b.app_org = #{appOrg}
        </if>
    </select>

    <select id="getUnitRankTop" resultMap="LargeUnitRankResult">
        SELECT
            app_name,
            total_car_num,
            pcd_num
        FROM
            large_plat_public_car a
                LEFT JOIN large_plat_app_org b ON a.app_encode = b.app_encode
        WHERE app_parent_org = '10001'
        ORDER BY
            pcd_num
            LIMIT 3
    </select>

    <select id="getUnitRankLast" resultMap="LargeUnitRankResult">
        SELECT
            app_name,
            total_car_num,
            pcd_num
        FROM
            large_plat_public_car a
                LEFT JOIN large_plat_app_org b ON a.app_encode = b.app_encode
        WHERE app_parent_org = '10001'
        ORDER BY
            pcd_num DESC
            LIMIT 3
    </select>

    <select id="getCarRank" resultMap="LargeCarRankResult">
        SELECT
            org_name,
            vehicle_num,
            pcd_count
        FROM
            large_plat_public_car_rank_person a
                LEFT JOIN large_plat_app_org b ON a.app_encode = b.app_encode
        ORDER BY
            pcd_count DESC
            LIMIT 10
    </select>

    <select id="getUnitRankList" resultMap="LargeUnitRankResult">
        SELECT
            a.id,
            a.app_encode,
            app_name,
            total_car_num,
            pcd_num
        FROM
            large_plat_public_car a
                LEFT JOIN large_plat_app_org b ON a.app_encode = b.app_encode
        WHERE app_parent_org = '10001'
        ORDER BY
            pcd_num
    </select>


    <select id="getSelfCarNum" parameterType="String" resultMap="LargePlatPublicCarResult">
        SELECT
            total_car_num, b.app_org
        FROM
            large_plat_public_car a
                LEFT JOIN large_plat_app_org b ON a.app_encode = b.app_encode
        WHERE app_org IN
        <foreach collection="list" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        ORDER BY
            pcd_num
    </select>

    <delete id="deleteAllCarDetail" parameterType="integer">
        delete from large_plat_public_car_change_detail where flag = #{flag}
    </delete>

    <delete id="deleteAllWarnDetail" parameterType="String">
        delete from large_plat_public_car_warn_detail where alert_type = #{alertType}
    </delete>

    <delete id="deleteAllCarRank">
        delete from large_plat_public_car_rank_person
    </delete>
</mapper>
