<?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.service.department.dao.IDepartmentAdjustmentDao">

    <cache flushInterval="3600000"/>

    <resultMap id="departmentAdjustmentDTO" type="ink.wgink.service.department.pojo.dtos.DepartmentAdjustmentDTO">
        <result column="old_department_name" property="oldDepartmentName"/>
        <result column="new_department_name" property="newDepartmentName"/>
        <result column="adjustment_type" property="adjustmentType"/>
        <result column="creator_name" property="creatorName"/>
        <result column="gmt_create" property="gmtCreate"/>
    </resultMap>

    <!-- 建表 -->
    <update id="createTable">
        CREATE TABLE IF NOT EXISTS `sys_department_adjustment` (
            `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            `old_department_id` char(36) NOT NULL,
            `old_department_name` varchar(255) NOT NULL COMMENT '旧部门名称',
            `new_department_id` char(36) NOT NULL,
            `new_department_name` varchar(255) NOT NULL COMMENT '新部门名称',
            `adjustment_type` varchar(10) NOT NULL,
            `gmt_create` datetime DEFAULT NULL,
            `creator` char(36) DEFAULT NULL,
            `creator_name` varchar(255) DEFAULT NULL COMMENT '创建人',
            PRIMARY KEY (`id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='部门调整';
    </update>

    <!-- 新增部门调整 -->
    <insert id="save" parameterType="map" flushCache="true">
        INSERT INTO sys_department_adjustment(
            old_department_id,
            old_department_name,
            new_department_id,
            new_department_name,
            adjustment_type,
            gmt_create,
            creator,
            creator_name
        ) VALUES(
            #{oldDepartmentId},
            #{oldDepartmentName},
            #{newDepartmentId},
            #{newDepartmentName},
            #{adjustmentType},
            #{gmtCreate},
            #{creator},
            #{creatorName}
        )
    </insert>

    <!-- 部门调整列表 -->
    <select id="list" parameterType="map" resultMap="departmentAdjustmentDTO" useCache="true">
        SELECT
            old_department_name,
            new_department_name,
            adjustment_type,
            LEFT(gmt_create, 19) gmt_create,
            creator_name
        FROM
            sys_department_adjustment
        WHERE
            1 = 1
        <if test="adjustmentType != null and adjustmentType != ''">
        AND
            adjustment_type = #{adjustmentType}
        </if>
        <if test="keywords != null and keywords != ''">
        AND (
            old_department_name LIKE CONCAT('%', #{keywords}, '%')
            OR
            new_department_name LIKE CONCAT('%', #{keywords}, '%')
        )
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            LEFT(gmt_create, 10) <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
        AND
            LEFT(gmt_create, 10) <![CDATA[ <= ]]> #{endTime}
        </if>
        ORDER BY
            gmt_create DESC, id DESC
    </select>


</mapper>