package com.fs.sop.mapper;

import com.fs.common.annotation.DataSource;
import com.fs.common.enums.DataSourceType;
import com.fs.qw.param.SopUserLogsVO;
import com.fs.qw.vo.UpdateSopUserLogDateVo;
import com.fs.sop.domain.SopUserLogs;
import com.fs.sop.domain.SopUserLogsInfo;
import com.fs.sop.params.*;
import com.fs.sop.vo.ReplaceUserDto;
import com.fs.sop.vo.SopUserLogsInfoVo;
import com.fs.sop.vo.SopUserLogsVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
public interface SopUserLogsMapper {

    @DataSource(DataSourceType.SOP)
    public int insertSopUserLogsByList(@Param("data") SopUserLogsList userLogsList);

    @DataSource(DataSourceType.SOP)
    public int deleteSopUserLogsById(String id);

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

    @DataSource(DataSourceType.SOP)
    public int insertSopUserLogsByDate(@Param("data") SopUserLogsParamByDate logsParamByDate);

    @DataSource(DataSourceType.SOP)
    public int updateSopUserLogsByStatus(@Param("data") SopUserLogs sopUserLogs);

//    @DataSource(DataSourceType.SOP)
//    public int updateSopUserLogsDistinct(@Param("data") SopUserLogs sopUserLogs);

//    @DataSource(DataSourceType.SOP)
//    public int updateSopUserLogsByexternalContactId(@Param("data") SopUserLogsUpdateList userLogsUpdateList);

    @DataSource(DataSourceType.SOP)
    public int deleteSopUserLogsBySopId(@Param("sopId") String sopId);

    @DataSource(DataSourceType.SOP)
    public int deleteSopUserLogsByRemoveSop(@Param("sopId") String sopId,@Param("qwUserId") String qwUserId , @Param("corpId") String corpId);

    @DataSource(DataSourceType.SOP)
    public int updateSopUserLogsByTempId(@Param("sopTempId") String sopTempId,@Param("sopId") String sopId);

//    @DataSource(DataSourceType.SOP)
//    int updateSopUserLogsDistinctByList(@Param("data") SopUserLogsArray userLogsArray);

    @DataSource(DataSourceType.SOP)
    public List<SopUserLogsVo> selectSopUserLogsListByTime(@Param("sopIds") List<String> sopidList);

    @DataSource(DataSourceType.SOP)
    public List<SopUserLogs> meetsTheRatingByUserInfo();

    @DataSource(DataSourceType.SOP)
    public List<SopUserLogs> meetsTheRatingByUserInfoWithPagination(@Param("offset") int offset,@Param("pageSize") int pageSize);

    @DataSource(DataSourceType.SOP)
    public List<SopUserLogs> meetsTheRatingByUserInfoWithPaginationStudyDays(@Param("offset") int offset,@Param("pageSize") int pageSize,@Param("notStudyDays") Integer notStudyDays);

    @DataSource(DataSourceType.SOP)
    public List<SopUserLogs> meetsTherestoreByIsDaysNotStudy(@Param("offset") int offset,@Param("pageSize") int pageSize,@Param("notStudyDays") Integer notStudyDays);


    @DataSource(DataSourceType.SOP)
    public List<SopUserLogs> meetsTheRatingByUserInfoBySopId(@Param("sopId") String sopId);


    @DataSource(DataSourceType.SOP)
    public List<SopUserLogsVo> selectSopUserLogsListByTest();

    /**
     * 根据时间查询sopUserInfoLogs
     * @return
     */
    @DataSource(DataSourceType.SOP)
    public List<SopUserLogsInfoVo> selectSopUserInfoLogsListByTime();

    @DataSource(DataSourceType.SOP)
    public String selectSopUserLogsByUnionSopId(@Param("data") SopUserLogs sopUserLogs) ;

    @DataSource(DataSourceType.SOP)
    public String selectSopUserLogsByUpdate(@Param("data") SopUserLogs sopUserLogs) ;

    @DataSource(DataSourceType.SOP)
    int  deleteSopUserLogsBySopIds(@Param("sopIds") String[] sopIds) ;

    @DataSource(DataSourceType.SOP)
    List<SopUserLogsInfoVo> selectSopUserLogsInfoListByTime();


    @DataSource(DataSourceType.SOP)
    List<SopUserLogsVO> selectSopUserLogsListBySopId(@Param("data") SopUserLogs sopUserLogs);

    @DataSource(DataSourceType.SOP)
    List<SopUserLogs> selectSopUserLogsList(SopUserLogs sopUserLogs);

    /**
     * 查询sopUserLogs
     *
     * @param id sopUserLogs主键
     * @return sopUserLogs
     */
    @DataSource(DataSourceType.SOP)
    public SopUserLogs selectSopUserLogsById(String id);


