package shop.alien.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import shop.alien.entity.result.R;
import shop.alien.entity.store.LawyerChatSession;
import shop.alien.mapper.LawyerChatSessionMapper;
import shop.alien.store.service.LawyerChatSessionService;

/**
 * 聊天会话 服务实现类
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Transactional
@Service
@RequiredArgsConstructor
public class LawyerChatSessionServiceImpl extends ServiceImpl<LawyerChatSessionMapper, LawyerChatSession> implements LawyerChatSessionService {

    private final LawyerChatSessionMapper chatSessionMapper;

    @Override
    public R<IPage<LawyerChatSession>> getChatSessionList(int pageNum, int pageSize, Integer consultationOrderId,
                                                     Integer clientUserId, Integer lawyerUserId, Integer status) {
        log.info("LawyerChatSessionServiceImpl.getChatSessionList?pageNum={},pageSize={},consultationOrderId={},clientUserId={},lawyerUserId={},status={}",
                pageNum, pageSize, consultationOrderId, clientUserId, lawyerUserId, status);
        Page<LawyerChatSession> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<LawyerChatSession> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LawyerChatSession::getDeleteFlag, 0);
        if (consultationOrderId != null) {
            queryWrapper.eq(LawyerChatSession::getConsultationOrderId, consultationOrderId);
        }
        if (clientUserId != null) {
            queryWrapper.eq(LawyerChatSession::getClientUserId, clientUserId);
        }
        if (lawyerUserId != null) {
            queryWrapper.eq(LawyerChatSession::getLawyerUserId, lawyerUserId);
        }
        if (status != null) {
            queryWrapper.eq(LawyerChatSession::getStatus, status);
        }
        queryWrapper.orderByDesc(LawyerChatSession::getLastMessageTime);
        IPage<LawyerChatSession> pageResult = this.page(page, queryWrapper);
        return R.data(pageResult);
    }

    @Override
    public LawyerChatSession getByOrderId(Integer consultationOrderId) {
        log.info("LawyerChatSessionServiceImpl.getByOrderId?consultationOrderId={}", consultationOrderId);
        LambdaQueryWrapper<LawyerChatSession> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LawyerChatSession::getConsultationOrderId, consultationOrderId)
                .eq(LawyerChatSession::getDeleteFlag, 0)
                .last("LIMIT 1");
        return this.getOne(queryWrapper);
    }

    @Override
    public R<LawyerChatSession> addChatSession(LawyerChatSession chatSession) {
        log.info("LawyerChatSessionServiceImpl.addChatSession?chatSession={}", chatSession);
        boolean result = this.save(chatSession);
        if (result) {
            return R.data(chatSession);
        }
        return R.fail("新增失败");
    }

    @Override
    public R<LawyerChatSession> editChatSession(LawyerChatSession chatSession) {
        log.info("LawyerChatSessionServiceImpl.editChatSession?chatSession={}", chatSession);
        boolean result = this.updateById(chatSession);
        if (result) {
            return R.data(chatSession);
        }
        return R.fail("修改失败");
    }

    @Override
    public R<Boolean> deleteChatSession(Integer id) {
        log.info("LawyerChatSessionServiceImpl.deleteChatSession?id={}", id);
        boolean result = this.removeById(id);
        if (result) {
            return R.success("删除成功");
        }
        return R.fail("删除失败");
    }
}

