<?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.lantone.security.mapper.DataAuthMapper">
    <select id="getDataAuthById" resultType="com.lantone.security.dto.SoftwareDataAuthDetail">
        SELECT DISTINCT
        rsd.id softwareRoleDataAuthDetailId,
        sd.id softwareDataAuthId,
        sd.software_id softwareId,
        s.`name` softwareName,
        r.id roleId,
        r.`name` roleName,
        sd.data_auth_id dataAuthId,
        d. NAME dataAuthName,
        d.`status`,
        detail.id detail,
        detail.data_type dataType,
        detail.detail_type detailType,
        detail.detail_id detailId
        FROM
        sys_role r,
        sys_software s,
        sys_role_software_data_auth rsd,
        sys_data_auth d,
        sys_software_data_auth sd
        LEFT JOIN sys_data_auth_detail detail ON sd.id = detail.software_data_auth_id
        WHERE
        r.id = rsd.role_id
        AND r.`status` = '1'
        AND d.`status` = '1'
        AND s.id = sd.software_id
        AND rsd.software_data_auth_id = sd.id
        AND sd.data_auth_id = d.id
        AND r.is_deleted = 'N'
        AND s.is_deleted = 'N'
        AND rsd.is_deleted = 'N'
        AND d.is_deleted = 'N'
        AND sd.is_deleted = 'N'
        <if test="Id != null ">
            AND d.id = #{Id}
        </if>
    </select>


    <select id="getOneSoftwareRoleDataAuthDetail" resultType="com.lantone.security.dto.SoftwareDataAuthDetail">
        SELECT DISTINCT
        detail.id detail,
        detail.data_type dataType,
        detail.detail_type detailType,
        detail.detail_id detailId
        FROM
        sys_role r,
        sys_software s,
        sys_role_software_data_auth rsd,
        sys_data_auth d,
        sys_software_data_auth sd
        LEFT JOIN sys_data_auth_detail detail ON sd.id = detail.software_data_auth_id
        WHERE
        r.id = rsd.role_id
        AND r.`status` = '1'
        AND d.`status` = '1'
        AND s.id = sd.software_id
        AND rsd.software_data_auth_id = sd.id
        AND sd.data_auth_id = d.id
        AND r.is_deleted = 'N'
        AND s.is_deleted = 'N'
        AND rsd.is_deleted = 'N'
        AND d.is_deleted = 'N'
        AND sd.is_deleted = 'N'
        <if test="softwareID != null ">
            AND d.id = #{dataDetailID}
        </if>
        <if test="dataDetailID != null ">
            AND s.id = #{softwareID}
        </if>
    </select>
    <select id="getDataAuthPage" resultType="com.lantone.security.dto.GetDataAuthPageDTO">
        SELECT DISTINCT
        auth.id,
        auth.name,
        r.id roleId,
        IF (
        auth.`status` = 1,
        "启用",
        "禁用"
        ) statusName,
        r.`name` roleName,
        auth.`status`
        FROM
        sys_role r,
        sys_data_auth auth,
        sys_role_software_data_auth rauth,
        sys_software_data_auth sauth
        WHERE
        r.id = rauth.role_id
        AND sauth.data_auth_id = auth.id
        AND sauth.id = rauth.software_data_auth_id
        AND r.is_deleted = 'N'
        AND auth.is_deleted = 'N'
        AND rauth.is_deleted = 'N'
        AND sauth.is_deleted = 'N'
        <if test="dataAuthVO.creator !=null ">
            AND auth.creator = #{dataAuthVO.creator}
        </if>
        <if test="dataAuthVO.status != null ">
            AND auth.`status` = #{dataAuthVO.status}
        </if>
        <if test="dataAuthVO.roles !=null and dataAuthVO.roles.size!=0">
            AND r.id IN
            <foreach collection="dataAuthVO.roles" item="id" open="(" close=")" separator=",">
                #{id}
            </foreach>
        </if>
        <if test="dataAuthVO.name!=null and dataAuthVO.name!=''">
            AND auth.name LIKE concat('%',#{dataAuthVO.name},'%')
        </if>
    </select>
    <select id="getDataAuth" resultType="com.lantone.security.dto.GetDataAuthDetailDTO">
        SELECT DISTINCT
        CONCAT(
        hu.hospital_id,
        '_',
        u.id,
        '_',
        sda.software_id
        ) id,
        dad.data_type dataType,
        dad.detail_id detailId,
        dad.detail_type detailType
        FROM
        sys_user u,
        sys_role r,
        sys_user_role ur,
        sys_hospital_user hu,
        sys_software_data_auth sda,
        sys_role_software_data_auth rsda
        LEFT JOIN sys_data_auth_detail dad ON rsda.software_data_auth_id = dad.software_data_auth_id
        WHERE
        u.is_deleted = 'N'
        AND r.is_deleted = 'N'
        AND ur.is_deleted = 'N'
        AND hu.is_deleted = 'N'
        AND rsda.is_deleted = 'N'
        AND dad.is_deleted = 'N'
        AND sda.is_deleted = 'N'
        AND u.`status` = '1'
        AND r.`status` = '1'
        AND u.id = hu.user_id
        AND sda.id = rsda.software_data_auth_id
        AND r.id = ur.role_id
        AND ur.user_id = u.id
        AND rsda.role_id = r.id
        <if test="softwareId !=null and softwareId != ''">
            AND sda.software_id = #{softwareId}
        </if>
        <if test="userID !=null">
            AND u.id = #{userID}
        </if>

    </select>
</mapper>