<?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.zcloud.primeport.persistence.mapper.VehicleApplyMapper">

    <select id="listPage" resultType="com.zcloud.primeport.persistence.dataobject.VehicleApplyDO">
        SELECT
            f.id,
            f.status_flag,
            f.licence_type,
            f.licence_type_name,
            f.licence_no,
            f.vehicle_type,
            f.vehicle_type_name,
            f.vehicle_belong_type,
            f.gate_level_auth_area,
            f.audit_flag,
            f.mkmj_permission,
            f.visit_start_time,
            f.visit_end_time,
            f.vehicle_corp_id,
            f.vehicle_corp_name,
            f.vehicle_department_name,
            f.vehicle_department_id,
            f.employee_vehicle_user_name,
            f.employee_vehicle_user_id,
            f.emission_standards_name,
            f.emission_standards,
            f.attachment_id,
            f.driving_license_id,
            f.driving_user_id,
            f.driving_user_name,
            f.inform_sign_id,
            f.project_id,
            f.project_name,
            f.blocked_flag,
            a.audit_user_id,
            a.audit_user_name,
            a.audit_dept_id,
            a.audit_dept_name,
            a.audit_corp_name,
            a.audit_corp_id,
            f.remarks check_department_name
        FROM
            vehicle_apply AS f
            LEFT JOIN vehicle_audit AS a ON f.id = a.vehicle_apply_id AND a.delete_enum = 'FALSE' AND a.audit_status = 1
        WHERE
            f.delete_enum = 'FALSE'
        <if test="params.remarks != null and params.remarks != ''">
            AND f.remarks like  CONCAT('%', #{params.remarks}, '%')
        </if>
        <if test="params.licenceNo != null and params.licenceNo != ''">
            AND f.licence_no like  CONCAT('%', #{params.licenceNo}, '%')
        </if>
        <if test="params.auditFlag != null">
            AND f.audit_flag = #{params.auditFlag}
        </if>
        <if test="params.visitStartTime != null and params.visitStartTime != ''">
            AND f.visit_start_time LIKE CONCAT('%', #{params.gateName}, '%')
        </if>
        <if test="params.visitEndTime != null and params.visitEndTime != ''">
            AND f.visit_end_time LIKE CONCAT('%', #{params.gateName}, '%')
        </if>
        <if test="params.vehicleBelongType != null and params.vehicleBelongType != ''">
            AND f.vehicle_belong_type = #{params.vehicleBelongType}
        </if>
        <if test="params.vehicleDepartmentId != null">
            AND f.vehicle_department_id = #{params.vehicleDepartmentId}
        </if>
        <if test="params.vehicleCorpId != null">
            AND f.vehicle_corp_id = #{params.vehicleCorpId}
        </if>
        <if test="params.employeeVehicleUserId != null">
            AND f.employee_vehicle_user_id = #{params.employeeVehicleUserId}
        </if>
        order by f.id desc
    </select>
    <select id="fgsCount" resultType="com.zcloud.primeport.persistence.dataobject.FgsVehicleCountDto">
        SELECT
            c.id corp_id,
            c.corp_name,
            IFNULL( utmp.user_count, 0 ) user_count,
            IFNULL( pubtmp.pub_car_count, 0 ) pub_car_count,
            IFNULL( prtmp.pri_car_count, 0 ) pri_car_count
        FROM
            corp_info c
                LEFT JOIN (
                SELECT
                    COUNT( u.id ) user_count,
                    u.corpinfo_id
                FROM
                    `user` u
                WHERE
                    u.delete_enum = 'FALSE'
                  AND u.main_corp_flag = 0
                GROUP BY
                    u.corpinfo_id
            ) utmp ON c.id = utmp.corpinfo_id
                LEFT JOIN (
                SELECT
                    va.vehicle_corp_id,
                    COUNT( va.id ) pri_car_count
                FROM
                    `vehicle_apply` va
                WHERE
                    va.delete_enum = 'FALSE'
        <if test="params.corpType != null">
            <if test="params.corpType == 1">    AND va.vehicle_belong_type IN ( 1, 3 ) </if>
            <if test="params.corpType == 2">  and va.vehicle_belong_type = 7 </if>
        </if>
                GROUP BY
                    va.vehicle_corp_id
            ) prtmp ON c.id = prtmp.vehicle_corp_id
                LEFT JOIN (
                SELECT
                    va.vehicle_corp_id,
                    COUNT(va.id) pub_car_count
                FROM
                    `vehicle_apply` va WHERE va.delete_enum = 'FALSE'
        <if test="params.corpType != null">
            <if test="params.corpType == 1">  and va.vehicle_belong_type in (2,4) </if>
            <if test="params.corpType == 2">  and va.vehicle_belong_type = 8 </if>
        </if>
                GROUP BY
                    va.vehicle_corp_id) pubtmp on pubtmp.vehicle_corp_id = c.id
        WHERE
            c.delete_enum = 'FALSE'
        <if test="params.corpType != null">
          <if test="params.corpType == 1">  AND c.type IN ( 0,1,6 ) </if>
          <if test="params.corpType == 2">  AND c.type = 5 </if>
        </if>
        <if test="params.corpName != null and params.corpName != ''">
            AND c.corp_name like CONCAT('%', #{params.corpName}, '%')
        </if>
        ORDER BY
            c.corp_order,
            c.create_time DESC
    </select>
    <select id="getInfoById" resultType="com.zcloud.primeport.persistence.dataobject.VehicleApplyDO">
        SELECT
            va.*,
            m.mkmj_name
        FROM
            vehicle_apply va
                LEFT JOIN mkmj m on va.mkmj_id = m.id
        WHERE va.id = #{id}
    </select>
    <select id="getAppCount" resultType="com.zcloud.primeport.persistence.dataobject.VehicleApplyDO">
        SELECT
            tmp.type,
            tmp.belong_type,
            tmp.audit_user_id,
            COUNT( 1 ) wait_audit_count
        FROM
            (
                SELECT
                    'one_level_car' type,
                    vap.vehicle_belong_type belong_type,
                    va.audit_user_id,
                    va.audit_user_name,
                    va.audit_status
                FROM
                    vehicle_audit va
                        LEFT JOIN vehicle_apply vap ON va.vehicle_apply_id = vap.id
                WHERE
                    va.delete_enum = 'FALSE'
                  AND va.audit_status = 1
                  AND vap.delete_enum = 'FALSE' UNION ALL
                SELECT
                    'two_level_car' type,
                    caca.car_belong_type belong_type,
                    caca.audit_person_user_id audit_user_id,
                    caca.audit_person_user_name audit_user_name,
                    caca.audit_flag audit_status
                FROM
                    closed_area_car_apply caca
                WHERE
                    caca.delete_enum = 'FALSE'
                  AND caca.audit_flag = 1 UNION ALL
                SELECT
                    'one_level_person' type,
                    xap.person_belong_type,
                    xap.audit_user_id,
                    xap.audit_user_name,
                    xap.audit_flag
                FROM
                    xgf_apply_person xap
                WHERE
                    xap.delete_enum = 'FALSE'
                  AND xap.audit_flag = 1 UNION ALL
                SELECT
                    'two_level_person' type,
                    capa.person_belong_type,
                    capa.audit_person_user_id audit_user_id,
                    capa.audit_person_user_name audit_user_name,
                    capa.audit_flag
                FROM
                    closed_area_person_apply capa
                WHERE
                    capa.delete_enum = 'FALSE'
                  AND capa.audit_flag = 1
            ) tmp
        where tmp.audit_user_id = #{params.userId}
        GROUP BY
            tmp.type,
            tmp.belong_type,
            tmp.audit_user_id
    </select>
</mapper>

