package com.ruoyi.task.impl;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.circuit.service.ICIntegratedCircuitService;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.indica.service.ICIndicationInfoService;
import com.ruoyi.madridmark.service.ICMadridmarkService;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.task.TrademarkSyncTask;
import com.ruoyi.trade.service.ICTrademarkService;

@Service("trademarkSyncTask")
public class TrademarkSyncTaskImpl implements TrademarkSyncTask {

	private static final Logger logger = LoggerFactory.getLogger(TrademarkSyncTaskImpl.class);

	@Autowired
	private RuoYiConfig ruoYiConfig;

	@Autowired
	private ICTrademarkService cTrademarkService;
	
	@Autowired
	private ICIntegratedCircuitService iCIntegratedCircuitService;
	
	@Autowired
	private ICIndicationInfoService iCIndicationInfoService;

	@Autowired
	private ICMadridmarkService cMadridmarkService;
	@Autowired
	private ISysConfigService sysConfigService;
	
	@Override
	public void syncCircuit() {
		
		if (ruoYiConfig.getAutoImpPub()) {
			String beginDate = "19890101";

			String endDate = new SimpleDateFormat("yyyyMMdd").format(new Date());
			// 同步集成电路
			logger.debug("开始全量同步集成电路数据。。。");
			iCIntegratedCircuitService.doSyncForIprd(beginDate, endDate);

			// 同步地理标志
			logger.debug("开始同步地理标志数据");
			iCIndicationInfoService.doSyncForIprd(beginDate, endDate);

			// 同步马德里商标
			logger.debug("开始同步马德里商标数据");
			cMadridmarkService.doSyncForIprd(beginDate, endDate);
			logger.debug("同步同步马德里商标完成");
			
			String lastTimeKey = "trademark_last_time";
			SysConfig config = sysConfigService.selectConfigByKey2(lastTimeKey);
			config.setConfigValue(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
			sysConfigService.updateConfig(config);
			
			
			String lastTimeKey2 = "indication_last_time";
			SysConfig config2 = sysConfigService.selectConfigByKey2(lastTimeKey2);
			config2.setConfigValue(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
			sysConfigService.updateConfig(config2);
			
			
			String lastTimeKey3 = "circuit_last_time";
			SysConfig config3 = sysConfigService.selectConfigByKey2(lastTimeKey3);
			config3.setConfigValue(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
			sysConfigService.updateConfig(config3);
			
			
		}
		
		
	}
	@Override
	public void syncTrademark() {

		logger.debug("同步商标数据定时任务开始。。。");

		try {
			if (ruoYiConfig.getAutoImpPub()) {
				String beginDate = "19890101";
				String endDate = new SimpleDateFormat("yyyyMMdd").format(new Date());

				// 同步商标
				logger.debug("开始全量同步商标数据。。。");
				cTrademarkService.doSyncForIprd(beginDate, endDate);

				logger.debug("同步商标数据完成");

			} else {
				logger.warn("跳过同步商标任务");
			}
		} catch (Exception e) {
			logger.error("同步商标数据失败," + e.getMessage(), e);
		}

	}

}


