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

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.AwardMaterial;
import com.ssj.bean.sys.award.domain.AwardStock;
import com.ssj.bean.sys.fx.domain.Merchant;
import com.ssj.dao.sys.award.dao.AwardQueryDao;
import com.ssj.dao.sys.award.dao.AwardStockDao;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.service.sys.award.service.AwardMaterialService;
import com.ssj.service.sys.award.service.AwardStockService;
import com.ssj.service.sys.fx.service.MerchantService;

@Service(version = "1.0")
@Transactional
public class AwardStockServiceImpl extends BaseServiceImpl<AwardStock, String> implements AwardStockService {
	
	@Autowired
	private AwardStockDao  awardStockDao;

	@Autowired
	private AwardQueryDao  queryDao;
	
	@Autowired
	private MerchantService merchantService;
	
	@Autowired
	private AwardMaterialService awardMaterialService;
	

	@Override
	public PagingAndSortingRepository<AwardStock, String> getDao() {
		return awardStockDao;
	}


	@Override
	public AwardStock getAwardStockByMaterialId(String merchantId, String awardMaterialId) {
		return awardStockDao.getAwardStockByMaterialId(merchantId, awardMaterialId);
	}

	
	//得到商户下的奖品库存
	@Override
	public Integer getStockNumByMerchantIdAndLevelMaterialId(String merchantId, String levelMaterialId) {
		 Integer stock=0;
		 if(StringUtils.isNotEmpty(merchantId) && StringUtils.isNotEmpty(levelMaterialId)) {
			 Merchant merchant=merchantService.getById(merchantId);
			 //当前商户是SSJ-FX商户,查询的是指点下的奖品库存。
			 if("SSJ-FX".equals(merchant.getOrgCode())) {
				 AwardMaterial awardMaterial=awardMaterialService.getAwardMaterialById(levelMaterialId);
				 if(awardMaterial!=null) {
					 stock=awardMaterial.getStockNumber();
				 }
			 }else {
				 //查询出该商户奖品下有多少库存
				 AwardStock awardStock= this.getAwardStockByMaterialId(merchant.getId(), levelMaterialId);
				 if(awardStock!=null) {
					 stock=awardStock.getStockNumber();
				 }
			 }
		 }
		 return stock;
	}


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



	
}
