package com.fs.sop.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.fs.common.annotation.DataSource;
import com.fs.common.enums.DataSourceType;
import com.fs.qw.param.SopMsgParam;
import com.fs.sop.domain.QwSopLogs;
import com.fs.sop.params.*;
import com.fs.sop.vo.QwSopLogsDoSendListTVO;
import com.fs.sop.vo.QwSopLogsListCVO;
import com.fs.sop.vo.QwSopLogsListVOByUserInfo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 企业微信SOP  定时任务Mapper接口
 *
 * @author fs
 * @date 2024-08-05
 */

@Repository
public interface QwSopLogsMapper extends BaseMapper<QwSopLogs> {
    /**
     * 查询企业微信SOP  定时任务
     *
     * @param id 企业微信SOP  定时任务主键
     * @return 企业微信SOP  定时任务
     */
    @DataSource(DataSourceType.SOP)
    public QwSopLogs selectQwSopLogsById(String id);

    /**
     * 查询企业微信SOP  定时任务列表
     *
     * @param qwSopLogs 企业微信SOP  定时任务
     * @return 企业微信SOP  定时任务集合
     */
    @DataSource(DataSourceType.SOP)
    public List<QwSopLogs> selectQwSopLogsList(QwSopLogs qwSopLogs);

    /**
     * 获取单条规则 的 执行记录历史
     */
    @DataSource(DataSourceType.SOP)
    public List<QwSopLogsListCVO> selectQwSopLogsListByQwSopId(@Param("map") QwSopLogsParam param);


//    @Select("<script> " +
//            "SELECT\n" +
//            "\tcu.nick_name AS userName,\n" +
//            "\tcu.user_id AS companyUserId,\n" +
//            "\tcwu.nick_name AS externalUserName,\n" +
//            "FROM  company_user cu ON #{map.companyUserId} = cu.user_id\n" +
//            "\tLEFT JOIN company_wx_user cwu  ON cwu.weixin_id = #{map.externalUserId} \n" +
//            "<where> " +
//            "<if test='map.qwUserName != null'> AND cu.nick_name like concat('%', #{map.qwUserName}, '%')  </if> " +
//            "<if test='map.externalUserName != null'> AND cwu.nick_name like concat('%', #{map.externalUserName}, '%') </if> " +
//            "</where> " +
//            "</script>")
//    public CompanyUserVo selectQwSopLogsListByGwSopId(@Param("map") QwSopsListParamByCompanyName param);

    @DataSource(DataSourceType.SOP)
    @Select("<script> " +
        "SELECT\n" +
        "\tqst.* \n" +
        "FROM\n" +
        "\tqw_sop_logs qst\n" +
        "<where> " +
        "qst.sop_id=#{map.sopId} and qst.corp_id=#{map.corpId} " +
        "<if test='map.sendType != null'> AND qst.send_type = #{map.sendType}</if> " +
        "<if test='map.sendStatus != null'> AND qst.send_status = #{map.sendStatus}</if> " +
        "<if test='map.receivingStatus != null'> AND qst.receiving_status = #{map.receivingStatus}</if> " +
        "</where> " +
        "order by qst.send_time " +
        "</script>")
    public List<QwSopLogsListCVO> selectQwSopLogsListByGwSopId(@Param("map") QwSopLogsParam param);
    /**
     * 新增企业微信SOP  定时任务
     *
     * @param qwSopLogs 企业微信SOP  定时任务
     * @return 结果
     */
    @DataSource(DataSourceType.SOP)
    public int insertQwSopLogs(@Param("data")QwSopLogs qwSopLogs);


    /**
     * 修改企业微信SOP  定时任务
     *
     * @param qwSopLogs 企业微信SOP  定时任务
     * @return 结果
     */
    @DataSource(DataSourceType.SOP)
    public int updateQwSopLogs(@Param("data") QwSopLogs qwSopLogs);

