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.PurchaseOrderAllDao;
import com.uas.platform.b2b.history.dao.PurchaseOrderDao;
import com.uas.platform.b2b.history.dao.PurchaseOrderEndDao;
import com.uas.platform.b2b.history.dao.PurchaseOrderReplyDao;
import com.uas.platform.b2b.history.dao.PurchaseOrderTodoDao;
import com.uas.platform.b2b.history.model.PurchaseOrder;
import com.uas.platform.b2b.history.model.PurchaseOrderAll;
import com.uas.platform.b2b.history.model.PurchaseOrderEnd;
import com.uas.platform.b2b.history.model.PurchaseOrderReply;
import com.uas.platform.b2b.history.model.PurchaseOrderTodo;
import com.uas.platform.b2b.history.service.PurchaseOrderService;
import com.uas.platform.core.exception.NotFoundException;
import com.uas.platform.core.model.PageInfo;

@Service("history.PurchaseOrderService")
public class PurchaseOrderServiceImpl implements PurchaseOrderService {

	@Autowired
	private PurchaseOrderDao purchaseOrderDao;

	@Autowired
	private PurchaseOrderAllDao purchaseOrderAllDao;

	@Autowired
	private PurchaseOrderTodoDao purchaseOrderTodoDao;

	@Autowired
	private PurchaseOrderEndDao purchaseOrderEndDao;

	@Autowired
	private PurchaseOrderReplyDao purchaseOrderReplyDao;

	public Page<PurchaseOrder> findAllByPageInfo(final PageInfo pageInfo) {
		return purchaseOrderDao.findAll(new Specification<PurchaseOrder>() {

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

	@Override
	public Page<PurchaseOrderAll> findAllDetailByPageInfo(final PageInfo pageInfo) {
		return purchaseOrderAllDao.findAll(new Specification<PurchaseOrderAll>() {

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

	@Override
	public PurchaseOrderAll findById(Long id) {
		PurchaseOrderAll purchaseOrder = purchaseOrderAllDao.findOne(id);
		if (purchaseOrder == null)
			throw new NotFoundException();
		return purchaseOrder;
	}

	@Override
	public Page<PurchaseOrderTodo> findTodoByPageInfo(final PageInfo pageInfo) {
		return purchaseOrderTodoDao.findAll(new Specification<PurchaseOrderTodo>() {

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

	@Override
	public Page<PurchaseOrderEnd> findEndByPageInfo(final PageInfo pageInfo) {
		return purchaseOrderEndDao.findAll(new Specification<PurchaseOrderEnd>() {

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

	@Override
	public List<PurchaseOrderReply> findReplyByOrderItem(long orderItemId) {
		return purchaseOrderReplyDao.findByOrderItemId(orderItemId);
	}

}
