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.CustomerDao;
import com.uas.platform.b2b.model.Customer;
import com.uas.platform.b2b.model.User;
import com.uas.platform.b2b.service.CustomerService;
import com.uas.platform.core.model.PageInfo;
import com.uas.platform.core.persistence.criteria.PredicateFactory;
import com.uas.platform.core.persistence.criteria.PredicateUtils;

@Service
public class CustomerServiceImpl implements CustomerService {

	@Autowired
	private CustomerDao customerDao;

	public Page<Customer> findAllByPageInfo(final Long enUU, final PageInfo pageInfo) {
		Page<Customer> customers = customerDao.findAll(new Specification<Customer>() {

			public Predicate toPredicate(Root<Customer> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
				pageInfo.filter("myEnUU", enUU);
				query.where(pageInfo.getPredicates(root, query, builder));
				return null;
			}
		}, pageInfo);
		return customers;
	}

	public List<Customer> findAllByCustUU(Long enUU, String custEnUUStr) {
		PredicateFactory<Customer> factory = new PredicateFactory<Customer>();
		factory.add(PredicateUtils.eq("myEnUU", enUU, true));
		factory.add(PredicateUtils.in("customerEnterprise.uu", custEnUUStr.split(","), true));
		return customerDao.findAll(factory);
	}

	public Customer findCustomer(Long custId) {
		return customerDao.findOne(custId);
	}

	public Customer saveCustomer(User user, Customer customer) {
		customer.setMyEnUU(user.getEnterprise().getUu());
		customer.setMyUserUU(user.getUserUU());
		return customerDao.saveAndLog(customer);
	}

	public List<Customer> saveCustomers(User user, List<Customer> customers) {
		for (Customer customer : customers) {
			customer.setMyUserUU(user.getUserUU());
			customer.setMyEnUU(user.getEnterprise().getUu());
		}
		return customerDao.saveAndLog(customers);
	}

	public Customer auditCustomer(User user, Customer customer) {
		customer = customerDao.auditAndLog(customer);
		return customer;
	}

	public List<Customer> auditCustomers(User user, List<Customer> customers) {
		customers = customerDao.auditAndLog(customers);
		return customers;
	}

	public Customer unAuditCustomer(User user, Customer customer) {
		customer = customerDao.unAuditAndLog(customer);
		return customer;
	}

	public List<Customer> unAuditCustomers(User user, List<Customer> customers) {
		customers = customerDao.unAuditAndLog(customers);
		return customers;
	}

	public void deleteCustomer(Customer customer) {
		customerDao.deleteAndLog(customer);
	}

	public void deleteCustomers(List<Customer> customers) {
		customerDao.deleteAndLog(customers);
	}

	public void deleteCustomers(String custIds) {
		String[] vendors = custIds.split(",");
		for (String vend : vendors) {
			customerDao.deleteAndLog(Long.valueOf(vend));
		}
	}

}
