package shop.alien.lawyer.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.LawyerChatMessage;
import shop.alien.lawyer.service.LawyerChatMessageService;
import shop.alien.mapper.LawyerChatMessageMapper;


import java.util.List;

/**
 * 聊天消息 服务实现类
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Transactional
@Service
@RequiredArgsConstructor
public class LawyerChatMessageServiceImpl extends ServiceImpl<LawyerChatMessageMapper, LawyerChatMessage> implements LawyerChatMessageService {

    private final LawyerChatMessageMapper chatMessageMapper;

    @Override
    public R<IPage<LawyerChatMessage>> getChatMessageList(int pageNum, int pageSize, Integer chatSessionId,
                                                     Integer consultationOrderId, Integer senderType) {
        log.info("LawyerChatMessageServiceImpl.getChatMessageList?pageNum={},pageSize={},chatSessionId={},consultationOrderId={},senderType={}",
                pageNum, pageSize, chatSessionId, consultationOrderId, senderType);
        Page<LawyerChatMessage> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<LawyerChatMessage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LawyerChatMessage::getDeleteFlag, 0);
        if (chatSessionId != null) {
            queryWrapper.eq(LawyerChatMessage::getChatSessionId, chatSessionId);
        }
        if (consultationOrderId != null) {
            queryWrapper.eq(LawyerChatMessage::getConsultationOrderId, consultationOrderId);
        }
        if (senderType != null) {
            queryWrapper.eq(LawyerChatMessage::getSenderType, senderType);
        }
        queryWrapper.orderByAsc(LawyerChatMessage::getMessageTimestamp);
        IPage<LawyerChatMessage> pageResult = this.page(page, queryWrapper);
        return R.data(pageResult);
    }

    @Override
    public List<LawyerChatMessage> getListBySessionId(Integer chatSessionId) {
        log.info("LawyerChatMessageServiceImpl.getListBySessionId?chatSessionId={}", chatSessionId);
        LambdaQueryWrapper<LawyerChatMessage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LawyerChatMessage::getChatSessionId, chatSessionId)
                .eq(LawyerChatMessage::getDeleteFlag, 0)
                .orderByAsc(LawyerChatMessage::getMessageTimestamp);
        return this.list(queryWrapper);
    }

    @Override
    public R<LawyerChatMessage> addChatMessage(LawyerChatMessage chatMessage) {
        log.info("LawyerChatMessageServiceImpl.addChatMessage?chatMessage={}", chatMessage);
        boolean result = this.save(chatMessage);
        if (result) {
            return R.data(chatMessage);
        }
        return R.fail("新增失败");
    }

    @Override
    public R<LawyerChatMessage> editChatMessage(LawyerChatMessage chatMessage) {
        log.info("LawyerChatMessageServiceImpl.editChatMessage?chatMessage={}", chatMessage);
        boolean result = this.updateById(chatMessage);
        if (result) {
            return R.data(chatMessage);
        }
        return R.fail("修改失败");
    }

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

