<?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.FamilyMembersMapper">
    
    <resultMap type="FamilyMembers" id="FamilyMembersResult">
        <result property="id"    column="id"    />
        <result property="custIsn"    column="cust_isn"    />
        <result property="custId"    column="cust_id"    />
        <result property="fmyCustId"    column="fmy_cust_id"    />
        <result property="fmyCustIdc"    column="fmy_cust_idc"    />
        <result property="fmyName"    column="fmy_name"    />
        <result property="tel"    column="tel"    />
        <result property="birthday"    column="birthday"    />
        <result property="fmyRln"    column="fmy_rln"    />
        <result property="label"    column="label"    />
        <result property="belongBusi"    column="belong_busi"    />
    </resultMap>

    <sql id="selectFamilyMembersVo">
        select id,cust_isn, cust_id, fmy_cust_id,fmy_cust_idc, fmy_name, tel, birthday, fmy_rln, label, belong_busi from family_members
    </sql>

    <select id="selectFamilyMembersList" parameterType="FamilyMembers" resultMap="FamilyMembersResult">
        <include refid="selectFamilyMembersVo"/>
        <where>
            <if test="id != null "> and id = #{id}</if>
            <if test="custIsn != null  and custIsn != ''"> and cust_isn = #{custIsn}</if>
            <if test="custId != null  and custId != ''"> and cust_id = #{custId}</if>
            <if test="fmyCustId != null  and fmyCustId != ''"> and fmy_cust_id = #{fmyCustId}</if>
            <if test="fmyCustIdc != null  and fmyCustIdc != ''"> and fmy_cust_idc = #{fmyCustIdc}</if>
            <if test="fmyName != null  and fmyName != ''"> and fmy_name like concat('%', #{fmyName}, '%')</if>
            <if test="tel != null  and tel != ''"> and tel = #{tel}</if>
            <if test="birthday != null "> and birthday = #{birthday}</if>
            <if test="fmyRln != null  and fmyRln != ''"> and fmy_rln = #{fmyRln}</if>
            <if test="label != null  and label != ''"> and label = #{label}</if>
            <if test="belongBusi != null  and belongBusi != ''"> and belong_busi = #{belongBusi}</if>
        </where>
    </select>
    
    <select id="selectFamilyMembersByCustIsn" parameterType="String" resultMap="FamilyMembersResult">
        <include refid="selectFamilyMembersVo"/>
        where cust_isn = #{custIsn}
    </select>

    <select id="selectCustEnumByCustId"  resultType="CustTagVo">
        select cstn.id, cstn.cust_tag_name, case when cst.cust_id is null then 'false' else 'true' end as flag,
        cstn.cust_tag_level1,cstn.cust_tag_level2,cstn.tag_type
        from sys_cust_tag_nemu  cstn
        left join
        (select * from sys_cust_tag  where cust_id = #{custId} )cst on cst.tag_id = cstn.id
        where cstn.dept_id = #{deptId3}  and cstn.cust_tag_type = #{custType}
        order by cstn.cust_tag_level1,cstn.cust_tag_level2,flag desc
    </select>

    <select id="selectManualTag" resultType="CustManualTagVO">
        select
            tm.id AS id,
            tm.parent_id AS parentId,
            tm.cust_tag_name AS custTagName,
            case when ctm.cust_id is null then 'false' else 'true' end as flag
        from sys_tag_manual tm
        left join sys_cust_tag_manual ctm
        on ctm.tag_id = tm.id and ctm.cust_id = #{custId}
        where tm.dept_id = #{deptId3} and tm.cust_tag_type = #{custType}
        order by tm.id, flag desc
    </select>

    <select id="selectmanualAll" resultType="CustManualTagVO">
        select id, parent_id, cust_tag_name
        from sys_tag_manual
        where dept_id = #{deptId} and cust_tag_type = #{custType}
    </select>


    <insert id="insertFamilyMembers" parameterType="FamilyMembers">
        insert into family_members
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="custIsn != null">cust_isn,</if>
            <if test="custId != null">cust_id,</if>
            <if test="fmyCustId != null">fmy_cust_id,</if>
            <if test="fmyCustIdc != null">fmy_cust_idc,</if>
            <if test="fmyName != null">fmy_name,</if>
            <if test="tel != null">tel,</if>
            <if test="birthday != null">birthday,</if>
            <if test="fmyRln != null">fmy_rln,</if>
            <if test="label != null">label,</if>
            <if test="belongBusi != null">belong_busi,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="custIsn != null">#{custIsn},</if>
            <if test="custId != null">#{custId},</if>
            <if test="fmyCustId != null">#{fmyCustId},</if>
            <if test="fmyCustIdc != null">#{fmyCustIdc},</if>
            <if test="fmyName != null">#{fmyName},</if>
            <if test="tel != null">#{tel},</if>
            <if test="birthday != null">#{birthday},</if>
            <if test="fmyRln != null">#{fmyRln},</if>
            <if test="label != null">#{label},</if>
            <if test="belongBusi != null">#{belongBusi},</if>
         </trim>
    </insert>

    <update id="updateFamilyMembers" parameterType="FamilyMembers">
        update family_members
        <trim prefix="SET" suffixOverrides=",">
            <if test="custId != null">cust_id = #{custId},</if>
            <if test="fmyCustId != null">fmy_cust_id = #{fmyCustId},</if>
            <if test="fmyCustIdc != null">fmy_cust_idc = #{fmyCustIdc},</if>
            <if test="fmyName != null">fmy_name = #{fmyName},</if>
            <if test="tel != null">tel = #{tel},</if>
            <if test="birthday != null">birthday = #{birthday},</if>
            <if test="fmyRln != null">fmy_rln = #{fmyRln},</if>
            <if test="label != null">label = #{label},</if>
            <if test="belongBusi != null">belong_busi = #{belongBusi},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteFamilyMembersByCustIsn" parameterType="String">
        delete from family_members where cust_isn = #{custIsn}
    </delete>

    <delete id="deleteFamilyMembersByCustIsns" parameterType="String">
        delete from family_members where cust_isn in 
        <foreach item="custIsn" collection="array" open="(" separator="," close=")">
            #{custIsn}
        </foreach>
    </delete>
</mapper>