package com.sqx.modules.gameChat.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.PageUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.gameChat.dao.ChatContentTcwmDao;
import com.sqx.modules.gameChat.entity.ChatContentTcwm;
import com.sqx.modules.gameChat.service.ChatContentTcwmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service
public class ChatContentTcwmServiceImpl extends ServiceImpl<ChatContentTcwmDao, ChatContentTcwm> implements ChatContentTcwmService {

    @Autowired
    private ChatContentTcwmDao chatContentTcwmDao;

    @Override
    public PageUtils selectChatContentPage(Integer page, Integer limit, Long chatConversationId, String content) {
        return new PageUtils(baseMapper.selectChatContentPage(new Page<>(page,limit),chatConversationId,content));
    }

    @Override
    public int updateChatContentStatusByUserIdAndChatId(Long userId,Long chatConversationId){
        return baseMapper.updateChatContentStatusByUserIdAndChatId(userId, chatConversationId);
    }

    @Override
    public int selectChatCount(Long userId) {
        return baseMapper.selectChatCount(userId);
    }

    @Override
    public Result selectOrdersChatPage(Integer page, Integer limit, Long userId, Long riderId, Long shopId) {
        Page<ChatContentTcwm> pages = new Page(page, limit);
        IPage<ChatContentTcwm> chatContentTcwmIPage = chatContentTcwmDao.selectOrdersChatPage(pages, userId, riderId, shopId);
        for(int i = 0;i<chatContentTcwmIPage.getRecords().size();i++){
            if(userId!=null){
                int userUnread = chatContentTcwmDao.selectUnreadCount(chatContentTcwmIPage.getRecords().get(i).getOrdersId(), 1);
                chatContentTcwmIPage.getRecords().get(i).setUserUnread(userUnread);
            }
            if(riderId!=null){
                int riderUnread = chatContentTcwmDao.selectUnreadCount(chatContentTcwmIPage.getRecords().get(i).getOrdersId(), 2);
                chatContentTcwmIPage.getRecords().get(i).setRiderUnread(riderUnread);
            }
            if(shopId!=null){
                int shopUnread = chatContentTcwmDao.selectUnreadCount(chatContentTcwmIPage.getRecords().get(i).getOrdersId(), 3);
                chatContentTcwmIPage.getRecords().get(i).setShopUnread(shopUnread);
            }
        }
        PageUtils pageUtils = new PageUtils(chatContentTcwmIPage);
        return Result.success().put("data", pageUtils);
    }

    @Override
    public Result selectOrdersChatList(Integer page, Integer limit, String userName) {
        Page<ChatContentTcwm> pages = new Page(page, limit);
        IPage<ChatContentTcwm> chatContentTcwmIPage = chatContentTcwmDao.selectOrdersChatList(pages,userName);
        PageUtils pageUtils = new PageUtils(chatContentTcwmIPage);
        return Result.success().put("data", pageUtils);
    }

    @Override
    public Result selectGameChatDetails(Integer page, Integer limit,Long ordersId, Integer type){
        chatContentTcwmDao.updateRead(ordersId, type);
        Page<ChatContentTcwm> pages = new Page<>(page, limit);
        IPage<ChatContentTcwm> chatContentTcwmIPage = chatContentTcwmDao.selectGameChatDetails(pages, ordersId);
        PageUtils pageUtils = new PageUtils(chatContentTcwmIPage);
        return Result.success().put("data", pageUtils);
    }

    @Override
    public Result selectGameChatList(Integer page, Integer limit,String userName,String content,Long ordersId){
        Page<ChatContentTcwm> pages = new Page<>(page, limit);
        IPage<ChatContentTcwm> chatContentTcwmIPage = chatContentTcwmDao.selectGameChatList(pages, userName,content,ordersId);
        PageUtils pageUtils = new PageUtils(chatContentTcwmIPage);
        return Result.success().put("data", pageUtils);
    }

    @Override
    public Result selectUnreadMessage(Long ordersId, Integer type) {
        int i = chatContentTcwmDao.selectUnreadMessage(ordersId, type);
        return Result.success().put("data", i);
    }

    @Override
    public Result selectAllUnreadCount(Long userId, Long riderUserId, Long shopId) {
        int i = chatContentTcwmDao.selectAllUnreadCount(userId, riderUserId, shopId);
        return Result.success().put("data", i);
    }

    @Override
    public Result updateShopAllUnreadCount(Long userId, Long riderUserId, Long shopId) {
        int i = chatContentTcwmDao.updateShopAllUnreadCount(userId, riderUserId, shopId);
        return Result.success();
    }

}