package com.uas.platform.b2b.mobile.service.impl;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import com.uas.platform.b2b.mobile.dao.NoticeCenterDao;
import com.uas.platform.b2b.mobile.model.NoticeCenter;
import com.uas.platform.b2b.mobile.service.NoticeCenterService;
import com.uas.platform.core.model.PageInfo;

@Service
public class NoticeCenterServiceImpl implements NoticeCenterService {

	@Autowired
	private NoticeCenterDao noticeCenterDao;

	@Override
	public ModelMap getAllNoticesByEnuu(Integer pageNumber, Integer pageSize, Long enuu) {
		ModelMap map = new ModelMap();
		final PageInfo pageInfo = new PageInfo();
		pageInfo.setPageNumber(pageNumber);
		pageInfo.setPageNumber(pageNumber);
		pageInfo.filter("enuu", enuu);
		Page<NoticeCenter> news = noticeCenterDao.findAll(new Specification<NoticeCenter>() {
			@Override
            public Predicate toPredicate(Root<NoticeCenter> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
				return query.where(pageInfo.getPredicates(root, query, builder)).getRestriction();
			}
		}, pageInfo);
		map.put("data", news.getContent());
		return map;
	}

}
