<?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.TcLeadHistoryMapper">

    <resultMap type="TcLeadHistory" id="TcLeadHistoryResult">
        <result property="id"    column="id"    />
        <result property="leadId"    column="lead_id"    />
        <result property="oriUser"    column="ori_user"    />
        <result property="actionType"    column="action_type"    />
        <result property="recoveryType"    column="recovery_type"    />
        <result property="recoveryReason"    column="recovery_reason"    />
        <result property="getUser"    column="get_user"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="remark"    column="remark"    />
        <result property="oriUserName" column="nick_name" />
    </resultMap>

    <sql id="selectTcLeadHistoryVo">
        select id, lead_id, ori_user, action_type, recovery_type, recovery_reason, get_user, del_flag, create_by, create_time, remark from tc_lead_history
    </sql>
    

    <select id="selectTcLeadHistoryList" parameterType="TcLeadHistory" resultMap="TcLeadHistoryResult">
        select T.*, u.nick_name
            from (
        <include refid="selectTcLeadHistoryVo"/>
        <where>
            and del_flag = '0'
            <if test="leadId != null"> and lead_id = #{leadId}</if>
            <if test="oriUser != null "> and ori_user = #{oriUser}</if>
            <if test="actionType != null "> and action_type = #{actionType}</if>
            <if test="getUser != null "> and get_user = #{getUser}</if>
        </where>
                        ) T
        left join sys_user U on T.ori_user = U.user_id
    </select>
    
    <select id="selectTcLeadHistoryByLeadId" resultMap="TcLeadHistoryResult">
        <include refid="selectTcLeadHistoryVo"/>
        where lead_id = #{leadId}
        and del_flag = '0'
    </select>
    <select id="selectTcLeadHistoryById" parameterType="Long" resultMap="TcLeadHistoryResult">
        <include refid="selectTcLeadHistoryVo"/>
        where id = #{id}
    </select>

    <insert id="insertTcLeadHistory" parameterType="TcLeadHistory" useGeneratedKeys="true" keyProperty="leadId">
        insert into tc_lead_history
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="leadId != null">lead_id,</if>
            <if test="oriUser != null">ori_user,</if>
            <if test="actionType != null">action_type,</if>
            <if test="recoveryType != null">recovery_type,</if>
            <if test="recoveryReason != null">recovery_reason,</if>
            <if test="getUser != null">get_user,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="leadId != null">#{leadId},</if>
            <if test="oriUser != null">#{oriUser},</if>
            <if test="actionType != null">#{actionType},</if>
            <if test="recoveryType != null">#{recoveryType},</if>
            <if test="recoveryReason != null">#{recoveryReason},</if>
            <if test="getUser != null">#{getUser},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateTcLeadHistory" parameterType="TcLeadHistory">
        update tc_lead_history
        <trim prefix="SET" suffixOverrides=",">
            <if test="oriUser != null">ori_user = #{oriUser},</if>
            <if test="actionType != null">action_type = #{actionType},</if>
            <if test="getUser != null">get_user = #{getUser},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where lead_id = #{leadId}
    </update>

    <delete id="deleteTcLeadHistoryByLeadId" parameterType="Long">
        delete from tc_lead_history where lead_id = #{leadId}
    </delete>

    <delete id="deleteTcLeadHistoryByLeadIds" parameterType="String">
        delete from tc_lead_history where lead_id in 
        <foreach item="leadId" collection="array" open="(" separator="," close=")">
            #{leadId}
        </foreach>
    </delete>
    
    <select id="existsUser" resultType="java.lang.Integer">
        select 1 from tc_lead_history where ori_user = #{userId} and lead_id = #{leadId}
                                        and del_flag = '0' limit 1
    </select>
    
</mapper>