<?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="cn.com.tenlion.configtable.dao.configtable.IConfigTableDao">

    <resultMap id="configTableDTO" type="cn.com.tenlion.configtable.pojo.dtos.configtable.ConfigTableDTO">
        <result column="config_table_id" property="configTableId"/>
        <result column="config_table_number" property="configTableNumber"/>
        <result column="config_table_code" property="configTableCode"/>
        <result column="config_table_name" property="configTableName"/>
        <result column="config_table_activation" property="configTableActivation"/>
        <result column="config_table_activation_time" property="configTableActivationTime"/>
        <result column="config_table_remark" property="configTableRemark"/>
        <result column="config_table_mode" property="configTableMode"/>
        <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="configTableBO" type="cn.com.tenlion.configtable.pojo.bos.configtable.ConfigTableBO">
        <result column="config_table_id" property="configTableId"/>
        <result column="config_table_number" property="configTableNumber"/>
        <result column="config_table_code" property="configTableCode"/>
        <result column="config_table_name" property="configTableName"/>
        <result column="config_table_activation" property="configTableActivation"/>
        <result column="config_table_activation_time" property="configTableActivationTime"/>
        <result column="config_table_remark" property="configTableRemark"/>
        <result column="config_table_mode" property="configTableMode"/>
        <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="configTablePO" type="cn.com.tenlion.configtable.pojo.pos.configtable.ConfigTablePO">
        <result column="config_table_id" property="configTableId"/>
        <result column="config_table_number" property="configTableNumber"/>
        <result column="config_table_code" property="configTableCode"/>
        <result column="config_table_name" property="configTableName"/>
        <result column="config_table_activation" property="configTableActivation"/>
        <result column="config_table_activation_time" property="configTableActivationTime"/>
        <result column="config_table_remark" property="configTableRemark"/>
        <result column="config_table_mode" property="configTableMode"/>
        <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="updateActivation" parameterType="map">
        UPDATE
            m_config_table
        SET
            config_table_activation_time = #{ configTableActivationTime},
            config_table_activation = '1'
        WHERE
            config_table_id = #{configTableId} AND is_delete = 0
    </update>

    <!-- 通过数据库表名查找, 用于排查是否已经存在 -->
    <select id="getTableCode" parameterType="java.lang.String" resultMap="configTableDTO">
        SELECT
            t1.config_table_number,
            t1.config_table_code,
            t1.config_table_name,
            t1.config_table_activation,
            t1.config_table_activation_time,
            t1.config_table_remark,
            t1.config_table_mode,
            t1.config_table_id
        FROM
            m_config_table t1
        WHERE
            t1.is_delete = 0 AND t1.config_table_code = #{configTableCode}
    </select>

    <!-- 通过编码查找, 用于排查是否已经存在 -->
    <select id="getTableNumber" parameterType="java.lang.String" resultMap="configTableDTO">
        SELECT
            t1.config_table_number,
            t1.config_table_code,
            t1.config_table_name,
            t1.config_table_activation,
            t1.config_table_activation_time,
            t1.config_table_remark,
            t1.config_table_mode,
            t1.config_table_id
        FROM
            m_config_table t1
        WHERE
            t1.is_delete = 0 AND t1.config_table_number = #{configTableNumber}
    </select>

    <!-- 新增 -->
    <insert id="save" parameterType="map">
        INSERT INTO m_config_table(
            config_table_id,
            config_table_number,
            config_table_code,
            config_table_name,
            config_table_activation,
            config_table_activation_time,
            config_table_remark,
            config_table_mode,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        ) VALUES(
            #{configTableId},
            #{configTableNumber},
            #{configTableCode},
            #{configTableName},
            #{configTableActivation},
            #{configTableActivationTime},
            #{configTableRemark},
            #{configTableMode},
            #{creator},
            #{gmtCreate},
            #{modifier},
            #{gmtModified},
            #{isDelete}
        )
    </insert>

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

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

    <!-- 修改 -->
    <update id="update" parameterType="map">
        UPDATE
            m_config_table
        SET
        <if test="configTableNumber != null and configTableNumber != ''">
            config_table_number = #{configTableNumber},
        </if>
        <if test="configTableCode != null and configTableCode != ''">
            config_table_code = #{configTableCode},
        </if>
        <if test="configTableName != null and configTableName != ''">
            config_table_name = #{configTableName},
        </if>
        <if test="configTableRemark != null and configTableRemark != ''">
            config_table_remark = #{configTableRemark},
        </if>
        <if test="configTableMode != null and configTableMode != ''">
            config_table_mode = #{configTableMode},
        </if>
            gmt_modified = #{gmtModified},
            modifier = #{modifier},
            config_table_id = config_table_id
        WHERE
            config_table_id = #{configTableId}
    </update>

    <!-- 详情 -->
    <select id="get" parameterType="map" resultMap="configTableDTO">
        SELECT
            t1.config_table_number,
            t1.config_table_code,
            t1.config_table_name,
            t1.config_table_activation,
            t1.config_table_activation_time,
            t1.config_table_remark,
            t1.config_table_mode,
            t1.config_table_id
        FROM
            m_config_table t1
        WHERE
            t1.is_delete = 0
            <if test="configTableId != null and configTableId != ''">
        AND
            t1.config_table_id = #{configTableId}
            </if>
    </select>

    <!-- 详情 -->
    <select id="getBO" parameterType="map" resultMap="configTableBO">
        SELECT
            t1.config_table_id,
            t1.config_table_number,
            t1.config_table_code,
            t1.config_table_name,
            t1.config_table_activation,
            t1.config_table_activation_time,
            t1.config_table_remark,
            t1.config_table_mode,
            t1.creator,
            t1.gmt_create,
            t1.modifier,
            t1.gmt_modified,
            t1.is_delete
        FROM
            m_config_table t1
        WHERE
            t1.is_delete = 0
            <if test="configTableId != null and configTableId != ''">
        AND
            t1.config_table_id = #{configTableId}
            </if>
    </select>

    <!-- 详情 -->
    <select id="getPO" parameterType="map" resultMap="configTablePO">
        SELECT
            t1.config_table_id,
            t1.config_table_number,
            t1.config_table_code,
            t1.config_table_name,
            t1.config_table_activation,
            t1.config_table_activation_time,
            t1.config_table_remark,
            t1.config_table_mode,
            t1.creator,
            t1.gmt_create,
            t1.modifier,
            t1.gmt_modified,
            t1.is_delete
        FROM
            m_config_table t1
        WHERE
            t1.is_delete = 0
            <if test="configTableId != null and configTableId != ''">
        AND
            t1.config_table_id = #{configTableId}
            </if>
    </select>

    <!-- 列表 -->
    <select id="list" parameterType="map" resultMap="configTableDTO">
        SELECT
            t1.config_table_id,
            t1.config_table_number,
            t1.config_table_code,
            t1.config_table_name,
            t1.config_table_activation,
            t1.config_table_activation_time,
            t1.config_table_remark,
            t1.config_table_mode,
            t1.creator,
            t1.gmt_create,
            t1.modifier,
            t1.gmt_modified,
            t1.is_delete,
            1
        FROM
            m_config_table t1
        WHERE
            t1.is_delete = 0
        <if test="configTableActivation != null and configTableActivation != ''">
            AND t1.config_table_activation = #{configTableActivation}
        </if>
        <if test="keywords != null and keywords != ''">
        AND (
            <!-- 这里添加其他条件 -->
            t1.config_table_code LIKE CONCAT('%', #{keywords}, '%')
            OR
            t1.config_table_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="configTableIds != null and configTableIds.size > 0">
        AND
            t1.config_table_id IN
            <foreach collection="configTableIds" index="index" open="(" separator="," close=")">
                #{configTableIds[${index}]}
            </foreach>
        </if>
        ORDER BY t1.gmt_create DESC
    </select>

    <!-- 列表 -->
    <select id="listBO" parameterType="map" resultMap="configTableBO">
        SELECT
            t1.config_table_id,
            t1.config_table_number,
            t1.config_table_code,
            t1.config_table_name,
            t1.config_table_activation,
            t1.config_table_activation_time,
            t1.config_table_remark,
            t1.config_table_mode,
            t1.creator,
            t1.gmt_create,
            t1.modifier,
            t1.gmt_modified,
            t1.is_delete
        FROM
            m_config_table 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="configTableIds != null and configTableIds.size > 0">
        AND
            t1.config_table_id IN
            <foreach collection="configTableIds" index="index" open="(" separator="," close=")">
                #{configTableIds[${index}]}
            </foreach>
        </if>
    </select>

    <!-- 列表 -->
    <select id="listPO" parameterType="map" resultMap="configTablePO">
        SELECT
            t1.config_table_id,
            t1.config_table_number,
            t1.config_table_code,
            t1.config_table_name,
            t1.config_table_activation,
            t1.config_table_activation_time,
            t1.config_table_remark,
            t1.config_table_mode,
            t1.creator,
            t1.gmt_create,
            t1.modifier,
            t1.gmt_modified,
            t1.is_delete
        FROM
            m_config_table 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="configTableIds != null and configTableIds.size > 0">
        AND
            t1.config_table_id IN
            <foreach collection="configTableIds" index="index" open="(" separator="," close=")">
                #{configTableIds[${index}]}
            </foreach>
        </if>
    </select>

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

</mapper>