<?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.njzscloud.supervisory.sys.auth.mapper.AuthMapper">
    <select id="selectUser" resultType="com.njzscloud.common.security.support.UserDetail">
        SELECT a.id account_id,
        a.user_id,
        a.secret,
        b.nickname,
        b.tenant_id,
        b.biz_obj,
        c.tenant_name,
        a.disabled
        FROM sys_user_account a
        INNER JOIN sys_user b ON a.user_id = b.id AND b.deleted = 0
        INNER JOIN sys_tenant c ON c.id = b.tenant_id AND c.deleted = 0
        <if test="ew.customSqlSegment != null and ew.customSqlSegment != ''">
            ${ew.customSqlSegment}
        </if>
    </select>

    <select id="selectUserMenu" resultType="com.njzscloud.common.security.support.MenuResource">
        SELECT DISTINCT a.id,
                        a.sn,
                        a.pid,
                        a.title,
                        a.icon,
                        a.tier,
                        a.breadcrumb,
                        a.sort,
                        a.route_name,
                        a.menu_category
        FROM sys_menu a
                 INNER JOIN sys_role_resource b ON b.data_id = a.id AND b.table_name = 'sys_menu'
            AND b.role_id IN (SELECT a.id
                              FROM sys_role a
                                       INNER JOIN sys_user_role b ON b.role_id = a.id AND b.user_id = #{userId}
                              WHERE a.deleted = 0)
        WHERE a.deleted = 0
    </select>

    <select id="selectUserEndpoint" resultType="com.njzscloud.common.security.support.EndpointResource">
        SELECT DISTINCT a.id,
                        a.request_method,
                        a.routing_path,
                        a.endpoint_path,
                        a.access_model,
                        a.memo,
                        a.creator_id,
                        a.modifier_id,
                        a.create_time,
                        a.modify_time,
                        a.deleted
        FROM sys_endpoint a
                 INNER JOIN sys_role_resource b ON b.data_id = a.id AND b.table_name = 'sys_endpoint'
            AND b.role_id IN (SELECT a.id
                              FROM sys_role a
                                       INNER JOIN sys_user_role b ON b.role_id = a.id AND b.user_id = #{userId}
                              WHERE a.deleted = 0)
        WHERE a.deleted = 0
    </select>
    <select id="selectRoleByUserId" resultType="java.lang.String">
        SELECT a.role_code
        FROM sys_role a
                 INNER JOIN sys_user_role b ON b.role_id = a.id AND b.user_id = #{userId}
        WHERE a.deleted = 0
    </select>
    <select id="selectCompanyInfo" resultType="com.njzscloud.supervisory.biz.pojo.result.SearchCompanyResult">
        SELECT id,
               user_id,
               station,
               uscc,
               company_name,
               business_license,
               license_start_time,
               license_end_time,
               legal_representative,
               province,
               city,
               area,
               town,
               province_name,
               city_name,
               area_name,
               town_name,
               address,
               lng,
               lat,
               contacts,
               phone,
               audit_status,
               audit_memo
        FROM biz_company
        WHERE user_id = #{userId}
    </select>
</mapper>