    @DataSource(DataSourceType.SOP)
    @Select("<script>" +
            "select li.sop_id,li.external_contact_id,li.qw_user_id,li.corp_id,li.external_id,li.external_user_name,li.fs_user_id,ul.start_time,li.is_days_not_study " +
            "from sop_user_logs ul  " +
            "INNER join  sop_user_logs_info li on ul.id=li.user_logs_id " +
            "        WHERE ul.id IN\n" +
            "        <foreach collection=\"ids\" item=\"id\" open=\"(\" separator=\",\" close=\")\">\n" +
            "            #{id}\n" +
            "        </foreach> " +
            "</script>")
    public List<SopUserLogsInfo> selectSopUserLogsByIds(@Param("ids") String[] ids);

    @DataSource(DataSourceType.SOP)
    public List<SopUserLogs> selectSopUserLogsBySopId(@Param("sopId") String sopId);



    @DataSource(DataSourceType.SOP)
    List<String> selectSopUserLogsByDate(@Param("scheduleStartTime") String scheduleStartTime, @Param("scheduleEndTime") String scheduleEndTime);

    @DataSource(DataSourceType.SOP)
    List<SopUserLogsVO> selectQwSopLogsMessSendList(@Param("sopIds") List<String> sopIds);

    @DataSource(DataSourceType.SOP)
    void updateSopUserLogsByStartTime(@Param("data") SopUserLogs param);

    @DataSource(DataSourceType.SOP)
    void updateSopUserLogsByCompanyInfo(@Param("data") SopUserLogs param);

    @DataSource(DataSourceType.SOP)
    List<SopUserLogs> repairSopUserLogsList();

    @DataSource(DataSourceType.SOP)
    SopUserLogs selectSopUserLogsByDateAndIds(SopUserLogsParamByDate sopUserLogs);

//    @DataSource(DataSourceType.SOP)
//    @Select({"<script> " +
//            "SELECT DISTINCT\n" +
//            "            l.id,\n" +
//            "            l.sop_id,\n" +
//            "            l.sop_temp_id,\n" +
//            "            l.qw_user_id,\n" +
//            "            l.corp_id,\n" +
//            "            l.chat_id,\n" +
//            "            l.start_time,\n" +
//            "            l.status,\n" +
//            "            l.user_id,\n" +
//            "            DATEDIFF(CURRENT_DATE, l.start_time) + 1 AS count_days\n" +
//            "        FROM sop_user_logs l " +
//            "inner join sop_user_logs_info i on l.id = i.user_logs_id "  +
//            "where 1=1 " +
//            "<if test = ' maps.id!=null '> " +
//            "and l.id = #{maps.id} " +
//            "</if>" +
//            "<if test = ' maps.sopId != null '> " +
//            "and l.sop_id = #{maps.sopId} " +
//            "</if>" +
//            "<if test = ' maps.sopTempId != null '> " +
//            "and l.sop_temp_id = #{maps.sopTempId} " +
//            "</if>" +
//            "<if test = ' maps.qwUserId!=null  '> " +
//            "and l.qw_user_id = #{maps.qwUserId} " +
//            "</if>" +
//            "<if test = ' maps.startTime!=null  '> " +
//            "and l.start_time = #{maps.startTime} " +
//            "</if>" +
//            "<if test = ' maps.status!=null  '> " +
//            "and l.status = #{maps.status} " +
//            "</if>" +
//            "<if test = ' maps.externalUserName!=null and maps.externalUserName != \"\"  '> " +
//            "and i.external_user_name like concat('%', #{maps.externalUserName}, '%') " +
//            "</if>" +
//            "order by start_time desc" +
//            "</script>"})
//    List<SopUserLogsVO> selectSopUserLogsListByParam(@Param("maps") SopUserLogsParam param);

