package com.fs.qw.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.fs.course.param.FsCourseListBySidebarParam;
import com.fs.qw.domain.QwUser;
import com.fs.qw.domain.QwWorkTask;
import com.fs.qw.dto.QwUserByToolDTO;
import com.fs.qw.dto.QwUserDTO;
import com.fs.qw.dto.QwUserKeyDTO;
import com.fs.qw.param.*;
import com.fs.qw.vo.*;
import com.fs.qw.vo.sidebar.ExternalContactQwUserVO;
import com.fs.sop.domain.QwSop;
import com.fs.sop.dto.SopUserLogsParamDTO;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * 企微用户Mapper接口
 *
 * @author fs
 * @date 2024-06-20
 */
@Repository
public interface QwUserMapper extends BaseMapper<QwUser>
{
    /**
     * 查询企微用户
     *
     * @param id 企微用户主键
     * @return 企微用户
     */
    public QwUser selectQwUserById(Long id);

    @Select("select qw_user_id from qw_user where qw_user_name = #{qwUserName} and corp_id = #{corpId}")
    public List<String> selectQwUserByQwUserName (@Param("qwUserName") String qwUserName,@Param("corpId") String corpId);

    @Select("SELECT qw_user_id,qw_user_name,corp_id,app_key FROM qw_user WHERE company_user_id is NULL and login_code_url is not NULL")
    public List<QwUser> unlockQwUserLoginCodeUrlTask();


    public List<QwUser> batchGetQwUser(@Param("list") List<QwUserKeyDTO> qwUserId);

    @Select("select id,company_user_id,company_id,welcome_text,qw_user_name,send_msg_type from qw_user where id = #{id}")
    public QwUser selectQwUserByIdByWeComeText(@Param("id") Long id);

    @Select("select * from qw_user where qw_user_id = #{qwUserId} and corp_id = #{corpId} ")
    public QwUser selectQwUserByIdByWeComeText2(@Param("qwUserId") String qwUserId, @Param("corpId") String corpId);
    /**
     * 根据companyUserId查询企微用户
     */
    @Select("select * from qw_user where company_user_id = #{companyUserId} and corp_id = #{corpId}")
    public QwUser selectQwUserByCompanyUserId(@Param("companyUserId") Long companyUserId,@Param("corpId") String corpId);

    /**  */
    /**
     * 查询企微用户列表
     *
     * @param qwUser 企微用户
     * @return 企微用户集合
     */
    public List<QwUser> selectQwUserList(QwUser qwUser);

    @Select("select qw_user_id,qw_user_name,login_code_url from qw_user" +
            " where company_id=#{companyId} " +
            "and company_user_id =#{userId} " +
            "and is_del=0 " +
            "and login_code_url is not null ")
    public List<QwUserByToolDTO> selectQwUserListByTools(@Param("companyId") Long companyId , @Param("userId") Long userId);


    /** 查询某个企微用户详情 */
    @Select("select welcome_text,welcome_image,is_send_msg from qw_user " +
            "where qw_user_id=#{userID} " +
            "and company_id=#{companyId} " +
            "and corp_id=#{corpId} and is_send_msg = 1 ")
    public QwUser selectQwUser(@Param("userID") String userID, @Param("companyId") Long companyId,@Param("corpId") String corpId);
    /**
     * 新增企微用户
     *
     * @param qwUser 企微用户
     * @return 结果
     */
    public int insertQwUser(QwUser qwUser);

    /**
     * 修改企微用户
     *
     * @param qwUser 企微用户
     * @return 结果
     */
    public int updateQwUser(QwUser qwUser);
    public int updateQwUserByAppKey(QwUser qwUser);

    @Update(" update qw_user set login_status = 0, remark = '-'  where app_key = #{appKey}")
    int updateQwUserRemarkByAppKey(@Param("appKey") String appKey);

    @Update(" update qw_user set  login_status = 0, remark ='企微崩溃' where app_key = #{appKey}")
    int updateQwUserDelRemarkByAppKey(@Param("appKey") String appKey);

    public int updateQwUserByAppKeyTool(QwUser qwUser);
    public int updateQwUserByAppKeyToUrl(QwUser qwUser);

