package com.gxwebsoft.love.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.common.system.service.UserService;
import com.gxwebsoft.love.entity.ChatConversation;
import com.gxwebsoft.love.entity.ChatMessage;
import com.gxwebsoft.love.mapper.ChatConversationMapper;
import com.gxwebsoft.love.param.ChatConversationParam;
import com.gxwebsoft.love.service.ChatConversationService;
import com.gxwebsoft.love.vo.ChatConversationVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 聊天消息表Service实现
 *
 * @author 科技小王子
 * @since 2023-06-15 21:26:48
 */
@Service
public class ChatConversationServiceImpl extends ServiceImpl<ChatConversationMapper, ChatConversation> implements ChatConversationService {

    @Resource
    private UserService userService;


    @Override
    public PageResult<ChatConversationVO> pageRel(ChatConversationParam param) {

        MPJLambdaWrapper<ChatConversation> wrapper = JoinWrappers.lambda("t",ChatConversation.class)
                .selectAll(ChatConversation.class)
                // 查询用户
//                .eq(ObjectUtils.isNotEmpty(param.getUserId()), ChatConversation::getUserId, param.getUserId())
                .and(ObjectUtils.isNotEmpty(param.getUserId()), w2-> {
                    w2.eq(ChatConversation::getUserId, param.getUserId()).or(w3->{
                        w3.eq(ChatConversation::getFriendId, param.getUserId());
                    });
                } )
                // 查询未读
                .gt(param.getStatus()!= null &&param.getStatus() == 1, ChatConversation::getUnRead, 0)
                //
                .selectAssociation("t1",User.class, ChatConversationVO::getFriendInfo)
                .selectAssociation("t2",User.class, ChatConversationVO::getUserInfo)
                .innerJoin(User.class,"t1", User::getUserId, ChatConversation::getFriendId)
                .innerJoin(User.class, "t2",  wrapper1 -> wrapper1.notLike(param.getOnlyFake(),User::getUsername, "wx_%").eq(User::getUserId, ChatConversation::getUserId))
                // 消息列表 影响分页 暂时无解User::getUserId, ChatConversation::getUserId,
//                .selectCollection(ChatMessage.class, ChatConversationVO::getMessages)
//                .leftJoin(ChatMessage.class, on ->
//                        on.and(w -> {
//                                    w.eq(ChatMessage::getFormUserId, ChatConversation::getUserId).eq(ChatMessage::getToUserId, ChatConversation::getFriendId);
//                                })
//                                .or(w -> {
//                                    w.eq(ChatMessage::getFormUserId, ChatConversation::getFriendId).eq(ChatMessage::getToUserId, ChatConversation::getUserId);
//                                })
//                                .orderByDesc(ChatMessage::getCreateTime)
//                )

                .orderByDesc(ChatConversation::getUpdateTime)
                ;


        Page<ChatConversationVO> chatConversationVOPage = baseMapper.selectJoinPage(new Page<>(param.getPage(), param.getLimit()), ChatConversationVO.class, wrapper);

        return new PageResult<>(chatConversationVOPage.getRecords(), chatConversationVOPage.getTotal());
    }

    @Override
    public List<ChatConversation> listRel(ChatConversationParam param) {
        List<ChatConversation> list = baseMapper.selectListRel(param);
        // 排序
        PageParam<ChatConversation, ChatConversationParam> page = new PageParam<>();
        //page.setDefaultOrder("create_time desc");

        return page.sortRecords(list);
    }

    @Override
    public ChatConversation getByIdRel(Integer id) {
        ChatConversationParam param = new ChatConversationParam();
        param.setId(id);
        return param.getOne(baseMapper.selectListRel(param));
    }

    @Override
    public ChatConversation getByBothId(Integer formUserId, Integer toUserId) {
        ChatConversationParam param = new ChatConversationParam();
        param.setUserId(formUserId);
        param.setFriendId(toUserId);
        return param.getOne(baseMapper.selectListRel(param));
    }

    @Override
    public List<ChatConversationVO> getFriendList(ChatConversationParam param) {
        MPJLambdaWrapper<ChatConversation> wrapper = JoinWrappers.lambda(ChatConversation.class)
                .selectAll(ChatConversation.class)
                .eq(ChatConversation::getUserId, param.getUserId())
                .selectAssociation(User.class, ChatConversationVO::getFriendInfo)
                .leftJoin(User.class, User::getUserId, ChatConversation::getFriendId)
                .selectCollection(ChatMessage.class, ChatConversationVO::getMessages)
                .leftJoin(ChatMessage.class, on ->
                        on.and(w -> {
                                    w.eq(ChatMessage::getFormUserId, param.getUserId()).eq(ChatMessage::getToUserId, ChatConversation::getFriendId);
                                })
                                .or(w -> {
                                    w.eq(ChatMessage::getFormUserId, ChatConversation::getFriendId).eq(ChatMessage::getToUserId, param.getUserId());
                                })
                                .orderByDesc(ChatMessage::getCreateTime)
                )
                .orderByDesc(ChatConversation::getUpdateTime);


        return baseMapper.selectJoinList(ChatConversationVO.class, wrapper);
    }

}
