package com.hssx.pcbms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hssx.pcbms.entity.NewsNotice;
import com.hssx.pcbms.entity.NewsNoticeUser;
import com.hssx.pcbms.mapper.NewsNoticeMapper;
import com.hssx.pcbms.mapper.NewsNoticeUserMapper;
import com.hssx.pcbms.service.NewsNoticeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hssx.pcbms.util.HttpRespMsg;
import com.hssx.pcbms.util.PageUtil;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 吴涛涛
 * @since 2019-11-06
 */
@Service
public class NewsNoticeServiceImpl extends ServiceImpl<NewsNoticeMapper, NewsNotice> implements NewsNoticeService {

    @Resource
    private NewsNoticeMapper newsNoticeMapper;
    @Resource
    private NewsNoticeUserMapper newsNoticeUserMapper;

    @Override
    public HttpRespMsg getNewsList(Integer uid, PageUtil page) {
        HttpRespMsg msg = new HttpRespMsg();
        PageHelper.startPage(page.getPageNum(),page.getPageSize());
        List<NewsNotice> list = newsNoticeMapper.selectListByCondition(uid);
        PageInfo<NewsNotice> pageInfo = new PageInfo<>(list);
        msg.data = pageInfo;
        //将消息标记成已读
        NewsNoticeUser noticeUser = new NewsNoticeUser();
        noticeUser.setIsRead(1);
        newsNoticeUserMapper.update(noticeUser,new QueryWrapper<NewsNoticeUser>().eq("user_id",uid));
        return msg;
    }

    @Override
    public HttpRespMsg getNoReadNewsCount(Integer uid) {
        HttpRespMsg msg = new HttpRespMsg();
        msg.data = newsNoticeMapper.getNoReadNewsCount(uid);
        return msg;
    }
}