    @Select("<script>" +
            "select cu.user_name,cu.nick_name,cu.user_id,qu.qw_user_id ,qu.qw_user_name,qu.id from company_user cu " +
            "LEFT JOIN  qw_user qu " +
            "ON qu.company_user_id=cu.user_id  " +
            "where qu.corp_id=#{corpId} " +
            "and cu.del_flag = 0 and  cu.qw_user_id is not null " +
            "AND qu.id IN " +
            "<foreach collection='qwUserIds' item='id' open='(' separator=',' close=')'> " +
            "#{id} " +
            "</foreach> " +
            "</script>")
    public List<QwUserVO> getQwUserByIds(@Param("qwUserIds") List<String> qwUserIds,@Param("corpId") String corpId);

    /**
     *  逻辑删除企微用户
     */
    public int updateQwUserByCompanyUserId(Long[] userIds);


    /**
     * 删除企微用户
     *
     * @param id 企微用户主键
     * @return 结果
     */
    public int deleteQwUserById(Long id);

    /**
     * 批量删除企微用户
     *
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteQwUserByIds(Long[] ids);

    /**
     *  查询企微用户绑定了的列表
     */
    @Select({"<script> " +
            "select qu.*, cu.nick_name, cu.user_name, qd.dept_name as departmentName,fr.role_name AS fastGptRoleName, cu.doctor_id from qw_user qu " +
            "LEFT JOIN company_user cu ON cu.user_id=qu.company_user_id " +
            "RIGHT JOIN qw_dept qd on qu.department=qd.dept_id and qd.corp_id=qu.corp_id " +
            "left join fastgpt_role fr on fr.role_id=qu.fastGpt_role_id " +
            "where qu.company_user_id is not null "+
            "            <if test=\"id != null  and id != ''\"> and qu.id = #{id}</if>\n" +
            "            <if test=\"qwUserId != null  and qwUserId != ''\"> and qu.qw_user_id = #{qwUserId}</if>\n" +
            "            <if test=\"loginStatus != null \"> and qu.ipad_status = #{loginStatus}</if>\n" +
            "            <if test=\"appKey != null  and appKey != ''\"> and qu.app_key = #{appKey}</if>\n" +
            "            <if test=\"nickName != null  and nickName != ''\"> and cu.nick_name like concat( #{nickName}, '%') </if>\n" +
            "            <if test=\"qwUserName != null  and qwUserName != ''\"> and qu.qw_user_name like concat('%', #{qwUserName}, '%') </if> " +
            "            <if test=\"corpId != null \"> and qu.corp_id = #{corpId}</if>\n" +
            "            <if test=\"companyUserId != null \"> and qu.company_user_id = #{companyUserId}</if>\n" +
            "            <if test=\"companyId != null \"> and qu.company_id = #{companyId}</if>\n " +
            "            <if test=\"isDel != null \"> and qu.is_del = #{isDel}</if>\n "+
            "            <if test=\"cuDeptIdList != null and !cuDeptIdList.isEmpty() and  userType != '00' \">" +
            "               AND cu.dept_id IN " +
            "                   <foreach collection='cuDeptIdList' item='item' open='(' separator=',' close=')'> " +
            "                       #{item} " +
            "                   </foreach> " +
            "            </if>" +
            "            <if test=\"qwDeptIdList != null and !qwDeptIdList.isEmpty() \">" +
            "               AND qd.dept_id IN " +
            "                   <foreach collection='qwDeptIdList' item='item' open='(' separator=',' close=')'> " +
            "                       #{item} " +
            "                   </foreach> " +
            "            </if>" +
            "ORDER BY  qu.login_status asc,qu.tool_status desc " +
            "</script>"})
    List<QwUserVO> selectQwUserListStaffVO(QwUserListParam qwUser);

