package com.sqx.modules.chats.respository;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sqx.modules.chats.entity.Chats;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Mapper
@Component
public interface ChatsMapper extends BaseMapper<Chats> {

    @Select({"<script>" +
            "select c.*,tu.phone ,tu.user_type  from chats c left join  tb_user tu  on c.user_id =tu.user_id where c.user_id is not null " +
            " <if test='type != null and type != \"\" and type != 3 and type != 0'>"+
            "and tu.user_type =#{type} " +
            " </if>"+
            " <if test='type != null and type == 3'>"+
            "and c.is_shop =1 " +
            " </if>"+
            " <if test='starTime != null and starTime != \"\" '>"+
            "and c.create_time >#{starTime} " +
            " </if>"+
            " <if test='endTime != null and endTime != \"\" '>"+
            "and #{endTime}> c.create_time " +
            " </if>"+
            " <if test='userName != null and userName != \"\" '>"+
            "and (tu.phone like '%${userName}%' or c.user_name like '%${userName}%') " +
            " </if>"+
            "and store_id =#{storeId} " +
            "order by c.create_time desc" +
            "</script>"})
    List<Chats> findAll(Long storeId, String userName, Long type, String starTime, String endTime);

    @Select({"<script>" +
            "select c.*,tu.phone ,tu.user_type  from chats c left join  tb_user tu  on c.user_id =tu.user_id where c.user_id is not null " +
            " <if test='type != null and type != \"\" and type != 3 and type != 0'>"+
            "and tu.user_type =#{type} " +
            " </if>"+
            " <if test='type != null and type == 3'>"+
            "and c.is_shop =1 " +
            " </if>"+
            " <if test='starTime != null and starTime != \"\" '>"+
            "and c.create_time >#{starTime} " +
            " </if>"+
            " <if test='endTime != null and endTime != \"\" '>"+
            "and #{endTime}> c.create_time " +
            " </if>"+
            " <if test='userName != null and userName != \"\" '>"+
            "and (tu.phone like '%${userName}%' or c.user_name like '%${userName}%') " +
            " </if>"+
            "and store_id =#{storeId} and c.store_count =0 " +
            "order by c.create_time desc" +
            "</script>"})
    List<Chats> findRead(Long storeId, String userName, Long type, String starTime, String endTime);

    @Select({"<script>" +
            "select c.*,tu.phone ,tu.user_type  from chats c left join  tb_user tu  on c.user_id =tu.user_id where c.user_id is not null " +
            " <if test='type != null and type != \"\" and type != 3 and type != 0'>"+
            "and tu.user_type =#{type} " +
            " </if>"+
            " <if test='type != null and type == 3'>"+
            "and c.is_shop =1 " +
            " </if>"+
            " <if test='starTime != null and starTime != \"\" '>"+
            "and c.create_time >#{starTime} " +
            " </if>"+
            " <if test='endTime != null and endTime != \"\" '>"+
            "and #{endTime}> c.create_time " +
            " </if>"+
            " <if test='userName != null and userName != \"\" '>"+
            "and (tu.phone like '%${userName}%' or c.user_name like '%${userName}%') " +
            " </if>"+
            "and store_id =#{storeId}  and (c.store_count !=0 or c.store_count is null)" +
            "order by c.create_time desc" +
            "</script>"})
    List<Chats> findUnRead(Long storeId, String userName, Long type, String starTime, String endTime);
}
