<?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.ruoyi.ccdi.mapper.CcdiEmployeeMapper">

    <!-- 员工基本信息ResultMap（用于列表查询，不包含亲属） -->
    <resultMap type="com.ruoyi.ccdi.domain.vo.CcdiEmployeeVO" id="CcdiEmployeeVOResult">
        <id property="employeeId" column="employee_id"/>
        <result property="name" column="name"/>
        <result property="tellerNo" column="teller_no"/>
        <result property="deptId" column="dept_id"/>
        <result property="deptName" column="dept_name"/>
        <result property="idCard" column="id_card"/>
        <result property="phone" column="phone"/>
        <result property="hireDate" column="hire_date"/>
        <result property="status" column="status"/>
        <result property="createTime" column="create_time"/>
    </resultMap>

    <!-- 员工详情ResultMap（包含亲属信息） -->
    <resultMap type="com.ruoyi.ccdi.domain.vo.CcdiEmployeeVO" id="CcdiEmployeeWithRelativesResult" extends="CcdiEmployeeVOResult">
        <collection property="relatives" ofType="CcdiEmployeeRelativeVO" notNullColumn="relative_id">
            <id property="relativeId" column="relative_id"/>
            <result property="employeeId" column="employee_id"/>
            <result property="relativeName" column="relative_name"/>
            <result property="relativeIdCard" column="relative_id_card"/>
            <result property="relativePhone" column="relative_phone"/>
            <result property="relationship" column="relationship"/>
        </collection>
    </resultMap>

    <select id="selectEmployeePageWithDept" resultMap="CcdiEmployeeVOResult">
        SELECT
            e.employee_id, e.name, e.teller_no, e.dept_id, e.id_card, e.phone, e.hire_date, e.status, e.create_time,
            d.dept_name
        FROM ccdi_employee e
        LEFT JOIN sys_dept d ON e.dept_id = d.dept_id
        <where>
            <if test="query.name != null and query.name != ''">
                AND e.name LIKE CONCAT('%', #{query.name}, '%')
            </if>
            <if test="query.tellerNo != null and query.tellerNo != ''">
                AND e.teller_no = #{query.tellerNo}
            </if>
            <if test="query.deptId != null">
                AND e.dept_id = #{query.deptId}
            </if>
            <if test="query.idCard != null and query.idCard != ''">
                AND e.id_card LIKE CONCAT('%', #{query.idCard}, '%')
            </if>
            <if test="query.status != null and query.status != ''">
                AND e.status = #{query.status}
            </if>
        </where>
        ORDER BY e.create_time DESC
    </select>

    <select id="selectEmployeeWithRelatives" parameterType="Long" resultMap="CcdiEmployeeWithRelativesResult">
        SELECT
            e.employee_id, e.name, e.teller_no, e.dept_id, e.id_card, e.phone, e.hire_date, e.status, e.create_time,
            d.dept_name,
            r.relative_id, r.employee_id, r.relative_name, r.relative_id_card, r.relative_phone, r.relationship
        FROM ccdi_employee e
        LEFT JOIN sys_dept d ON e.dept_id = d.dept_id
        LEFT JOIN ccdi_employee_relative r ON e.employee_id = r.employee_id
        WHERE e.employee_id = #{employeeId}
    </select>

</mapper>
