package com.fs.qw.service;


import com.baomidou.mybatisplus.extension.service.IService;
import com.fs.common.core.domain.R;
import com.fs.course.param.FsCourseListBySidebarParam;
import com.fs.qw.domain.QwExternalContact;
import com.fs.qw.domain.QwExternalContactTransferLog;
import com.fs.qw.domain.QwUser;
import com.fs.qw.param.*;
import com.fs.qw.param.newparam.ExternalContactPageListParam;
import com.fs.qw.param.sidebar.ExternalContactInfoParam;
import com.fs.qw.param.sidebar.TagGroupUpdateParam;
import com.fs.qw.result.QwExternalContactLogVo;
import com.fs.qw.result.QwExternalContactVo;
import com.fs.qw.vo.*;
import com.fs.qw.vo.newvo.ExternalContactListVO;
import com.fs.qw.vo.newvo.ExternalContactPageVO;
import com.fs.qw.vo.sidebar.ExternalContactInfoVO;
import com.fs.qw.vo.sidebar.ExternalContactTagVO;
import com.fs.qwApi.param.QwExternalContactHParam;
import org.codehaus.jettison.json.JSONException;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * 企业微信客户Service接口
 *
 * @author fs
 * @date 2024-06-20
 */
public interface IQwExternalContactService extends IService<QwExternalContact> {
    /**
     * 查询企业微信客户
     *
     * @param id 企业微信客户主键
     * @return 企业微信客户
     */
    public QwExternalContact selectQwExternalContactById(Long id);

    /** 根据外部联系人的userid 获取到所属员工详细信息 */
    public List<QwExternalContact> selectQwExternalContactByExternalUserId(String externalUserId,String corpId);
    /**
     * 预计发送消息人员的客户列表
     */
    public ArrayList<String> selectQwGroupMsgExpectCustomerList(QwCountCustomerParam userIdList, String corpId);

    /**
     *  统计预期发送消息人员的客户总数
     */
    public R expectQwGroupMsgCountCustomer(QwCountCustomerParam userIdList);

    /**
     * 查询企业微信客户列表
     *
     * @param qwExternalContact 企业微信客户
     * @return 企业微信客户集合
     */
    public List<QwExternalContact> selectQwExternalContactList(QwExternalContact qwExternalContact);

    /**
     * 新增企业微信客户
     *
     * @param qwExternalContact 企业微信客户
     * @return 结果
     */
    public int insertQwExternalContact(QwExternalContact qwExternalContact);

    /**
     * 修改企业微信客户
     *
     * @param qwExternalContact 企业微信客户
     * @return 结果
     */
    public int updateQwExternalContact(QwExternalContact qwExternalContact);

    /**
     * 修改企微客户关联会员拉黑状态（写入 fs_user）
     */
    int updateExternalContactManageStatus(QwExternalContactManageParam param);
    public int updateQwExternalContactCallEdit(QwExternalContact qwExternalContact);

    /**
     * 批量删除企业微信客户
     *
     * @param ids 需要删除的企业微信客户主键集合
     * @return 结果
     */
    public int deleteQwExternalContactByIds(Long[] ids);

    /**
     * 删除企业微信客户信息
     *
     * @param id 企业微信客户主键
     * @return 结果
     */
    public int deleteQwExternalContactById(Long id);

    List<QwExternalContactVO> selectQwExternalContactListVO(QwExternalContactParam qwExternalContact);

    List<QwExternalContactUnionIdExportVO> selectQwExternalContactUnionIdExportVO(QwExternalContactParam qwExternalContact);

    List<QwExternalContactVOTime> selectQwExternalContactListVOByIds(List<Long> ids);

    /**
     * 按 fs_user_id 批量查询企微客户
     */
    List<QwExternalContact> selectExternalByFsUserIds(List<Long> userIds);

    R syncQwExternalContact(String corpId) throws IOException;

    R syncMyQwExternalContact(Long id) throws IOException;

    R syncQwExternalContactUnassigned(String corpId);

    R resignedTransfer(ResignedTransferParam param);

    R transfer(TransferParam param);

    R transferPreCheck(TransferPreCheckParam param);

    R addUserTag(QwExternalContactAddTagParam param) throws JSONException;
    R addTagByWatch(QwExtContactAddTagByWatchParam param) throws JSONException;

    R delUserTag(QwExternalContactAddTagParam param);
    R delTagByWatch(QwExtContactAddTagByWatchParam param);

    QwUser getQwUserByRedis(String corpId, String userID);

    QwUser getQwUserByRedisForId(String qwUserId);

    void insertQwExternalContactByExternalUserId(String externalUserID, String userID, Long companyId, String corpId, String state, String welcomeCode, QwUser qwUser) throws ParseException;

    void insertQwExternalContactByExternalUserId2(String externalUserID, String userID, Long companyId, String corpId, String state, String welcomeCode) throws ParseException;

    //todo 聊天sop添加营期人员
    void qwChatSopRuleTimeTool(List<QwSopRuleTimeVO> qwSopRuleTimeVOS, String userID,  String corpId, String externalUserID, String externalContactName, QwExternalContact contact);

    void updateQwExternalContactByExternalUserId(String externalUserID, String userID, String corpId);

    void deleteQwExternalContactByExternalUserId(String externalUserID, String userID, String corpId);