    @Select("SELECT\n" +
            "\tqu.qw_user_id AS bindAccount,\n" +
            "\tqu.qw_user_name AS bindName,\n" +
            "\tqu.company_id AS bindCompanyId,\n" +
            "\tc.company_name AS bindCompanyName,\n" +
            "\tqu.department AS bindDepartmentId,\n" +
            "\tqd.dept_name AS bindDepartmentName,\n" +
            "\tqu.corp_id AS bindCropId,\n" +
            "\tqc.corp_name AS bindCropName,\n" +
            "\tcu.address_id AS value,\n" +
            "\tfs.city_name AS label \n" +
            "FROM\n" +
            "\tqw_user qu\n" +
            "\tLEFT JOIN company_user cu ON cu.user_id = qu.company_user_id\n" +
            "\tLEFT JOIN company c ON qu.company_id = c.company_id\n" +
            "\tLEFT JOIN fs_city fs on fs.city_id=cu.address_id\n" +
            "\tRIGHT JOIN qw_dept qd ON qu.department = qd.dept_id \n" +
            "\tAND qd.corp_id = qu.corp_id\n" +
            "\tLEFT JOIN qw_company qc ON qc.corp_id = qu.corp_id \n" +
            "WHERE\n" +
            "qu.app_key = #{appKey}\n" +
            "\tLIMIT 1 ")
    QwFsServerBindParam selectQwServerBindParam(@Param("appKey") String appKey);



    /**
     *  查询企微用户绑定了的列表
     */
    @Select({"<script> " +
            "select qu.*, " +
            "COALESCE(cu.nick_name, qu.qw_user_name) as nickName, " +
            "COALESCE(cu.user_name, qu.qw_user_id) as userName, " +
            "qd.dept_name as departmentName from qw_user qu " +
            "LEFT JOIN company_user cu ON cu.user_id=qu.company_user_id " +
            "LEFT JOIN qw_dept qd on qu.department=qd.dept_id and qd.corp_id=qu.corp_id " +
            "where qu.is_del=0 " +
            "<if test=\"forTransfer == null or forTransfer != 1\"> and qu.company_user_id is not null </if>" +
            "            <if test=\"qwUserId != null  and qwUserId != ''\"> and qu.qw_user_id = #{qwUserId}</if>\n" +
            "            <if test=\"forTransfer == null or forTransfer != 1\"><if test=\"companyId != null \"> and qu.company_id = #{companyId}</if></if>\n" +
            "            <if test=\"nickName != null  and nickName != ''\"> and (cu.nick_name like concat('%', #{nickName}, '%') or qu.qw_user_name like concat('%', #{nickName}, '%')) </if>\n" +
            "            <if test=\"qwUserName != null  and qwUserName != ''\"> and qu.qw_user_name like concat('%', #{qwUserName}, '%') </if>\n" +
            "            <if test=\"companyUserId != null \"> and qu.company_user_id = #{companyUserId}</if>\n" +
            "            <if test=\"corpId != null \"> and qu.corp_id = #{corpId}</if>\n" +
            "            <if test=\"deptId != null \"> and qd.dept_id = #{deptId}</if>\n" +
            "            <if test=\"deptName != null \"> and qd.dept_name like concat('%', #{deptName}, '%') </if>\n" +
            "            <if test=\"status != null \"> and qu.status = #{status}</if>\n" +
            "            <if test=\"type != null and sendType !=null and type==2 and (sendType==2 or sendType==4 or sendType==11) and (allowOfficial == null or allowOfficial != 1) \"> and qu.app_key IS NOT NULL  </if>\n" +
            "</script>"})
    List<QwUserVO> selectQwUserListVO(QwUserListParam qwUser);


    /**
     *  查询所有企微用户列表
     */
    @Select({"<script> " +
            "select qu.*, cu.nick_name, cu.user_name, qd.dept_name as departmentName,qc.corp_name  from qw_user qu " +
            "LEFT JOIN company_user cu ON cu.user_id=qu.company_user_id " +
            "LEFT JOIN qw_company qc ON qc.corp_id=qu.corp_id " +
            "LEFT JOIN qw_dept qd on qu.department=qd.dept_id and qd.corp_id=qu.corp_id " +
            "where qu.is_del=0 "+
            "            <if test=\"qwUserId != null  and qwUserId != ''\"> and qu.qw_user_id = #{qwUserId}</if>\n" +
            "            <if test=\"companyId != null \"> and qu.company_id = #{companyId}</if>\n" +
            "            <if test=\"nickName != null  and nickName != ''\"> and cu.nick_name = #{nickName}</if>\n" +
            "            <if test=\"companyUserId != null \"> and qu.company_user_id = #{companyUserId}</if>\n" +
            "and qu.corp_id in" +
            "<foreach item='corpId' collection='corpId' open='(' separator=',' close=')'>" +
            "#{corpId}" +
            "</foreach> " +
            "            <if test=\"status != null \"> and qu.status = #{status}</if>\n" +
            "</script>"})
    @MapKey("corpId")
    List<QwUserVO> selectQwUserListBindVO(QwUserParam qwUser);


