<?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.TcFollowMapper">
    
    <resultMap type="com.ruoyi.project.business.domain.TcFollow" id="TcFollowResult">
        <result property="followId"    column="follow_id"    />
        <result property="followType"    column="follow_type"    />
        <result property="followNo"    column="follow_no"    />
        <result property="businessId"    column="business_id"    />
        <result property="followPerson"    column="follow_person"    />
        <result property="followWay"    column="follow_way"    />
        <result property="contactPerson"    column="contact_person"    />
        <result property="contact"    column="contact"    />
        <result property="followTime"    column="follow_time"    />
        <result property="followDesc"    column="follow_desc"    />
        <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"    />
        <result property="createByName"    column="createByName"    />
        <result property="follower"    column="follower"    />
    </resultMap>

    <resultMap type="com.ruoyi.project.business.domain.TcFollowForList" id="TcFollowResultForList" extends="TcFollowResult">
        <result property="followTypeK"    column="follow_type_K"    />
        <result property="businessNo"    column="business_no"    />
        <result property="followWayK"    column="follow_way_K"    />
        <result property="permissionChar"    column="permission_char"    />
        <result property="followTimeFormat"    column="follow_time_format"    />
        <result property="follower"   column="follower"    />
    </resultMap>

    <resultMap type="com.ruoyi.project.business.domain.vo.reminder.AreaSalesDirectorVO" id="AreaSalesDirectorResultList" >
        <result property="userId"    column="user_id"    />
        <result property="userName"    column="user_name"    />
        <result property="areaId"    column="area_id"    />
        <result property="permissionChar"    column="permission_char"    />
    </resultMap>

    <resultMap type="com.ruoyi.project.system.domain.SysUser" id="SysUserResult">
        <id     property="userId"       column="user_id"      />
        <result property="deptId"       column="dept_id"      />
    </resultMap>

    <sql id="selectTcFollowVo">
        SELECT
            follow_id,
            follow_type,
            follow_no,
            business_id,
            follow_person,
            follow_way,
            contact_person,
            contact,
            follow_time,
            follow_desc,
            del_flag,
            create_time,
            create_by,
            update_time,
            update_by,
            remark
        FROM
            tc_follow
    </sql>


    <sql id="selectTcFollowVo2">
        SELECT
            sub1.follow_id,
            sub1.follow_time,
            sub1.follow_time_format,
            sub1.follow_type,
            sub1.follow_type_k,
            sub1.follow_no,
            sub1.business_id,
            sub1.business_no,
            sub1.follow_person,
            sub1.follow_way,
            sub1.follow_way_k,
            sub1.contact_person,
            sub1.contact,
            sub1.follow_desc,
            sub1.permission_char,
            sub1.del_flag,
            sub1.create_time,
            sub1.create_by,
            sub1.update_time,
            sub1.update_by,
            sub1.remark,
            sub1.follower
        FROM
            (
                SELECT
                    t1.follow_id AS follow_id,
                    t1.follow_time AS follow_time,
                    DATE_FORMAT( t1.follow_time, '%Y-%m-%d' ) AS follow_time_format,
                    t1.follow_type AS follow_type,
                    CASE #{language}
                        WHEN 'english' THEN
                        t2.dict_labelUS
                        WHEN 'russia' THEN
                        t2.dict_labelRU
                        WHEN 'spanish' THEN
                        t2.dict_labelES ELSE t2.dict_label
                    END AS follow_type_k,
                    t1.follow_no AS follow_no,
                    t1.business_id AS business_id,
                    CASE
                        WHEN t1.follow_type = '0' THEN
                        t4.lead_no
                        WHEN t1.follow_type = '1' THEN
                        t5.account_no ELSE t6.project_no
                    END AS business_no,
                    t1.follow_person AS follow_person,
                    t1.follow_way AS follow_way,
                    CASE #{language}
                        WHEN 'english' THEN
                        t3.dict_labelUS
                        WHEN 'russia' THEN
                        t3.dict_labelRU
                        WHEN 'spanish' THEN
                        t3.dict_labelES ELSE t3.dict_label
                        END AS follow_way_k,
                    t1.contact_person AS contact_person,
                    t1.contact AS contact,
                    t1.follow_desc AS follow_desc,
                    t1.permission_char AS permission_char,
                    t1.del_flag AS del_flag,
                    t1.create_time AS create_time,
                    t1.create_by AS create_by,
                    t1.update_time AS update_time,
                    t1.update_by AS update_by,
                    t1.remark AS remark,
                    su.nick_name AS follower
                FROM
                    tc_follow AS t1
                    LEFT JOIN sys_dict_data AS t2 ON t2.dict_type = 'crm_follow_type'
                        AND t1.follow_type = t2.dict_value
                    LEFT JOIN sys_dict_data AS t3 ON t3.dict_type = 'crm_follow_way'
                        AND t1.follow_way = t3.dict_value
                    LEFT JOIN tc_lead AS t4 ON t4.lead_id = t1.business_id
                    LEFT JOIN tc_account AS t5 ON t5.account_id = t1.business_id
                    LEFT JOIN tc_project AS t6 ON t6.project_id = t1.business_id
                    /* 跟进人 */
                    LEFT JOIN sys_user su ON su.user_id = t1.create_by
                WHERE
                    t1.del_flag = 0) sub1
            INNER JOIN ( SELECT p.premession_char  FROM (  SELECT  @premessionStr := #{permissionChar} p) param , getpremessionview p) sub2
            ON sub1.permission_char = sub2.premession_char
    </sql>

    <select id="selectTcFollowList" parameterType="com.ruoyi.project.business.domain.TcFollow" resultMap="TcFollowResultForList">
        <include refid="selectTcFollowVo2"/>
        <where>
            <if test="followIds != null and followIds.size() > 0">
                and sub1.follow_id in
                <foreach item="followId" collection="followIds" open="(" separator="," close=")">
                    #{followId}
                </foreach>
            </if>

            <if test="loginUserId != null  and loginUserId != ''">
                and (sub1.permission_char &lt;&gt; #{permissionChar} or sub1.create_by = #{loginUserId})
            </if>

            <if test="followTypeArray != null  and followTypeArray.length != 0">
                and sub1.follow_type in
                <foreach item="type" collection="followTypeArray" open="(" separator="," close=")">
                    #{type}
                </foreach>
             </if>
            <if test="followNo != null  and followNo != ''"> and sub1.follow_no like CONCAT('%', #{followNo},'%')  escape '/' </if>
            <if test="beginTime != null and beginTime != ''">
                and date_format(sub1.follow_time,'%Y%m%d') &gt;= date_format(#{beginTime},'%Y%m%d')
            </if>
            <if test="endTime != null and endTime != ''">
                and date_format(sub1.follow_time,'%Y%m%d') &lt;= date_format(#{endTime},'%Y%m%d')
            </if>
 <!--           <if test="beginTime != null  and beginTime != ''"> and sub1.follow_time BETWEEN #{beginTime} AND #{endTime}</if>-->
            <if test="businessId != null "> and sub1.business_id = #{businessId}</if>
            <if test="businessNo != null "> and sub1.business_no like CONCAT('%', #{businessNo},'%')  escape '/' </if>
            <if test="followPerson != null  and followPerson != ''"> and sub1.follow_person like CONCAT('%', #{followPerson},'%')  escape '/'</if>
            <if test="followWayArray != null  and followWayArray.length > 0">
                and sub1.follow_way in
                <foreach item="way" collection="followWayArray" open="(" separator="," close=")">
                    #{way}
                </foreach>
             </if>
            <if test="contactPerson != null  and contactPerson != ''"> and sub1.contact_person like CONCAT('%', #{contactPerson},'%')  escape '/'</if>
            <if test="contact != null  and contact != ''"> and sub1.contact = #{contact}</if>
            <if test="followDesc != null  and followDesc != ''"> and sub1.follow_desc like CONCAT('%', #{followDesc},'%')  escape '/' </if>
        </where>
        ORDER BY  sub1.follow_id DESC
    </select>
    
    <select id="selectTcFollowByFollowId" parameterType="java.lang.Long" resultMap="TcFollowResult">
        select t.*, u.nick_name as follower from
        (
        <include refid="selectTcFollowVo"/>
        ) t
        left join sys_user u on u.user_id = t.create_by
        where t.follow_id = #{followId}
        and t.del_flag = '0'
    </select>
    <select id="selectTcFollowListByTypeAndBusinessId" resultMap="TcFollowResult">
        <include refid="selectTcFollowVo"/>
        where del_flag = '0' and follow_type = #{followType} and business_id = #{businessId}
        order by follow_id desc
    </select>


    <select id="selectTcFollowListByAccount" parameterType="TcFollow" resultMap="TcFollowResult">
        select f.*, u.nick_name as createByName from tc_follow f
        left join sys_user u on u.user_id = f.create_by
        where f.business_id = #{businessId} and f.del_flag = '0'
        order by f.follow_id desc
    </select>

    <select id="selectUserInfoByAgentId" resultMap="SysUserResult">
        select user_id, dept_id from sys_user
        where  del_flag = 0 and status = 0
          and dept_id = #{agentId}
    </select>

    <select id="getCountByAgentPermissionChar" parameterType="com.ruoyi.project.business.domain.vo.reminder.CheckAreaSalesDirectorVO" resultType="java.lang.Long">
        SELECT
            count(*)
        FROM
            ( SELECT p.premession_char FROM ( SELECT @premessionStr := #{directorPermissionChar} p ) param, getpremessionview p ) sub
        WHERE
            sub.premession_char = #{agentPermissionChar}
    </select>

    <select id="selectAreaSalesDirector"  resultMap="AreaSalesDirectorResultList">
        SELECT
            T3.user_id,
            T3.user_name,
            T3.area_id,
            T4.permission_char
        FROM
            sys_user_role T1
                INNER JOIN sys_role T2 ON ( T2.role_id = T1.role_id AND T2.del_flag = 0 AND T2.STATUS = 0 AND T2.role_key = 'area_sales_director' )
                INNER JOIN sys_user T3 ON ( T3.user_id = T1.user_id AND T3.del_flag = 0 AND T3.STATUS = 0 )
                INNER JOIN tb_area T4 ON ( T4.area_id = T3.area_id AND T4.del_flag = 0)
    </select>

    <select id="selectCountByAccountId" resultType="java.lang.Integer">
        select count(1) from tc_follow where business_id = #{accountId}
        and follow_type = 1 and del_flag = '0'
    </select>

    <insert id="insertTcFollow" parameterType="com.ruoyi.project.business.domain.TcFollow" useGeneratedKeys="true" keyProperty="followId">
        insert into tc_follow
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="followId != null">follow_id,</if>
            <if test="followType != null and followType != ''">follow_type,</if>
            <if test="followNo != null">follow_no,</if>
            <if test="businessId != null">business_id,</if>
            <if test="followPerson != null">follow_person,</if>
            <if test="followWay != null">follow_way,</if>
            <if test="contactPerson != null">contact_person,</if>
            <if test="contact != null">contact,</if>
            <if test="followTime != null">follow_time,</if>
            <if test="followDesc != null">follow_desc,</if>
            <if test="permissionChar != null">permission_char,</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="followId != null">#{followId},</if>
            <if test="followType != null and followType != ''">#{followType},</if>
            <if test="followNo != null">#{followNo},</if>
            <if test="businessId != null">#{businessId},</if>
            <if test="followPerson != null">#{followPerson},</if>
            <if test="followWay != null">#{followWay},</if>
            <if test="contactPerson != null">#{contactPersonName},</if>
            <if test="contact != null">#{contact},</if>
            <if test="followTime != null">#{followTime},</if>
            <if test="followDesc != null">#{followDesc},</if>
            <if test="permissionChar != null">#{permissionChar},</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="updateTcFollow" parameterType="com.ruoyi.project.business.domain.TcFollow">
        update tc_follow
        <trim prefix="SET" suffixOverrides=",">
            <if test="followType != null and followType != ''">follow_type = #{followType},</if>
            <if test="followNo != null">follow_no = #{followNo},</if>
            <if test="businessId != null">business_id = #{businessId},</if>
            <if test="followPerson != null">follow_person = #{followPerson},</if>
            <if test="followWay != null">follow_way = #{followWay},</if>
            <if test="contactPerson != null">contact_person = #{contactPersonName},</if>
            <if test="contact != null">contact = #{contact},</if>
            <if test="followTime != null">follow_time = #{followTime},</if>
            <if test="followDesc != null">follow_desc = #{followDesc},</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 follow_id = #{followId}
    </update>

    <delete id="deleteTcFollowByFollowId" parameterType="java.lang.Long">
        update tc_follow set del_flag = 1 where follow_id = #{followId}
    </delete>

    <delete id="deleteTcFollowByFollowIds" parameterType="java.lang.String">
        update tc_follow set del_flag = 1 where follow_id in
        <foreach item="followId" collection="array" open="(" separator="," close=")">
            #{followId}
        </foreach>
    </delete>
</mapper>