package com.ssj.service.sys.award.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.ssj.bean.sys.award.domain.AwardActivity;
import com.ssj.bean.sys.award.domain.AwardConsume;
import com.ssj.bean.sys.award.domain.AwardGrant;
import com.ssj.bean.sys.award.domain.AwardMaterial;
import com.ssj.bean.sys.award.domain.AwardStock;
import com.ssj.dao.sys.award.dao.AwardGrantDao;
import com.ssj.dao.sys.award.dao.AwardQueryDao;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.service.sys.award.service.AwardConsumeService;
import com.ssj.service.sys.award.service.AwardGrantService;
import com.ssj.service.sys.award.service.AwardMaterialService;
import com.ssj.service.sys.award.service.AwardStockService;

@Service(version = "1.0")
@Transactional
public class AwardGrantServiceImpl extends BaseServiceImpl<AwardGrant, String> implements AwardGrantService {
	
	@Autowired
	private AwardGrantDao  awardGrantDao;

	@Autowired
	private AwardQueryDao  queryDao;

	@Autowired
	private AwardStockService  awardStockService;
	
	@Autowired
	private AwardMaterialService  awardMaterialService;
	
	@Autowired
	private AwardConsumeService  awardConsumeService;

	@Override
	public PagingAndSortingRepository<AwardGrant, String> getDao() {
		return awardGrantDao;
	}


