<?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.storlead.sales.mapper.customer.UserInfoMapper">

        <!-- 通用查询映射结果 -->
        <resultMap id="BaseResultMap" type="com.storlead.sales.pojo.entity.customer.UserInfoEntity">
                    <id column="user_id" property="userId" />
                    <result column="invested_id" property="investedId" />
                    <result column="company_id" property="companyId" />
                    <result column="company_type" property="companyType" />
                    <result column="cooperation_business_type" property="cooperationBusinessType" />
                    <result column="focus_module" property="focusModule" />
                    <result column="mobile" property="mobile" />
                    <result column="wechat_nick_name" property="wechatNickName" />
                    <result column="wechat_profile_photo" property="wechatProfilePhoto" />
                    <result column="open_id" property="openId" />
                    <result column="password" property="password" />
                    <result column="user_num" property="userNum" />
                    <result column="status" property="status" />
                    <result column="company_auth_status" property="companyAuthStatus" />
                    <result column="created_source" property="createdSource" />
                    <result column="created_by" property="createdBy" />
                    <result column="created_time" property="createdTime" />
                    <result column="updated_by" property="updatedBy" />
                    <result column="updated_time" property="updatedTime" />
                    <result column="user_name" property="userName" />
                    <result column="follow_up_status" property="followUpStatus" />
                    <result column="register_time" property="registerTime" />
                    <result column="register_ip" property="registerIp" />
                    <result column="register_platform" property="registerPlatform" />
                    <result column="register_country_code" property="registerCountryCode" />
                    <result column="register_country" property="registerCountry" />
                    <result column="register_region_code" property="registerRegionCode" />
                    <result column="register_region" property="registerRegion" />
                    <result column="register_city" property="registerCity" />
                    <result column="last_login_time" property="lastLoginTime" />
                    <result column="last_login_ip" property="lastLoginIp" />
                    <result column="last_login_platform" property="lastLoginPlatform" />
                    <result column="last_login_country_code" property="lastLoginCountryCode" />
                    <result column="last_login_country" property="lastLoginCountry" />
                    <result column="last_login_region_code" property="lastLoginRegionCode" />
                    <result column="last_login_region" property="lastLoginRegion" />
                    <result column="last_login_city" property="lastLoginCity" />
                    <result column="top_follow_sys_user_id" property="topFollowSysUserId" />
                    <result column="follow_sys_user_id" property="followSysUserId" />
        </resultMap>

        <!-- 通用查询结果列 -->
        <sql id="Base_Column_List">
            user_id, invested_id, company_id, company_type, cooperation_business_type, focus_module, mobile, wechat_nick_name, wechat_profile_photo, open_id, password, user_num, status, company_auth_status, created_source, created_by, created_time, updated_by, updated_time, user_name, follow_up_status, register_time, register_ip, register_platform, register_country_code, register_country, register_region_code, register_region, register_city, last_login_time, last_login_ip, last_login_platform, last_login_country_code, last_login_country, last_login_region_code, last_login_region, last_login_city, top_follow_sys_user_id, follow_sys_user_id
        </sql>

        <select id="queryUserInfoListByType" resultType="com.storlead.sales.pojo.entity.customer.UserInfoEntity">
            SELECT
                temp.*,
                (SELECT created_time FROM `user_info_log` WHERE user_id = temp.user_id ORDER BY user_log_id DESC LIMIT 1) AS last_time
            FROM (
                SELECT
                ui.user_id,
                ui.mobile,
                ui.user_name,
                ui.created_source,
                ui.created_time,
                ui.invested_id,
                CASE
                WHEN ui.invested_id IS NULL THEN 1
                WHEN ui.invested_id IS NOT NULL AND ur.user_type = 10 THEN 2
                WHEN ui.invested_id IS NOT NULL AND ur.user_type = 9 THEN 3
                WHEN ui.invested_id IS NOT NULL AND i.industry_type = 3 THEN 4
                ELSE 1
                END AS user_status,
                CASE
                WHEN ui.invested_id IS NULL THEN '普通用户'
                WHEN ui.invested_id IS NOT NULL AND ur.user_type = 10 THEN '实际投资人'
                WHEN ui.invested_id IS NOT NULL AND ur.user_type = 9 THEN '意向投资人'
                WHEN ui.invested_id IS NOT NULL AND i.industry_type = 3 THEN '寻求投资人'
                ELSE '普通用户'
                END AS user_status_name,
                ui.focus_module,
                ui.cooperation_business_type,
                ui.follow_sys_user_id,
                ui.top_follow_sys_user_id,
                su.name AS sys_user_name,
                follow_up_status
                FROM `user_info` AS ui
                LEFT JOIN invested AS i ON ui.invested_id = i.invested_id
                LEFT JOIN user_role_relationship AS urr
                ON urr.user_id = ui.user_id AND urr.`status` = 1
                LEFT JOIN user_role AS ur
                ON ur.user_role_id = urr.user_role_id AND ur.`status` = 1
                LEFT JOIN sys_user AS su
                ON su.sys_user_id = ui.follow_sys_user_id
                WHERE
                ui.company_id IS NULL
                AND ui.`status` = 1
                AND ui.user_id > #{maxUserId}
                AND (ui.focus_module != "采购公告" OR ui.focus_module IS NULL)
                ) AS temp
            WHERE
                user_status = 1

            UNION ALL

            SELECT
                user_id,
                mobile,
                user_name,
                created_source,
                created_time,
                invested_id,
                1 AS user_status,
                '普通用户' AS user_status_name,
                focus_module,
                cooperation_business_type,
                follow_sys_user_id,
                top_follow_sys_user_id,
                NULL AS sys_user_name,
                NULL AS follow_up_status,
                (SELECT created_time FROM `user_info_log` WHERE user_id = user_info.user_id ORDER BY user_log_id DESC LIMIT 1) AS last_time
            FROM
                `user_info`
            WHERE
                `status` = 1
              AND company_auth_status = 3
              AND invested_id IS NULL
              AND (focus_module != "采购公告" OR focus_module IS NULL)
              AND user_id > #{maxUserId}
            ORDER BY user_id
        </select>

</mapper>
