package com.fs.qw.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.fs.qw.domain.QwUser;
import com.fs.qw.dto.QwUserByToolDTO;
import com.fs.qw.param.QwFsServerBindParam;
import com.fs.qw.param.QwUserListParam;
import com.fs.qw.param.QwUserParam;
import com.fs.qw.param.QwWatchLogStatisticsListParam;
import com.fs.qw.vo.QwHookAuthVO;
import com.fs.qw.vo.QwOptionsVO;
import com.fs.qw.vo.QwUserVO;
import com.fs.qw.vo.QwWatchLogStatisticsListVO;
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;

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

    @Select("select welcome_text,qw_user_name from qw_user where id = #{id}")
    public QwUser selectQwUserByIdByWeComeText(@Param("id") Long id);
    /**
     * 根据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 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.is_del=0 and qu.company_user_id is not null "+
            "            <if test=\"qwUserId != null  and qwUserId != ''\"> and qu.qw_user_id = #{qwUserId}</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=\"companyId != null \"> and qu.company_id = #{companyId}</if>\n " +
            "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.*, 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=\"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 like concat( #{nickName}, '%') </if>\n" +
            "            <if test=\"qwUserName != null  and qwUserName != ''\"> and qu.qw_user_uame 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=\"status != null \"> and qu.status = #{status}</if>\n" +
            "            <if test=\"type != null and sendType !=null and type==2 and (sendType==2 or sendType==4) \"> 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,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 from qw_company where FIND_IN_SET(#{companyId},company_ids)")
    List<QwOptionsVO> selectQwCompanyListOptionsVOByCompanyId(Long companyId);

    @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" +
            "            <if test=\"type != null and sendType !=null and type==2 and sendType==2 \"> and qu.app_key IS NOT NULL  </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 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);

    @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);
}
