<?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.position.dao.IPositionDao">

    <cache flushInterval="3600000"/>

    <resultMap id="positionPO" type="ink.wgink.pojo.pos.PositionPO">
        <id property="positionId" column="position_id"/>
        <result property="positionParentId" column="position_parent_id"/>
        <result property="positionParentName" column="position_parent_name"/>
        <result property="positionName" column="position_name"/>
        <result property="positionSummary" column="position_summary"/>
        <result property="positionCode" column="position_code"/>
        <result property="positionNameEn" column="position_name_en"/>
    </resultMap>

    <resultMap id="positionDTO" type="ink.wgink.pojo.dtos.position.PositionDTO">
        <id property="positionId" column="position_id"/>
        <result property="positionParentId" column="position_parent_id"/>
        <result property="positionParentName" column="position_parent_name"/>
        <result property="positionName" column="position_name"/>
        <result property="positionSummary" column="position_summary"/>
        <result property="positionCode" column="position_code"/>
        <result property="positionNameEn" column="position_name_en"/>
    </resultMap>

    <resultMap id="positionZTreeDTO" type="ink.wgink.pojo.dtos.ZTreeDTO">
        <id property="id" column="position_id"/>
        <result property="pId" column="position_parent_id"/>
        <result property="name" column="position_name"/>
    </resultMap>

    <!-- 建表 -->
    <update id="createTable">
        CREATE TABLE IF NOT EXISTS `sys_position` (
            `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            `position_id` char(36) NOT NULL COMMENT '职位主键',
            `position_parent_id` char(36) NOT NULL DEFAULT '0' COMMENT '职位父ID',
            `position_name` varchar(255) NOT NULL COMMENT '职位名称',
            `position_summary` varchar(255) DEFAULT NULL COMMENT '职位说明',
            `position_code` varchar(255) NOT NULL COMMENT '职位编码',
            `position_name_en` varchar(255) DEFAULT NULL COMMENT '英文名称',
            `creator` char(36) DEFAULT NULL,
            `gmt_create` datetime DEFAULT NULL,
            `modifier` char(36) DEFAULT NULL,
            `gmt_modified` datetime DEFAULT NULL,
            `is_delete` int(2) DEFAULT '0',
            PRIMARY KEY (`id`,`position_id`),
            UNIQUE KEY `position_id` (`position_id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    </update>

    <!-- 新增职位 -->
    <insert id="save" parameterType="map" flushCache="true">
        INSERT INTO sys_position(
            position_id,
            position_parent_id,
            position_name,
            position_summary,
            position_code,
            position_name_en,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        ) VALUES(
            #{positionId},
            #{positionParentId},
            #{positionName},
            #{positionSummary},
            #{positionCode},
            #{positionNameEn},
            #{creator},
            #{gmtCreate},
            #{modifier},
            #{gmtModified},
            #{isDelete}
        )
    </insert>

    <!-- 删除职位 -->
    <update id="remove" parameterType="map" flushCache="true">
        UPDATE
            sys_position
        SET
            is_delete = 1,
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            position_id IN
        <foreach collection="positionIds" index="index" open="(" separator="," close=")">
            #{positionIds[${index}]}
        </foreach>
    </update>

    <!-- 修改职位 -->
    <update id="update" parameterType="map" flushCache="true">
        UPDATE
            sys_position
        SET
        <if test="positionName != null and positionName != ''">
            position_name = #{positionName},
        </if>
        <if test="positionCode != null and positionCode != ''">
            position_code = #{positionCode},
        </if>
        <if test="positionSummary != null">
            position_summary = #{positionSummary},
        </if>
        <if test="positionNameEn != null">
            position_name_en = #{positionNameEn},
        </if>
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            position_id = #{positionId}
    </update>

    <!-- ztree列表 -->
    <select id="listZTree" parameterType="map" resultMap="positionZTreeDTO" useCache="true">
        SELECT
            *
        FROM
            sys_position
        WHERE
            is_delete = 0
        <if test="positionParentId != null and positionParentId != ''">
        AND
            position_parent_id = #{positionParentId}
        </if>
    </select>

    <!-- 职位列表 -->
    <select id="list" parameterType="map" resultMap="positionDTO" useCache="true">
        SELECT
            *
        FROM
            sys_position
        WHERE
            is_delete = 0
        <if test="positionParentId != null and positionParentId != ''">
        AND
            position_parent_id = #{positionParentId}
        </if>
        <if test="keywords != null and keywords != ''">
        AND
            position_name LIKE CONCAT('%', #{keywords}, '%')
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            gmt_create <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            gmt_create <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="positionIds != null and positionIds.size > 0">
        AND
            position_id IN
            <foreach collection="positionIds" index="index" open="(" separator="," close=")">
                #{positionIds[${index}]}
            </foreach>
        </if>
        <choose>
            <when test="sort != null and (sort == 'positionName' or sort == 'positionCode')">
                ORDER BY
                <if test="sort == 'positionName'">
                    position_name ${order}
                </if>
                <if test="sort == 'positionCode'">
                    position_code ${order}
                </if>
            </when>
            <otherwise>
            ORDER BY
                gmt_create desc
            </otherwise>
        </choose>
    </select>

    <!-- 职位详情 -->
    <select id="get" parameterType="map" resultMap="positionDTO" useCache="false">
        SELECT
            t1.*,
            t2.position_name position_parent_name
        FROM
            sys_position t1
        LEFT JOIN
            sys_position t2
        ON
            t1.position_parent_id = t2.position_id
        AND
            t2.is_delete = 0
        WHERE
            t1.is_delete = 0
        <if test="positionId != null and positionId != ''">
        AND
            t1.position_id = #{positionId}
        </if>
    </select>

    <!-- 子节点数量 -->
    <select id="countByParentId" parameterType="String" resultType="Integer" useCache="false">
        SELECT
            COUNT(*)
        FROM
            sys_position
        WHERE
            is_delete = 0
        AND
            position_parent_id = #{_parameter}
    </select>

    <!-- 获取最后一个子职位，实际数据，包含已删除，方式编码重复 -->
    <select id="getLastByParentId" parameterType="String" resultMap="positionDTO" useCache="false">
        SELECT
            *
        FROM
            sys_position
        WHERE
            position_parent_id = #{_parameter}
        ORDER BY
            position_code desc
        LIMIT 0, 1
    </select>

    <!-- 统计职位 -->
    <select id="count" resultType="java.lang.Integer" useCache="true">
        SELECT
            COUNT(*)
        FROM
            sys_position
        WHERE
            is_delete = 0
    </select>

    <!-- 职位列表 -->
    <select id="listPO" parameterType="map" resultMap="positionPO" useCache="true">
        SELECT
            *
        FROM
            sys_position
        WHERE
            is_delete = 0
        <if test="positionParentId != null and positionParentId != ''">
            AND
            position_parent_id = #{positionParentId}
        </if>
        <if test="positionIds != null and positionIds.size > 0">
            AND
            position_id IN
            <foreach collection="positionIds" index="index" open="(" separator="," close=")">
                #{positionIds[${index}]}
            </foreach>
        </if>
    </select>

</mapper>