<?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.hotent.portal.persistence.dao.PortalBizSystemDao">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hotent.portal.model.PortalBizSystem">
        <result column="ID_" property="id" />
        <result column="SYS_CODE_" property="sysCode" />
        <result column="SYS_NAME_" property="sysName" />
        <result column="ENABLED_" property="enabled" />
        <result column="SECRET_KEY_" property="secretKey" />
        <result column="MANAGER_ID_" property="managerId" />
        <result column="MANAGER_NAME_" property="managerName" />
        <result column="ROOT_ADDRESS_" property="rootAddress" />
        <result column="HOME_ADDRESS_" property="homeAddress" />
        <result column="BPM_MSG_QUEUE_" property="bpmMsgQueue" />
        <result column="DESCRIPTION_" property="description" />
        <result column="ROBOT_ID_" property="robotId" />
        <result column="CREATE_TIME_" property="createTime" />
        <result column="CREATE_BY_" property="createBy" />
        <result column="CREATE_ORG_ID_" property="createOrgId" />
        <result column="IS_DELE_" property="isDele" />
        <result column="UPDATE_TIME_" property="updateTime" />
        <result column="UPDATE_BY_" property="updateBy" />
        <result column="ROBOT_NAME_" property="robotName" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        ID_, SYS_CODE_, SYS_NAME_, ENABLED_, SECRET_KEY_, MANAGER_ID_, MANAGER_NAME_, ROOT_ADDRESS_, HOME_ADDRESS_, BPM_MSG_QUEUE_, DESCRIPTION_, ROBOT_ID_, CREATE_TIME_, CREATE_BY_, CREATE_ORG_ID_, IS_DELE_, UPDATE_TIME_, UPDATE_BY_,ROBOT_NAME_
    </sql>

    <select id="getAuthUserBySystemCode" parameterType="java.util.Map" resultMap="BaseResultMap">
        select * from portal_biz_system ${ew.customSqlSegment}
    </select>


    <select id="selectData" resultType="com.hotent.portal.model.PortalBizSystem">
        SELECT
            *
        FROM
            portal_biz_system
        WHERE
                sys_code_ IN (
                SELECT DISTINCT
                    a.sys_code_
                FROM
                    portal_biz_system a
                        LEFT JOIN portal_biz_system_auth b ON a.SYS_CODE_ = b.SYS_CODE_
                WHERE
                    a.IS_DELE_ = '0'
                    <if test="isAdmin == false">
                        AND a.CREATE_BY_ = #{userId}
                    </if>
                   <if test="sysCode !=null ">
                        AND a.SYS_CODE_ like '%${sysCode}%'
                   </if>
                   <if test="sysCode == null">
                       OR b.MANAGER_ID_ = #{userId}
                   </if>
            )
          AND portal_biz_system.IS_DELE_ = '0'
        <if test="@com.hotent.base.ognl.Ognl@isNotEmpty(orderFields)">
            ORDER BY ${orderFields}
        </if>
    </select>

    <update id="deleteBySystemCode" parameterType="java.lang.String">
        UPDATE portal_biz_system
        SET IS_DELE_ = '1'
        WHERE
            SYS_CODE_ = (
            SELECT
                    SYS_CODE_
            FROM
            ( SELECT DISTINCT SYS_CODE_ FROM portal_biz_system WHERE SYS_CODE_ = #{systemCode} ) temp)
</update>

    <update id="updateSystem" parameterType="com.hotent.portal.model.PortalBizSystem">
        update portal_biz_system set ENABLED_ =#{enabled},UPDATE_BY_=#{updateBy} where SYS_CODE_ =#{sysCode}
    </update>
</mapper>
