package com.uas.platform.b2b.history.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.history.dao.PurchaseNoticeDao;
import com.uas.platform.b2b.history.dao.SaleSendItemDao;
import com.uas.platform.b2b.history.model.PurchaseNotice;
import com.uas.platform.b2b.history.model.SaleSendItem;
import com.uas.platform.b2b.history.service.PurchaseNoticeService;
import com.uas.platform.core.model.PageInfo;

@Service("history.PurchaseNoticeService")
public class PurchaseNoticeServiceImpl implements PurchaseNoticeService {

	@Autowired
	private PurchaseNoticeDao purchaseNoticeDao;

	@Autowired
	private SaleSendItemDao saleSendItemDao;

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

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

	@Override
	public PurchaseNotice findById(Long id) {
		return purchaseNoticeDao.findOne(id);
	}

	@Override
	public List<SaleSendItem> findSendById(long noticeId) {
		return saleSendItemDao.findByNoticeId(noticeId);
	}

}
