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

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

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.stereotype.Service;

import com.ssj.bean.sys.fx.domain.Merchant;
import com.ssj.bean.sys.fx.domain.MerchantSign;
import com.ssj.bean.weixin.libmy.domain.LibVip;
import com.ssj.bean.weixin.libmy.domain.TbLibJoin;
import com.ssj.dao.sys.fx.dao.FxQueryDao;
import com.ssj.dao.sys.fx.dao.MerchantSignDao;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.service.sys.fx.service.MerchantService;
import com.ssj.service.sys.fx.service.MerchantSignService;
import com.ssj.service.weixin.library.service.ILibJoinService;
import com.ssj.service.weixin.library.service.ILibVipService;


@Service
public class MerchantSignServiceImpl extends BaseServiceImpl<MerchantSign, String> implements MerchantSignService {

	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private MerchantSignDao dao;
	
    @Autowired
    private FxQueryDao queryDao;
	
	@Autowired
	private MerchantService merchantService;
	
	@Autowired
	private ILibJoinService libJoinService;
	
	@Autowired
	private ILibVipService libVipService;
	
    @Override
    public PagingAndSortingRepository<MerchantSign, String> getDao() {
        return dao;
    }

    //12点定时任务，处理套餐的数据
    //1：处理加油包过期的数据
    //2：处理商户主套餐过期的数据,加油包全过期。
    @Override
	public void runMerchantSignByEndTime() {
    	//1：处理加油包过期的数据
		List<MerchantSign> merchantSigns=dao.findMerchantSignByEndTime();
		for (MerchantSign merchantSign : merchantSigns) {
			try {
				merchantSign.setRemarks("加油包过期");
				merchantSign.setStatus(1);
				merchantSign.setUpdateTime(new Date());
				this.save(merchantSign);
				
				Merchant merchant = merchantService.getById(merchantSign.getMerchantId());
				merchant.setLibService(merchant.getLibService()-merchantSign.getLibService());
				merchantService.save(merchant);
	
				//同步商户表与机构表
				this.syncMerchantByLibJoin(merchant);
				
				//如果加油包过期后，现有的学生大于商户的套餐人数， 就全部学生停止服务。
				if(StringUtils.isNotEmpty(merchant.getLibId())) {
					List<LibVip> libVips=libVipService.findByLibId(merchant.getLibId());
					if(libVips.size()>merchant.getLibService()) {
						//这里需要调用灿哥的代码。
						
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		
		//2：处理商户主套餐过期的数据
		List<Merchant> merchants=merchantService.findMerchantSignByEndTime();
		for (Merchant merchant : merchants) {
			try {
				List<MerchantSign> signs=this.findMerchantSignByMerchantId(merchant.getId(), 0);
				for (MerchantSign merchantSign : signs) {
					merchantSign.setRemarks("主套餐过期引起加油包过期");
					merchantSign.setStatus(1);
					merchantSign.setUpdateTime(new Date());
					this.save(merchantSign);
				}
				
				merchant.setSignTime(null);
				merchant.setEndSignTime(null);
				merchant.setLibService(0);
				merchantService.save(merchant);
				
				this.syncMerchantByLibJoin(merchant);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
	}
    
    
	//套餐模式 【1优选套餐 2VIP套餐 3尊享套餐】 4加油包月包+月 5加油包量包+量。
	//现在暂时只记录加油的记录。
	@Override
	public void insertMerchantSignAndSync(String merchantId, Integer signType,Integer num) {
		Merchant merchant = merchantService.getById(merchantId);
		MerchantSign merchantSign=new MerchantSign();
		merchantSign.setMerchantId(merchantId);
		merchantSign.setSignType(signType);
		merchantSign.setNum(num);
		merchantSign.setStatus(0);
		merchantSign.setCreateTime(new Date());
		
		Calendar cal = Calendar.getInstance();
		cal.setTime(merchant.getSignTime());
		switch (signType) {
		case 4:
			cal.add(Calendar.MONTH, num);
			
			merchantSign.setLibService(10);
			merchantSign.setSignTime(merchant.getSignTime());
			merchantSign.setEndSignTime(cal.getTime());
			break;
		case 5:
			cal.add(Calendar.MONTH, 1);
			
			merchantSign.setLibService(10*num);
			merchantSign.setSignTime(merchant.getSignTime());
			merchantSign.setEndSignTime(cal.getTime());
			break;
		default:
			break;
		}
		//保存加油包的记录
		this.save(merchantSign);
		
		//增量商户表的人数
		merchant.setLibService(merchant.getLibService()+merchantSign.getLibService());
		merchantService.save(merchant);	
		
		//同步商户表与机构表
		this.syncMerchantByLibJoin(merchant);
	}

	
	@Override
	public void syncMerchantByLibJoin(Merchant merchant) {
		if(merchant!=null && StringUtils.isNotEmpty(merchant.getLibId())) {
			TbLibJoin libJoin = libJoinService.getById(merchant.getLibId());
			libJoin.setLibService(merchant.getLibService());
			libJoinService.save(libJoin);
		}
	}

	@Override
	public List<MerchantSign> findMerchantSignByMerchantId(String merchantId, Integer status) {
		return dao.findMerchantSignByMerchantId(merchantId, status);
	}

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

}


