<?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.ibs.list.mapper.FamilyMemberMapper">
    
    <resultMap type="com.ruoyi.ibs.list.domain.FamilyMember" id="FamilyMemberResult">
        <result property="id"    column="id"    />
        <result property="idCard"    column="id_card"    />
        <result property="name"    column="name"    />
        <result property="fatherCard"    column="father_card"    />
        <result property="motherCard"    column="mother_card"    />
        <result property="spouseCard"    column="spouse_card"    />
        <result property="familyId"    column="family_id"    />
        <result property="gender"    column="gender"    />
        <result property="age"    column="age"    />
        <result property="bsId"    column="bs_id"    />
        <result property="spouseFamilyId"    column="spouse_family_id"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="birthDate"    column="birth_date"    />
        <result property="isVirtual"    column="is_virtual"    />
        <result property="depositBalance"    column="deposit_balance"    />
        <result property="loanBalance"    column="loan_balance"    />
        <result property="loanManager"    column="loan_manager"    />
    </resultMap>

    <sql id="selectFamilyMemberVo">
        select id,id_card, name, father_card, mother_card, spouse_card, family_id, gender, age,bs_id,spouse_family_id,create_time,update_time,
               create_by,update_by,birth_date,is_virtual, deposit_balance, loan_balance, loan_manager
        from family_member
    </sql>

    <select id="selectFamilyMemberList" parameterType="FamilyMember" resultMap="FamilyMemberResult">
        <include refid="selectFamilyMemberVo"/>
        <where>  
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="fatherCard != null  and fatherCard != ''"> and father_card = #{fatherCard}</if>
            <if test="motherCard != null  and motherCard != ''"> and mother_card = #{motherCard}</if>
            <if test="spouseCard != null  and spouseCard != ''"> and spouse_card = #{spouseCard}</if>
            <if test="familyId != null  and familyId != ''"> and family_id = #{familyId}</if>
            <if test="gender != null  and gender != ''"> and gender = #{gender}</if>
            <if test="age != null "> and age = #{age}</if>
            <if test="bsId != null "> and bs_id = #{bsId}</if>
            <if test="spouseFamilyId != null "> and spouse_family_id = #{spouseFamilyId}</if>
        </where>
    </select>
    
    <select id="selectFamilyMemberByIdCard" parameterType="String" resultMap="FamilyMemberResult">
        <include refid="selectFamilyMemberVo"/>
        where id_card = #{idCard}
    </select>

    <select id="selectFamilyMemberByBsId" parameterType="String" resultMap="FamilyMemberResult">
        <include refid="selectFamilyMemberVo"/>
        where bs_id = #{bsId}
    </select>



    <insert id="insertFamilyMember" parameterType="FamilyMember">
        insert into family_member
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="idCard != null">id_card,</if>
            <if test="name != null and name != ''">name,</if>
            <if test="fatherCard != null">father_card,</if>
            <if test="motherCard != null">mother_card,</if>
            <if test="spouseCard != null">spouse_card,</if>
            <if test="familyId != null">family_id,</if>
            <if test="gender != null">gender,</if>
            <if test="age != null">age,</if>
            <if test="bsId != null">bs_id,</if>
            <if test="spouseFamilyId != null "> spouse_family_id ,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="birthDate != null">birth_date,</if>
            <if test="isVirtual != null">is_virtual,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="idCard != null">#{idCard},</if>
            <if test="name != null and name != ''">#{name},</if>
            <if test="fatherCard != null">#{fatherCard},</if>
            <if test="motherCard != null">#{motherCard},</if>
            <if test="spouseCard != null">#{spouseCard},</if>
            <if test="familyId != null">#{familyId},</if>
            <if test="gender != null">#{gender},</if>
            <if test="age != null">#{age},</if>
            <if test="bsId != null">#{bsId},</if>
            <if test="spouseFamilyId != null "> #{spouseFamilyId},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="birthDate != null">#{birthDate},</if>
            <if test="isVirtual != null">#{isVirtual},</if>
        </trim>
    </insert>

    <update id="updateFamilyMember" parameterType="FamilyMember">
        update family_member
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="fatherCard != null">father_card = #{fatherCard},</if>
            <if test="motherCard != null">mother_card = #{motherCard},</if>
            <if test="spouseCard != null">spouse_card = #{spouseCard},</if>
            <if test="familyId != null">family_id = #{familyId},</if>
            <if test="gender != null">gender = #{gender},</if>
            <if test="age != null">age = #{age},</if>
            <if test="bsId != null">bs_id = #{bsId},</if>
            <if test="spouseFamilyId != null "> spouse_family_id = #{spouseFamilyId},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="birthDate != null">birth_date = #{birthDate},</if>
            <if test="isVirtual != null">is_virtual= #{isVirtual},</if>
        </trim>
        where id_card = #{idCard}
    </update>

    <delete id="deleteFamilyMemberByIdCard" parameterType="String">
        delete from family_member where id_card = #{idCard}
    </delete>

    <delete id="deleteFamilyMemberByIdCards" parameterType="String">
        delete from family_member where id_card in 
        <foreach item="idCard" collection="array" open="(" separator="," close=")">
            #{idCard}
        </foreach>
    </delete>

    <select id="getMemberByIdCard" resultMap="FamilyMemberResult">
        SELECT * FROM family_member
        WHERE id_card = #{idCard}
    </select>

    <select id="getMembersByFamilyId" resultMap="FamilyMemberResult">
        <include refid="selectFamilyMemberVo"/>
        WHERE family_id = #{familyId} or spouse_family_id = #{familyId}
        union
        select f1.id,f1.id_card, f1.name, f1.father_card, f1.mother_card, f1.spouse_card, f1.family_id, f1.gender, f1.age,f1.bs_id,f1.spouse_family_id,
               f1.create_time,f1.update_time,f1.create_by,f1.update_by,f1.birth_date, f1.is_virtual, f1.deposit_balance, f1.loan_balance, f1.loan_manager
        from family_member f1
        inner join family_member f2 on f1.mother_card = f2.id_card
        where f2.family_id = #{familyId} and f2.gender = 'F'
    </select>


    <select id="getMembersByFamilyId2" resultMap="FamilyMemberResult">
        <include refid="selectFamilyMemberVo"/>
        WHERE family_id = #{familyId}
        union all
        select distinct f1.id,f1.id_card, f1.name, f1.father_card, f1.mother_card, f1.spouse_card, f1.family_id, f1.gender, f1.age,f1.bs_id,f1.spouse_family_id,
                        f1.create_time,f1.update_time,f1.create_by,f1.update_by,f1.birth_date, f1.is_virtual , f1.deposit_balance, f1.loan_balance, f1.loan_manager
        from family_member f1
        inner join family_member f2 on (f1.id_card = f2.mother_card or f1.id_card = f2.spouse_card)
        where f2.family_id = #{familyId}
        union all
        select f1.id,f1.id_card, f1.name, f1.father_card, f1.mother_card, f1.spouse_card, f1.family_id, f1.gender, f1.age,f1.bs_id,
               f1.spouse_family_id,f1.create_time,f1.update_time,f1.create_by,f1.update_by,f1.birth_date, f1.is_virtual, f1.deposit_balance, f1.loan_balance, f1.loan_manager
        from family_member f1
        inner join family_member f2 on f1.mother_card = f2.id_card where f2.family_id =  #{familyId} and f2.gender = 'F'
    </select>

    <select id="getMembersSpouseByFamilyId" resultMap="FamilyMemberResult">
        <include refid="selectFamilyMemberVo"/>
        WHERE id_card in (SELECT  spouse_card FROM family_member where family_id = #{familyId})
    </select>

    <select id="getSpouseFamilyId" resultType="String">
        SELECT fm2.family_id
        FROM family_member fm1
                 JOIN family_member fm2 ON fm1.spouse_card = fm2.id_card
        WHERE fm1.id_card = #{idCard}
    </select>

    <select id="getChildrenByAnyParent" resultMap="FamilyMemberResult">
        SELECT * FROM family_member  WHERE father_card = #{parentCard} OR mother_card = #{parentCard}
    </select>

    <select id="batchSelect" resultMap="FamilyMemberResult">
        SELECT * FROM family_member WHERE id_card IN
        <foreach item='id' collection='ids' open='(' separator=',' close=')'>
          #{id}
        </foreach>
    </select>



    <select id="selectChildrenIds" resultType="String">
        SELECT id_card FROM family_member WHERE father_card = #{memberId} OR mother_card = #{memberId}
    </select>

    <select id="selectSiblingIds" resultType="String">
        SELECT id_card FROM family_member   WHERE (father_card = #{fatherId} OR mother_card = #{motherId} or bs_id = #{bsId})
          AND id_card != #{memberId}
    </select>
</mapper>