<?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.gangquan360.smartadmin.module.position.PositionDao">


    <sql id="baseSql">
        id,
        position_name,
        remark,
        update_time,
        create_time
    </sql>

    <select id="selectByPage" resultType="com.gangquan360.smartadmin.module.position.domain.entity.PositionEntity">
        SELECT
        <include refid="baseSql"></include>
        FROM
        t_position
        <where>
            <if test="positionName != null and positionName != ''">
                AND INSTR(position_name,#{positionName})
            </if>
        </where>
        order by create_time desc
    </select>

    <select id="selectRelation" resultType="com.gangquan360.smartadmin.module.position.domain.dto.PositionRelationResultDTO">
        SELECT
        job.position_name,
        rel.position_id,
        rel.employee_id,
        rel.update_time,
        rel.create_time
        FROM
        t_position_relation rel
        LEFT JOIN t_position job ON rel.position_id = job.id
        <where>
            <if test="positionId != null">
                AND rel.position_id = #{positionId}
            </if>
            <if test="employeeId != null">
                AND rel.employee_id = #{employeeId}
            </if>
        </where>
    </select>

    <select id="selectEmployeesRelation" resultType="com.gangquan360.smartadmin.module.position.domain.dto.PositionRelationResultDTO">
        SELECT
        job.position_name,
        rel.position_id,
        rel.employee_id,
        rel.update_time,
        rel.create_time
        FROM
        t_position_relation rel
        LEFT JOIN t_position job ON rel.position_id = job.id
        <where>
            rel.employee_id IN
            <foreach collection="employeeIdList" item="item" separator="," open="(" close=")">
                #{item}
            </foreach>
        </where>
    </select>

    <insert id="insertBatchRelation">
        INSERT INTO t_position_relation (position_id,employee_id) VALUES
        <foreach collection="batchDTO.positionIdList" item="item" separator=",">
            (#{item},#{batchDTO.employeeId})
        </foreach>
    </insert>


    <delete id="deleteRelationByEmployeeId">
        DELETE FROM t_position_relation WHERE employee_id = #{employeeId}
    </delete>

</mapper>