<?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.XgfApplyPersonMapper">
    <select id="listPage" resultType="com.zcloud.primeport.persistence.dataobject.XgfApplyPersonDO">
        SELECT
        xap.id,
        xap.person_belong_type,
        xap.gate_level_auth_area,
        xap.mkmj_id,
        xap.mkmj_name,
        xap.audit_flag,
        xap.reasons_refusal,
        xap.reason_visit,
        xap.visit_start_time,
        xap.visit_end_time,
        xap.inform_sign_id,
        xap.project_id,
        xap.project_name,
        xap.apply_corp_id,
        xap.apply_corp_name,
        xap.audit_user_id,
        xap.audit_user_name,
        xap.audit_dept_id,
        xap.audit_dept_name,
        xap.audit_corp_id,
        xap.audit_corp_name,
        xap.audit_time,
        xap.create_name apply_user_name,
        pa.id person_apply_id,
        pa.person_corp_id,
        pa.person_corp_name,
        pa.person_department_id,
        pa.person_department_name,
        pa.mkmj_permission,
        pa.employee_person_user_name,
        pa.employee_person_user_id,
        pa.user_face_url,
        pa.user_phone,
        pa.user_card
        FROM
        xgf_apply_person AS xap
        LEFT JOIN person_apply AS pa ON xap.id = pa.xgf_apply_person_id
        AND pa.delete_enum = 'FALSE'
        WHERE
        xap.delete_enum = 'FALSE'
        <if test="params.auditFlag != null">
            AND xap.audit_flag = #{params.auditFlag}
        </if>
        <if test="params.processOrRecord != null">
            <if test="params.processOrRecord == 1">
                AND xap.audit_flag = 1
            </if>
            <if test="params.processOrRecord == 2">
                AND xap.audit_flag != 1
            </if>
        </if>
        <if test="params.mkmjId != null">
            AND xap.mkmj_id = #{params.mkmjId}
        </if>
        <if test="params.visitStartTime != null and params.visitStartTime != ''">
            AND xap.visit_start_time LIKE CONCAT('%', #{params.visitStartTime}, '%')
        </if>
        <if test="params.employeePersonUserName != null and params.employeePersonUserName != ''">
            AND xap.employee_person_user_name LIKE CONCAT('%', #{params.employeePersonUserName}, '%')
        </if>
        <if test="params.visitEndTime != null and params.visitEndTime != ''">
            AND xap.visit_end_time LIKE CONCAT('%', #{params.visitEndTime}, '%')
        </if>
        <if test="params.personBelongType != null and params.personBelongType != ''">
            AND xap.person_belong_type = #{params.personBelongType}
        </if>
        <if test="params.vehicleBelongType != null and params.vehicleBelongType != ''">
            AND xap.person_belong_type = #{params.vehicleBelongType}
        </if>
        ORDER BY
        xap.id DESC
    </select>

    <select id="xgfPersonAuditList" resultType="com.zcloud.primeport.persistence.dataobject.XgfApplyPersonDO">
        SELECT
            p.*,
            p.create_time apply_time,
            p.create_name apply_user_name,
            tmp.person_apply_count
        FROM
            xgf_apply_person p
                LEFT JOIN ( SELECT f.xgf_apply_person_id, COUNT( f.id ) person_apply_count FROM person_apply f WHERE f.delete_enum = 'FALSE' GROUP BY f.xgf_apply_person_id ) tmp ON p.id = tmp.xgf_apply_person_id
        where p.delete_enum = 'FALSE'
        <if test="params.processOrRecord != null">
            <if test="params.processOrRecord == 1">
                AND p.audit_flag = 1
            </if>
            <if test="params.processOrRecord == 2">
                AND p.audit_flag != 1
            </if>
        </if>
        <if test="params.auditFlag != null">
            and p.audit_flag = #{params.auditFlag}
        </if>
        <if test="params.applyCorpId != null and params.applyCorpId != ''">
            AND p.apply_corp_id = #{params.applyCorpId}
        </if>
        <if test="params.projectName != null and params.projectName != ''">
            AND p.project_name like  CONCAT('%', #{params.projectName}, '%')
        </if>
        <if test="params.applyCorpName != null and params.applyCorpName != ''">
            AND p.apply_corp_name like  CONCAT('%', #{params.applyCorpName}, '%')
        </if>
    </select>
    <select id="personnelVehicleManagementList"
            resultType="com.zcloud.primeport.persistence.dataobject.PersonCountDto">
        SELECT
            u.corpinfo_id,
            c.corp_name,
            u.id user_id,
            u.phone user_phone,
            u.name user_name,
            u.user_id_card user_card,
            d.id department_id,
            d.`name` department_name,
            p.post_name,
            xap.project_id,
            xap.project_name,
            xap.visit_start_time,
            xap.visit_end_time,
            pa.id apply_person_id,
            pa.xgf_apply_person_id,
            IFNULL( tmp.vehicle_count, 0 ) vehicle_count,
            IFNULL( pa.mkmj_permission, 2 ) mkmj_permission,
            u.user_avatar_url user_face_url
        FROM
            USER u
                LEFT JOIN department d ON u.department_id = d.id
                LEFT JOIN post p ON u.post_id = p.id
                LEFT JOIN corp_info c ON u.corpinfo_id = c.id
                LEFT JOIN person_apply pa ON u.id = pa.employee_person_user_id
                LEFT JOIN xgf_apply_person xap ON pa.xgf_apply_person_id = xap.id
                LEFT JOIN (
                SELECT
                    va.employee_vehicle_user_id,
                    COUNT( va.id ) vehicle_count
                FROM
                    vehicle_apply va
                WHERE
                    va.delete_enum = 'FALSE'
                  AND va.employee_vehicle_user_id != ''
		AND va.employee_vehicle_user_id IS NOT NULL
                GROUP BY
                    va.employee_vehicle_user_id
            ) tmp ON u.id = tmp.employee_vehicle_user_id
        WHERE
            u.delete_enum = 'FALSE'
          AND u.main_corp_flag = 0
          AND u.corpinfo_id = #{params.corpId}
        <if test="params.departmentId != null and params.departmentId != ''">
            AND u.department_id = #{params.departmentId}
        </if>
        <if test="params.name != null and params.name != ''">
            AND u.name like  CONCAT('%', #{params.name}, '%')
        </if>
        ORDER BY
            u.department_id
    </select>
    <select id="listTodayHasPort" resultType="com.zcloud.primeport.persistence.dataobject.XgfApplyPersonDO">
        SELECT
        pa.employee_person_user_name,
        pa.user_card,
        pa.user_face_url,
        xap.gate_level_auth_area,
        xap.audit_flag,
        xap.person_belong_type,
        xap.visit_start_time,
        xap.visit_end_time
        FROM
        person_apply pa
        LEFT JOIN xgf_apply_person xap ON pa.xgf_apply_person_id = xap.id
        WHERE
        pa.delete_enum = "FALSE"
        AND xap.delete_enum = 'FALSE'
        AND xap.audit_flag = 2
        AND xap.person_belong_type IN ( 3, 4 )
        AND xap.visit_start_time &lt;= DATE_FORMAT( now(), '%y-%m-%d' ) AND xap.visit_end_time &gt;= DATE_FORMAT( now(), '%y-%m-%d' )
        GROUP BY
        pa.user_card
    </select>
</mapper>

