<?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.dispose.sys.auth.mapper.AuthMapper">
    <resultMap id="selectUserMap" autoMapping="true" type="com.njzscloud.dispose.sys.auth.pojo.result.MyResult">
        <result property="accountId" column="account_id"/>
        <result property="userId" column="user_id"/>
        <result property="secret" column="secret"/>
        <result property="nickname" column="nickname"/>
        <result property="disabled" column="disabled"/>
        <result property="clientCode" column="client_code"/>
    </resultMap>
    <select id="selectUser" resultMap="selectUserMap">
        SELECT a.id account_id,
        a.user_id,
        a.secret,
        b.nickname,
        b.avatar,
        b.phone,
        a.disabled,
        a.client_code
        FROM sys_user_account a
        INNER JOIN sys_user b ON a.user_id = b.id AND b.deleted = 0
        <if test="ew.customSqlSegment != null and ew.customSqlSegment != ''">
            ${ew.customSqlSegment}
        </if>
    </select>

    <select id="selectRole" 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}
        ORDER BY a.id
    </select>

    <resultMap id="selectUserMenuMap" autoMapping="true" type="com.njzscloud.dispose.sys.auth.pojo.result.MenuResource">
        <result column="breadcrumb" property="breadcrumb" typeHandler="com.njzscloud.common.mp.support.handler.j.JsonTypeHandler"/>
    </resultMap>
    <select id="selectUserMenu" resultMap="selectUserMenuMap">
        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
          AND a.client_code <![CDATA[ & ]]> ${client} != 0
        ORDER BY a.tier, a.sort, a.id
    </select>

    <select id="selectUserEndpoint" resultType="com.njzscloud.dispose.sys.auth.pojo.result.EndpointResource">
        SELECT DISTINCT a.id,
                        a.request_method,
                        a.routing_path,
                        a.endpoint_path,
                        a.access_model,
                        a.memo
        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})
        ORDER BY a.id
    </select>
    <select id="selectUserIdentity" resultType="com.njzscloud.dispose.sys.auth.pojo.result.IdentityInfo">
        SELECT a.identity_category,
               a.id customer_id,
               a.org_id,
               a.customer_name,
               a.phone,
               a.settlement_way,
               a.manager,
               b.org_category,
               b.uscc,
               b.org_name,
               b.business_license,
               b.license_start_time,
               b.license_end_time,
               b.legal_representative,
               b.idcard,
               b.idcard_start_time,
               b.idcard_end_time,
               b.idcard_front,
               b.idcard_back,
               b.province,
               b.city,
               b.area,
               b.town,
               b.province_name,
               b.city_name,
               b.area_name,
               b.town_name,
               b.address,
               b.lng,
               b.lat
        FROM cst_customer a
                 LEFT JOIN cst_org b ON b.id = a.org_id AND b.deleted = 0
        WHERE a.deleted = 0
          AND a.user_id = #{userId}
        ORDER BY a.create_time, a.id
    </select>

</mapper>
