package com.storlead.message.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.storlead.framework.mybatis.service.impl.MyBaseServiceImpl;
import com.storlead.framework.util.LoginUserUtil;
import com.storlead.message.pojo.dto.MessageDTO;
import com.storlead.message.pojo.entity.InsideMessageSendLogEntity;
import com.storlead.message.pojo.vo.MessageAppNoReadVO;
import com.storlead.message.pojo.vo.MessageDetailVO;
import com.storlead.message.pojo.vo.MessageNoReadTotalVO;
import com.storlead.message.mapper.InsideMessageSendLogMapper;
import com.storlead.message.pojo.vo.MessageTypeReadStateVO;
import com.storlead.message.service.InsideMessageSendLogService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 内部消息人员接收记录表 服务实现类
 * </p>
 *
 * @author chenkq
 * @since 2024-04-19
 */
@Service
public class InsideMessageSendLogServiceImpl extends MyBaseServiceImpl<InsideMessageSendLogMapper, InsideMessageSendLogEntity> implements InsideMessageSendLogService {

    @Override
    public IPage<MessageDetailVO> queryListMessageLog(Page<InsideMessageSendLogEntity> page, Wrapper<InsideMessageSendLogEntity> wrapper) {
        return this.baseMapper.pageListMessageLog(page,wrapper);
    }

    @Override
    public IPage<MessageDetailVO> queryListMessageLog(MessageDTO dto) {

        Page<InsideMessageSendLogEntity> page = new Page<>(dto.getPageIndex(),dto.getPageSize());
        QueryWrapper<InsideMessageSendLogEntity> queryWrapper = new QueryWrapper();
        queryWrapper.eq("mlog.receiver_user_id", LoginUserUtil.getCurrentUserId());
        if (Objects.nonNull(dto.getMessageType())) {
            queryWrapper.eq("mr.message_type",dto.getMessageType());
        }
        if (Objects.nonNull(dto.getAppId())) {
            queryWrapper.eq("mr.app_id", dto.getAppId());
        }
        if (Objects.nonNull(dto.getIsRead())) {
            queryWrapper.eq("mlog.is_read",dto.getIsRead());
            queryWrapper.eq("mlog.is_read",dto.getIsRead());
        }
        queryWrapper.eq("mlog.is_delete",Integer.valueOf(0));
        queryWrapper.orderByAsc("mlog.is_read");
        queryWrapper.orderByDesc("mlog.id");

        return queryListMessageLog(page,queryWrapper);
    }

    @Override
    public MessageNoReadTotalVO countNoReadtotal(Wrapper<InsideMessageSendLogEntity> wrapper) {
        return this.baseMapper.countNoReadNumber(wrapper);
    }

    @Override
    public List<MessageTypeReadStateVO> getCountNoReadCount(Wrapper<InsideMessageSendLogEntity> wrapper) {
        return this.baseMapper.countNoReadCount(wrapper);
    }

    @Override
    public List<MessageAppNoReadVO> getCountNoReadCountByApp(Wrapper<InsideMessageSendLogEntity> wrapper) {
        return this.baseMapper.countNoReadCountByApp(wrapper);
    }

}
