package com.gic.haoban.manage.service.dao.mapper;

import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.Param;

import com.gic.haoban.manage.api.dto.ExternalClerkRelatedDTO;
import com.gic.haoban.manage.service.entity.TabHaobanExternalClerkRelated;
import com.github.pagehelper.Page;

public interface TabHaobanExternalClerkRelatedMapper {

    int insertSelective(TabHaobanExternalClerkRelated record);

    TabHaobanExternalClerkRelated selectByPrimaryKey(String externalClerkRelatedId);

    int updateByPrimaryKeySelective(TabHaobanExternalClerkRelated record);

    int selectByClerkIdAndExternalUserId(@Param("staffId") String staffId,
                                                                   @Param("externalUserId") String externalUserId);

    int delByUserIdAndExternalUserId(@Param("externalUserId") String externalUserId , @Param("staffId") String staffId);

    void delByUserIdAndExternalUserIdBatch(@Param("staffId") String staffId,
                                           @Param("wxEnterpriseId") String wxEnterpriseId,
                                           @Param("externalUserIds") List<String> externalUserIds);

    void delByClerkIdAndStaffId(@Param("clerkId") String clerkId, @Param("staffId") String staffId);

    void delByStoreIdAndStaffId(@Param("storeId") String storeId, @Param("staffId") String staffId);

    int delByStoreIds(@Param("storeIds") Collection<String> storeIds);

    void cleanStaffRelated(@Param("wxEnterpriseId") String wxEnterpriseId, @Param("staffIds") List<String> staffIds);
    List<TabHaobanExternalClerkRelated> listByExTernalUseridAndWxUserId(@Param("externalUserid") String externalUserid,
                                                                        @Param("staffId") String staffId,
                                                                        @Param("statusFlag") Integer statusFlag);

    /**
     * 查询该会员该企业下的好友关系
     *
     * @param externalUserid 外部用户标识
     * @param wxEnterpriseId wx企业标识
     * @param statusFlags    状态标志
     * @return {@link List<TabHaobanExternalClerkRelated> }
     * @author mozhu
     * @date 2022-02-10 10:11:21
     */
    List<TabHaobanExternalClerkRelated> listByExternalUseridAndWxEnterpriseId(@Param("externalUserid") String externalUserid,
                                                                              @Param("wxEnterpriseId") String wxEnterpriseId,
                                                                              @Param("statusFlags") List<String> statusFlags);

    List<TabHaobanExternalClerkRelated> listByExTernalUseridAndWxUserIdAllStatus(@Param("wxEnterpriseId") String wxEnterpriseId,
                                                                                 @Param("externalUserid") String externalUserid,
                                                                                 @Param("staffId") String staffId);

    List<TabHaobanExternalClerkRelated> listByEnterpriseIdAndDate(@Param("enterpriseId") String enterpriseId,
                                                                  @Param("date") Date date);

    Page<TabHaobanExternalClerkRelated> pageMemberUnionByParams(@Param("staffIdList") List<String> staffIdList,
                                                                @Param("sendMemberIds") List<String> sendMemberIds,
                                                                @Param("enterpriseId") String enterpriseId);

    List<TabHaobanExternalClerkRelated> listByWxEnterpriseIdAndWxUserId(@Param("staffId") String staffId,
                                                                        @Param("wxEnterpriseId") String wxEnterpriseId);

    List<TabHaobanExternalClerkRelated> listByWxEnterpriseIdAndWxUserIds(@Param("staffIdList") List<String> staffIdList,
                                                                         @Param("wxEnterpriseId") String wxEnterpriseId);

    TabHaobanExternalClerkRelated getByMemberId(@Param("staffId") String staffId, @Param("memberId") String memberId);

    /**
     * 外部联系人的unionid
     *
     * @param enterpriseId
     * @param unionId
     * @return
     */
    List<TabHaobanExternalClerkRelated> listExternalByUnionId(@Param("enterpriseId") String enterpriseId,
                                                              @Param("unionId") String unionId);

    /**
     * @param relationKey
     * @return
     */
    TabHaobanExternalClerkRelated getByRelationKey(@Param("relationKey") String relationKey , @Param("filterMember")int filterMember);

    /**
     * 列表数据 根据状态 时间倒序
     *
     * @param wxEnterpriseId
     * @param staffId
     * @param statusFlags
     * @return
     */
    List<TabHaobanExternalClerkRelated> listByWxEnterpriseIdAndWxUserIdAndStatus(@Param("wxEnterpriseId") String wxEnterpriseId,
                                                                                 @Param("staffId") String staffId,
                                                                                 @Param("statusFlags") List<Integer> statusFlags);

