<?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.cool.store.mapper.EnterpriseUserMapper">
    <resultMap id="BaseResultMap" type="com.cool.store.entity.EnterpriseUserDO">
        <id column="id" jdbcType="VARCHAR" property="id"/>
        <result column="user_id" jdbcType="VARCHAR" property="userId"/>
        <result column="name" jdbcType="VARCHAR" property="name"/>
        <result column="remark" jdbcType="VARCHAR" property="remark"/>
        <result column="mobile" jdbcType="VARCHAR" property="mobile"/>
        <result column="email" jdbcType="VARCHAR" property="email"/>
        <result column="main_admin" jdbcType="BIT" property="mainAdmin"/>
        <result column="is_admin" jdbcType="BIT" property="isAdmin"/>
        <result column="unionid" jdbcType="VARCHAR" property="unionid"/>
        <result column="avatar" jdbcType="VARCHAR" property="avatar"/>
        <result column="jobnumber" jdbcType="VARCHAR" property="jobnumber"/>
        <result column="is_leader" jdbcType="BIT" property="isLeader"/>
        <result column="is_leader_in_depts" jdbcType="VARCHAR" property="isLeaderInDepts"/>
        <result column="face_url" jdbcType="VARCHAR" property="faceUrl"/>
        <result column="user_status" jdbcType="TINYINT" property="userStatus"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="third_oa_unique_flag" jdbcType="VARCHAR" property="thirdOaUniqueFlag"/>
        <result column="departments" jdbcType="VARCHAR" property="departments"/>
        <result column="roles" jdbcType="VARCHAR" property="roles"/>
        <result column="language" jdbcType="VARCHAR" property="language"/>
        <result column="position" jdbcType="VARCHAR" property="position"/>
        <result column="active" jdbcType="BIT" property="active"/>
        <result column="userType" jdbcType="BIT" property="userType"/>
    </resultMap>
    <resultMap extends="BaseResultMap" id="ResultMapWithBLOBs" type="com.cool.store.entity.EnterpriseUserDO">
        <result column="user_region_ids" jdbcType="LONGVARCHAR" property="userRegionIds"/>
    </resultMap>
    <sql id="Base_Column_List">
        id, user_id, name, remark, mobile, email, main_admin, is_admin, unionid,departments,
        avatar, jobnumber, is_leader, is_leader_in_depts, face_url, user_status, create_time,
        third_oa_unique_flag
    </sql>
    <sql id="Blob_Column_List">
        user_region_ids
    </sql>

    <select id="getUserInfoById" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>,
        <include refid="Blob_Column_List"/>
        from
        enterprise_user_${enterpriseId}
        where
        user_id = #{userId}
    </select>

    <select id="getUserInfoByJobnumber" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>,
        <include refid="Blob_Column_List"/>
        from
        enterprise_user_${enterpriseId}
        where
        jobnumber = #{jobnumber}
    </select>

    <select id="getUserInfoByThirdOaUniqueFlag" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>,
        <include refid="Blob_Column_List"/>
        from
        enterprise_user_${enterpriseId}
        where
        third_oa_unique_flag = #{thirdOaUniqueFlag}
    </select>

    <select id="getUserInfoByUserIds" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>,
        <include refid="Blob_Column_List"/>
        from
        enterprise_user_${enterpriseId}
        <where>
            <if test="userIdList !=null and userIdList.size>0">
                <foreach collection="userIdList" item="userId" open="and user_id in (" close=")" separator=",">
                    #{userId}
                </foreach>
            </if>
        </where>
    </select>

    <select id="getUserInfoByUserMobileList" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>,
        <include refid="Blob_Column_List"/>
        from
        enterprise_user_${enterpriseId}
        <where>
            <if test="mobileList !=null and mobileList.size>0">
                <foreach collection="mobileList" item="mobile" open="and mobile in (" close=")" separator=",">
                    #{mobile}
                </foreach>
            </if>
        </where>
    </select>
    
    <select id="searchUserByRegionIdsAndKeyword" resultMap="BaseResultMap">
        select
            user_id, name, mobile
        from
            enterprise_user_${enterpriseId}
        <where>
            active = true
            and <foreach collection="regionIds" item="regionId" separator=" or " open="(" close=")">user_region_ids like concat('%', #{regionId}, '%')</foreach>
            <if test="keyword != null">
                and (name like concat("%", #{keyword}, "%") or mobile like concat("%", #{keyword}, "%"))
            </if>
            <if test="leaderRegionIds != null and leaderRegionIds.size()>0">
                and <foreach collection="leaderRegionIds" item="regionId" separator=" or " open="(" close=")">user_region_ids like concat('%', #{regionId}, '%')</foreach>
            </if>
        </where>
    </select>

    <select id="getUserCountByRegionId" resultType="integer">
        select count(1) from enterprise_user_${enterpriseId} where active = true and user_region_ids like concat("%", #{regionId}, "%")
    </select>

    <select id="getUserListByDeptLeader" resultMap="BaseResultMap">
        select
            <include refid="Base_Column_List"/>,
            <include refid="Blob_Column_List"/>
        from
            enterprise_user_${enterpriseId}
        where
            is_leader_in_depts like concat("%", #{regionId}, "%") and active = true
    </select>

    <select id="getUserListByDeptLeaders" resultMap="BaseResultMap">
        select
            user_id, name, mobile
        from
            enterprise_user_${enterpriseId}
        where
        active = true and <foreach collection="regionIds" separator="or" open="(" close=")" item="regionId"> is_leader_in_depts like concat("%", #{regionId}, "%") </foreach>
    </select>

    <select id="getUserListByRegionIds" resultMap="BaseResultMap">
        select
            user_id, name, mobile
        from
            enterprise_user_${enterpriseId}
        where
            active = true and is_leader = 0 and
            <foreach collection="regionIds" separator="or" open="(" close=")" item="regionId">
            user_region_ids like concat("%", #{regionId}, "%")
            </foreach>
        ORDER BY
        CASE WHEN jobnumber='' OR jobnumber IS NULL THEN 1 ELSE 0 END,
        SUBSTR(jobnumber,1,1),
        CAST(SUBSTR(jobnumber,2) AS UNSIGNED) ASC
    </select>

    <select id="selectByMobile" resultMap="BaseResultMap">
        SELECT   *  FROM  enterprise_user_${enterpriseId} WHERE  mobile =#{mobile}  and  active = true LIMIT 1
    </select>

    <select id="selectByInvestmentManager"  resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM  enterprise_user_${enterpriseId} WHERE (  mobile  =  #{investmentManager}  or  `name` = #{investmentManager} )  and  active = true  LIMIT 1
    </select>
    <select id="selectNameByUserId" resultType="com.cool.store.dto.openPreparation.UserNameDTO">
        select user_id as userId ,name
        from enterprise_user_${enterpriseId}
        where user_id in
        <foreach item="userId" index="index" collection="userIdList" open="(" separator="," close=")">
            #{userId}
        </foreach>

    </select>
    <select id="getUserIdListByRegionId" resultType="java.lang.String">
        select user_id as userId
        from enterprise_user_${enterpriseId}
        where user_id in
        <foreach item="userId" index="index" collection="userIdList" open="(" separator="," close=")">
            #{userId}
        </foreach>
        and user_region_ids like concat('%', #{regionId}, '%')
    </select>

    <select id="getUserListByRegionId" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>,
        <include refid="Blob_Column_List"/>
        from enterprise_user_${enterpriseId}
        where user_region_ids like concat('%', #{regionId}, '%') and active = true
    </select>
    <select id="getAllUser" resultType="com.cool.store.dto.UserDTO">
        select
        DISTINCT a.user_id as userId,
        a.name,
        a.mobile
        from enterprise_user_${eid}  a left join enterprise_user_role_${eid} b  on a.`user_id` = b.`user_id`
        where active = true
        <if test="keyword!=null and keyword!=''">
            and( a.name like concat('%',#{keyword},'%') or a.mobile like concat('%',#{keyword},'%'))
        </if>
        <if test="roleIdList !=null and roleIdList.size>0">
            and b.role_id in
            <foreach item="roleId" index="index" collection="roleIdList" open="(" separator="," close=")">
                #{roleId}
            </foreach>
        </if>
    </select>

    <select id="searchUserByUserIdsAndKeyword" resultMap="BaseResultMap">
        select
        user_id, name, mobile
        from
        enterprise_user_${enterpriseId}
        <where>
            active = true
            <if test="userIdList !=null and userIdList.size>0">
                <foreach collection="userIdList" item="userId" open="and user_id in (" close=")" separator=",">
                    #{userId}
                </foreach>
            </if>
            <if test="keyword!=null and keyword!=''">
                and( name like concat('%',#{keyword},'%'))
            </if>
        </where>
    </select>

    <select id="getUserIdsByRegionIdList" resultType="string">
        select
        user_id
        from enterprise_user_${enterpriseId} where user_id != 'a100000001' and active=1
        <if test="regionIdList != null and regionIdList.size >0 ">
            <foreach collection="regionIdList" item="regionId" separator=" or " open="and (" close=" )">
                user_region_ids like concat('%/', #{regionId}, '/%')
            </foreach>
        </if>
    </select>

    <select id="getUserLoginByUnionid" resultType="com.cool.store.entity.login.UserLoginDO">
        SELECT user_id, mobile, password
        FROM enterprise_user
        WHERE unionid = #{unionid} AND active = true
    </select>
</mapper>