<?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.yeejoin.equipmanage.mapper.OrgUsrMapper">
    <select id="getCompanyTree" resultType="com.yeejoin.equipmanage.common.entity.OrgUsr">
        SELECT
            usr.sequence_nbr,
            usr.biz_org_name,
            usr.biz_org_code,
            usr.parent_id,
            usr.`code`,
            CASE
                WHEN ibsi.`code` = usr.`code` THEN
                    1 ELSE 0
                END AS canClick
        FROM
            cb_org_usr usr
                LEFT JOIN idx_biz_station_info ibsi ON ibsi.`code` = usr.`code`
        WHERE
            usr.biz_org_type = 'COMPANY'
          AND usr.is_delete = 0
          AND usr.sequence_nbr  IN (
            (
                SELECT
                    usr.parent_id AS id
                FROM
                    cb_org_usr usr
                        LEFT JOIN idx_biz_station_info ibsi ON ibsi.`code` = usr.`code`
                WHERE
                    usr.biz_org_type = 'COMPANY'
                  AND usr.is_delete = 0
                  AND ibsi.`online` = 1
            ) UNION ALL
            (
                SELECT
                    usr.sequence_nbr AS id
                FROM
                    cb_org_usr usr
                        LEFT JOIN idx_biz_station_info ibsi ON ibsi.`code` = usr.`code`
                WHERE
                    usr.biz_org_type = 'COMPANY'
                  AND usr.is_delete = 0
                  AND ibsi.`online` = 1
            )  UNION ALL
            (
                SELECT
                    usr.sequence_nbr AS id
                FROM
                    cb_org_usr usr
                WHERE
                    usr.biz_org_type = 'COMPANY'
                  AND usr.is_delete = 0
                  AND parent_id IS NULL
            )
        )
    </select>
    <select id="getOrgUsrDtoInfo" resultType="com.yeejoin.equipmanage.common.dto.OrgUsrDto">
        SELECT
            biz_org_code bizOrgCode,
            biz_org_name bizOrgName
        FROM
            cb_org_usr
        <where>
            <if test="bizOrgType != null and bizOrgType != ''">
                biz_org_type = #{bizOrgType}
            </if>
            <if test="bizOrgCode != null and bizOrgCode != ''">
                AND locate( biz_org_code, #{bizOrgCode} ) > 0
            </if>
        </where>
        ORDER BY sequence_nbr
    </select>
</mapper>
