package com.uas.finance.service.Impl;

import com.uas.finance.dao.CommonDao;
import com.uas.finance.dao.ErpLogDao;
import com.uas.finance.model.ErpLog;
import com.uas.finance.model.SearchFilter;
import com.uas.finance.service.ErpLogService;
import com.uas.platform.core.model.PageInfo;
import com.uas.platform.core.persistence.criteria.PredicateUtils;
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 javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

@Service
public class ErpLogServiceImpl implements ErpLogService {
	
	@Autowired
	private ErpLogDao erpLogDao;

	@Autowired
	private CommonDao commonDao;

	@Override
	public void save(ErpLog log) {
		erpLogDao.save(log);
	}

	@Override
	public void save(Iterable<ErpLog> logs) {
		erpLogDao.save(logs);
	}
	
	/**
	 * @description 根据搜索参数查询
	 * @author Hao
	 * @date 2015年9月28日17:17:38
	 * 
	 */
	@Override
	public Page<ErpLog> findSearchByPageInfo(final PageInfo pageInfo, SearchFilter filter) {
		if(filter != null) {
			if(filter.getFromDate() != null){
				pageInfo.expression(PredicateUtils.gte("time", new Long(filter.getFromDate()), false));
			}
			if(filter.getEndDate() != null)
				pageInfo.expression(PredicateUtils.lte("time", new Long(filter.getEndDate()), false));
		}
		Page<ErpLog> erpLog = erpLogDao.findAll(new Specification<ErpLog>() {
			public Predicate toPredicate(Root<ErpLog> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
				return query.where(pageInfo.getPredicates(root, query, builder)).getRestriction();
			}
		},pageInfo);
		return erpLog;
	}

}