    /**
     * 批量删除企业微信SOP  定时任务
     *
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    @DataSource(DataSourceType.SOP)
    public int deleteQwSopLogsByIds(String[] ids);

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

    @DataSource(DataSourceType.MASTER)
    @Select("<script>" +
            "SELECT cc.weixin, cc.mobile, cu.nick_name AS userName, qu.qw_user_name, qu.id AS qwId, " +
            "cu.user_id AS companyUserId, qecc.external_user_name AS externalUserName " +
            "FROM qw_user qu " +
            "LEFT JOIN company_user cu ON qu.company_user_id = cu.user_id " +
            "LEFT JOIN qw_external_contact_crm qecc ON qecc.external_user_id = #{map.externalUserId} " +
            "LEFT JOIN crm_customer cc ON qecc.customer_id = cc.customer_id " +
            "WHERE qu.qw_user_id = #{map.qwUserid}" +
            "</script>")
    QwSopLogsListVOByUserInfo selectSopSendCustomerLogsListVO(@Param("map") QwSopsListParamByExternal param1);

//    @Select("<script>" +
//            "SELECT cc.weixin, cc.mobile, cu.nick_name AS userName, qu.qw_user_name, qu.id AS qwId, " +
//            "cu.user_id AS companyUserId, qecc.external_user_name AS externalUserName, qst.* " +
//            "FROM qw_sop_logs qst " +
//            "LEFT JOIN qw_user qu ON qst.qw_userid = qu.qw_user_id " +
//            "LEFT JOIN company_user cu ON qu.company_user_id = cu.user_id " +
//            "LEFT JOIN qw_external_contact_crm qecc ON qecc.external_user_id = qst.external_user_id " +
//            "LEFT JOIN crm_customer cc ON qecc.customer_id = cc.customer_id " +
//            "<where> " +
//            "cu.user_id = #{map.userId} AND qst.corp_id = #{map.corpId} " +
//            "<if test='map.sendType != null'> AND qst.send_type = #{map.sendType}</if> " +
//            "<if test='map.sopId != null'> AND qst.sop_id = #{map.sopId}</if> " +
//            "<if test='map.sendStatus != null'> " +
//            "AND qst.send_status = #{map.sendStatus} " +
//            "</if> " +
//            "<if test='map.sendStatus != null '> AND qst.send_time &lt; NOW() </if> " +
//            "<if test='map.receivingStatus != null'> AND qst.receiving_status = #{map.receivingStatus}</if> " +
//            "</where> " +
//            "</script>")
//    List<QwSopLogsListVO> selectSopSendCustomerLogsListVO(@Param("map") QwSopParam qwSopParam);

        @DataSource(DataSourceType.SOP)
        @Select("<script>" +
            "SELECT cc.weixin, cc.mobile,cu.nick_name as userName,qu.qw_user_name, qu.id as qwId,cu.user_id as companyUserId, qecc.external_user_name as externalUserName " +
            "FROM  qw_user qu " +
            "LEFT JOIN company_user cu on qu.company_user_id=cu.user_id " +
            "LEFT JOIN qw_external_contact_crm qecc on qecc.external_user_id= #{map.externalUserId} " +
            "LEFT JOIN crm_customer cc on qecc.customer_id=cc.customer_id " +
            "where " +
            "cu.user_id=#{map.companyUserId} " +
            "</script>")
        QwSopLogsListVOByUserInfo getQwSopLogsDetailVO(@Param("map") QwSopsListParamByCompany param1);

//    @Select("<script>" +
//            "SELECT cc.weixin, cc.mobile,cu.nick_name as userName,qu.qw_user_name, qu.id as qwId,cu.user_id as companyUserId, qecc.external_user_name as externalUserName, qst.*  " +
//            "FROM " +
//            "qw_sop_logs qst " +
//            "LEFT JOIN qw_user qu ON qst.qw_userid=qu.qw_user_id " +
//            "LEFT JOIN company_user cu on qu.company_user_id=cu.user_id " +
//            "LEFT JOIN qw_external_contact_crm qecc on qecc.external_user_id=qst.external_user_id " +
//            "LEFT JOIN crm_customer cc on qecc.customer_id=cc.customer_id " +
//            "where " +
//            "cu.user_id=#{map.userId} " +
//            "and qst.corp_id=#{map.corpId} " +
//            "and qst.id= #{map.id} " +
//            "and qst.send_type=#{map.sendType}" +
//            "</script>")
//    QwSopLogsVO getQwSopLogsDetailVO(@Param("map") QwSopLogsAiParam qwSopLogsAiParam);



    /**
     * 删除企业微信SOP  定时任务
     *
     * @param id 企业微信SOP  定时任务主键
     * @return 结果
     */
    @DataSource(DataSourceType.SOP)
    public int deleteQwSopLogsById(String id);


