<?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.project.business.mapper.TcAccountContactHistoryMapper">
    
    <resultMap type="TcAccountContactHistory" id="TcAccountContactHistoryResult">
        <result property="contactId"    column="contact_id"    />
        <result property="accountId"    column="account_id"    />
        <result property="contactName"    column="contact_name"    />
        <result property="phone"    column="phone"    />
        <result property="email"    column="email"    />
        <result property="address"    column="address"    />
        <result property="jobTitle"    column="job_title"    />
        <result property="birthday"    column="birthday"    />
        <result property="horoscope"    column="horoscope"    />
        <result property="maritalStatus"    column="marital_status"    />
        <result property="hobbies"    column="hobbies"    />
        <result property="version"    column="version"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <sql id="selectTcAccountContactHistoryVo">
        select contact_id, account_id, contact_name, phone, email, address, job_title, birthday, horoscope, marital_status, hobbies, version, del_flag, create_time, create_by, update_time, update_by, remark from tc_account_contact_history
    </sql>

    <select id="selectTcAccountContactHistoryList" parameterType="TcAccountContactHistory" resultMap="TcAccountContactHistoryResult">
        <include refid="selectTcAccountContactHistoryVo"/>
        <where>  
            <if test="accountId != null "> and account_id = #{accountId}</if>
            <if test="contactName != null  and contactName != ''"> and contact_name like concat('%', #{contactName}, '%')</if>
            <if test="phone != null  and phone != ''"> and phone = #{phone}</if>
            <if test="email != null  and email != ''"> and email = #{email}</if>
            <if test="address != null  and address != ''"> and address = #{address}</if>
            <if test="jobTitle != null  and jobTitle != ''"> and job_title = #{jobTitle}</if>
            <if test="birthday != null "> and birthday = #{birthday}</if>
            <if test="horoscope != null  and horoscope != ''"> and horoscope = #{horoscope}</if>
            <if test="maritalStatus != null "> and marital_status = #{maritalStatus}</if>
            <if test="hobbies != null  and hobbies != ''"> and hobbies = #{hobbies}</if>
            <if test="version != null "> and version = #{version}</if>
        </where>
    </select>
    
    <select id="selectTcAccountContactHistoryByContactId" parameterType="Long" resultMap="TcAccountContactHistoryResult">
        <include refid="selectTcAccountContactHistoryVo"/>
        where contact_id = #{contactId}
    </select>

    <select id="selectMaxVersionByAccountId" resultType="java.lang.Long">
        select max(version) from tc_account_contact_history where account_id = #{accountId}
    </select>

    <insert id="insertTcAccountContactHistory" parameterType="TcAccountContactHistory" useGeneratedKeys="true" keyProperty="contactId">
        insert into tc_account_contact_history
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="accountId != null">account_id,</if>
            <if test="contactName != null">contact_name,</if>
            <if test="phone != null">phone,</if>
            <if test="email != null">email,</if>
            <if test="address != null">address,</if>
            <if test="jobTitle != null">job_title,</if>
            <if test="birthday != null">birthday,</if>
            <if test="horoscope != null">horoscope,</if>
            <if test="maritalStatus != null">marital_status,</if>
            <if test="hobbies != null">hobbies,</if>
            <if test="version != null">version,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="accountId != null">#{accountId},</if>
            <if test="contactName != null">#{contactName},</if>
            <if test="phone != null">#{phone},</if>
            <if test="email != null">#{email},</if>
            <if test="address != null">#{address},</if>
            <if test="jobTitle != null">#{jobTitle},</if>
            <if test="birthday != null">#{birthday},</if>
            <if test="horoscope != null">#{horoscope},</if>
            <if test="maritalStatus != null">#{maritalStatus},</if>
            <if test="hobbies != null">#{hobbies},</if>
            <if test="version != null">#{version},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateTcAccountContactHistory" parameterType="TcAccountContactHistory">
        update tc_account_contact_history
        <trim prefix="SET" suffixOverrides=",">
            <if test="accountId != null">account_id = #{accountId},</if>
            <if test="contactName != null">contact_name = #{contactName},</if>
            <if test="phone != null">phone = #{phone},</if>
            <if test="email != null">email = #{email},</if>
            <if test="address != null">address = #{address},</if>
            <if test="jobTitle != null">job_title = #{jobTitle},</if>
            <if test="birthday != null">birthday = #{birthday},</if>
            <if test="horoscope != null">horoscope = #{horoscope},</if>
            <if test="maritalStatus != null">marital_status = #{maritalStatus},</if>
            <if test="hobbies != null">hobbies = #{hobbies},</if>
            <if test="version != null">version = #{version},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where contact_id = #{contactId}
    </update>

    <delete id="deleteTcAccountContactHistoryByContactId" parameterType="Long">
        delete from tc_account_contact_history where contact_id = #{contactId}
    </delete>

    <delete id="deleteTcAccountContactHistoryByContactIds" parameterType="String">
        delete from tc_account_contact_history where contact_id in 
        <foreach item="contactId" collection="array" open="(" separator="," close=")">
            #{contactId}
        </foreach>
    </delete>
</mapper>