package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.SocketMessageBo;
import com.ruoyi.system.domain.vo.SocketMessageVo;
import com.ruoyi.system.domain.SocketMessage;
import com.ruoyi.system.mapper.SocketMessageMapper;
import com.ruoyi.system.service.ISocketMessageService;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 对话信息Service业务层处理
 *
 * @author hdka
 * @date 2024-10-27
 */
@RequiredArgsConstructor
@Service
public class SocketMessageServiceImpl implements ISocketMessageService {

    private final SocketMessageMapper baseMapper;

    /**
     * 查询对话信息
     */
    @Override
    public SocketMessageVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询对话信息列表
     */
    @Override
    public TableDataInfo<SocketMessageVo> queryPageList(SocketMessageBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SocketMessage> lqw = buildQueryWrapper(bo);
        Page<SocketMessageVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询对话信息列表
     */
    @Override
    public List<SocketMessageVo> queryList(SocketMessageBo bo) {
        LambdaQueryWrapper<SocketMessage> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SocketMessage> buildQueryWrapper(SocketMessageBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SocketMessage> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getContactId() != null, SocketMessage::getContactId, bo.getContactId());
        lqw.eq(bo.getUserId() != null, SocketMessage::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), SocketMessage::getContent, bo.getContent());
        lqw.eq(bo.getRoomId() != null, SocketMessage::getRoomId, bo.getRoomId());
        return lqw;
    }

    /**
     * 新增对话信息
     */
    @Override
    public Boolean insertByBo(SocketMessageBo bo) {
        SocketMessage add = BeanUtil.toBean(bo, SocketMessage.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改对话信息
     */
    @Override
    public Boolean updateByBo(SocketMessageBo bo) {
        SocketMessage update = BeanUtil.toBean(bo, SocketMessage.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SocketMessage entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除对话信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 根据对话的id查询列表
     * @param contactId
     * @return
     */
    @Override
    public List<SocketMessageVo> queryByContactId(Long contactId) {
        LambdaQueryWrapper<SocketMessage> lqw = Wrappers.lambdaQuery();
        lqw.eq(SocketMessage::getContactId, contactId);
        return  baseMapper.selectVoList(lqw);
    }
}