    @DataSource(DataSourceType.SOP)
    public List<QwSopLogs> checkQwSopLogs();


    @DataSource(DataSourceType.SOP)
    public List<QwSopLogs> selectSopLogsByCreateCorpMassSending(@Param("date") String date);

    /**
    * 为了避免一直轮询无效（发送不了，给不了反馈的）数据，只查与定时的时间过后，之前3天内的的数据反馈
    */
    @DataSource(DataSourceType.SOP)
    public List<QwSopLogs> qwSopLogsResult();

    @DataSource(DataSourceType.SOP)
    public int updateQwSopLogsByWatchLogType(@Param("id") String id,@Param("remark") String remark);


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

    @DataSource(DataSourceType.SOP)
    int updateQwSopLogsBySendStatus();


    @DataSource(DataSourceType.SOP)
    int updateQwSopLogsBycollectIds(@Param("collectIds") List<String> collectIds,@Param("sendStatus") Long sendStatus,@Param("msgId") String msgId);


    @DataSource(DataSourceType.SOP)
    List<QwSopLogsDoSendListTVO> getQwSopLogsByJsApiAll(@Param("data") GetQwSopLogsByJsApiParam param);

    @DataSource(DataSourceType.SOP)
    List<QwSopLogsDoSendListTVO> getQwSopLogsByJsApi(@Param("data") GetQwSopLogsByJsApiParam param);

    @DataSource(DataSourceType.SOP)
    List<QwSopLogsDoSendListTVO> getQwSopLogsByJsApiOver(@Param("data") GetQwSopLogsByJsApiParam param);


    @DataSource(DataSourceType.SOP)
    List<QwSopLogsDoSendListTVO> getExpiredMessagesByQwSopLogs();


    @DataSource(DataSourceType.SOP)
    QwSopLogs getQwSopLogsByJsApiNext(@Param("data") GetQwSopLogsByJsApiParam param);


    @DataSource(DataSourceType.SOP)
    public boolean deleteQwSopLogsByJsApi(@Param("data") GetQwSopLogsByJsApiParam param);

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

    @DataSource(DataSourceType.SOP)
    public boolean deleteQwSopLogsByExternalUserIdList(@Param("data") DeleteQwSopParam param);

    @DataSource(DataSourceType.SOP)
    List<QwSopLogs> selectQwSopLogsListVO(SopMsgParam param);

    @DataSource(DataSourceType.SOP)
    void batchInsertQwSopLogs(@Param("qwSopLogs") List<QwSopLogs> logsToInsert);


    @DataSource(DataSourceType.SOP)
    void batchInsertQwSopLogsOneTouch(@Param("qwSopLogs") List<QwSopLogs> logsToInsert);

    @DataSource(DataSourceType.SOP)
    void batchUpdateQwSopLogsByTakeRecordsList(@Param("data")List<QwSopLogsDoSendListTVO> listTVOList);

    @DataSource(DataSourceType.SOP)
    void batchUpdateQwSopLogsBySendTime(@Param("data")List<QwSopLogsDoSendListTVO> listTVOList);

    @DataSource(DataSourceType.SOP)
    List<QwSopLogs> selectQwSopLogsByCancel();

    //批量更新过期的记录
    @DataSource(DataSourceType.SOP)
    void batchUpdateQwSopLogsByCancel(@Param("data") List<QwSopLogs> logs);


    @DataSource(DataSourceType.SOP)
    void deleteQwSopLogsByDate();

    @DataSource(DataSourceType.SOP)
    int updateQwSopLogsByDeleted(@Param("data")GetQwSopLogsByJsApiParam param);

    @DataSource(DataSourceType.SOP)
    List<QwSopLogs> getWxMsgByAccountId(@Param("accountId") long accountId);

    @DataSource(DataSourceType.SOP)
    void batchUpdateStatus(@Param("list") List<QwSopLogs> updateList);

    @DataSource(DataSourceType.SOP)
    void batchUpdateStatusByQwResult(@Param("list") List<QwSopLogs> updateList);

    @DataSource(DataSourceType.SOP)
    List<QwSopLogs> selectSopLogsByCreateCorpMassSendResult();
    @DataSource(DataSourceType.SOP)
    List<QwSopLogsListCVO> selectQwSopLogsListByChatSopId(@Param("map") QwSopLogsParam param);
}