    @Select("select * from qw_user where corp_id=#{corpId} and qw_user_id=#{qwUserId} limit 1 ")
    QwUser selectQwUserByCorpIdAndUserId(@Param("corpId")String corpId,@Param("qwUserId") String qwUserId);

    @Select("select id,app_key from qw_user where corp_id=#{corpId} and qw_user_id=#{qwUserId} limit 1 ")
    QwUser selectQwUserAppKeyAndIdByCorpIdAndUserId(@Param("corpId")String corpId,@Param("qwUserId") String qwUserId);

    @Select("select qu.id dictValue,u.nick_name dictLabel from qw_user qu LEFT JOIN company_user u ON u.user_id=qu.company_user_id where user_id is not null and qu.corp_id=#{corpId}")
    List<QwOptionsVO> selectQwUserListOptionsVO(String corpId);

    @Select("select c.nick_name from qw_user qu LEFT JOIN company_user c on qu.company_user_id=c.user_id WHERE qu.qw_user_id=#{userId} limit 1")
    String selectQwUserByCompanyNickName(String userId);


    @Select("select * from qw_user where qw_user_id=#{qwUserId}")
    QwUser selectQwUserByQwUserId(@Param("qwUserId")String qwUserId);

    @Select("select app_key from qw_user where qw_user_id=#{qwUserId} and corp_id =#{corpId} limit 1")
    String selectQwUserByQwUserIdAndCorId(@Param("qwUserId")String qwUserId,@Param("corpId") String corpId);

    @Select("UPDATE qw_user SET company_user_id = NULL,company_id = NULL,status=0 where company_user_id=#{companyUserId}")
    void updateUserByUserId(Long companyUserId);

    @Select("UPDATE qw_user SET company_user_id = NULL,status=0 where id=#{id} ")
    void updateUnBindUserById(@Param("id") String id);

    @Select("SELECT\n" +
            "\tqu.id AS dictValue,\n" +
            "\tqu.qw_user_name AS dictLabel,\n" +
            "\tqu.corp_id,\n" +
            "\tqc.corp_name\n" +
            "FROM\n" +
            "\tqw_user qu\n" +
            "\tLEFT JOIN qw_company qc on qu.corp_id=qc.corp_id\n" +
            "WHERE\n" +
            "company_user_id =#{userId}")
    List<QwOptionsVO> selectQwUserListOptionsVOByCompanyUserId(Long userId);

    @Select("select * from qw_user where app_key=#{key}")
    QwUser selectQwUserByAppKey(String key);

    @Select("SELECT\n" +
            "\tqu.qw_user_id,\n" +
            "\tqu.corp_id,\n" +
            "\tqw.corp_name \n" +
            "FROM\n" +
            "\tqw_user qu\n" +
            "\tLEFT JOIN qw_company qw ON qu.corp_id = qw.corp_id \n" +
            "WHERE\n" +
            "\tqu.app_key = #{key}")
    QwHookAuthVO selectQwUserByAppKeyAuth(@Param("key") String key);

//    @Select("select login_code_url from qw_user where app_key=#{key}")
    String selectQwUserByAppKeyToIP(String key);

    @Select("select corp_id as dictValue,corp_name as dictLabel,corp_id,corp_name,allow_official from qw_company where FIND_IN_SET(#{companyId},company_ids)")
    List<QwOptionsVO> selectQwCompanyListOptionsVOByCompanyId(Long companyId);

    @Select("select corp_id as dictValue,corp_name as dictLabel,corp_id,corp_name,allow_official from qw_company where status=1")
    List<QwOptionsVO> selectQwCompanyListOptionsVOAll();

    @Select("select  *  from qw_user where qw_hook_id=#{qwHookId} ")
    QwUser selectQwUserByQwHookId(String qwHookId);

    @Update("UPDATE qw_user SET fastGpt_role_id = NULL where fastGpt_role_id=#{roleId}")
    int relieveFastGptRoleId(Long roleId);

    @Update("UPDATE qw_user SET fastGpt_role_id = NULL where id=#{id}")
    int relieveFastGptRoleById(Long id);

