package com.ssj.task.sz;

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

import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.ssj.bean.sys.fx.domain.Merchant;
import com.ssj.framework.core.util.SystemResourceLocator;
import com.ssj.service.sys.fx.service.MerchantBackupService;
import com.ssj.service.sys.fx.service.MerchantMealService;
import com.ssj.service.sys.fx.service.MerchantService;


@DisallowConcurrentExecution
@Component
public class MerchantSummaryTask implements Job{

	private static Logger logger = LoggerFactory.getLogger(MerchantSummaryTask.class);
	
	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		
		//先进行上一个月的数据备份，再进行下面的结算。
		try{
			Calendar calendar=Calendar.getInstance();
			calendar.setTime(new Date());
			int day=calendar.get(Calendar.DATE);
			if(day==1) {
				MerchantBackupService merchantBackupService = SystemResourceLocator.getBean(MerchantBackupService.class);
				merchantBackupService.runMerchantBackup();
			}
		}catch (Exception e) {
			logger.error("【商户备份】月商户数据备份定时任务，异常："+e.getMessage());
		}
		
		MerchantService merchantService = SystemResourceLocator.getBean(MerchantService.class);
		//0点系统刷新服务人数与日期
		try{
			MerchantMealService merchantMealService = SystemResourceLocator.getBean(MerchantMealService.class);
			List<Merchant> merchants=merchantService.findMerchantSignByAll();
			for (Merchant merchant : merchants) {
				try {
					logger.info("0点系统刷新服务人数与日期，开始>id="+merchant.getId());
					merchantMealService.updateMerchantByMeal(merchant.getId());
				} catch (Exception e) {
					logger.info("0点系统刷新服务人数与日期，异常...>id="+merchant.getId());
					e.printStackTrace();
				}
				//停2秒
				Thread.sleep(2000);
			}
		}catch (Exception e) {
			logger.error("【套餐模式】处理套餐的数据定时任务，异常："+e.getMessage());
		}
		
		
		//12点定时任务，处理签约模式的数据
		try{
			logger.error("【签约模式】结算扣币定时任务，开始...");
			//merchantService.runSummaryByMerchant_new();
			logger.error("【签约模式】结算扣币定时任务，结束。");
		}catch (Exception e) {
			logger.error("【签约模式】结算扣币定时任务，异常："+e.getMessage());
		}
		

		
		

	}	
}
