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.MyTagMessageDto;
import com.fjhx.mail.entity.my.dto.MyTagMessageSelectDto;
import com.fjhx.mail.entity.my.po.MyTagMessage;
import com.fjhx.mail.entity.my.vo.MessageVo;
import com.fjhx.mail.mapper.my.MyTagMessageMapper;
import com.fjhx.mail.service.my.MyTagMessageService;
import com.obs.services.internal.ServiceException;
import com.ruoyi.common.utils.wrapper.IWrapper;
import org.springframework.stereotype.Service;


/**
 * <p>
 * 我的标签关联邮件 服务实现类
 * </p>
 *
 * @author
 * @since 2023-05-29
 */
@Service
public class MyTagMessageServiceImpl extends ServiceImpl<MyTagMessageMapper, MyTagMessage> implements MyTagMessageService {

    @Override
    public Page<MessageVo> getPage(MyTagMessageSelectDto dto) {
        IWrapper<MyTagMessage> wrapper = getWrapper();
        wrapper.eq("mtm", MyTagMessage::getMyTagId, dto.getMyTagId());
        wrapper.orderByDesc("mtm", MyTagMessage::getUpdateTime);
        return this.baseMapper.getPage(dto.getPage(), wrapper, dto.getType());
    }

    @Override
    public void add(MyTagMessageDto myTagMessageDto) {

        MyTagMessage myTagMessage = getOne(q -> q
                .eq(MyTagMessage::getMessageId, myTagMessageDto.getMessageId())
                .eq(MyTagMessage::getMyTagId, myTagMessageDto.getMyTagId())
        );
        if (myTagMessage != null) {
            throw new ServiceException("标签已被添加");
        }

        this.save(myTagMessageDto);
    }

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

    @Override
    public void deleteTag(MyTagMessage myTagMessage) {

        remove(q -> q
                .eq(MyTagMessage::getMyTagId, myTagMessage.getMyTagId())
                .eq(MyTagMessage::getMessageId, myTagMessage.getMessageId())
        );

    }

}