    /**
     * 触达会员 给会员发送
     *
     * @param relationKey
     * @return
     */
    int pushExternalByRelationKey(@Param("relationKey") String relationKey);

    /**
     * 获取企业下的关联关系列表
     *
     * @param wxEnterpriseId
     * @param wxUserId
     * @param enterpriseId
     * @param externalUserId
     * @return
     */
    TabHaobanExternalClerkRelated getExternalClerkByWxUserIdAncExternalUserId(@Param("wxEnterpriseId") String wxEnterpriseId,
                                                                              @Param("staffId") String staffId,
                                                                              @Param("enterpriseId") String enterpriseId,
                                                                              @Param("externalUserId") String externalUserId);
    
    TabHaobanExternalClerkRelated getExternalClerkByWxUserIdAncExternalUserIdSelf(@Param("wxEnterpriseId") String wxEnterpriseId,
            @Param("staffId") String staffId,
            @Param("enterpriseId") String enterpriseId,
            @Param("externalUserId") String externalUserId);

    /**
     * 查询会员在企业下的好友关系
     *
     * @param memberId
     * @param wxEnterpriseId
     * @param enterpriseId
     * @return
     */
    List<TabHaobanExternalClerkRelated> listByMemberIdAndWxEnterpriseId(@Param("memberId") String memberId,
                                                                        @Param("wxEnterpriseId") String wxEnterpriseId,
                                                                        @Param("enterpriseId") String enterpriseId);

    /**
     * 查询会员在企业下的好友关系
     *
     * @param memberId
     * @param wxEnterpriseId
     * @param enterpriseId
     * @return
     */
    List<ExternalClerkRelatedDTO> listByMemberIdList(@Param("memberIdList") List<String> memberIdList,
                                                     @Param("wxEnterpriseId") String wxEnterpriseId,
                                                     @Param("enterpriseId") String enterpriseId);

    /**
     * 获取最新的最近的外部联系人
     *
     * @param externalUserid
     * @param wxUserId
     * @return
     */
    TabHaobanExternalClerkRelated getLastByExTernalUseridAndWxUserIdNoStatus(@Param("wxEnterpriseId") String wxEnterpriseId, @Param("externalUserid") String externalUserid, @Param("staffId") String staffId);

    TabHaobanExternalClerkRelated getLastByExTernalUseridAndWxUserIdNoStatusSelf(@Param("wxEnterpriseId") String wxEnterpriseId, @Param("externalUserid") String externalUserid, @Param("staffId") String staffId);


    /**
     * 根据id 查询列表
     *
     * @param wxEnterpriseId
     * @param ids
     * @return
     */
    List<TabHaobanExternalClerkRelated> listByIds(@Param("wxEnterpriseId") String wxEnterpriseId,
                                                  @Param("ids") List<String> ids);

    /**
     * 根据企业id 查询列表
     *
     * @param wxEnterpriseId
     * @return
     */
    List<String> listMemberIdByEnterpriseId(@Param("wxEnterpriseId") String wxEnterpriseId,
                                            @Param("enterpriseId") String enterpriseId);


    /**
     * 查询会员在企业下的好友关系memberIds 根据会员ids
     *
     * @param memberIds
     * @param wxEnterpriseId
     * @return
     */
    List<String> listMemberIdsByMemberIds(@Param("memberIds") List<String> memberIds,
                                          @Param("wxEnterpriseId") String wxEnterpriseId);

    /**
     * 查询会员在企业下的好友关系 根据会员id
     *
     * @param memberId
     * @param enterpriseId
     * @return
     */
    List<TabHaobanExternalClerkRelated> listByMemberId(@Param("memberId") String memberId,
                                                       @Param("enterpriseId") String enterpriseId);

    /**
     * 通过会员id查询门店id
     *
     * @param memberId 成员身份
     * @return {@link List }<{@link String }>
     * @author mozhu
     * @date 2022-07-06 14:40:15
     */
    List<String> listStoreIdByMemberId(@Param("memberId") String memberId , @Param("addCreateTime") String addCreateTime);

    /**
     * 查询会员在企业下的好友关系 根据会员id
     *
     * @param wxEnterpriseId
     * @return
     */
    List<TabHaobanExternalClerkRelated> listByExternalUserId(@Param("wxEnterpriseId") String wxEnterpriseId,
                                                             @Param("externalUserId") String externalUserId);

    /**
     * 根据会员id更新新老会员id
     *
     * @param memberId
     * @param oldMemberId  老会员id
     * @param enterpriseId 企业标识
     * @return int
     * @author mozhu
     * @date 2022-03-24 10:28:40
     */
    int updateMemberIdByMemberId(@Param("memberId") String memberId,
                                 @Param("oldMemberId") String oldMemberId,
                                 @Param("enterpriseId") String enterpriseId);