    @DataSource(DataSourceType.SOP)
    @Select({"<script> " +
            "SELECT DISTINCT\n" +
            "            l.id,\n" +
            "            l.sop_id,\n" +
            "            l.sop_temp_id,\n" +
            "            l.qw_user_id,\n" +
            "            l.actual_qw_user_id,\n" +
            "            l.chat_id,\n" +
            "            l.corp_id,\n" +
            "            l.start_time,\n" +
            "            l.status,\n" +
            "            l.user_id,\n" +
            "            DATEDIFF(CURRENT_DATE, l.start_time) + 1 AS count_days\n" +
            "        FROM sop_user_logs l " +
            "inner join sop_user_logs_info i on l.id = i.user_logs_id "  +
            "where 1=1 " +
            "<if test = ' maps.id!=null '> " +
            "and l.id = #{maps.id} " +
            "</if>" +
            "<if test = ' maps.sopId != null '> " +
            "and l.sop_id = #{maps.sopId} " +
            "</if>" +
            "<if test = ' maps.sopTempId != null '> " +
            "and l.sop_temp_id = #{maps.sopTempId} " +
            "</if>" +
            "<if test = ' maps.qwUserId!=null  '> " +
            "and l.qw_user_id = #{maps.qwUserId} " +
            "</if>" +
            "<if test = ' maps.startTime!=null  '> " +
            "and l.start_time = #{maps.startTime} " +
            "</if>" +
            "<if test = ' maps.status!=null  '> " +
            "and l.status = #{maps.status} " +
            "</if>" +
            "<if test = ' maps.externalId!=null  '> " +
            "and i.external_id = #{maps.externalId} " +
            "</if>" +
            "<if test = ' maps.externalUserName!=null and maps.externalUserName != \"\"  '> " +
            "and i.external_user_name like concat('%', #{maps.externalUserName}, '%') " +
            "</if>" +
            "<if test=\"maps.qwUseridList != null and maps.qwUseridList.size() > 0\">\n" +
            "    AND l.qw_user_id IN\n" +
            "    <foreach collection=\"maps.qwUseridList\" item=\"item\" open=\"(\" separator=\",\" close=\")\">\n" +
            "        #{item}\n" +
            "    </foreach>\n" +
            "</if>" +
            "<if test=\"maps.type==2 and maps.qwIdsList != null and !maps.qwIdsList.isEmpty() \">\n" +
            "    AND l.qw_user_id IN\n" +
            "    <foreach collection=\"maps.qwIdsList\" item=\"item\" open=\"(\" separator=\",\" close=\")\">\n" +
            "        #{item}\n" +
            "    </foreach>\n" +
            "</if>" +
            "order by start_time desc" +
            "</script>"})
    List<SopUserLogsVO> selectSopUserLogsListByParam(@Param("maps") SopUserLogsParam param);

//    @DataSource(DataSourceType.SOP)
//    @Select({"<script> " +
//            "SELECT DISTINCT\n" +
//            "            l.id,\n" +
//            "            l.sop_id,\n" +
//            "            l.sop_temp_id,\n" +
//            "            l.qw_user_id,\n" +
//            "            l.corp_id,\n" +
//            "            l.chat_id,\n" +
//            "            l.start_time,\n" +
//            "            l.status,\n" +
//            "            l.user_id,\n" +
//            "            DATEDIFF(CURRENT_DATE, l.start_time) + 1 AS count_days\n" +
//            "        FROM sop_user_logs l " +
//            "left join sop_user_logs_info i on l.id = i.user_logs_id "  +
//            "where 1=1 " +
//            "<if test = ' maps.id!=null '> " +
//            "and l.id = #{maps.id} " +
//            "</if>" +
//            "<if test = ' maps.sopId != null '> " +
//            "and l.sop_id = #{maps.sopId} " +
//            "</if>" +
//            "<if test = ' maps.sopTempId != null '> " +
//            "and l.sop_temp_id = #{maps.sopTempId} " +
//            "</if>" +
//            "<if test = ' maps.qwUserId!=null  '> " +
//            "and l.qw_user_id = #{maps.qwUserId} " +
//            "</if>" +
//            "<if test = ' maps.startTime!=null  '> " +
//            "and l.start_time = #{maps.startTime} " +
//            "</if>" +
//            "<if test = ' maps.status!=null  '> " +
//            "and l.status = #{maps.status} " +
//            "</if>" +
//            "<if test = ' maps.externalUserName!=null and maps.externalUserName != \"\"  '> " +
//            "and i.external_user_name like concat('%', #{maps.externalUserName}, '%') " +
//            "</if>" +
//            "order by start_time desc" +
//            "</script>"})
//    List<SopUserLogsVO> selectSopUserLogsGroupListByParam(@Param("maps") SopUserLogsParam param);
@DataSource(DataSourceType.SOP)
@Select({"<script> " +
        "SELECT DISTINCT\n" +
        "            l.id,\n" +
        "            l.sop_id,\n" +
        "            l.sop_temp_id,\n" +
        "            l.qw_user_id,\n" +
        "            l.actual_qw_user_id,\n" +
        "            l.corp_id,\n" +
        "            l.chat_id,\n" +
        "            l.start_time,\n" +
        "            l.status,\n" +
        "            l.user_id,\n" +
        "            DATEDIFF(CURRENT_DATE, l.start_time) + 1 AS count_days\n" +
        "        FROM sop_user_logs l " +
        "left join sop_user_logs_info i on l.id = i.user_logs_id "  +
        "where 1=1 " +
        "<if test = ' maps.id!=null '> " +
        "and l.id = #{maps.id} " +
        "</if>" +
        "<if test = ' maps.sopId != null '> " +
        "and l.sop_id = #{maps.sopId} " +
        "</if>" +
        "<if test = ' maps.sopTempId != null '> " +
        "and l.sop_temp_id = #{maps.sopTempId} " +
        "</if>" +
        "<if test = ' maps.qwUserId!=null  '> " +
        "and l.qw_user_id = #{maps.qwUserId} " +
        "</if>" +
        "<if test = ' maps.startTime!=null  '> " +
        "and l.start_time = #{maps.startTime} " +
        "</if>" +
        "<if test = ' maps.status!=null  '> " +
        "and l.status = #{maps.status} " +
        "</if>" +
        "<if test=\" maps.type==2 and maps.qwIdsList != null and maps.qwIdsList.size() > 0  \">\n" +
        "    AND l.qw_user_id IN\n" +
        "    <foreach collection=\"maps.qwIdsList\" item=\"item\" open=\"(\" separator=\",\" close=\")\">\n" +
        "        #{item}\n" +
        "    </foreach>\n" +
        "</if>" +
        "order by start_time desc" +
        "</script>"})
List<SopUserLogsVO> selectSopUserLogsGroupListByParam(@Param("maps") SopUserLogsParam param);

