package com.ssj.service.weixin.library.service.impl;

import java.util.Arrays;
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.fx.domain.Merchant;
import com.ssj.bean.weixin.libmy.domain.LibVip;
import com.ssj.bean.weixin.libmy.domain.TbLibJoinConsume;
import com.ssj.dao.weixin.library.dao.ILibJoinConsumeDao;
import com.ssj.dao.weixin.library.dao.ILibJoinConsumeQueryDao;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.service.sys.fx.service.MerchantService;
import com.ssj.service.weixin.library.service.ILibJoinConsumeService;
import com.ssj.service.weixin.library.service.ILibVipService;

@Service(version="1.0")
@Transactional
public class LibJoinServiceConsumeImpl extends BaseServiceImpl<TbLibJoinConsume, String> implements ILibJoinConsumeService{
	
	@Autowired
	private ILibJoinConsumeDao  dao;
	
	@Autowired
    private ILibVipService vipService;
	
	@Autowired
	private ILibJoinConsumeQueryDao  queryDao;
	
	@Autowired
	private MerchantService  merchantService;
	
	@Override
	public PagingAndSortingRepository<TbLibJoinConsume, String> getDao() {
		return dao;
	}

	//通过商户添加币
	@Override
	public void rechargeMerchantConsume(String merchantId, Integer libServiceNum, Integer platformServiceNum) {
		//同步币
		merchantService.syncMerchantByLibJoin(merchantId);
		try {
			Merchant merchant=merchantService.getById(merchantId);
			TbLibJoinConsume consume=new TbLibJoinConsume();
			consume.setToLibId(merchant!=null ? merchant.getLibId() : null);
			consume.setToMerchantId(merchant.getId());
			consume.setLibService(libServiceNum);
			consume.setPlatformService(platformServiceNum);
			consume.setType(1);
			consume.setRemarks("充值");
			consume.setCreateTime(new Date());
			dao.save(consume);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
	//通过机构添加币
	@Override
	public void rechargeTbLibJoinConsume(String libId, Integer libServiceNum, Integer platformServiceNum) {
		//同步币
		merchantService.syncLibJoinByMerchant(libId);
		try {
			Merchant merchant=null;
			if(StringUtils.isNotEmpty(libId)) {
				merchant=merchantService.getMerchantByLibId(libId);
			}
			TbLibJoinConsume consume=new TbLibJoinConsume();
			consume.setToLibId(libId);
			consume.setToMerchantId(merchant!=null ? merchant.getId() : null);
			consume.setLibService(libServiceNum);
			consume.setPlatformService(platformServiceNum);
			consume.setType(1);
			consume.setRemarks("充值");
			consume.setCreateTime(new Date());
			dao.save(consume);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}

	//机构消费币
	@Override
	public void consumeTbLibJoinConsume(String libId, Integer libServiceNum, Integer platformServiceNum,String vipId) {
		/*//同步币
		merchantService.syncLibJoinByMerchant(libId);
		try {
			Merchant merchant=null;
			if(StringUtils.isNotEmpty(libId)) {
				merchant=merchantService.getMerchantByLibId(libId);
			}
			TbLibJoinConsume consume=new TbLibJoinConsume();
			consume.setFromLibId(libId);
			consume.setFromMerchantId(merchant!=null ? merchant.getId() : null);
			consume.setLibService(libServiceNum);
			consume.setPlatformService(platformServiceNum);
			consume.setType(2);
			consume.setRemarks("消费");
			consume.setCreateTime(new Date());
			
			if(StringUtils.isNotEmpty(vipId)) {
				consume.setVipId(vipId);
				LibVip libVip=vipService.getById(vipId);
				if(libVip!=null) {
					consume.setDerver(libVip.getDerver());
				}
			}
			this.save(consume);
		} catch (Exception e) {
			e.printStackTrace();
		}*/
		this.batchConsumeTbLibJoinConsume(libId, libServiceNum, platformServiceNum, Arrays.asList(vipId));
	}

	@Override
	public void batchConsumeTbLibJoinConsume(String libId, Integer libServiceNum, Integer platformServiceNum, List<String> vipIdList){
		//同步币
		merchantService.syncLibJoinByMerchant(libId);
		try {
			Merchant merchant = null;
			if (StringUtils.isNotEmpty(libId)) {
				merchant = merchantService.getMerchantByLibId(libId);
			}
			for (String vipId : vipIdList) {
				TbLibJoinConsume consume = new TbLibJoinConsume();
				consume.setFromLibId(libId);
				consume.setFromMerchantId(merchant != null ? merchant.getId() : null);
				consume.setLibService(libServiceNum);
				consume.setPlatformService(platformServiceNum);
				consume.setType(2);
				consume.setRemarks("消费");
				consume.setCreateTime(new Date());

				if (StringUtils.isNotEmpty(vipId)) {
					consume.setVipId(vipId);
					LibVip libVip = vipService.getById(vipId);
					if (libVip != null) {
						consume.setDerver(libVip.getDerver());
					}
				}
				this.save(consume);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	//通过libId分配币
	@Override
	public void allotTbLibJoinConsume(String fromLibId, String toLibId, Integer libServiceNum,Integer platformServiceNum) {
		//同步币
		merchantService.syncLibJoinByMerchant(fromLibId);
		//同步币
		merchantService.syncLibJoinByMerchant(toLibId);
		try {
			Merchant fMerchant=null;
			Merchant tMerchant=null;
			if(StringUtils.isNotEmpty(fromLibId)) {
				fMerchant=merchantService.getMerchantByLibId(fromLibId);
			}
			if(StringUtils.isNotEmpty(toLibId)) {
				tMerchant=merchantService.getMerchantByLibId(toLibId);
			}
			TbLibJoinConsume fromConsume=new TbLibJoinConsume();
			fromConsume.setFromLibId(fromLibId);
			fromConsume.setToLibId(toLibId);
			fromConsume.setFromMerchantId(fMerchant!=null ? fMerchant.getId() : null);
			fromConsume.setToMerchantId(tMerchant!=null ? tMerchant.getId() : null);
			fromConsume.setLibService(libServiceNum);
			fromConsume.setPlatformService(platformServiceNum);
			fromConsume.setType(5);
			fromConsume.setRemarks("向下分配币，"+fromLibId+"-->"+toLibId);
			fromConsume.setCreateTime(new Date());
			this.save(fromConsume);
			
			TbLibJoinConsume toConsume=new TbLibJoinConsume();
			fromConsume.setFromLibId(fromLibId);
			fromConsume.setToLibId(toLibId);
			fromConsume.setFromMerchantId(fMerchant!=null ? fMerchant.getId() : null);
			fromConsume.setToMerchantId(tMerchant!=null ? tMerchant.getId() : null);
			toConsume.setLibService(libServiceNum);
			toConsume.setPlatformService(platformServiceNum);
			toConsume.setType(4);
			toConsume.setRemarks("上级分配币，"+fromLibId+"-->"+toLibId);
			toConsume.setCreateTime(new Date());
			this.save(toConsume);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//通过MerchantId分配币
	@Override
	public void allotMerchantConsume(String fromMerchant, String toMerchant, Integer libServiceNum,Integer platformServiceNum) {

		//同步币
		merchantService.syncMerchantByLibJoin(fromMerchant);

		//同步币
		merchantService.syncMerchantByLibJoin(toMerchant);
		
		Merchant fMerchant=null;
		Merchant tMerchant=null;
		if(StringUtils.isNotEmpty(fromMerchant)) {
			fMerchant=merchantService.getById(fromMerchant);
		}
		if(StringUtils.isNotEmpty(toMerchant)) {
			tMerchant=merchantService.getById(toMerchant);
		}

		try {
			
		   TbLibJoinConsume tbLibJoinConsume=new TbLibJoinConsume();
		   tbLibJoinConsume.setFromMerchantId(fromMerchant);
		   tbLibJoinConsume.setToMerchantId(toMerchant);
		   tbLibJoinConsume.setFromLibId(fMerchant!=null ? fMerchant.getLibId() : null);
		   tbLibJoinConsume.setToLibId(tMerchant!=null ? tMerchant.getLibId() : null);
		   tbLibJoinConsume.setLibService(libServiceNum);
	       tbLibJoinConsume.setPlatformService(platformServiceNum);
		   tbLibJoinConsume.setRemarks("上级分配币，"+fromMerchant+"-->"+toMerchant);
		   tbLibJoinConsume.setType(4);
		   tbLibJoinConsume.setCreateTime(new Date());
		   this.save(tbLibJoinConsume);

		   tbLibJoinConsume=new TbLibJoinConsume();
		   tbLibJoinConsume.setFromMerchantId(fromMerchant);
		   tbLibJoinConsume.setToMerchantId(toMerchant);
		   tbLibJoinConsume.setFromLibId(fMerchant!=null ? fMerchant.getLibId() : null);
		   tbLibJoinConsume.setToLibId(tMerchant!=null ? tMerchant.getLibId() : null);
		   tbLibJoinConsume.setLibService(libServiceNum);
	       tbLibJoinConsume.setPlatformService(platformServiceNum);
		   tbLibJoinConsume.setRemarks("向下级分配币 ，"+fromMerchant+"-->"+toMerchant);
		   tbLibJoinConsume.setType(5);
		   tbLibJoinConsume.setCreateTime(new Date());
		   this.save(tbLibJoinConsume);
		   
		} catch (Exception e) {
			e.printStackTrace();
		}
	   
	}
	

	@Override
	public Integer countTbLibJoinConsumeByConsumeTime(String[] libIds, Date startTime, Date endTime) {
		return dao.countTbLibJoinConsumeByConsumeTime(libIds, startTime, endTime);
	}
	
	@Override
	public Integer countTbLibJoinDistributionByConsumeTime(String[] libIds, Date startTime, Date endTime) {
		return dao.countTbLibJoinDistributionByConsumeTime(libIds, startTime, endTime);
	}
	
	@Override
	public Integer countMerchantConsumeByConsumeTime(String[] merchantIds, Date startTime, Date endTime) {
		return dao.countMerchantConsumeByConsumeTime(merchantIds, startTime, endTime);
	}

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

	
	 
}