package com.fs.qw.mapper;

import com.fs.qw.domain.QwAutoTagsLogs;
import com.fs.qw.param.QwAutoTagsLogsParams;
import com.fs.qw.vo.QwAutoTagsLogsVO;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 自动打标签的日志Mapper接口
 *
 * @author fs
 * @date 2024-07-20
 */
public interface QwAutoTagsLogsMapper
{
    /**
     * 查询自动打标签的日志
     *
     * @param id 自动打标签的日志主键
     * @return 自动打标签的日志
     */
    public QwAutoTagsLogs selectQwAutoTagsLogsById(Long id);

    @Select("SELECT * FROM qw_auto_tags_logs WHERE auto_tag_id= #{auto_tag_id}")
    public List<QwAutoTagsLogs> selectQwAutoTagsLogsByIdVO(Long autoTagId);

    /**
     * 查询自动打标签的日志列表
     *
     * @param qwAutoTagsLogs 自动打标签的日志
     * @return 自动打标签的日志集合
     */
    public List<QwAutoTagsLogs> selectQwAutoTagsLogsList(QwAutoTagsLogs qwAutoTagsLogs);

    @Select("<script>" +
            "SELECT\n" +
            "\tqat.*,\n" +
            "\tqec.NAME AS externalUserName,\n" +
            "\tqgc.NAME AS chatName,\n" +
            "\tqu.qw_user_name AS qwUsername,\n" +
            "\tcu.nick_name as companyName,\n" +
            "\tqec.customer_id AS customerId \n" +
            "FROM\n" +
            "\tqw_auto_tags_logs qat\n" +
            "\tLEFT JOIN qw_user qu ON qat.qw_userid = qu.id\n" +
            "\tleft JOIN company_user cu on qu.company_user_id=cu.user_id\n" +
            "\tLEFT JOIN qw_external_contact qec ON qat.external_user_id = qec.external_user_id " +
            "<if test='type ==3 '> " +
            "AND qec.qw_user_id = qat.qw_userid " +
            "</if>" +
            "left join qw_group_chat qgc ON qat.chat_id = qgc.chat_id   " +
            "        <where>  " +
            "            <if test=\"autoTagId != null \"> and qat.auto_tag_id = #{autoTagId}</if> " +
            "            <if test=\"type != null \"> and qat.type = #{type}</if> " +
            "            <if test=\"qwUserid != null  and qwUserid != ''\"> and cu.nick_name like concat(#{qwUserid}, '%') </if> " +
            "            <if test=\"externalUserId != null  and externalUserId != ''\"> and qec.name like concat( #{externalUserId}, '%') </if> " +
            "            <if test=\"effectiveRules != null  and effectiveRules != ''\"> and qat.effective_rules = #{effectiveRules}</if> " +
            "            <if test=\"addTime != null and addTime.size()!=0 \"> and qat.add_time  BETWEEN #{addTime[0]} and #{addTime[1]} </if> " +
            "            <if test=\"chatId != null  and chatId != '' and type ==2 \"> and qgc.name like concat( #{chatId}, '%')</if> " +
            "            <if test=\"joinScene != null  and joinScene != ''\"> and qat.join_scene = #{joinScene}</if> " +
            "            <if test=\"companyId != null \"> and qat.company_id = #{companyId}</if> " +
            "            <if test=\"corpId != null \"> and qat.corpId = #{corpId}</if> " +
            "        </where> " +
            "        order by qat.add_time desc " +
            "</script>")
    public List<QwAutoTagsLogsVO> selectQwAutoTagsLogsListVO(QwAutoTagsLogsParams qwAutoTagsLogs);

    /**
     * 新增自动打标签的日志
     *
     * @param qwAutoTagsLogs 自动打标签的日志
     * @return 结果
     */
    public int insertQwAutoTagsLogs(QwAutoTagsLogs qwAutoTagsLogs);
    public int insertOrUpdateQwAutoTagsLogs(QwAutoTagsLogs qwAutoTagsLogs);

    /**
     * 修改自动打标签的日志
     *
     * @param qwAutoTagsLogs 自动打标签的日志
     * @return 结果
     */
    public int updateQwAutoTagsLogs(QwAutoTagsLogs qwAutoTagsLogs);

    /**
     * 删除自动打标签的日志
     *
     * @param id 自动打标签的日志主键
     * @return 结果
     */
    public int deleteQwAutoTagsLogsById(Long id);

    /**
     * 批量删除自动打标签的日志
     *
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteQwAutoTagsLogsByIds(Long[] ids);

    @Select("select * from qw_auto_tags_logs where auto_tag_id = #{autoTagId} and type= #{type} and qw_userid = #{qwUserid} and external_user_id = #{externalUserId}  limit 1")
    public QwAutoTagsLogs selectQwAutoTagsLogsByIdLogs(QwAutoTagsLogs qwAutoTagsLogs);
}
