<?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="ink.wgink.database.dao.mysql.IMysqlDao">

    <resultMap id="mysqlDTO" type="ink.wgink.database.pojo.dtos.mysql.MysqlDTO">
        <result column="mysql_id" property="mysqlId"/>
        <result column="mysql_name" property="mysqlName"/>
        <result column="mysql_host" property="mysqlHost"/>
        <result column="mysql_port" property="mysqlPort"/>
        <result column="mysql_username" property="mysqlUsername"/>
        <result column="mysql_password" property="mysqlPassword"/>
        <result column="gmt_create" property="gmtCreate"/>
        <result column="gmt_modified" property="gmtModified"/>
    </resultMap>

    <resultMap id="mysqlBO" type="ink.wgink.database.pojo.bos.mysql.MysqlBO">
        <result column="mysql_id" property="mysqlId"/>
        <result column="mysql_name" property="mysqlName"/>
        <result column="mysql_host" property="mysqlHost"/>
        <result column="mysql_port" property="mysqlPort"/>
        <result column="mysql_username" property="mysqlUsername"/>
        <result column="mysql_password" property="mysqlPassword"/>
        <result column="creator" property="creator"/>
        <result column="gmt_create" property="gmtCreate"/>
        <result column="modifier" property="modifier"/>
        <result column="gmt_modified" property="gmtModified"/>
        <result column="is_delete" property="isDelete"/>
    </resultMap>

    <resultMap id="mysqlPO" type="ink.wgink.database.pojo.pos.mysql.MysqlPO">
        <result column="mysql_id" property="mysqlId"/>
        <result column="mysql_name" property="mysqlName"/>
        <result column="mysql_host" property="mysqlHost"/>
        <result column="mysql_port" property="mysqlPort"/>
        <result column="mysql_username" property="mysqlUsername"/>
        <result column="mysql_password" property="mysqlPassword"/>
        <result column="creator" property="creator"/>
        <result column="gmt_create" property="gmtCreate"/>
        <result column="modifier" property="modifier"/>
        <result column="gmt_modified" property="gmtModified"/>
        <result column="is_delete" property="isDelete"/>
    </resultMap>

    <update id="createTable">
        CREATE TABLE IF NOT EXISTS `conn_mysql` (
            `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            `mysql_id` char(36) DEFAULT NULL COMMENT '主键',
            `mysql_name` varchar(255) DEFAULT NULL COMMENT '连接名',
            `mysql_host` varchar(255) DEFAULT NULL COMMENT '主机',
            `mysql_port` varchar(10) DEFAULT NULL COMMENT '端口',
            `mysql_username` varchar(255) DEFAULT NULL COMMENT '用户名',
            `mysql_password` varchar(255) DEFAULT NULL COMMENT '密码',
            `creator` char(36) DEFAULT NULL COMMENT '创建人',
            `gmt_create` datetime DEFAULT NULL COMMENT '创建时间',
            `modifier` char(36) DEFAULT NULL COMMENT '修改人',
            `gmt_modified` datetime DEFAULT NULL COMMENT '修改时间',
            `is_delete` int(1) DEFAULT '0' COMMENT '是否删除',
            PRIMARY KEY (`id`),
            KEY `connection_id` (`mysql_id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='mysql连接';
    </update>

    <!-- 新增mysql连接 -->
    <insert id="save" parameterType="map">
        INSERT INTO conn_mysql(
            mysql_id,
            mysql_name,
            mysql_host,
            mysql_port,
            mysql_username,
            mysql_password,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        ) VALUES(
            #{mysqlId},
            #{mysqlName},
            #{mysqlHost},
            #{mysqlPort},
            #{mysqlUsername},
            #{mysqlPassword},
            #{creator},
            #{gmtCreate},
            #{modifier},
            #{gmtModified},
            #{isDelete}
        )
    </insert>

    <!-- 删除mysql连接 -->
    <update id="remove" parameterType="map">
        UPDATE
            conn_mysql
        SET
            gmt_modified = #{gmtModified},
            modifier = #{modifier},
            is_delete = 1
        WHERE
            mysql_id IN
        <foreach collection="mysqlIds" index="index" open="(" separator="," close=")">
            #{mysqlIds[${index}]}
        </foreach>
    </update>

    <!-- 删除mysql连接（物理） -->
    <update id="delete" parameterType="map">
        DELETE FROM
            conn_mysql
        WHERE
            mysql_id IN
        <foreach collection="mysqlIds" index="index" open="(" separator="," close=")">
            #{mysqlIds[${index}]}
        </foreach>
    </update>

    <!-- 修改mysql连接 -->
    <update id="update" parameterType="map">
        UPDATE
            conn_mysql
        SET
        <if test="mysqlName != null and mysqlName != ''">
            mysql_name = #{mysqlName},
        </if>
        <if test="mysqlHost != null and mysqlHost != ''">
            mysql_host = #{mysqlHost},
        </if>
        <if test="mysqlPort != null and mysqlPort != ''">
            mysql_port = #{mysqlPort},
        </if>
        <if test="mysqlUsername != null and mysqlUsername != ''">
            mysql_username = #{mysqlUsername},
        </if>
        <if test="mysqlPassword != null and mysqlPassword != ''">
            mysql_password = #{mysqlPassword},
        </if>
            gmt_modified = #{gmtModified},
            modifier = #{modifier},
            mysql_id = mysql_id
        WHERE
            mysql_id = #{mysqlId}
    </update>

    <!-- mysql连接详情 -->
    <select id="get" parameterType="map" resultMap="mysqlDTO">
        SELECT
            t1.mysql_name,
            t1.mysql_host,
            t1.mysql_port,
            t1.mysql_username,
            t1.mysql_password,
            t1.mysql_id
        FROM
            conn_mysql t1
        WHERE
            t1.is_delete = 0
            <if test="mysqlId != null and mysqlId != ''">
        AND
            t1.mysql_id = #{mysqlId}
            </if>
    </select>

    <!-- mysql连接详情 -->
    <select id="getBO" parameterType="map" resultMap="mysqlBO">
        SELECT
            t1.mysql_id,
            t1.mysql_name,
            t1.mysql_host,
            t1.mysql_port,
            t1.mysql_username,
            t1.mysql_password,
            t1.creator,
            t1.gmt_create,
            t1.modifier,
            t1.gmt_modified,
            t1.is_delete
        FROM
            conn_mysql t1
        WHERE
            t1.is_delete = 0
            <if test="mysqlId != null and mysqlId != ''">
        AND
            t1.mysql_id = #{mysqlId}
            </if>
    </select>

    <!-- mysql连接详情 -->
    <select id="getPO" parameterType="map" resultMap="mysqlPO">
        SELECT
            t1.mysql_id,
            t1.mysql_name,
            t1.mysql_host,
            t1.mysql_port,
            t1.mysql_username,
            t1.mysql_password,
            t1.creator,
            t1.gmt_create,
            t1.modifier,
            t1.gmt_modified,
            t1.is_delete
        FROM
            conn_mysql t1
        WHERE
            t1.is_delete = 0
            <if test="mysqlId != null and mysqlId != ''">
        AND
            t1.mysql_id = #{mysqlId}
            </if>
    </select>

    <!-- mysql连接列表 -->
    <select id="list" parameterType="map" resultMap="mysqlDTO">
        SELECT
            t1.mysql_id,
            t1.mysql_name,
            t1.mysql_host,
            t1.mysql_port,
            t1.mysql_username,
            t1.mysql_password,
            t1.gmt_create,
            t1.gmt_modified
        FROM
            conn_mysql t1
        WHERE
            t1.is_delete = 0
        <if test="keywords != null and keywords != ''">
        AND (
            t1.mysql_name LIKE CONCAT('%', #{keywords}, '%')
        )
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="mysqlIds != null and mysqlIds.size > 0">
        AND
            t1.mysql_id IN
            <foreach collection="mysqlIds" index="index" open="(" separator="," close=")">
                #{mysqlIds[${index}]}
            </foreach>
        </if>
    </select>

    <!-- mysql连接列表 -->
    <select id="listBO" parameterType="map" resultMap="mysqlBO">
        SELECT
            t1.mysql_id,
            t1.mysql_name,
            t1.mysql_host,
            t1.mysql_port,
            t1.mysql_username,
            t1.mysql_password,
            t1.creator,
            t1.gmt_create,
            t1.modifier,
            t1.gmt_modified,
            t1.is_delete
        FROM
            conn_mysql t1
        WHERE
            t1.is_delete = 0
        <if test="keywords != null and keywords != ''">
        AND (
            <!-- 这里添加其他条件 -->
            t1.id LIKE CONCAT('%', #{keywords}, '%')
        )
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="mysqlIds != null and mysqlIds.size > 0">
        AND
            t1.mysql_id IN
            <foreach collection="mysqlIds" index="index" open="(" separator="," close=")">
                #{mysqlIds[${index}]}
            </foreach>
        </if>
    </select>

    <!-- mysql连接列表 -->
    <select id="listPO" parameterType="map" resultMap="mysqlPO">
        SELECT
            t1.mysql_id,
            t1.mysql_name,
            t1.mysql_host,
            t1.mysql_port,
            t1.mysql_username,
            t1.mysql_password,
            t1.creator,
            t1.gmt_create,
            t1.modifier,
            t1.gmt_modified,
            t1.is_delete
        FROM
            conn_mysql t1
        WHERE
            t1.is_delete = 0
        <if test="keywords != null and keywords != ''">
        AND (
            <!-- 这里添加其他条件 -->
            t1.id LIKE CONCAT('%', #{keywords}, '%')
        )
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="mysqlIds != null and mysqlIds.size > 0">
        AND
            t1.mysql_id IN
            <foreach collection="mysqlIds" index="index" open="(" separator="," close=")">
                #{mysqlIds[${index}]}
            </foreach>
        </if>
    </select>

    <!-- mysql连接统计  -->
    <select id="count" parameterType="map" resultType="Integer">
        SELECT
            COUNT(*)
        FROM
            conn_mysql t1
        WHERE
            t1.is_delete = 0
    </select>

</mapper>