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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.PageUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.errand.dao.NoticeDao;
import com.sqx.modules.errand.entity.Notice;
import com.sqx.modules.errand.service.NoticeService;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeDao, Notice> implements NoticeService {
    @Override
    public Result selectNoticeList(Integer page, Integer limit) {
        Page<Notice> pages=new Page<>(page,limit);
        IPage<Notice> noticeIPage = baseMapper.selectPage(pages, new QueryWrapper<Notice>().orderByDesc("create_time").eq("status", 0));
        PageUtils pageUtils = new PageUtils(noticeIPage);
        return Result.success().put("data", pageUtils);
    }

    @Override
    public Result selectNoticeById(Long id) {
        Notice notice = baseMapper.selectById(id);
        return Result.success().put("data", notice);
    }

    @Override
    public Result insertNotice(Notice notice) {
        notice.setStatus(0);
        notice.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        baseMapper.insert(notice);
        return Result.success();
    }

    @Override
    public Result updateNotice(Notice notice) {
        notice.setUpdateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        baseMapper.updateById(notice);
        return Result.success();
    }

    @Override
    public Result deleteNotice(Long id) {
        baseMapper.deleteById(id);
        return Result.success();
    }
}