    void deletefollowUserByExternalUserId(String externalUserID, String userID, String corpId);

    /**
     * 删了又加：命中 Redis 防重或仅需恢复好友关系时，将 status 恢复为 0
     */
    void restoreExternalContactOnReAdd(String externalUserID, String userID, String corpId);

    void transferFailByExternalUserId(String externalUserID, String userID, String corpId,String failReason);

    /**
     * 转接完成：设置 is_transfer=1 并继承 fs_user_id
     */
    void applyTransferContactInfo(Long newContactId, QwExternalContactTransferLog transferLog, Long oldContactId);

    int updateQwExternalContactStatus(QwExternalContact qwExternalContact);
    /**
     *  企业微信客户与企业客户绑定
     */
    int updateQwExternalContactBindCustomer(QwExternalContact qwExternalContact);

    /**
     * 设置客户-课节SOP
     */
    R setCustomerCourseSop(QwFsCustomerCourseSopParam param);

    /**
     * 查询是否已经设置过客户-某个课节的SOP
     */
    Boolean getCustomerCourseSop(QwFsCustomerCourseSopGetParam param);

    /**
     * 批量设置客户-课节SOP
     */
    R setCustomerCourseSopList(QwFsCustomerCourseSopListParam param);

    R updateQwExternalContactBindUserId(QwExternalContact qwExternalContact);

    /**
     * 修改当前企微客户所绑定小程序会员的首次登录奖励地址（fs_user.first_login_reward_address）
     */
    R updateBoundFsUserFirstLoginRewardAddress(QwExternalContactFirstLoginRewardAddressParam param, Long companyId);

    R updateQwExternalContactUnBindUserId(Long id);
    QwExternalContact getQwExternalContactDetails(QwExternalContactHParam contactHParam);

    void qwExternalContactSync();

    List<QwExternalContactVo> selectAllQwJoinAdClickLog(Integer type);

    List<QwExternalContactLogVo> selectAllFormAd(ConversionStatisticsParam param);

    R syncAddMyQwExternalContact(Long id);

    void qwExternalContactSyncAddAndDel();

    void qwExternalContactSyncAddRedis();
     void qwExternalContactAddRedis();

    void qwExternalContactAddAndDelByRedis();

    void qwExternalContactAddCourseTag();

    void addQwCourseJob();

    void synchronizeQwExternalContactTask();

    /**
     * 获取会员答题和看课情况
     * @param param 参数
     * @return
     */
    ExternalContactDetailsVO getUserDetails(ExternalContactDetailsParam param);

    List<QwExternalContactVOTime> selectQwExternalContactListVOByUserIds(List<String> externalIdList);

    Integer selectQwIsRepeat(Long id);


    List<QwExternalListByHeavyVO> getQwExternalListByHeavy(FsCourseListBySidebarParam param);

    /**
     * 根据userid修改外部联系人评论状态
     * @param commentStatus 评论状态
     * @param fsUserId 用户id
     * @return
     */
    int updateQwExternalContactByFsUserId(Integer commentStatus, Long fsUserId);

    List<ExternalContactListVO> externalContactPageList(ExternalContactPageListParam param);

    ExternalContactPageVO getContactNumber(Long l);

    Boolean disabledUser(String[] ids, boolean b);

    /**
     * 获取外部联系人基本信息
     * @param qwExternalContactId 联系人主键id
     * @return
     */
    ExternalContactInfoVO getExternalContactInfo(Long qwExternalContactId);

    /**
     * 获取侧边栏外部联系人标签
     * @param qwExternalContactId 联系人主键id
     * @return
     */
    List<ExternalContactTagVO> getExternalContactTag(Long qwExternalContactId);

    /**
     * 侧边栏-编辑外部联系人信息
     * @param param 入参
     * @return
     */
    R updateExternalContactInfo(ExternalContactInfoParam param);

    /**
     * 修改外部联系人的标签
     * @param param
     */
    int updateExternalContactTag(TagGroupUpdateParam param);

    List<QwExternalContact> selectQwExternalContactByFsUserIdAndCompany(Long userId,Long companyUserId);


    QwExternalContact selectQwExternalContactByRemark(Long qwExternalContactId);


    /**
     * 企微用户流失删除统计
     * @param param 查询参数
     */
    List<QwUserDelLossLogVO> selectQwUserDelLossLogList(QwUserDelLossLogParam param);

    void updateQwExternalContactStatusById(QwExternalContact qwExternalContact);

    R getRepeat(RepeatParam param);
    List<QwExternalContactVO> selectQwExternalContactListVONewSys(QwExternalContactParam qwExternalContact);
    /**
     * 根据qw_user_id+crop_id+external_user_id查询外部联系人信息
     * */
    QwExternalContact selectQwUserListVOByQwUserIdAndCorpIdAndExternalUserId(ExternalContactParam externalContactParam);

    List<QwExternalContact> selectQwUserAndLevel(List<Long> qwUserIdList, List<String> levelList, boolean isReg);

    List<String> selectQwExternalContactMandatoryRegistration();

    List<QwMandatoryRegistrParam> selectQwExternalContactMandatoryRegistrationByIds(String corpId);

    int batchUpdateQwExternalContactMandatoryRegistration(List<QwMandatoryRegistrParam> batchList);
}
