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

import java.util.List;

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 com.uas.platform.b2b.dao.NoticeBodyDao;
import com.uas.platform.b2b.dao.NoticeDao;
import com.uas.platform.b2b.model.Notice;
import com.uas.platform.b2b.model.NoticeBody;
import com.uas.platform.b2b.service.NoticeService;
import com.uas.platform.core.model.PageInfo;

@Service
public class NoticeServiceImpl implements NoticeService {

	@Autowired
	private NoticeDao noticeDao;

	@Autowired
	private NoticeBodyDao noticeBodyDao;

	@Override
	public Page<Notice> findAllByPageInfo(final PageInfo pageInfo) {
		return noticeDao.findAll(new Specification<Notice>() {

			public Predicate toPredicate(Root<Notice> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
				query.where(pageInfo.getPredicates(root, query, builder));
				return null;
			}
		}, pageInfo);
	}

	@Override
	public NoticeBody findNoticeBody(Long noticeId) {
		List<NoticeBody> bodys = noticeBodyDao.findByNotice(noticeId);
		if (bodys.size() > 0)
			return bodys.get(0);
		return null;
	}

}