    /**
     * 更新状态通过会员id
     *
     * @param memberId     会员ID
     * @param enterpriseId 企业id
     * @return int
     * @author mozhu
     * @date 2021-09-13 16:59:49
     */
    int updateStatusByMemberId(@Param("memberId") String memberId, @Param("enterpriseId") String enterpriseId);


    /**
     * 通过会员id和员工id查询数据
     *
     * @param staffId      员工id
     * @param enterpriseId 企业标识
     * @param memberId     会员ID
     * @return {@link TabHaobanExternalClerkRelated }
     * @author mozhu
     * @date 2021-10-21 10:41:28
     */
    TabHaobanExternalClerkRelated getByMemberIdAndStaffId(@Param("staffId") String staffId,
                                                          @Param("enterpriseId") String enterpriseId,
                                                          @Param("memberId") String memberId);

    TabHaobanExternalClerkRelated getByUnionIdAndStaffId(@Param("staffId") String staffId,
                                                          @Param("enterpriseId") String enterpriseId,
                                                          @Param("unionId") String unionId);

    /**
     * 根据企业id分页
     *
     * @param wxEnterpriseId 企业标识
     * @return {@link List<TabHaobanExternalClerkRelated> }
     * @author mozhu
     * @date 2021-12-13 14:59:03
     */
    List<TabHaobanExternalClerkRelated> pageExternalByEnterpriseId(@Param("wxEnterpriseId") String wxEnterpriseId);

    /**
     * 更新外部用户id的老外部用户id
     *
     * @param newExternalUserId 外部用户id
     * @param oldExternalUserId 旧的外部用户id
     * @param wxEnterpriseId    wx企业标识
     * @return int
     * @author mozhu
     * @date 2021-12-13 15:33:41
     */
    int updateExternalUserIdByOldExternalUserId(@Param("newExternalUserId") String newExternalUserId,
                                                @Param("oldExternalUserId") String oldExternalUserId,
                                                @Param("wxEnterpriseId") String wxEnterpriseId);

    /**
     * 获取好友数量
     *
     * @param wxEnterpriseId 企业id
     * @param enterpriseId   商户id
     * @param staffId        企业微信导购账号
     * @param clerkId        导购id
     * @return
     */
    int countByClerkId(@Param("wxEnterpriseId") String wxEnterpriseId,
                       @Param("enterpriseId") String enterpriseId,
                       @Param("staffId") String staffId,
                       @Param("clerkId") String clerkId);

    /**
     * 客户好友需触达列表
     */
    TabHaobanExternalClerkRelated getByEnterpriseIdAndExternalUserId(@Param("wxEnterpriseId") String wxEnterpriseId, @Param("staffId") String staffId,
                                                                     @Param("enterpriseId") String enterpriseId, @Param("externalUserId") String externalUserId);


    /**
     * 查询所有好友关系
     *
     * @param wxEnterpriseId wx企业标识
     * @param staffId        wx用户id
     * @return {@link List}<{@link TabHaobanExternalClerkRelated}>
     */
    List<TabHaobanExternalClerkRelated> getByWxUserIdAndWxEnterpriseId(@Param("wxEnterpriseId") String wxEnterpriseId,
                                                                       @Param("staffId") String staffId);


    /**
     * 根据staffId和会员unionid进行查询数据
     */
    TabHaobanExternalClerkRelated getByWxEnterpriseIdAndUnionId(@Param("wxEnterpriseId") String wxEnterpriseId,
                                                                @Param("staffId") String staffId,
                                                                @Param("unionid") String unionid);


    /**
     * 查询最新的会员好友
     *
     * @param enterpriseId 企业标识
     * @param memberId     成员身份
     * @param statusFlag   状态标志
     * @return {@link TabHaobanExternalClerkRelated }
     * @author mozhu
     * @date 2022-04-15 17:12:38
     */
    TabHaobanExternalClerkRelated getByEnterpriseIdAndMemberId(@Param("enterpriseId") String enterpriseId,
                                                               @Param("memberId") String memberId,
                                                               @Param("statusFlag") Integer statusFlag);

    /**
     * 会员好友数量
     *
     * @param memberId     成员身份
     * @param enterpriseId 企业标识
     * @return int
     * @author mozhu
     * @date 2022-04-21 10:06:21
     */
    int countByMemberId(@Param("memberId") String memberId, @Param("enterpriseId") String enterpriseId);
    
    List<String> getIdList(@Param("memberId") String memberId, @Param("enterpriseId") String enterpriseId) ;
    
    void updateMemberByIdList(@Param("memberId") String memberId, @Param("list") List<String> idList) ;