    @Select({"<script> " +
            " select qu.*,  qd.dept_name as departmentName,corp_name  from qw_user qu  LEFT JOIN qw_dept qd on qu.department=qd.dept_id and qd.corp_id=qu.corp_id \tLEFT JOIN qw_company qc on qc.corp_id=qu.corp_id  where qu.is_del=0 " +
            "            <if test=\"nickName != null  and nickName != ''\"> and cu.nick_name like concat( '%',#{nickName}, '%') </if>\n" +
            "            <if test=\"qwUserName != null  and qwUserName != ''\"> and qu.qw_user_name like concat( '%',#{qwUserName}, '%') </if>\n" +
            "            <if test=\"corpId != null \"> and qu.corp_id = #{corpId}</if>\n" +
            "            <if test=\"qwUserId != null \"> and qu.qw_user_id  like concat( #{qwUserId}, '%') </if>\n" +
            "            <if test=\"status != null \"> and qu.status = #{status}</if>\n" +
            "</script>"})
    List<QwUserVO> selectAllQwUserListVO(QwUserListParam qwUser);

    @Select("select qu.*,qc.corp_name from qw_user  qu left join qw_company qc ON qc.corp_id=qu.corp_id where qu.id=#{id}")
    QwUserVO selectQwUserVOById(Long id);

    @Select("<script> " +
            "select qu.*,qc.corp_name from qw_user  qu " +
            "left join qw_company qc ON qc.corp_id=qu.corp_id" +
            " where qu.id in  " +
            "        <foreach item=\"id\" collection=\"array\" open=\"(\" separator=\",\" close=\")\">\n" +
            "            #{id}\n" +
            "        </foreach> " +
            "</script>")
    List<QwUserVO> selectQwUserVOByIds(Long[] ids);

    @Select("select corp_id as dictValue,corp_name as dictLabel,allow_official from qw_company")
    List<QwOptionsVO> selectQwCompanyListAllOptionsVO();

    @Select("select qw_user_name from qw_user  WHERE qw_user_id=#{userId} and corp_id=#{corpId}")
    String selectQwUserByQwUserIdAndCorpId(@Param("userId") String userId,@Param("corpId")String corpId);

    List<QwUser> batchSelectQwUserByQwUserIdAndCorpId(@Param("data") List<SopUserLogsParamDTO> queryList);


    @Select("select id from qw_user  WHERE qw_user_id=#{userId} and corp_id=#{corpId}")

    Long selectQwUserByQwUserIdAndCorpIdToId(@Param("userId") String userId,@Param("corpId")String corpId);

    @Select("select * from qw_user  WHERE corp_id=#{corpId} and company_user_id is not null ")
    List<QwUser> selectQwUserByCorp(String corpId);

    @Select("select * from qw_user  WHERE qw_user_id=#{userId} and corp_id=#{corpId} limit 1 ")
    QwUser selectQwUserByQwUseridAndCorpId(@Param("userId")String userId,@Param("corpId") String corpId);


    List<QwUser> selectQwUserByUserIds(@Param("list")List<String> qwUserIds);


    @Select("select id as dictValue,qw_user_name as dictLabel from qw_user where is_del = 0")
    List<QwOptionsVO> getQwUserAll();

    @Select("select * from qw_user where app_key is not null")
    List<QwUser> getQwUserAllKey();

    @Select({"<script> " +
            " select qu.*,qc.corp_name  from qw_user qu  LEFT JOIN qw_company qc on qc.corp_id=qu.corp_id  where qu.is_del=0 and app_key is not null " +
            "            <if test=\"qwUserName != null  and qwUserName != ''\"> and qu.qw_user_name like concat('%', #{qwUserName}, '%') </if>\n" +
            "            <if test=\"corpId != null \"> and qu.corp_id = #{corpId}</if>\n" +
            "</script>"})
    List<QwWatchLogStatisticsListVO> selectQwWatchLogStatisticsListVO(QwWatchLogStatisticsListParam param);

    @Select("select id,qw_user_id,qw_user_name from  qw_user where corp_id =#{corpId} and is_del=0")
    List<QwUser> selectQwUserByCorpId(String corpId);

    @Select("select company_id from qw_user where corp_id = #{corpId} and qw_user_id = #{qwUserid}")
    Long getQwUserCompanyId(@Param("corpId") String corpId, @Param("qwUserid") String qwUserid);

