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.PurchaseApBillDao;
import com.uas.platform.b2b.dao.PurchaseApBillItemDao;
import com.uas.platform.b2b.dao.VendorDao;
import com.uas.platform.b2b.model.PurchaseApBill;
import com.uas.platform.b2b.model.PurchaseApBillItem;
import com.uas.platform.b2b.model.Vendor;
import com.uas.platform.b2b.service.PurchaseApBillService;
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 PurchaseApBillServiceImpl implements PurchaseApBillService{
	
	@Autowired
	private PurchaseApBillDao purchaseApBillDao;
	
	@Autowired
	private VendorDao vendorDao;
	
	@Autowired
	private PurchaseApBillItemDao purchaseApBillItemDao;

	@Override
	public void save(List<PurchaseApBillItem> apBillItems) {
		System.out.println("b2b save apBillItems : " + apBillItems.size());
		List<PurchaseApBillItem> newApBillItems = purchaseApBillItemDao.save(apBillItems);
		if(newApBillItems.size() > 0){
			//发送推送消息
			Set<Long> apBillIds = new HashSet<Long>();
			//每一次保存只发一条推送，通知单据个数
			for(PurchaseApBillItem apBillItem : newApBillItems) {
				if( ! apBillIds.contains(apBillItem.getaPBill().getId())){
					List<Vendor> vend = vendorDao.findByMyEnUUAndVendUU(apBillItem.getaPBill().getEnUU(), apBillItem.getaPBill().getVendUU());
					if(vend.size() > 0) {
						Long vendUseruu = vend.get(0).getVendUserUU();//供应商联系人
						String customerName = apBillItem.getaPBill().getEnterprise().getEnName();//客户名
						XingePusher.pushSingleAccountAndroid(vendUseruu.toString(), 
								"收到一张客户应付票据", "客户：" + customerName,null);
						XingePusher.pushSingleAccountIOS(vendUseruu.toString(), 
								"收到一张客户应付票据  | 客户：" + customerName);
					}
					apBillIds.add(apBillItem.getaPBill().getId());
				}
			}
		}
	}

	@Override
	public List<PurchaseApBill> findNotUpload() {
		return purchaseApBillDao.findPurchaseApBillByVendUUAndSendStatus(SystemSession.getUser().getEnterprise().getUu(),
				(short) Status.NOT_UPLOAD.value());
	}

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

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

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

	@Override
	public PurchaseApBill findById(Long id) {
		return purchaseApBillDao.findOne(id);
	}

}
