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

    <select id="getCorpUserList" resultType="com.zcloud.primeport.persistence.dataobject.UserDto">
        SELECT
        u.id user_id,
        u.username user_name,
        u.`name`,
        u.corpinfo_id corpId,
        c.corp_name,
        d.id dept_Id,
        d.`name` dept_Name,
        u.phone,
        u.user_avatar_url user_face_url,
        u.rz_flag,
        u.employment_flag,
        u.user_id_card,
        IFNULL( vehicle_tmp.vehicle_count, 0 ) vehicle_count,
        IFNULL(cafpa.mkmj_permission,2) mkmj_permission
        FROM
        `user` AS u
        LEFT JOIN corp_info c ON u.corpinfo_id = c.id
        LEFT JOIN department d ON u.department_id = d.id
        LEFT JOIN closed_area_fgs_person_auth cafpa on cafpa.user_id = u.user_id and cafpa.delete_enum = 'FALSE'
        LEFT JOIN (
        SELECT
        va.employee_vehicle_user_id,
        COUNT( va.id ) vehicle_count
        FROM
        `vehicle_apply` va
        WHERE
        va.audit_flag = 2
        AND va.vehicle_belong_type = 3
        AND va.delete_enum = 'FALSE'
        GROUP BY
        va.employee_vehicle_user_id
        ) vehicle_tmp ON u.user_id = vehicle_tmp.employee_vehicle_user_id
        WHERE
        u.delete_enum = 'FALSE'
        AND u.main_corp_flag = 0
        AND u.corpinfo_id = #{params.corpinfoId}
        <if test="params.deptId != null">
            AND u.department_id = #{params.deptId}
        </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="listPage" resultType="com.zcloud.primeport.persistence.dataobject.ClosedAreaPersonApplyDO">
        select c.*,c.create_name apply_user_name from closed_area_person_apply c
        where  c.delete_enum = 'FALSE'
        <if test="params.processOrRecord != null">
            <if test="params.processOrRecord == 1">
                AND c.audit_flag = 1
            </if>
            <if test="params.processOrRecord == 2">
                AND c.audit_flag != 1
            </if>
        </if>
        <if test="params.personBelongType != null">
            <if test="params.personBelongType == 4">
                AND c.person_belong_type = 4
            </if>
            <if test="params.personBelongType == 1">
                AND c.person_belong_type = 1
            </if>
            <if test="params.personBelongType == 2">
                AND c.person_belong_type = 2
            </if>
            <if test="params.personBelongType == 3">
                AND c.person_belong_type = 3
            </if>
            <if test="params.personBelongType == -1">
                AND c.person_belong_type != 4
            </if>
        </if>
        <if test="params.applyPersonUserName != null and params.applyPersonUserName != ''">
            AND c.apply_person_user_name like  CONCAT('%', #{params.applyPersonUserName}, '%')
        </if>
        order by c.id desc
    </select>
</mapper>