	@Override
	public Page<Map<String, Object>> findAwardGrantBylist(Map<String, Object> searchParams, Pageable pageable) {
		return queryDao.findAwardGrantBylist(searchParams, pageable);
	}

	
	@Override
	public boolean addAwardGrantAndAwardStock(AwardGrant awardGrant) {	
		List<AwardStock> awardStocks=new ArrayList<AwardStock>();
		List<AwardConsume> awardConsumes=new ArrayList<AwardConsume>();
		
		//指点商户的处理库存数
		if(StringUtils.isNotEmpty(awardGrant.getLevel1MaterialId())) {
			
			//减量
			AwardStock fAwardStock=awardStockService.getAwardStockByMaterialId(awardGrant.getFromMerchantId(), awardGrant.getLevel1MaterialId());
			if(fAwardStock==null) {
				fAwardStock=new AwardStock();
				fAwardStock.setMerchantId(awardGrant.getFromMerchantId());
				fAwardStock.setAwardMaterialId(awardGrant.getLevel1MaterialId());
				fAwardStock.setStockNumber(0);
				fAwardStock.setUseNumber(0);
			}
			fAwardStock.setStockNumber(fAwardStock.getStockNumber()-awardGrant.getLevel1Num());
			awardStocks.add(fAwardStock);
			
			AwardConsume awardConsume=new AwardConsume();
			awardConsume.setFromMerchantId(awardGrant.getFromMerchantId());
			awardConsume.setToMerchantId(awardGrant.getToMerchantId());
			awardConsume.setAwardMaterialId(awardGrant.getLevel1MaterialId());
			awardConsume.setNumber(awardGrant.getLevel1Num());
			awardConsume.setType(1);
			awardConsume.setRemarks("我向下配送");
			awardConsumes.add(awardConsume);
			
			
			//增量
			AwardStock tAwardStock=awardStockService.getAwardStockByMaterialId(awardGrant.getToMerchantId(), awardGrant.getLevel1MaterialId());
			if(tAwardStock==null) {
				tAwardStock=new AwardStock();
				tAwardStock.setMerchantId(awardGrant.getToMerchantId());
				tAwardStock.setAwardMaterialId(awardGrant.getLevel1MaterialId());
				tAwardStock.setStockNumber(0);
				tAwardStock.setUseNumber(0);
			}
			tAwardStock.setStockNumber(tAwardStock.getStockNumber()+awardGrant.getLevel1Num());
			awardStocks.add(tAwardStock);
			
			awardConsume=new AwardConsume();
			awardConsume.setFromMerchantId(awardGrant.getFromMerchantId());
			awardConsume.setToMerchantId(awardGrant.getToMerchantId());
			awardConsume.setAwardMaterialId(awardGrant.getLevel1MaterialId());
			awardConsume.setNumber(awardGrant.getLevel1Num());
			awardConsume.setType(2);
			awardConsume.setRemarks("上级向我配送");
			awardConsumes.add(awardConsume);
			
			
		}
		
		if(StringUtils.isNotEmpty(awardGrant.getLevel2MaterialId())) {

			//减量
			AwardStock fAwardStock=awardStockService.getAwardStockByMaterialId(awardGrant.getFromMerchantId(), awardGrant.getLevel2MaterialId());
			if(fAwardStock==null) {
				fAwardStock=new AwardStock();
				fAwardStock.setMerchantId(awardGrant.getFromMerchantId());
				fAwardStock.setAwardMaterialId(awardGrant.getLevel2MaterialId());
				fAwardStock.setStockNumber(0);
				fAwardStock.setUseNumber(0);
			}
			fAwardStock.setStockNumber(fAwardStock.getStockNumber()-awardGrant.getLevel2Num());
			awardStocks.add(fAwardStock);
			
			AwardConsume awardConsume=new AwardConsume();
			awardConsume.setFromMerchantId(awardGrant.getFromMerchantId());
			awardConsume.setToMerchantId(awardGrant.getToMerchantId());
			awardConsume.setAwardMaterialId(awardGrant.getLevel2MaterialId());
			awardConsume.setNumber(awardGrant.getLevel2Num());
			awardConsume.setType(1);
			awardConsume.setRemarks("我向下配送");
			awardConsumes.add(awardConsume);
			
			//增量
			AwardStock tAwardStock=awardStockService.getAwardStockByMaterialId(awardGrant.getToMerchantId(), awardGrant.getLevel2MaterialId());
			if(tAwardStock==null) {
				tAwardStock=new AwardStock();
				tAwardStock.setMerchantId(awardGrant.getToMerchantId());
				tAwardStock.setAwardMaterialId(awardGrant.getLevel2MaterialId());
				tAwardStock.setStockNumber(0);
				tAwardStock.setUseNumber(0);
			}
			tAwardStock.setStockNumber(tAwardStock.getStockNumber()+awardGrant.getLevel2Num());
			awardStocks.add(tAwardStock);
			
			awardConsume=new AwardConsume();
			awardConsume.setFromMerchantId(awardGrant.getFromMerchantId());
			awardConsume.setToMerchantId(awardGrant.getToMerchantId());
			awardConsume.setAwardMaterialId(awardGrant.getLevel2MaterialId());
			awardConsume.setNumber(awardGrant.getLevel2Num());
			awardConsume.setType(2);
			awardConsume.setRemarks("我向下配送");
			awardConsumes.add(awardConsume);
			
		}
		
		if(StringUtils.isNotEmpty(awardGrant.getLevel3MaterialId())) {
			
			//减量
			AwardStock fAwardStock=awardStockService.getAwardStockByMaterialId(awardGrant.getFromMerchantId(), awardGrant.getLevel3MaterialId());
			if(fAwardStock==null) {
				fAwardStock=new AwardStock();
				fAwardStock.setMerchantId(awardGrant.getFromMerchantId());
				fAwardStock.setAwardMaterialId(awardGrant.getLevel3MaterialId());
				fAwardStock.setStockNumber(0);
				fAwardStock.setUseNumber(0);
			}
			fAwardStock.setStockNumber(fAwardStock.getStockNumber()-awardGrant.getLevel3Num());
			awardStocks.add(fAwardStock);
			
			AwardConsume awardConsume=new AwardConsume();
			awardConsume.setFromMerchantId(awardGrant.getFromMerchantId());
			awardConsume.setToMerchantId(awardGrant.getToMerchantId());
			awardConsume.setAwardMaterialId(awardGrant.getLevel3MaterialId());
			awardConsume.setNumber(awardGrant.getLevel3Num());
			awardConsume.setType(1);
			awardConsume.setRemarks("我向下配送");
			awardConsumes.add(awardConsume);
			
			//增量
			AwardStock tAwardStock=awardStockService.getAwardStockByMaterialId(awardGrant.getToMerchantId(), awardGrant.getLevel3MaterialId());
			if(tAwardStock==null) {
				tAwardStock=new AwardStock();
				tAwardStock.setMerchantId(awardGrant.getToMerchantId());
				tAwardStock.setAwardMaterialId(awardGrant.getLevel3MaterialId());
				tAwardStock.setStockNumber(0);
				tAwardStock.setUseNumber(0);
			}
			tAwardStock.setStockNumber(tAwardStock.getStockNumber()+awardGrant.getLevel3Num());
			awardStocks.add(tAwardStock);
			
			awardConsume=new AwardConsume();
			awardConsume.setFromMerchantId(awardGrant.getFromMerchantId());
			awardConsume.setToMerchantId(awardGrant.getToMerchantId());
			awardConsume.setAwardMaterialId(awardGrant.getLevel3MaterialId());
			awardConsume.setNumber(awardGrant.getLevel3Num());
			awardConsume.setType(2);
			awardConsume.setRemarks("上级向我配送");
			awardConsumes.add(awardConsume);
		}
		
		this.save(awardGrant);
		awardStockService.save(awardStocks);
		awardConsumeService.saveAwardConsumeLog(awardConsumes);
		return true;
	}


