package com.sqx.modules.announcement.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.exception.SqxException;
import com.sqx.modules.announcement.entity.Announcement;
import com.sqx.modules.announcement.service.AnnouncementService;
import com.sqx.modules.announcement.mapper.AnnouncementMapper;
import com.sqx.modules.common.dto.AddAnnouncementDTO;
import com.sqx.modules.common.dto.UpdateAnnouncementDTO;
import org.springframework.stereotype.Service;

/**
* @author Administrator
* @description 针对表【announcement(公告表)】的数据库操作Service实现
* @createDate 2025-01-02 14:33:44
*/
@Service
public class AnnouncementServiceImpl extends ServiceImpl<AnnouncementMapper, Announcement>
    implements AnnouncementService{

    @Override
    public Object listInfo(String title, Integer state, Integer id) {
        LambdaQueryWrapper<Announcement> queryWrapper = new LambdaQueryWrapper<>();
        boolean flag = false;
        if (StrUtil.isNotBlank(title)) {
            queryWrapper.like(Announcement::getTitle, title);
            flag = true;
        }
        if (state != null) {
            queryWrapper.eq(Announcement::getState, state);
            flag = true;
        }
        if (id != null) {
            queryWrapper.eq(Announcement::getId, id);
        }
        return flag ? list(queryWrapper) : list();
    }

    @Override
    public Object add(AddAnnouncementDTO announcementDTO) {
        Announcement announcement = new Announcement();
        BeanUtil.copyProperties(announcementDTO, announcement);
        announcement.setCreateTime(DateUtil.date());
        return save(announcement);
    }

    @Override
    public Object updateInfo(UpdateAnnouncementDTO updateAnnouncementDTO) {
        Announcement announcement = getById(updateAnnouncementDTO.getId());
        if (announcement == null) {
            throw new SqxException("公告不存在");
        }
        BeanUtil.copyProperties(updateAnnouncementDTO, announcement);
        announcement.setUpdateTime(DateUtil.date());
        return updateById(announcement);
    }
}




