<?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.dsk.system.mapper.BusinessFollowRecordMapper">

    <resultMap type="com.dsk.common.core.domain.entity.BusinessFollowRecord" id="BusinessFollowRecordResult">
        <result property="id" column="id"/>
        <result property="businessId" column="business_id"/>
        <result property="userId" column="user_id"/>
        <result property="visitPerson" column="visit_person"/>
        <result property="position" column="position"/>
        <result property="visitTime" column="visit_time"/>
        <result property="nextVisitTime" column="next_visit_time"/>
        <result property="recordInfo" column="record_info"/>
        <result property="visitWay" column="visit_way"/>
        <result property="creatTime" column="creat_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectBusinessFollowRecordVo">
        select id,
               business_id,
               user_id,
               visit_person,
               position,
               visit_time,
               next_visit_time,
               record_info,
               visit_way,
               creat_time,
               update_time
        from business_follow_record
    </sql>

    <select id="selectBusinessFollowRecordList" resultType="com.dsk.common.core.domain.entity.BusinessFollowRecord">
        select f.*,u.nick_name as nickName
        from business_follow_record f
        left join sys_user u on f.user_id = u.user_id
        where f.business_id = #{businessId}
        ORDER BY f.creat_time DESC
    </select>

    <select id="selectBusinessFollowRecordById" parameterType="Long" resultMap="BusinessFollowRecordResult">
        <include refid="selectBusinessFollowRecordVo"/>
        where id = #{id}
    </select>
    <select id="businessFollowRecordPaging" resultType="com.dsk.common.core.domain.entity.BusinessFollowRecord">
        <include refid="selectBusinessFollowRecordVo"></include>
        <where>
            <if test="businessId != null ">and f.business_id = #{businessId}</if>
            <if test="userId != null ">and f.user_id = #{userId}</if>
            <if test="visitPerson != null and visitPerson != ''">and f.visit_person = #{visitPerson}</if>
            <if test="position != null and position != ''">and f.position = #{position}</if>
            <if test="visitTime != null ">and f.visit_time = #{visitTime}</if>
            <if test="nextVisitTime != null ">and f.next_visit_time = #{nextVisitTime}</if>
            <if test="recordInfo != null  and recordInfo != ''">and f.record_info = #{recordInfo}</if>
            <if test="visitWay != null  and visitWay != ''">and f.visit_way = #{visitWay}</if>
            <if test="creatTime != null ">and f.creat_time = #{creatTime}</if>
        </where>
        ORDER BY creat_time DESC
    </select>
    <select id="allFollow" resultType="com.dsk.common.core.domain.entity.BusinessFollowRecord">
        select f.*,
               i.project_name as projectName,
               i.construction_unit as ownerCompany,
               u.nick_name as nickName
        from business_follow_record f
        left join business_info i on i.id = f.business_id
        left join sys_user u on f.user_id = u.user_id
        left join sys_dept d on u.dept_id = d.dept_id
        <where>
            <if test="userId != null"> and (f.user_id = #{userId} or i.is_private = 1)</if>
            <if test="startTime != null and startTime != '' "> and f.visit_time &gt;= #{startTime} </if>
            <if test="endTime != null and endTime != '' "> and f.visit_time &lt;= #{endTime} </if>
            ${params.dataScope}
        </where>
        ORDER BY f.visit_time DESC
    </select>
    <select id="selectRelateProject" resultType="com.dsk.system.domain.business.vo.BusinessListVo">
        select i.id,i.project_name as projectName
        from business_info i
        left join business_user u on u.business_id = i.id
        where u.user_id = #{userId}
    </select>
    <select id="selectRelateCompany" resultType="java.lang.String">
        select i.construction_unit
        from business_info i
        left join business_user u on u.business_id = i.id
        where u.user_id = #{userId}
    </select>

    <insert id="insertBusinessFollowRecord" parameterType="com.dsk.common.core.domain.entity.BusinessFollowRecord" useGeneratedKeys="true"
            keyProperty="id">
        insert into business_follow_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="businessId != null">business_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="visitPerson != null and visitPerson != ''">visit_person,</if>
            <if test="position != null and position != ''">position,</if>
            <if test="visitTime != null">visit_time,</if>
            <if test="nextVisitTime != null">next_visit_time,</if>
            <if test="recordInfo != null">record_info,</if>
            <if test="visitWay != null">visit_way,</if>
            <if test="creatTime != null">creat_time,</if>
            <if test="updateTime != null">update_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="businessId != null">#{businessId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="visitPerson != null and visitPerson != ''">#{visitPerson},</if>
            <if test="position != null and position != ''">#{position},</if>
            <if test="visitTime != null">#{visitTime},</if>
            <if test="nextVisitTime != null">#{nextVisitTime},</if>
            <if test="recordInfo != null">#{recordInfo},</if>
            <if test="visitWay != null">#{visitWay},</if>
            <if test="creatTime != null">#{creatTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
        </trim>
    </insert>

    <update id="updateBusinessFollowRecord" parameterType="com.dsk.common.core.domain.entity.BusinessFollowRecord">
        update business_follow_record
        <trim prefix="SET" suffixOverrides=",">
            <if test="businessId != null">business_id = #{businessId},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="visitPerson != null and visitPerson != ''">visit_person = #{visitPerson},</if>
            <if test="position != null and position != ''">position = #{position},</if>
            <if test="visitTime != null">visit_time = #{visitTime},</if>
            <if test="nextVisitTime != null">next_visit_time = #{nextVisitTime},</if>
            <if test="recordInfo != null">record_info = #{recordInfo},</if>
            <if test="visitWay != null">visit_way = #{visitWay},</if>
            <if test="creatTime != null">creat_time = #{creatTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteBusinessFollowRecordById" parameterType="Long">
        delete
        from business_follow_record
        where id = #{id}
    </delete>

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