	@Override
	public boolean addAwardGrantAndAwardStockBySSJFX(AwardGrant awardGrant) {
		List<AwardStock> awardStocks=new ArrayList<AwardStock>();
		List<AwardMaterial> awardMaterials=new ArrayList<AwardMaterial>();
		List<AwardConsume> awardConsumes=new ArrayList<AwardConsume>();
		
		//指点商户的处理库存数
		if(StringUtils.isNotEmpty(awardGrant.getLevel1MaterialId())) {
			
			//减量
			AwardMaterial awardMaterial=awardMaterialService.getById(awardGrant.getLevel1MaterialId());
			if(awardMaterial.getStockNumber()<awardGrant.getLevel1Num()) {
				return false;
			}
			awardMaterial.setStockNumber(awardMaterial.getStockNumber()-awardGrant.getLevel1Num());
			awardMaterial.setLockNumber(awardMaterial.getLockNumber()+awardGrant.getLevel1Num());
			awardMaterials.add(awardMaterial);
			
//			AwardConsume awardConsume=new AwardConsume();
//			awardConsume.setFromMerchantId(awardGrant.getFromMerchantId());
//			awardConsume.setToMerchantId(awardGrant.getToMerchantId());
//			awardConsume.setAwardMaterialId(awardGrant.getLevel1MaterialId());
//			awardConsume.setNumber(awardGrant.getLevel1Num());
//			awardConsume.setType(1);
//			awardConsume.setRemarks("我向下分发");
//			awardConsumes.add(awardConsume);
			
			//增量
//			AwardStock tAwardStock=awardStockService.getAwardStockByMaterialId(awardGrant.getToMerchantId(), awardGrant.getLevel1MaterialId());
//			if(tAwardStock==null) {
//				tAwardStock=new AwardStock();
//				tAwardStock.setMerchantId(awardGrant.getToMerchantId());
//				tAwardStock.setAwardMaterialId(awardGrant.getLevel1MaterialId());
//				tAwardStock.setStockNumber(0);
//			}
//			tAwardStock.setStockNumber(tAwardStock.getStockNumber()+awardGrant.getLevel1Num());
//			awardStocks.add(tAwardStock);
//			
//			awardConsume=new AwardConsume();
//			awardConsume.setFromMerchantId(awardGrant.getFromMerchantId());
//			awardConsume.setToMerchantId(awardGrant.getToMerchantId());
//			awardConsume.setAwardMaterialId(awardGrant.getLevel1MaterialId());
//			awardConsume.setNumber(awardGrant.getLevel1Num());
//			awardConsume.setType(2);
//			awardConsume.setRemarks("上级向我分发");
//			awardConsumes.add(awardConsume);
			
		}
		
		if(StringUtils.isNotEmpty(awardGrant.getLevel2MaterialId())) {

			//减量
			AwardMaterial awardMaterial=awardMaterialService.getById(awardGrant.getLevel2MaterialId());
			if(awardMaterial.getStockNumber()<awardGrant.getLevel2Num()) {
				return false;
			}
			awardMaterial.setStockNumber(awardMaterial.getStockNumber()-awardGrant.getLevel2Num());
			awardMaterial.setLockNumber(awardMaterial.getLockNumber()+awardGrant.getLevel2Num());
			awardMaterials.add(awardMaterial);
			
//			AwardConsume awardConsume=new AwardConsume();
//			awardConsume.setFromMerchantId(awardGrant.getFromMerchantId());
//			awardConsume.setToMerchantId(awardGrant.getToMerchantId());
//			awardConsume.setAwardMaterialId(awardGrant.getLevel2MaterialId());
//			awardConsume.setNumber(awardGrant.getLevel2Num());
//			awardConsume.setType(1);
//			awardConsume.setRemarks("我向下分发");
//			awardConsumes.add(awardConsume);
//			
//			//增量
//			AwardStock tAwardStock=awardStockService.getAwardStockByMaterialId(awardGrant.getToMerchantId(), awardGrant.getLevel2MaterialId());
//			if(tAwardStock==null) {
//				tAwardStock=new AwardStock();
//				tAwardStock.setMerchantId(awardGrant.getToMerchantId());
//				tAwardStock.setAwardMaterialId(awardGrant.getLevel2MaterialId());
//				tAwardStock.setStockNumber(0);
//			}
//			tAwardStock.setStockNumber(tAwardStock.getStockNumber()+awardGrant.getLevel2Num());
//			awardStocks.add(tAwardStock);
			
			
//			awardConsume=new AwardConsume();
//			awardConsume.setFromMerchantId(awardGrant.getFromMerchantId());
//			awardConsume.setToMerchantId(awardGrant.getToMerchantId());
//			awardConsume.setAwardMaterialId(awardGrant.getLevel2MaterialId());
//			awardConsume.setNumber(awardGrant.getLevel2Num());
//			awardConsume.setType(2);
//			awardConsume.setRemarks("上级向我分发");
//			awardConsumes.add(awardConsume);
		}
		
		if(StringUtils.isNotEmpty(awardGrant.getLevel3MaterialId())) {
			
			//减量
			AwardMaterial awardMaterial=awardMaterialService.getById(awardGrant.getLevel3MaterialId());
			if(awardMaterial.getStockNumber()<awardGrant.getLevel3Num()) {
				return false;
			}
			awardMaterial.setStockNumber(awardMaterial.getStockNumber()-awardGrant.getLevel3Num());
			awardMaterial.setLockNumber(awardMaterial.getLockNumber()+awardGrant.getLevel3Num());
			awardMaterials.add(awardMaterial);

//			AwardConsume awardConsume=new AwardConsume();
//			awardConsume.setFromMerchantId(awardGrant.getFromMerchantId());
//			awardConsume.setToMerchantId(awardGrant.getToMerchantId());
//			awardConsume.setAwardMaterialId(awardGrant.getLevel3MaterialId());
//			awardConsume.setNumber(awardGrant.getLevel3Num());
//			awardConsume.setType(1);
//			awardConsume.setRemarks("我向下分发");
//			awardConsumes.add(awardConsume);
//			
//			//增量
//			AwardStock tAwardStock=awardStockService.getAwardStockByMaterialId(awardGrant.getToMerchantId(), awardGrant.getLevel3MaterialId());
//			if(tAwardStock==null) {
//				tAwardStock=new AwardStock();
//				tAwardStock.setMerchantId(awardGrant.getToMerchantId());
//				tAwardStock.setAwardMaterialId(awardGrant.getLevel3MaterialId());
//				tAwardStock.setStockNumber(0);
//			}
//			tAwardStock.setStockNumber(tAwardStock.getStockNumber()+awardGrant.getLevel3Num());
//			awardStocks.add(tAwardStock);
//			
//			awardConsume=new AwardConsume();
//			awardConsume.setFromMerchantId(awardGrant.getFromMerchantId());
//			awardConsume.setToMerchantId(awardGrant.getToMerchantId());
//			awardConsume.setAwardMaterialId(awardGrant.getLevel3MaterialId());
//			awardConsume.setNumber(awardGrant.getLevel3Num());
//			awardConsume.setType(2);
//			awardConsume.setRemarks("上级向我分发");
//			awardConsumes.add(awardConsume);
			
		}
		
		this.save(awardGrant);
		awardMaterialService.save(awardMaterials);
		awardStockService.save(awardStocks);
		awardConsumeService.saveAwardConsumeLog(awardConsumes);
		return true;
	}


