package com.fs.sop.mapper;

import com.fs.common.annotation.DataSource;
import com.fs.common.enums.DataSourceType;
import com.fs.sop.domain.SopUserLogsInfo;
import com.fs.sop.params.*;
import com.fs.sop.vo.SopUserLogsInfoVOE;
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;

@Repository
public interface SopUserLogsInfoMapper {
    /**
     * 插入一条记录
     * @param sopUserLogsInfo 实体对象
     * @return 影响行数
     */
    @DataSource(DataSourceType.SOP)
    int insertSopUserLogsInfo(SopUserLogsInfo sopUserLogsInfo);

    /**
     * 插入一条记录
     * @param sopUserLogsInfo 实体对象
     * @return 影响行数
     */
    @DataSource(DataSourceType.SOP)
    int insertChatSopUserLogsInfo(SopUserLogsInfo sopUserLogsInfo);

    /**
     * 根据ID查询记录
     * @param id 主键ID
     * @return 查询结果
     */
    @DataSource(DataSourceType.SOP)
    SopUserLogsInfo selectById(String id);

    /**
     * 查询所有记录
     * @return 记录列表
     */
    @DataSource(DataSourceType.SOP)
    List<SopUserLogsInfo> selectAll();

    /**
     * 根据ID删除记录
     * @param id 主键ID
     * @return 影响行数
     */
    @DataSource(DataSourceType.SOP)
    int deleteById(String id);

    @DataSource(DataSourceType.SOP)
    int deleteSopUserLogsInfoByIds(String[] ids);

    /**
    * 根据员工id删除
    */
    @DataSource(DataSourceType.SOP)
    int deleteByQwUserIdAndCorpIdToContactId(@Param("qwUserId") String qwUserId, @Param("corpId") String corpId, @Param("externalContactId") String externalContactId);


    @DataSource(DataSourceType.SOP)
    int deleteByQwUserIdAndCorpIdToContactIdByChat(@Param("qwUserId") String qwUserId, @Param("corpId") String corpId, @Param("externalContactId") String externalContactId);


    /**
     * 根据员工id删除
     */
    @DataSource(DataSourceType.SOP)
    int deleteByQwUserIdAndCorpIdToContactIdList(@Param("data") DeleteQwSopParam param);


    @DataSource(DataSourceType.SOP)
    int deleteByQwUserIdAndCorpIdToContactIdByChatList(@Param("data") DeleteQwSopParam param);


    /**
     * 根据sop任务-标签-删除对应的客户的营期-
     */
    @DataSource(DataSourceType.SOP)
    int deleteBySopIdToContactId(@Param("data") SopUserLogsInfoDelParam logsInfoDelParam);

    @DataSource(DataSourceType.SOP)
    int deleteBySopIdToContactIdChat(@Param("data") SopUserLogsInfoDelParam logsInfoDelParam);


    /**
    * 批量删除
    */
    @DataSource(DataSourceType.SOP)
    int deleteSopUserLogsInfoBySopIds(@Param("sopIds") String[] sopIds);


    /**
     * 根据ID修改记录
     * @return 影响行数
     */
    @DataSource(DataSourceType.SOP)
    int updateById(SopUserLogsInfo info);

    /**
     * 查询某个SOP的任务的营期的客户列表
     * @return 记录列表
     */
    @DataSource(DataSourceType.SOP)
    List<SopUserLogsInfo> selectSopUserLogsInfoList(SopUserLogsInfo info);

    @DataSource(DataSourceType.SOP)
    List<SopUserLogsInfo> selectSopUserLogsInfoListByIsRegister(@Param("data") SopUserLogsInfoParam logsInfoParam);

    @DataSource(DataSourceType.SOP)
    @Select("<script> SELECT li.*,ul.start_time FROM sop_user_logs_info li left join  sop_user_logs ul on li.user_logs_id=ul.id  " +
            "        <where>\n" +
            "            <if test=\"data.sopId != null\">and li.sop_id = #{data.sopId}</if>\n" +
            "            <if test=\"data.userLogsId != null\">and li.user_logs_id = #{data.userLogsId}</if>\n" +
            "            <if test=\"data.externalContactId != null\">and li.external_contact_id = #{data.externalContactId}</if>\n" +
            "            <if test=\"data.qwUserId != null\">and li.qw_user_id = #{data.qwUserId}</if>\n" +
            "            <if test=\"data.corpId != null\">and li.corp_id = #{data.corpId}</if>\n" +
            "            <if test=\"data.externalId != null\">and li.external_id = #{data.externalId}</if>\n" +
            "            <if test=\"data.fsUserId != null\">and li.fs_user_id = #{data.fsUserId}</if>\n" +
            "            <if test=\"data.externalUserName != null\">and li.external_user_name like CONCAT('%', #{data.externalUserName}, '%')</if>\n" +
            "            <if test=\"data.createTime != null\">and li.create_time = #{data.createTime}</if>\n" +
            "            <if test=\"data.crtTime != null\">and li.crt_Time = #{data.crtTime}</if>\n" +
            "            <if test=\"data.updateTime != null\">and li.updateTime = #{data.updateTime}</if>\n" +
            "        </where>" +
            "</script>")
    List<SopUserLogsInfoVOE> selectSopUserLogsInfoListVO(@Param("data") SopUserLogsInfo info);


