package shop.alien.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import shop.alien.entity.store.LifeMessage;
import shop.alien.entity.store.vo.LifeFansVo;
import shop.alien.entity.store.vo.LifeMessageVo;

import java.util.List;

/**
 * 消息
 */
@Mapper
public interface LifeMessageMapper extends BaseMapper<LifeMessage> {

    @Select("select id, user_name userName, user_image userImage, concat('user_', user_phone) phoneId " +
            "from life_user " +
            "where delete_flag = 0 and user_phone != '' and user_phone in (${userPhones})  " +
            "union " +
            "select info.id, user.nick_name userName, user.head_img userImage, concat('store_', user.phone) phoneId " +
            "from store_user user " +
            "join store_info info on info.id = user.store_id " +
            "left join store_img img on img.store_id = info.id and img.img_type = '10' and img.delete_flag = 0 " +
            "where user.delete_flag = 0 and info.delete_flag = 0 and user.phone != '' and phone in (${storePhones}) ")
    List<LifeMessageVo> getLifeUserAndStoreUserByPhone(@Param("storePhones") String storePhones, @Param("userPhones") String userPhones);

    @Select("with message_num as ( " +
            "    with message as ( " +
            "        select id, type, content, created_time, if(sender_id = #{phoneId}, receiver_id, sender_id) phoneId, sender_id, receiver_id, is_read " +
            "        from life_message " +
            "        where delete_flag = 0 and (sender_id = #{phoneId} or receiver_id = #{phoneId}) " +
            "               and (instr(delete_phone_id, #{phoneId}) is null or instr(delete_phone_id, #{phoneId}) = 0)" +
            "    ) " +
            "    select *, row_number() over (partition by phoneId order by created_time desc ) num, " +
            "           substring_index(phoneId, '_', 1) flag, substring_index(phoneId, '_', -1) phone " +
            "    from message " +
            ") " +
            "select message.id, message.type, message.phoneId, message.content, message.created_time createdTime, message.is_read, " +
            "case "+
            "when message.flag = 'user' then user.user_name "+
            "when message.flag = 'store' then suser.nick_name "+
            "when message.flag = 'lawyer' then luser.name "+
            "else '' "+
            "end as userName, "+
            "case "+
            "when INSTR(  message.phoneId, 'user' ) > 0 then user.user_image "+
            "when INSTR(  message.phoneId, 'store' ) > 0 then suser.head_img "+
            "when INSTR(  message.phoneId, 'lawyer' ) > 0 then luser.head_img "+
            "else ''  "+
            "end as user_image, "+
            "case "+
            "when message.flag = 'user' then user.id "+
            "when message.flag = 'store' then suser.id "+
            "when message.flag = 'lawyer' then luser.id "+
            "else '' "+
            "end as userId "+
            "from message_num message " +
            "left join life_user user on message.flag = 'user' and message.phone = user.user_phone and user.delete_flag = 0 " +
            "left join store_user suser on message.flag = 'store' and message.phone = suser.phone and suser.delete_flag = 0 " +
            "left join lawyer_user luser on message.flag = 'lawyer' and message.phone = luser.phone and luser.delete_flag = 0 "+
            "left join store_img img on img.store_id = suser.store_id and img.img_type = '10' and img.delete_flag = 0 " +
            "${ew.customSqlSegment}")
    List<LifeMessageVo> getLifeMessagePageByPhoneId(@Param("phoneId") String phoneId, @Param(Constants.WRAPPER) QueryWrapper<LifeFansVo> dynamicsWrapper);

    @Update("update life_message set delete_phone_id = (if(delete_phone_id is null, #{receiverId}, concat(delete_phone_id, ',', #{receiverId}))) " +
            "where ((receiver_id = #{senderId} and sender_id = #{receiverId}) or (sender_id = #{senderId} and receiver_id = #{receiverId})) " +
            "   and (instr(delete_phone_id, #{receiverId}) is null or instr(delete_phone_id, #{receiverId}) = 0)")
    int deleteMessageByPhoneId(@Param("senderId") String senderId, @Param("receiverId") String receiverId);

    @Select("select lm.id,lm.updated_time,lm.created_user_id,lm.receiver_name,lm.is_read,lm.type,lm.content,lm.current_time,lm.delete_flag," +
            "       lm.sender_id,lm.sender_name,lm.receiver_id,lm.delete_phone_id,lm.created_time,lm.updated_user_id,lu.user_image " +
            "from life_message lm " +
            "left join life_user lu on SUBSTRING(lm.sender_id, INSTR(lm.sender_id, '_') + 1) = lu.user_phone " +
            "where lm.delete_flag = 0 " +
            "   and (((lm.receiver_id = #{senderId} and lm.sender_id = #{receiverId}) or (lm.sender_id = #{senderId} and lm.receiver_id = #{receiverId}))) " +
            "   and (instr(delete_phone_id, #{receiverId}) is null or instr(delete_phone_id, #{receiverId}) = 0) " +
            "order by lm.created_time asc ")
    List<LifeMessageVo> getMessageListByReceiverId(@Param("receiverId") String receiverId, @Param("senderId") String senderId);

    @Select("select user.id, user.user_name name, user.user_image image, concat('user_', user.user_phone) phoneId, user.jianjie blurb " +
            "from life_user user " +
            "where user_phone in ( " +
            "    select substring_index(sender_id, '_', -1) phone from life_message " +
            "    where sender_id in " +
            "          (select receiver_id from life_message " +
            "            where sender_id = #{senderId} and receiver_id != #{senderId} and delete_flag = 0 " +
            "            and left(receiver_id, 5) = 'user_' and receiver_id not in ( " +
            "                    select blocked_phone_id from life_blacklist " +
            "                    where blocker_phone_id = #{senderId} and delete_flag = 0 " +
            "                ) " +
            "            group by receiver_id) " +
            "    and receiver_id = #{senderId} and delete_flag = 0 " +
            "    group by sender_id) " +
            "and delete_flag = 0 ")
    List<LifeFansVo> getTalkedUserList(@Param("senderId") String senderId);
}