	@Override
	public boolean updateAwardGrantByStatus(AwardGrant awardGrant, Integer status) {
		List<AwardStock> awardStocks=new ArrayList<AwardStock>();
		List<AwardMaterial> awardMaterials=new ArrayList<AwardMaterial>();
		List<AwardConsume> awardConsumes=new ArrayList<AwardConsume>();
		
		switch (status) {
			case 0://取消，解锁锁定的库存，返回总库存中
				if(awardGrant.getStatus()==0) {break;}
				
				if(StringUtils.isNotEmpty(awardGrant.getLevel1MaterialId())) {
					//返量
					AwardMaterial awardMaterial=awardMaterialService.getById(awardGrant.getLevel1MaterialId());
					
					awardMaterial.setStockNumber(awardMaterial.getStockNumber()+awardGrant.getLevel1Num());
					awardMaterial.setLockNumber(awardMaterial.getLockNumber()-awardGrant.getLevel1Num());
					awardMaterials.add(awardMaterial);
				}
				
				if(StringUtils.isNotEmpty(awardGrant.getLevel2MaterialId())) {
					//返量
					AwardMaterial awardMaterial=awardMaterialService.getById(awardGrant.getLevel2MaterialId());
					
					awardMaterial.setStockNumber(awardMaterial.getStockNumber()+awardGrant.getLevel2Num());
					awardMaterial.setLockNumber(awardMaterial.getLockNumber()-awardGrant.getLevel2Num());
					awardMaterials.add(awardMaterial);
				}
				
				if(StringUtils.isNotEmpty(awardGrant.getLevel3MaterialId())) {
					//返量
					AwardMaterial awardMaterial=awardMaterialService.getById(awardGrant.getLevel3MaterialId());
					
					awardMaterial.setStockNumber(awardMaterial.getStockNumber()+awardGrant.getLevel3Num());
					awardMaterial.setLockNumber(awardMaterial.getLockNumber()-awardGrant.getLevel3Num());
					awardMaterials.add(awardMaterial);
				}
				
				break;
			case 1://已通过，解锁锁定的库存，添加到使用库存中,添加到商户的总库存中
				//if(awardGrant.getStatus()==1) {break;}
				
				if(StringUtils.isNotEmpty(awardGrant.getLevel1MaterialId())) {
					//减量
					AwardMaterial awardMaterial=awardMaterialService.getById(awardGrant.getLevel1MaterialId());
					
					awardMaterial.setLockNumber(awardMaterial.getLockNumber()-awardGrant.getLevel1Num());
					awardMaterial.setUseNumber(awardMaterial.getUseNumber()+awardGrant.getLevel1Num());
					awardMaterials.add(awardMaterial);
					
					AwardConsume awardConsume=new AwardConsume();
					awardConsume.setFromMerchantId(awardGrant.getFromMerchantId());
					awardConsume.setToMerchantId(awardGrant.getToMerchantId());
					awardConsume.setAwardMaterialId(awardGrant.getLevel1MaterialId());
					awardConsume.setNumber(awardGrant.getLevel1Num());
					awardConsume.setType(1);
					awardConsume.setRemarks("我向下配送");
					awardConsumes.add(awardConsume);
					
					//增量
					AwardStock tAwardStock=awardStockService.getAwardStockByMaterialId(awardGrant.getToMerchantId(), awardGrant.getLevel1MaterialId());
					if(tAwardStock==null) {
						tAwardStock=new AwardStock();
						tAwardStock.setMerchantId(awardGrant.getToMerchantId());
						tAwardStock.setAwardMaterialId(awardGrant.getLevel1MaterialId());
						tAwardStock.setStockNumber(0);
						tAwardStock.setUseNumber(0);
					}
					tAwardStock.setStockNumber(tAwardStock.getStockNumber()+awardGrant.getLevel1Num());
					awardStocks.add(tAwardStock);
					
					awardConsume=new AwardConsume();
					awardConsume.setFromMerchantId(awardGrant.getFromMerchantId());
					awardConsume.setToMerchantId(awardGrant.getToMerchantId());
					awardConsume.setAwardMaterialId(awardGrant.getLevel1MaterialId());
					awardConsume.setNumber(awardGrant.getLevel1Num());
					awardConsume.setType(2);
					awardConsume.setRemarks("上级向我配送");
					awardConsumes.add(awardConsume);
					
				}
				
				if(StringUtils.isNotEmpty(awardGrant.getLevel2MaterialId())) {
					//减量
					AwardMaterial awardMaterial=awardMaterialService.getById(awardGrant.getLevel2MaterialId());
					
					awardMaterial.setLockNumber(awardMaterial.getLockNumber()-awardGrant.getLevel2Num());
					awardMaterial.setUseNumber(awardMaterial.getUseNumber()+awardGrant.getLevel2Num());
					awardMaterials.add(awardMaterial);
					
					
					AwardConsume awardConsume=new AwardConsume();
					awardConsume.setFromMerchantId(awardGrant.getFromMerchantId());
					awardConsume.setToMerchantId(awardGrant.getToMerchantId());
					awardConsume.setAwardMaterialId(awardGrant.getLevel2MaterialId());
					awardConsume.setNumber(awardGrant.getLevel2Num());
					awardConsume.setType(1);
					awardConsume.setRemarks("我向下配送");
					awardConsumes.add(awardConsume);
					
					//增量
					AwardStock tAwardStock=awardStockService.getAwardStockByMaterialId(awardGrant.getToMerchantId(), awardGrant.getLevel2MaterialId());
					if(tAwardStock==null) {
						tAwardStock=new AwardStock();
						tAwardStock.setMerchantId(awardGrant.getToMerchantId());
						tAwardStock.setAwardMaterialId(awardGrant.getLevel2MaterialId());
						tAwardStock.setStockNumber(0);
						tAwardStock.setUseNumber(0);
					}
					tAwardStock.setStockNumber(tAwardStock.getStockNumber()+awardGrant.getLevel2Num());
					awardStocks.add(tAwardStock);
					
					awardConsume=new AwardConsume();
					awardConsume.setFromMerchantId(awardGrant.getFromMerchantId());
					awardConsume.setToMerchantId(awardGrant.getToMerchantId());
					awardConsume.setAwardMaterialId(awardGrant.getLevel2MaterialId());
					awardConsume.setNumber(awardGrant.getLevel2Num());
					awardConsume.setType(2);
					awardConsume.setRemarks("上级向我配送");
					awardConsumes.add(awardConsume);
					
					
				}
				
				if(StringUtils.isNotEmpty(awardGrant.getLevel3MaterialId())) {
					//减量
					AwardMaterial awardMaterial=awardMaterialService.getById(awardGrant.getLevel3MaterialId());
					
					awardMaterial.setLockNumber(awardMaterial.getLockNumber()-awardGrant.getLevel3Num());
					awardMaterial.setUseNumber(awardMaterial.getUseNumber()+awardGrant.getLevel3Num());
					awardMaterials.add(awardMaterial);
					
					
					AwardConsume awardConsume=new AwardConsume();
					awardConsume.setFromMerchantId(awardGrant.getFromMerchantId());
					awardConsume.setToMerchantId(awardGrant.getToMerchantId());
					awardConsume.setAwardMaterialId(awardGrant.getLevel3MaterialId());
					awardConsume.setNumber(awardGrant.getLevel3Num());
					awardConsume.setType(1);
					awardConsume.setRemarks("我向下配送");
					awardConsumes.add(awardConsume);
					
					//增量
					AwardStock tAwardStock=awardStockService.getAwardStockByMaterialId(awardGrant.getToMerchantId(), awardGrant.getLevel3MaterialId());
					if(tAwardStock==null) {
						tAwardStock=new AwardStock();
						tAwardStock.setMerchantId(awardGrant.getToMerchantId());
						tAwardStock.setAwardMaterialId(awardGrant.getLevel3MaterialId());
						tAwardStock.setStockNumber(0);
						tAwardStock.setUseNumber(0);
					}
					tAwardStock.setStockNumber(tAwardStock.getStockNumber()+awardGrant.getLevel3Num());
					awardStocks.add(tAwardStock);
					
					awardConsume=new AwardConsume();
					awardConsume.setFromMerchantId(awardGrant.getFromMerchantId());
					awardConsume.setToMerchantId(awardGrant.getToMerchantId());
					awardConsume.setAwardMaterialId(awardGrant.getLevel3MaterialId());
					awardConsume.setNumber(awardGrant.getLevel3Num());
					awardConsume.setType(2);
					awardConsume.setRemarks("上级向我配送");
					awardConsumes.add(awardConsume);
					
				}
				
				
				
				break;
			case 2://审核中
				if(awardGrant.getStatus()==1) {break;}
				
				break;
			case 3://审核不通过，解锁锁定的库存，添加到使用库存中
				if(awardGrant.getStatus()==3) {break;}
				
				if(StringUtils.isNotEmpty(awardGrant.getLevel1MaterialId())) {
					//返量
					AwardMaterial awardMaterial=awardMaterialService.getById(awardGrant.getLevel1MaterialId());
					
					awardMaterial.setStockNumber(awardMaterial.getStockNumber()+awardGrant.getLevel1Num());
					awardMaterial.setLockNumber(awardMaterial.getLockNumber()-awardGrant.getLevel1Num());
					awardMaterials.add(awardMaterial);
				}
				
				if(StringUtils.isNotEmpty(awardGrant.getLevel2MaterialId())) {
					//返量
					AwardMaterial awardMaterial=awardMaterialService.getById(awardGrant.getLevel2MaterialId());
					
					awardMaterial.setStockNumber(awardMaterial.getStockNumber()+awardGrant.getLevel2Num());
					awardMaterial.setLockNumber(awardMaterial.getLockNumber()-awardGrant.getLevel2Num());
					awardMaterials.add(awardMaterial);
				}
				
				if(StringUtils.isNotEmpty(awardGrant.getLevel3MaterialId())) {
					//返量
					AwardMaterial awardMaterial=awardMaterialService.getById(awardGrant.getLevel3MaterialId());
					
					awardMaterial.setStockNumber(awardMaterial.getStockNumber()+awardGrant.getLevel3Num());
					awardMaterial.setLockNumber(awardMaterial.getLockNumber()-awardGrant.getLevel3Num());
					awardMaterials.add(awardMaterial);
				}
				
				break;
			default:
				break;
		}
		awardGrant.setStatus(status);
		awardGrant.setUpdateTime(new Date());
		this.save(awardGrant);
		awardMaterialService.save(awardMaterials);
		awardStockService.save(awardStocks);
		awardConsumeService.saveAwardConsumeLog(awardConsumes);
		return false;
	}



	
}