    @Select("<script>" +
            "select cu.user_name,cu.nick_name,cu.user_id,qu.qw_user_id ,qu.qw_user_name,qu.id from company_user cu " +
            "LEFT JOIN  qw_user qu " +
            "ON qu.company_user_id=cu.user_id  " +
            "where cu.del_flag = 0 and  cu.qw_user_id is not null " +
            "AND qu.id IN " +
            "<foreach collection='qwUserIds' item='id' open='(' separator=',' close=')'> " +
            "#{id} " +
            "</foreach> " +
            "</script>")
    public List<QwUserVO> getQwUserByIdsNoCropId(@Param("qwUserIds") List<String> qwUserIds);

    @Select("<script> " +
            "select qu.qw_user_id,qu.qw_user_name,qe.create_time from qw_external_contact qe " +
            "left join  qw_user qu " +
            "on qe.user_id=qu.qw_user_id and qe.corp_id=qu.corp_id" +
            "where qe.external_user_id=#{data.externalUserId} " +
            "   and qe.corp_id=#{data.corpId} " +
            "   and qe.status=0 " +
            "   and qe.user_id != #{data.qwUserId} " +
            "</script>")
    List<QwExternalListByHeavyVO> getQwExternalListByHeavy(@Param("data") FsCourseListBySidebarParam param);

    List<QwWorkTask> selectQwWorkTaskList(SelectQwWorkTaskListParam param);

    @Select("select id,qw_user_id,company_user_id from qw_user where is_del=0 and company_user_id is not null")
    List<QwUser> selectQwUserAllList();

    @Select("select qw_user_id, corp_id, id from qw_user where qw_user_id is not null and corp_id is not null")
    List<QwUserDTO> selectAllQwUserMapping();

    @Select("select qw_user_name from qw_user where qw_user_id=#{qwUserId} and corp_id=#{corpId}")
    String selectQwUserName(@Param("qwUserId") String qwUserId,@Param("corpId") String corpId);

    @Select("select qw_user_id from qw_user where company_user_id = ${companyUserId}")
    List<String> findQwUserIdListByCompanyUserId(@Param("companyUserId") Long companyUserId);

    @Select("select qw_user_id from qw_user where company_user_id = #{userId} and corp_id = #{corpId}")
    List<String> selectQwUserListByCompanyUserId(@Param("userId") Long userId,@Param("corpId") String corpId);

    @Select("<script>" +
            "select qu.id from qw_user qu " +
            "left join company_user cu on cu.user_id=qu.company_user_id " +
            "where 1=1 " +
            "            <if test=\"map.corpId != null \"> and qu.corp_id = #{map.corpId}</if>\n" +
            "            <if test=\"map.userId != null \"> and qu.company_user_id = #{map.userId}</if>\n " +
            "            <if test=\"map.cuDeptIdList != null and !map.cuDeptIdList.isEmpty() and  map.userType != '00' \">" +
            "               AND cu.dept_id IN " +
            "                   <foreach collection='map.cuDeptIdList' item='item' open='(' separator=',' close=')'> " +
            "                       #{item} " +
            "                   </foreach> " +
            "            </if>" +
            "</script>")
    List<Long> selectQwUserListByCuDeptIdList(@Param("map") QwSop qwSop);

    @Select("<script>" +
            "select qu.id,qu.qw_user_id,qu.qw_user_name,qu.department,qu.corp_id,qu.company_id,qu.company_user_id, " +
            "cu.nick_name, cu.user_name, qd.dept_name as departmentName from qw_user qu " +
            "LEFT JOIN company_user cu ON cu.user_id=qu.company_user_id " +
            "RIGHT JOIN qw_dept qd on qu.department=qd.dept_id and qd.corp_id=qu.corp_id " +
            "where qu.is_del=0 and qu.company_user_id is not null "+
            "            <if test=\"map.corpId != null \"> and qu.corp_id = #{map.corpId}</if>\n" +
            "            <if test=\"map.companyId != null \"> and qu.company_id = #{map.companyId}</if>\n" +
            "            <if test=\"map.deptIds != null and !map.deptIds.isEmpty() \">" +
            "               AND qu.department IN " +
            "                   <foreach collection='map.deptIds' item='item' open='(' separator=',' close=')'> " +
            "                       #{item} " +
            "                   </foreach> " +
            "            </if>" +
            "</script>")
    List<QwUserVO> selectQwUserByDeptList(@Param("map") QwUserByDeptParam deptParam);