    @DataSource(DataSourceType.SOP)
    void batchInsertSopUserLogs(@Param("list") List<SopUserLogs> list);

    @DataSource(DataSourceType.SOP)
    List<String> selectSopUserLogsByQwUserIds(@Param("qwUserIds") List<String> qwUserIdList);

    /**
     *  未自动创建营期 开启的 (相当于固定营期)
     *  否则按进线时间每天创建
     * @return
     */
    @DataSource(DataSourceType.SOP)
    @Select(" SELECT log.id, log.qw_user_id,log.sop_id,log.corp_id, qw_sop.is_auto_sop,log.start_time\n" +
            "            FROM sop_user_logs log\n" +
            "            LEFT JOIN qw_sop ON log.sop_id = qw_sop.id\n" +
            "            WHERE \n" +
            "              log.status = 1\n" +
            "             AND qw_sop.status in(2,3)")
    List<SopUserLogs> queryAllSopUserLogs();

    @DataSource(DataSourceType.SOP)
    List<SopUserLogs> selectSopUserLogsListByIds(@Param("ids") List<String> ids);

    @DataSource(DataSourceType.SOP)
    void updateSopuserLogsDateById(UpdateSopUserLogDateVo vo);

    @DataSource(DataSourceType.SOP)
    List<SopUserLogs> querySopUserLogsByParam(GetSOPTaskDataParam param);

    @DataSource(DataSourceType.SOP)
    List<SopUserLogs> getSopUserLogsInfoById(@Param("ids") String[] ids);

    @DataSource(DataSourceType.SOP)
    @Select({"<script> " +
            "SELECT id FROM sop_user_logs " +
            "WHERE corp_id = #{param.corpId} " +
            "and sop_id in" +
            "<foreach  item='item' index='index' collection='param.sopIds' open='(' separator=',' close=')'> #{item}    </foreach>" +
            "<if test=\"param.qwUserIds != null and param.qwUserIds.size > 0\">" +
            "and qw_user_id in" +
            "<foreach  item='item' index='index' collection='param.qwUserIds' open='(' separator=',' close=')'> #{item}    </foreach>" +
            "</if>" +
            "</script>"})
    List<String> getSopUserLogsIds(@Param("param")SendUserLogsInfoMsgParam param);

    @DataSource(DataSourceType.SOP)
    @Select("<script>" +
            "UPDATE sop_user_logs " +
            "SET actual_qw_user_id = " +
            "<choose>" +
            "  <when test='vo.actualQwUserId != null'>#{vo.actualQwUserId}</when>" +
            "  <otherwise>null</otherwise>" +
            "</choose>" +
            ", actual_qw_id = " +
            "<choose>" +
            "  <when test='vo.actualQwId != null'>#{vo.actualQwId}</when>" +
            "  <otherwise>null</otherwise>" +
            "</choose>" +
            "WHERE id IN " +
            "<foreach collection='vo.ids' item='id' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    void replaceUser(@Param("vo") ReplaceUserDto vo);

    @DataSource(DataSourceType.SOP)
    @Select("<script>" +"SELECT * FROM sop_user_logs WHERE sop_id = #{sopId} AND qw_user_id = #{qwUserId} " +
            "<if test=\"chatId != null and chatId != ''\"> and chat_id = #{chatId} </if>" +
            " limit 1 " +
            "</script>")
    SopUserLogs queryUserBySopId(@Param("qwUserId")String qwUserId,@Param("sopId") String sopId,@Param("chatId") String chatId);
}
