package top.baogutang.admin.services.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.baogutang.admin.dao.entity.NoticeLogEntity;
import top.baogutang.admin.dao.mapper.NoticeLogMapper;
import top.baogutang.admin.services.INoticeLogService;
import top.baogutang.common.constants.NoticeTypeEnum;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author: nikooh
 * @date: 2023/10/12 : 10:01
 */
@Slf4j
@Service
public class NoticeLogServiceImpl extends ServiceImpl<NoticeLogMapper, NoticeLogEntity> implements INoticeLogService {

    @Resource
    private NoticeLogMapper noticeLogMapper;

    @Override
    @Async("commonExecutor")
    @Transactional(rollbackFor = Exception.class)
    public void saveNotice(NoticeTypeEnum noticeType, Long noticeId, String type, String title, String coverUrl, String detailUrl, String noticeCreator, Date noticeCreatedAt) {
        NoticeLogEntity noticeLog = new NoticeLogEntity();
        noticeLog.setNoticeType(noticeType.getDesc());
        noticeLog.setNoticeId(noticeId);
        noticeLog.setTitle(title);
        noticeLog.setType(type);
        noticeLog.setCover(coverUrl);
        noticeLog.setNoticeCreator(noticeCreator);
        noticeLog.setDetailUrl(detailUrl);
        noticeLog.setNoticeCreatedAt(noticeCreatedAt);
        noticeLogMapper.insert(noticeLog);
    }

    @Override
    public List<NoticeLogEntity> queryLogByConditions(Long noticeId, NoticeTypeEnum noticeType) {
        return new LambdaQueryChainWrapper<>(noticeLogMapper)
                .eq(NoticeLogEntity::getNoticeId, noticeId)
                .eq(NoticeLogEntity::getNoticeType, noticeType.getDesc())
                .eq(NoticeLogEntity::getDeleted, Boolean.FALSE)
                .list();
    }
}