    /**
     * 根据外部联系人的fsUserId查询关联的企微员工信息
     * @param fsUserId 会员id
     * @return
     */
    List<ExternalContactQwUserVO> selectQwUserByFsUserId(@Param("fsUserId") Long fsUserId);

    void updateSendType(UpdateSendTypeVo vo);

    QwUser selectOfflineUser();

    /**
     * 根据销售公司和企微ID查询企微用户
     */
    List<QwUserVO> selectQwUserListVOByCompanyIdAndCorpIdAndNickName(@Param("companyId") Long companyId, @Param("corpId") String corpId, @Param("nickName") String nickName);

    List<QwOptionsVO> selectQwCompanyListOptionsVOBySys();

    @Select("select corp_id from qw_user where id=#{id} limit 1")
    String selectCorpIdById(@Param("id") Long id);
    List<Long> selectDeptByParentId(@Param("deptId")Long deptId,@Param("corpId") String corpId);

    @Select("  select company_id from qw_user where qw_user_id = #{owner} and corp_id = #{corpId}  limit 1")
    Long getCompanyIdByCorpIdAndOwner(@Param("corpId")String corpId, @Param("owner")String owner);

    @Select("<script>" +
            "select * from qw_user where qw_user_id in " +
            "<foreach collection='qwUserIdList' item='item' open='(' separator=',' close=')'> " +
            "#{item} " +
            "</foreach> " +
            "</script>")
    List<QwUser> selectQwUserByIds(@Param("qwUserIdList") List<Long> qwUserIdList);

    /**
     * 按主键批量查询 PAD 在线状态（仅查 id、ipad_status，用于跨库补全）
     */
    @Select("<script>" +
            "select id, ipad_status from qw_user where id in " +
            "<foreach collection='ids' item='item' open='(' separator=',' close=')'>" +
            "#{item}" +
            "</foreach>" +
            "</script>")
    List<QwUser> selectIpadStatusByIds(@Param("ids") List<Long> ids);

    /**
     * 按主键批量查询企微 userid（SOP.qw_user_ids 存的是 qw_user.id，营期表存的是 qw_user.qw_user_id）
     */
    @Select("<script>" +
            "select id, qw_user_id from qw_user where id in " +
            "<foreach collection='ids' item='item' open='(' separator=',' close=')'>" +
            "#{item}" +
            "</foreach>" +
            "</script>")
    List<QwUser> selectQwUserIdByPkIds(@Param("ids") List<Long> ids);

    List<QwUser> selectQwUserByTest();

    @Select("<script>" +
            "select id from qw_user where 1=1 " +
            "            <if test=\"userIds != null and userIds.size() > 0\">\n" +
            "                AND company_user_id IN\n" +
            "                <foreach collection=\"userIds\" open=\"(\" close=\")\" separator=\",\" item=\"item\">\n" +
            "                    ${item}\n" +
            "                </foreach>\n" +
            "            </if>" +
            "</script>")
    List<Long> selectQwUserListByCompanyUserIdS(@Param("userIds") List<String> userIds);

    List<QwUser> selectQwUserByServerIds(@Param("serverIds")List<String> serverIds);

    int batchUpdateUnbind(@Param("ids")List<Long> ids);

    @Select("select * from qw_user where qw_user_id=#{qwUserId} and corp_id =#{corpId} limit 1")
    QwUser selectQwUserEntityByQwUserIdAndCorId(@Param("qwUserId")String qwUserId,@Param("corpId") String corpId);

    @Select("select * from qw_user where ipad_status = 1 and corp_id=#{corpId} and qw_user_id=#{qwUserId} limit 1 ")
    QwUser selectQwUserAppKeyAndIdByCorpIdAndUserIdAndIpad(@Param("corpId")String corpId,@Param("qwUserId") String qwUserId);

    List<Long> selectByChatIdAndType(@Param("chatIdList")List<String> chatIdList);

    List<Long> selectQwUserByIdsAndcorpId(@Param("qwUserIds")List<String> qwUserIds,@Param("corpId") String corpId);
}
