<?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.info.collection.mapper.CcdiStaffFmyRelationMapper">

    <!-- 员工亲属关系ResultMap -->
    <resultMap type="com.ruoyi.info.collection.domain.vo.CcdiStaffFmyRelationVO" id="CcdiStaffFmyRelationVOResult">
        <id property="id" column="id"/>
        <result property="personId" column="person_id"/>
        <result property="personName" column="person_name"/>
        <result property="relationType" column="relation_type"/>
        <result property="relationName" column="relation_name"/>
        <result property="gender" column="gender"/>
        <result property="birthDate" column="birth_date"/>
        <result property="relationCertType" column="relation_cert_type"/>
        <result property="relationCertNo" column="relation_cert_no"/>
        <result property="mobilePhone1" column="mobile_phone1"/>
        <result property="mobilePhone2" column="mobile_phone2"/>
        <result property="annualIncome" column="annual_income"/>
        <result property="wechatNo1" column="wechat_no1"/>
        <result property="wechatNo2" column="wechat_no2"/>
        <result property="wechatNo3" column="wechat_no3"/>
        <result property="contactAddress" column="contact_address"/>
        <result property="relationDesc" column="relation_desc"/>
        <result property="effectiveDate" column="effective_date"/>
        <result property="invalidDate" column="invalid_date"/>
        <result property="status" column="status"/>
        <result property="remark" column="remark"/>
        <result property="dataSource" column="data_source"/>
        <result property="isEmpFamily" column="is_emp_family"/>
        <result property="isCustFamily" column="is_cust_family"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="createdBy" column="created_by"/>
        <result property="updatedBy" column="updated_by"/>
    </resultMap>

    <!-- 分页查询员工亲属关系列表 -->
    <select id="selectRelationPage" resultMap="CcdiStaffFmyRelationVOResult">
        SELECT
            r.id, r.person_id, s.name as person_name, r.relation_type, r.relation_name,
            r.gender, r.birth_date, r.relation_cert_type, r.relation_cert_no,
            r.mobile_phone1, r.mobile_phone2, r.annual_income, r.wechat_no1, r.wechat_no2, r.wechat_no3,
            r.contact_address, r.relation_desc, r.effective_date, r.invalid_date,
            r.status, r.remark, r.data_source, r.is_emp_family, r.is_cust_family,
            r.created_by, r.create_time, r.updated_by, r.update_time
        FROM ccdi_staff_fmy_relation r
        LEFT JOIN ccdi_base_staff s ON r.person_id = s.id_card
        WHERE 1 = 1
          AND r.is_emp_family = 1
        <if test="query.personId != null and query.personId != ''">
            AND r.person_id = #{query.personId}
        </if>
        <if test="query.personName != null and query.personName != ''">
            AND s.name LIKE CONCAT('%', #{query.personName}, '%')
        </if>
        <if test="query.relationType != null and query.relationType != ''">
            AND r.relation_type = #{query.relationType}
        </if>
        <if test="query.relationName != null and query.relationName != ''">
            AND r.relation_name LIKE CONCAT('%', #{query.relationName}, '%')
        </if>
        <if test="query.relationCertNo != null and query.relationCertNo != ''">
            AND r.relation_cert_no LIKE CONCAT('%', #{query.relationCertNo}, '%')
        </if>
        <if test="query.status != null">
            AND r.status = #{query.status}
        </if>
        <if test="query.dataSource != null and query.dataSource != ''">
            AND r.data_source = #{query.dataSource}
        </if>
        <if test="query.effectiveDateStart != null">
            AND r.effective_date &gt;= #{query.effectiveDateStart}
        </if>
        <if test="query.effectiveDateEnd != null">
            AND r.effective_date &lt;= #{query.effectiveDateEnd}
        </if>
        ORDER BY r.create_time DESC
    </select>

    <!-- 查询员工亲属关系详情 -->
    <select id="selectRelationById" resultMap="CcdiStaffFmyRelationVOResult">
        SELECT
            r.id, r.person_id, s.name as person_name, r.relation_type, r.relation_name,
            r.gender, r.birth_date, r.relation_cert_type, r.relation_cert_no,
            r.mobile_phone1, r.mobile_phone2, r.annual_income, r.wechat_no1, r.wechat_no2, r.wechat_no3,
            r.contact_address, r.relation_desc, r.effective_date, r.invalid_date,
            r.status, r.remark, r.data_source, r.is_emp_family, r.is_cust_family,
            r.created_by, r.create_time, r.updated_by, r.update_time
        FROM ccdi_staff_fmy_relation r
        LEFT JOIN ccdi_base_staff s ON r.person_id = s.id_card
        WHERE r.id = #{id}
    </select>

    <!-- 查询员工亲属关系列表（用于导出） -->
    <select id="selectRelationListForExport" resultMap="CcdiStaffFmyRelationVOResult">
        SELECT
            r.id, r.person_id, s.name as person_name, r.relation_type, r.relation_name,
            r.gender, r.birth_date, r.relation_cert_type, r.relation_cert_no,
            r.mobile_phone1, r.mobile_phone2, r.annual_income, r.wechat_no1, r.wechat_no2, r.wechat_no3,
            r.contact_address, r.relation_desc, r.effective_date, r.invalid_date,
            r.status, r.remark, r.data_source, r.is_emp_family, r.is_cust_family,
            r.created_by, r.create_time, r.updated_by, r.update_time
        FROM ccdi_staff_fmy_relation r
        LEFT JOIN ccdi_base_staff s ON r.person_id = s.id_card
        WHERE 1 = 1
          AND r.is_emp_family = 1
        <if test="query.personId != null and query.personId != ''">
            AND r.person_id = #{query.personId}
        </if>
        <if test="query.personName != null and query.personName != ''">
            AND s.name LIKE CONCAT('%', #{query.personName}, '%')
        </if>
        <if test="query.relationType != null and query.relationType != ''">
            AND r.relation_type = #{query.relationType}
        </if>
        <if test="query.relationName != null and query.relationName != ''">
            AND r.relation_name LIKE CONCAT('%', #{query.relationName}, '%')
        </if>
        <if test="query.relationCertNo != null and query.relationCertNo != ''">
            AND r.relation_cert_no LIKE CONCAT('%', #{query.relationCertNo}, '%')
        </if>
        <if test="query.status != null">
            AND r.status = #{query.status}
        </if>
        <if test="query.dataSource != null and query.dataSource != ''">
            AND r.data_source = #{query.dataSource}
        </if>
        <if test="query.effectiveDateStart != null">
            AND r.effective_date &gt;= #{query.effectiveDateStart}
        </if>
        <if test="query.effectiveDateEnd != null">
            AND r.effective_date &lt;= #{query.effectiveDateEnd}
        </if>
        ORDER BY r.create_time DESC
    </select>

    <!-- 批量插入员工亲属关系数据 -->
    <insert id="insertBatch">
        INSERT INTO ccdi_staff_fmy_relation
        (person_id, relation_type, relation_name, gender, birth_date, relation_cert_type, relation_cert_no,
         mobile_phone1, mobile_phone2, annual_income, wechat_no1, wechat_no2, wechat_no3, contact_address, relation_desc,
         effective_date, invalid_date, status, remark, data_source, is_emp_family, is_cust_family,
         created_by, create_time, updated_by, update_time)
        VALUES
        <foreach collection="list" item="item" separator=",">
            (#{item.personId}, #{item.relationType}, #{item.relationName}, #{item.gender},
            #{item.birthDate}, #{item.relationCertType}, #{item.relationCertNo}, #{item.mobilePhone1},
            #{item.mobilePhone2}, #{item.annualIncome}, #{item.wechatNo1}, #{item.wechatNo2}, #{item.wechatNo3}, #{item.contactAddress},
            #{item.relationDesc}, #{item.effectiveDate}, #{item.invalidDate}, #{item.status}, #{item.remark},
            #{item.dataSource}, #{item.isEmpFamily}, #{item.isCustFamily}, #{item.createdBy}, NOW(), #{item.updatedBy}, NOW())
        </foreach>
    </insert>

    <!-- 批量查询已存在的员工亲属关系（用于导入唯一性校验） -->
    <select id="selectExistingRelations" resultType="com.ruoyi.info.collection.domain.CcdiStaffFmyRelation">
        SELECT
            id, person_id, relation_type, relation_name, gender, birth_date,
            relation_cert_type, relation_cert_no
        FROM ccdi_staff_fmy_relation
        WHERE is_emp_family = 1
          AND person_id IN
        <foreach collection="personIds" item="personId" open="(" separator="," close=")">
            #{personId}
        </foreach>
          AND relation_cert_no IN
        <foreach collection="relationCertNos" item="relationCertNo" open="(" separator="," close=")">
            #{relationCertNo}
        </foreach>
    </select>

</mapper>
