package top.baogutang.admin.services.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
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.domain.req.NoticeLogQueryParam;
import top.baogutang.admin.domain.res.NoticeLogRes;
import top.baogutang.admin.services.INoticeLogService;
import top.baogutang.common.constants.NoticeTypeEnum;
import top.baogutang.common.domain.PageUtil;
import top.baogutang.common.utils.MyBatisPlusPageUtil;

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

/**
 * @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();
    }

    @Override
    public PageUtil<NoticeLogRes> queryPage(Integer pageNum, Integer pageSize, NoticeTypeEnum noticeType) {
        NoticeLogQueryParam queryParam = new NoticeLogQueryParam(pageNum, pageSize, noticeType);
        return MyBatisPlusPageUtil.page(page -> noticeLogMapper.selectPage(page, Wrappers.lambdaQuery(NoticeLogEntity.class)
                .eq(Objects.nonNull(queryParam.getNoticeType()), NoticeLogEntity::getNoticeType, queryParam.getNoticeType().getDesc())
                .eq(NoticeLogEntity::getDeleted, Boolean.FALSE)
                .orderByDesc(NoticeLogEntity::getCreateTime)), queryParam, this::geneResFromEntity);
    }

    private NoticeLogRes geneResFromEntity(NoticeLogEntity noticeLogEntity) {
        NoticeLogRes noticeLogRes = new NoticeLogRes();
        BeanUtils.copyProperties(noticeLogEntity, noticeLogRes);
        return noticeLogRes;
    }
}