    List<TabHaobanExternalClerkRelated> listPageByWxenterpriseId(@Param("wxEnterpriseId") String wxEnterpriseId,
                                                                 @Param("startItem") int startItem, @Param("pageSize") int pageSize ,@Param("type") int type);


    /**
     * 获取所有的好友会员id
     *
     * @param storeId 存储id
     * @param staffId 员工id
     * @return {@link List}<{@link String}>
     */
    List<String> getByStoreIdAndStaffId(@Param("storeId") String storeId,
                                        @Param("staffId") String staffId);

    List<String> listMemberAndClerk(@Param("memberIdList")List<String> memberIdList, @Param("clerkIdList")List<String> clerkIdList, @Param("enterpriseId")String enterpriseId);
    
    int getCountExternalClerk(@Param("wxEnterpriseId") String wxEnterpriseId,
                              @Param("staffId")  String staffId,
                              @Param("statusFlags") List<Integer> statusFlags);

    int getCountByMemberId(@Param("memberId") String memberId);
    
    void updateClerkInfo(@Param("staffId")String staffId, @Param("oldClerkId")String oldClerkId, @Param("newClerkId")String newClerkId, @Param("storeId")String storeId);
    
    List<String> listByClerkId(@Param("staffId")String staffId, @Param("clerkId")String clerkId) ;
    
    int updateExternalStatus(@Param("wxEnterpriseId")String wxEnterpriseId, @Param("externalUserid")String externalUserid, @Param("status")int status);

    int updateExternalStatusById(@Param("id")String id,  @Param("status")int status);


    /**
     * 通过第三方或者待开发查询是否好友
     */
    TabHaobanExternalClerkRelated getMemberForExternalId(@Param("wxEnterpriseId") String wxEnterpriseId,
            @Param("externalUserId") String externalUserId);
    
    List<String> listMemberAndStaff(@Param("memberIdList")List<String> memberIdList, @Param("staffId")String staffId, @Param("enterpriseId")String enterpriseId);

    List<TabHaobanExternalClerkRelated> listRelationMemberAndStaff(@Param("memberIdList")List<String> memberIdList, @Param("staffId")String staffId, @Param("enterpriseId")String enterpriseId);

    /**
     * 查询成员的好友列表
     */
    List<String> queryExternalUserIdWithStaffId(@Param("staffId") String staffId);


    /**
     * 查询会员与导购的好友关系
     * @param staffId
     * @param memberIds
     * @return
     */
    List<TabHaobanExternalClerkRelated> queryClerkMemberRelations(@Param("staffId") String staffId, @Param("memberIds") List<String> memberIds);

    /**
     * 查询企业微信下面 外部联系人是null的所有成员
     * @param wxEnterpriseId
     * @param enterpriseIdList
     * @return
     */
    List<String> listByWxEnterpriseId(@Param("wxEnterpriseId")String wxEnterpriseId, @Param("enterpriseIdList") List<String> enterpriseIdList);

    /**
     * 删除企业微信下面,所有外部联系人是null的成员
     * @param wxEnterpriseId
     * @param enterpriseIdList
     */
    void deleteByWxEnterpriseId(@Param("wxEnterpriseId")String wxEnterpriseId, @Param("enterpriseIdList") List<String> enterpriseIdList);

    List<TabHaobanExternalClerkRelated> getAddTimeByMemberId(@Param("memberId") String memberId, @Param("enterpriseId") String enterpriseId,@Param("flag") String flag);

    /**
     * 根据外部联系人id查询memberId
     * @param wxEnterpriseId
     * @param externalUserId
     * @return
     */
    String getMemberIdByExternalId(@Param("wxEnterpriseId") String wxEnterpriseId, @Param("externalUserId") String externalUserId);

    int halfFriendCount(@Param("wxEnterpriseId") String wxEnterpriseId, @Param("storeId")String storeId, @Param("clerkId")String clerkId, @Param("staffId")String staffId);

    List<TabHaobanExternalClerkRelated> halfFriendPage(@Param("wxEnterpriseId") String wxEnterpriseId, @Param("storeId")String storeId, @Param("clerkId")String clerkId, @Param("staffId")String staffId,  @Param("type")int type);

    List<TabHaobanExternalClerkRelated> halfDelFriendPage(@Param("wxEnterpriseId") String wxEnterpriseId , @Param("startItem")int startItem);
    TabHaobanExternalClerkRelated getDelFriend(@Param("wxEnterpriseId") String wxEnterpriseId , @Param("enterpriseId") String enterpriseId , @Param("staffId")String staffId) ;
    void updateHalfTime(@Param("id") String id , @Param("delTime") Date delTime) ;

    int getFriendNum(@Param("memberId")String memberId, @Param("storeId")String storeId,  @Param("clerkId")String clerkId);
}