<?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="cn.iocoder.yudao.module.staff.mapper.CompanyStaffMapper">

    <resultMap id="BaseResultMap" type="cn.iocoder.yudao.module.staff.entity.CompanyStaff">
        <id property="id" column="cs_id" jdbcType="VARCHAR"/>
        <result property="corpId" column="cs_corp_id" jdbcType="VARCHAR"/>
        <result property="userId" column="cs_user_id" jdbcType="BIGINT"/>
        <result property="deptId" column="cs_dept_id" jdbcType="BIGINT"/>
        <result property="workNo" column="cs_work_no" jdbcType="VARCHAR"/>
        <result property="name" column="cs_name" jdbcType="VARCHAR"/>
        <result property="tel" column="cs_tel" jdbcType="VARCHAR"/>
        <result property="sex" column="cs_sex" jdbcType="VARCHAR"/>
        <result property="address" column="cs_address" jdbcType="VARCHAR"/>
        <result property="workDate" column="cs_work_date" jdbcType="DATE"/>
        <result property="workYear" column="cs_work_year" jdbcType="DECIMAL"/>
        <result property="joinedDate" column="cs_joined_date" jdbcType="DATE"/>
        <result property="joinedYear" column="cs_joined_year" jdbcType="DECIMAL"/>
        <result property="education" column="cs_education" jdbcType="VARCHAR"/>
        <result property="content" column="cs_content" jdbcType="VARCHAR"/>
        <result property="uniqueCode" column="cs_unique_code" jdbcType="VARCHAR"/>
        <result property="fileUrls" column="cs_file_urls" jdbcType="VARCHAR"/>
        <result property="roleNames" column="role_names"/>
    </resultMap>

    <select id="selectListPage" resultMap="BaseResultMap">
        SELECT cs.id          AS cs_id,
        cs.corp_id     AS cs_corp_id,
        cs.user_id     AS cs_user_id,
        cs.dept_id     AS cs_dept_id,
        cs.work_no     AS cs_work_no,
        cs.name        AS cs_name,
        cs.tel         AS cs_tel,
        cs.sex         AS cs_sex,
        cs.address     AS cs_address,
        cs.work_date   AS cs_work_date,
        cs.work_year   AS cs_work_year,
        cs.joined_date AS cs_joined_date,
        cs.joined_year AS cs_joined_year,
        cs.education   AS cs_education,
        cs.content     AS cs_content,
        cs.unique_code AS cs_unique_code,
        cs.file_urls   AS cs_file_urls,
        rn.role_names  AS role_names
        FROM company_staff cs
        LEFT JOIN
        (
        SELECT cs.user_id,
        sr.id,
        GROUP_CONCAT(DISTINCT sr.name ORDER BY sr.name SEPARATOR ',') AS role_names
        FROM company_staff cs
        LEFT JOIN
        system_user_role sur ON cs.user_id = sur.user_id
        LEFT JOIN
        system_role sr ON sur.role_id = sr.id
        WHERE cs.deleted = '0'
        AND sur.deleted = '0'
        <if test="map.roleIds != null and map.roleIds.size > 0">
            and sr.id in
            <foreach collection="map.roleIds" item="id" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
        GROUP BY cs.user_id
        ) rn ON cs.user_id = rn.user_id
        WHERE cs.deleted = '0'
        <if test="map.name != null and map.name != ''">
            and cs.name like concat('%', #{map.name}, '%')
        </if>
        <if test="map.workNo != null and map.workNo != ''">
            and cs.work_no like concat('%', #{map.workNo}, '%')
        </if>
        <if test="map.tel != null and map.tel != ''">
            and cs.tel like concat('%', #{map.tel}, '%')
        </if>
        <if test="map.sex != null and map.sex != ''">
            and cs.sex = #{map.sex}
        </if>
        <if test="map.address != null and map.address != ''">
            and cs.address like concat('%', #{map.address}, '%')
        </if>
        <if test="map.workYear != null">
            and cs.work_year = #{map.workYear}
        </if>
        <if test="map.joinedYear != null">
            and cs.joined_year = #{map.joinedYear}
        </if>
        <if test="map.education != null">
            and cs.education = #{map.education}
        </if>
        <if test="map.uniqueCode != null and map.uniqueCode != ''">
            and cs.unique_code like concat('%', #{map.uniqueCode}, '%')
        </if>
        <if test="map.workDateArray.length > 0">
            and cs.work_date between #{map.workDateArray[0]} and #{map.workDateArray[1]}
        </if>
        <if test="map.joinedDateArray.length > 0">
            and cs.joined_date between #{map.joinedDateArray[0]} and #{map.joinedDateArray[1]}
        </if>
        <if test="map.roleIds != null and map.roleIds.size > 0">
            and rn.role_names is not null
        </if>
        order by cs.create_time desc
    </select>
    <select id="selectMyAdviser" resultType="cn.iocoder.yudao.module.staff.vo.CompanyStaffRespVO">
        SELECT
            cs.*
        FROM
            base_customer_main bcm
                LEFT JOIN company_staff cs ON bcm.inviter = cs.user_id AND cs.tenant_id = #{tenantId}
        WHERE
            bcm.user_id = #{userId}
          AND bcm.tenant_id = #{tenantId}
            LIMIT 1
    </select>
    <select id="getStaff" resultType="cn.iocoder.yudao.module.system.api.user.dto.UserDTO"
            parameterType="java.lang.Long">
        SELECT
        su.id,
        su.username,
        su.nickname,
        su.user_type,
        su.remark,
        su.dept_id,
        su.mobile,
        su.password,
        su.sex,
        su.open_id,
        su.tenant_id,
        su.status,
        su.avatar AS avatar,
        dsc.folder_id AS folderId,
        dsc.safe_date AS safeDate,
        dsc.formal_date AS formalDate,
        dsc.address AS address,
        dsc.id_number AS idNumber,
        GROUP_CONCAT(DISTINCT sr2.name SEPARATOR ',') AS roleNames
        FROM
        system_users su
        LEFT JOIN
        system_user_role sr ON su.id = sr.user_id AND sr.deleted = 0
        LEFT JOIN
        system_role sr2 ON sr.role_id = sr2.id
        INNER JOIN
        company_staff dsc ON dsc.user_id = su.id
        <where>
            su.deleted = 0
            AND sr2.service_package_id = 'weixiu' AND dsc.deleted = 0
            <if test="userId != null">
                AND su.id = #{userId}
            </if>
        </where>
        GROUP BY
        su.id
        ORDER BY
        su.nickname
    </select>
</mapper>
