<?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.op.system.mapper.SysDatasourceMapper">

    <resultMap type="SysDatasource" id="SysDatasourceResult">
        <result property="id"    column="id"    />
        <result property="url"    column="url"    />
        <result property="ipPort"    column="ip_port"    />
        <result property="userName"    column="user_name"    />
        <result property="password"    column="password"    />
        <result property="driveClassName"    column="drive_class_name"    />
        <result property="poolName"    column="pool_name"    />
        <result property="parkName"    column="park_name"    />
        <result property="status"    column="status"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="deptId"    column="dept_id"    />
        <result property="schemaCreated"    column="schema_created"    />
        <result property="deptName"    column="dept_name"    />
        <result property="systemType"    column="system_type"    />
    </resultMap>

    <sql id="selectSysDatasourceVo">
        select id, url, ip_port, user_name, password, drive_class_name, pool_name, park_name,
               status, del_flag, create_by, create_time, update_by, update_time, remark,system_type,
               dept_id, schema_created from sys_datasource
    </sql>

    <select id="selectSysDatasourceList" parameterType="SysDatasource" resultMap="SysDatasourceResult">
        select sd.id, sd.url, sd.ip_port, sd.user_name, sd.password, sd.drive_class_name, sd.pool_name, sd.park_name,
        sd.status, sd.del_flag, sd.create_by, sd.create_time, sd.update_by, sd.update_time, sd.remark,sd.system_type,
        sd.dept_id,d.dept_name, sd.schema_created from sys_datasource sd
        left join sys_dept d on sd.dept_id = d.dept_id
        <where>
            <if test="url != null  and url != ''"> and url = #{url}</if>
            <if test="ipPort != null  and ipPort != ''"> and ip_port like concat('%', #{ipPort}, '%')</if>
            <if test="userName != null  and userName != ''"> and user_name like concat('%', #{userName}, '%')</if>
            <if test="password != null  and password != ''"> and password = #{password}</if>
            <if test="driveClassName != null  and driveClassName != ''"> and drive_class_name like concat('%', #{driveClassName}, '%')</if>
            <if test="poolName != null  and poolName != ''"> and pool_name like concat('%', #{poolName}, '%')</if>
            <if test="parkName != null  and parkName != ''"> and park_name like concat('%', #{parkName}, '%')</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="deptId != null "> and dept_id = #{deptId}</if>
            <if test="schemaCreated != null  and schemaCreated != ''"> and schema_created = #{schemaCreated}</if>
        </where>
    </select>

    <select id="selectSysDatasourceById" parameterType="String" resultMap="SysDatasourceResult">
        <include refid="selectSysDatasourceVo"/>
        where id = #{id}
    </select>
    <select id="selectAllocatedList" resultType="com.op.system.domain.SysDatasource">
        select sd.id,
               sd.ip_port ipPort,
               sd.pool_name poolName,
               sud.user_id userId,
               su.user_name userName,
               su.nick_name nickName
        from sys_datasource sd
        left join sys_user_datasource sud on sd.id = sud.datasource_id
        left join sys_user su on su.user_id = sud.user_id
        where sud.user_id is not null
        <if test="nickName != null and nickName != ''">
            AND su.nick_name like concat('%', #{nickName}, '%')
        </if>
        <if test="datasourceId != null and datasourceId != ''">
            AND sd.id = #{datasourceId}
        </if>
    </select>
    <select id="selectUnallocatedList" resultType="com.op.system.api.domain.SysUser">
        select distinct u.user_id userId, u.dept_id deptId, u.user_name userName, u.nick_name nickName,
                        u.email, u.phonenumber, u.status, u.create_time createTime
        from sys_user u
        left join sys_user_datasource ur on u.user_id = ur.user_id
        left join sys_datasource r on r.id = ur.datasource_id
        where u.del_flag = '0' and (r.id != #{datasourceId} or r.id IS NULL)
        and u.user_id not in (select ur.user_id from sys_user_datasource ur where ur.datasource_id = #{datasourceId})
        <if test="userName != null and userName != ''">
            AND u.user_name like concat('%', #{userName}, '%')
        </if>
        <if test="phonenumber != null and phonenumber != ''">
            AND u.phonenumber like concat('%', #{phonenumber}, '%')
        </if>
    </select>
    <select id="selectDBListByDept" resultType="com.op.system.domain.SysDatasource">
        select id
        from sys_datasource
        where del_flag = 0 and dept_id = #{deptId}
    </select>

    <insert id="insertSysDatasource" parameterType="SysDatasource">
        insert into sys_datasource
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="url != null and url != ''">url,</if>
            <if test="ipPort != null">ip_port,</if>
            <if test="userName != null and userName != ''">user_name,</if>
            <if test="password != null and password != ''">password,</if>
            <if test="driveClassName != null and driveClassName != ''">drive_class_name,</if>
            <if test="poolName != null and poolName != ''">pool_name,</if>
            <if test="parkName != null">park_name,</if>
            <if test="status != null">status,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="schemaCreated != null">schema_created,</if>
            <if test="systemType != null">system_type,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="url != null and url != ''">#{url},</if>
            <if test="ipPort != null">#{ipPort},</if>
            <if test="userName != null and userName != ''">#{userName},</if>
            <if test="password != null and password != ''">#{password},</if>
            <if test="driveClassName != null and driveClassName != ''">#{driveClassName},</if>
            <if test="poolName != null and poolName != ''">#{poolName},</if>
            <if test="parkName != null">#{parkName},</if>
            <if test="status != null">#{status},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="schemaCreated != null">#{schemaCreated},</if>
            <if test="systemType != null">#{systemType},</if>
         </trim>
    </insert>
    <insert id="batchUserDB">
        insert into sys_user_datasource(user_id, datasource_id) values
        <foreach item="item" index="index" collection="list" separator=",">
            (#{item.userId},#{item.datasourceId})
        </foreach>
    </insert>

    <update id="updateSysDatasource" parameterType="SysDatasource">
        update sys_datasource
        <trim prefix="SET" suffixOverrides=",">
            <if test="url != null and url != ''">url = #{url},</if>
            <if test="ipPort != null">ip_port = #{ipPort},</if>
            <if test="userName != null and userName != ''">user_name = #{userName},</if>
            <if test="password != null and password != ''">password = #{password},</if>
            <if test="driveClassName != null and driveClassName != ''">drive_class_name = #{driveClassName},</if>
            <if test="poolName != null and poolName != ''">pool_name = #{poolName},</if>
            <if test="parkName != null">park_name = #{parkName},</if>
            <if test="status != null">status = #{status},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="schemaCreated != null">schema_created = #{schemaCreated},</if>
            <if test="systemType != null">system_type = #{systemType},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSysDatasourceById" parameterType="String">
        delete from sys_datasource where id = #{id}
    </delete>

    <delete id="deleteSysDatasourceByIds" parameterType="String">
        delete from sys_datasource where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <delete id="deleteUserRoleInfo">
        delete from sys_user_datasource where user_id=#{userId} and datasource_id=#{datasourceId}
    </delete>

    <delete id="deleteUserRoleInfos">
        delete from sys_user_datasource where datasource_id=#{datasourceId} and user_id in
        <foreach collection="userIds" item="userId" open="(" separator="," close=")">
            #{userId}
        </foreach>
    </delete>
</mapper>
