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.BulletinCenterDao;
import com.uas.platform.b2b.mobile.model.BulletinCenter;
import com.uas.platform.b2b.mobile.service.BulletinCenterService;
import com.uas.platform.core.model.PageInfo;
import com.uas.platform.core.model.PageParams;

@Service
public class BulletinCenterServiceImpl implements BulletinCenterService {

	@Autowired
	private BulletinCenterDao bulletinCenterDao;

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

}
