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

import java.util.HashSet;
import java.util.List;
import java.util.Set;

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.PurchaseAcceptDao;
import com.uas.platform.b2b.dao.PurchaseAcceptItemDao;
import com.uas.platform.b2b.model.PurchaseAccept;
import com.uas.platform.b2b.model.PurchaseAcceptItem;
import com.uas.platform.b2b.service.PurchaseAcceptService;
import com.uas.platform.b2b.support.SystemSession;
import com.uas.platform.b2b.support.XingePusher;
import com.uas.platform.core.model.PageInfo;
import com.uas.platform.core.model.Status;

@Service
public class PurchaseAcceptServiceImpl implements PurchaseAcceptService{
	
	@Autowired
	private PurchaseAcceptItemDao purchaseAcceptItemDao;
	
	@Autowired
	private PurchaseAcceptDao purchaseAcceptDao;

	@Override
	public void save(List<PurchaseAcceptItem> acceptItems) {
		purchaseAcceptItemDao.save(acceptItems);
		//发送推送消息
		Set<Long> productId = new HashSet<Long>();
		for(PurchaseAcceptItem acceptItem : acceptItems) {
			//不是每一个明细都发送，每个物料发一个
			if( ! productId.contains(acceptItem.getOrderItem().getProduct().getId())){
				System.out.println("推送至=" + acceptItem.getOrderItem().getOrder().getVendUserUU());
				XingePusher.pushSingleAccountAndroid(acceptItem.getOrderItem().getOrder().getVendUserUU().toString(), 
						"物料已被客户验收入库", "物料：" + acceptItem.getOrderItem().getProduct().getTitle(),
						null);
				XingePusher.pushSingleAccountIOS(acceptItem.getOrderItem().getOrder().getVendUserUU().toString(),
						acceptItem.getOrderItem().getProduct().getTitle() + "已被客户验收入库");
				productId.add(acceptItem.getOrderItem().getProduct().getId());
			}
		}
	}

	@Override
	public List<PurchaseAccept> findNotUpload() {
		return purchaseAcceptDao.findPurchaseAcceptByVendUUAndSendStatus(SystemSession.getUser().getEnterprise().getUu(),
				(short) Status.NOT_UPLOAD.value());
	}

	@Override
	public void onUploadSuccess(String[] idArray) {
		for(String id : idArray) {
			PurchaseAccept accept = purchaseAcceptDao.findOne(Long.parseLong(id));
			if(accept != null) {
				accept.setSendStatus((short) Status.DOWNLOADED.value());
				purchaseAcceptDao.save(accept);
			}
		}
	}

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

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

	@Override
	public PurchaseAccept findById(Long id) {
		return purchaseAcceptDao.findOne(id);
	}


}