    @DataSource(DataSourceType.SOP)
    SopUserLogsInfo selectSopUserLogsInfo(SopUserLogsInfo info);

    @DataSource(DataSourceType.SOP)
    SopUserLogsInfo selectSopUserLogsInfoByCheckJobTransfer(SopUserLogsInfo info);

    @DataSource(DataSourceType.SOP)
    SopUserLogsInfo selectChatSopUserLogsInfo(SopUserLogsInfo info);

    @DataSource(DataSourceType.SOP)
    List<SopUserLogsInfo> selectSopUserLogsInfoListBySopId(@Param("sopId") String sopId, @Param("userLogsId") String userLogsId);



    @DataSource(DataSourceType.SOP)
    void batchInsertSopUserLogsInfo(@Param("SopUserLogsInfo") List<SopUserLogsInfo> logsToInsert);

    @DataSource(DataSourceType.SOP)
    void batchUpdateSopUserLogsInfoToTime(BatchSopUserLogsInfoParamU paramU);

    /**
     * 查询sopUserLogsInfo
     *
     * @param id sopUserLogsInfo主键
     * @return sopUserLogsInfo
     */
    @DataSource(DataSourceType.SOP)
    public SopUserLogsInfo selectSopUserLogsInfoById(String id);

    @DataSource(DataSourceType.SOP)
    public List<SopUserLogsInfo> selectSopUserLogsInfoByIds(@Param("ids") String[] ids);

    @DataSource(DataSourceType.SOP)
    public List<SopUserLogsInfo> selectSopUserLogsInfoByIdsHasUserId(SopUserLogsInfoByIdsHasUserIdParam param);

    /**
     * 修改sopUserLogsInfo
     *
     * @param sopUserLogsInfo sopUserLogsInfo
     * @return 结果
     */
    @DataSource(DataSourceType.SOP)
    public int updateSopUserLogsInfoToTime(@Param("data") SopUserLogsInfo sopUserLogsInfo);

    @DataSource(DataSourceType.SOP)
    List<SopUserLogsInfo> selectChatSopUserLogsByQwUserId(@Param("qwUserId")String qwUserId,@Param("corpId") String corpId);

    @DataSource(DataSourceType.SOP)
    List<SopUserLogsInfo> selectEerGroup();

    @DataSource(DataSourceType.SOP)
    List<SopUserLogsInfo> selectSopUserLogsInfoListByUserLogsId(String id);

    @DataSource(DataSourceType.SOP)
    @Update("update sop_user_logs_info set fs_user_id=null where external_id =#{externalId}")
    int updateQwExternalContactUnBindUserId(@Param("externalId") Long externalId);

    @DataSource(DataSourceType.SOP)
    @Update("update sop_user_logs_info set fs_user_id=#{fsUserId} where external_id =#{externalId}")
    int updateQwExternalContactChangeUserId(@Param("externalId") Long externalId,@Param("fsUserId") Long fsUserId);

    @DataSource(DataSourceType.SOP)
    @Select("SELECT external_id  FROM `sop_user_logs_info` where sop_id = #{sopId}  and Date(create_time) = Date(#{minDay})")
    List<SopUserLogsInfo> selectDayBySopId(@Param("sopId")String sopId, @Param("minDay")String minDay);

    @DataSource(DataSourceType.SOP)
    @Select("SELECT external_id,create_time  FROM sop_user_logs_info where sop_id = #{sopId} ")
    List<SopUserLogsInfo> selectSopUserLogsInfoBySopId(@Param("sopId")String sopId);

    @DataSource(DataSourceType.SOP)
    @Select("SELECT create_time,fs_user_id  FROM sop_user_logs_info where sop_id = #{sopId} ")
    List<SopUserLogsInfo> selectFsUserIdSopUserLogsInfoBySopId(@Param("sopId")String sopId);

    @DataSource(DataSourceType.SOP)
    List<SopUserLogsInfo> repeatProject(@Param("projects") List<Integer> projects, @Param("externalUserID") String externalUserID);
    @DataSource(DataSourceType.SOP)
    @Select("SELECT id  FROM sop_user_logs_info where external_id = #{extId} ")
    List<String> selectSopUserLogsInfoByExtId(@Param("extId")Long extId );
    @DataSource(DataSourceType.SOP)
    void updateSopUserLogsInfoFsUserIdById(@Param("data")List<String> list,  @Param("userId") Long userId);
}
