package com.fjhx.mail.service.my.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.mail.entity.my.dto.MyFolderMessageDto;
import com.fjhx.mail.entity.my.dto.MyFolderMessageSelectDto;
import com.fjhx.mail.entity.my.po.MyFolderMessage;
import com.fjhx.mail.entity.my.vo.MessageVo;
import com.fjhx.mail.mapper.my.MyFolderMessageMapper;
import com.fjhx.mail.service.my.MyFolderMessageService;
import com.ruoyi.common.utils.wrapper.IWrapper;
import org.springframework.stereotype.Service;


/**
 * <p>
 * 我的文件夹关联邮件 服务实现类
 * </p>
 *
 * @author
 * @since 2023-05-29
 */
@Service
public class MyFolderMessageServiceImpl extends ServiceImpl<MyFolderMessageMapper, MyFolderMessage> implements MyFolderMessageService {

    @Override
    public Page<MessageVo> getPage(MyFolderMessageSelectDto dto) {
        IWrapper<MyFolderMessage> wrapper = getWrapper();
        wrapper.eq("mfm", MyFolderMessage::getMyFolderId, dto.getMyFolderId());
        wrapper.orderByDesc("mfm", MyFolderMessage::getUpdateTime);
        return this.baseMapper.getPage(dto.getPage(), wrapper, dto.getType());
    }

    @Override
    public void add(MyFolderMessageDto dto) {
        MyFolderMessage myFolderMessage = getOne(q -> q.eq(MyFolderMessage::getMessageId, dto.getMessageId()));
        if (myFolderMessage != null) {
            myFolderMessage.setMyFolderId(dto.getMyFolderId());
            updateById(myFolderMessage);
        } else {
            save(dto);
        }
    }

    @Override
    public void delete(Long id) {
        this.removeById(id);
    }

}
