package com.ruoyi;

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

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

import com.google.gson.Gson;
import com.ruoyi.adjust.service.IDAdjustSyncLogService;
import com.ruoyi.circuit.service.ICIntegratedCircuitService;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.dimp.service.IDPatentAuthInfoService;
import com.ruoyi.dimp.service.IDPatentEffectiveInfoService;
import com.ruoyi.indica.service.ICIndicationInfoService;
import com.ruoyi.madridmark.service.ICMadridmarkService;
import com.ruoyi.reportpub.service.IDPatentPubInfoService;
import com.ruoyi.system.domain.BladeRegion;
import com.ruoyi.system.service.IBladeRegionService;
import com.ruoyi.system.service.IDIpcTypeService;
import com.ruoyi.trade.service.ICTrademarkService;

/**
 * 启动程序
 *
 * @author ruoyi
 */
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
public class RuoYiApplication implements ApplicationRunner {

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

	@Autowired
	private RuoYiConfig ruoYiConfig;
	@Autowired
	private IDPatentPubInfoService dPatentPubInfoService;
	@Autowired
	private IDPatentAuthInfoService dPatentAuthInfoService;
	
	@Autowired
	private IDPatentEffectiveInfoService dPatentEffectiveInfoService;

	@Autowired
	private IBladeRegionService bladeRegionService;
	@Autowired
	private IDIpcTypeService dIpcTypeService;
	@Autowired
	private ICTrademarkService cTrademarkService;
	@Autowired
	private ICIntegratedCircuitService iCIntegratedCircuitService;
	@Autowired
	private ICIndicationInfoService iCIndicationInfoService;

	@Autowired
	private ICMadridmarkService cMadridmarkService;
	@Autowired
	private IDAdjustSyncLogService dAdjustSyncLogService;

	public static void main(String[] args) {
		SpringApplication.run(RuoYiApplication.class, args);
		System.out.println("(♥◠‿◠)ﾉﾞ  若依启动成功");
	}

	// 处理专利调整数据
	private void syncPatentAdjuest() {
		dAdjustSyncLogService.syncAdjuestAfterRestart();
	}

	private void syncArea() {
		List<BladeRegion> list = bladeRegionService.selectBladeRegionList(new BladeRegion());
		for (BladeRegion br : list) {
			BladeRegion qo = new BladeRegion();
			qo.setParentCode(br.getCode());
			br.setAreaList(bladeRegionService.selectBladeRegionList(qo));

			if (br.getAreaList() != null && !br.getAreaList().isEmpty()) {
				for (BladeRegion sbr : br.getAreaList()) {
					BladeRegion qo2 = new BladeRegion();
					qo2.setParentCode(sbr.getCode());
					sbr.setAreaList(bladeRegionService.selectBladeRegionList(qo2));
				}
			}
		}

		AreaItem item = new AreaItem();
		item.code = "53";
		item.name = "云南省";
		item.level = 0;
		List<AreaItem> areaList = new ArrayList<AreaItem>();
		item.areaList = areaList;

		int level = 1;
		for (BladeRegion br : list) {
			if (br.getCode().equals("53")) {
				continue;
			}
			item.areaList.add(toAreaItem(br, level));
		}

		System.out.println(new Gson().toJson(item));

	}

	private static AreaItem toAreaItem(BladeRegion br, int level) {
		AreaItem item = new AreaItem();
		item.code = br.getCode();
		item.level = level;
		String name = br.getName();
		if (StringUtils.isNotBlank(br.getCityShortName())) {
			name = br.getCityShortName().replace("-", "");
		} else if (StringUtils.isNotBlank(br.getDistrictShortName())) {
			name = br.getDistrictShortName();
		}

		item.name = name;
		if (br.getAreaList() != null && !br.getAreaList().isEmpty()) {
			List<AreaItem> areaList = new ArrayList<AreaItem>();
			item.areaList = areaList;
			int sublev = level + 1;
			for (BladeRegion sbr : br.getAreaList()) {
				item.areaList.add(toAreaItem(sbr, sublev));
			}
		}

		return item;
	}

	private static class AreaItem {
		String code;
		int level;
		String name;
		List<AreaItem> areaList;
	}

	public void refreshHv() {
		new Thread() {
			public void run() {
				try {
					dPatentAuthInfoService.doRefreshHv();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}.start();
		new Thread() {
			public void run() {
				try {
					dPatentEffectiveInfoService.doRefreshHv();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}.start();
	}

	
	/**
	 * 同步商标地区
	 * 
	 * 20231017
	 * 
	 */
	private void syncTrademarkArea() {
		cTrademarkService.syncTrademarkArea();
	}
	
	public static Thread authReMatchThread=null;
	public static Thread actReMatchThread=null;
	
	
	private void reMatch3() {
		authReMatchThread = new Thread() {
			public void run() {
				dPatentAuthInfoService.reMatch3();
			}
		};
		authReMatchThread.start();
		
		actReMatchThread = new Thread() {
			public void run() {
				dPatentEffectiveInfoService.reMatch3();
			}
		};
		actReMatchThread.start();
	}
	
	@Override
	public void run(ApplicationArguments args) throws Exception {
		new Thread() {
			public void run() {
				try {

//					syncArea();
					if (ruoYiConfig.getAutoImpPub()) {

//						refreshHv();
						String beginDate = "20210101";
						String endDate = new SimpleDateFormat("yyyyMMdd").format(new Date());
//						PatentPubImportUtils.imp(bladeRegionService, dIpcTypeService);

//						dPatentEffectiveInfoService.reMatch();
//						PatentPubImportUtils.impByIprd(ruoYiConfig, dPatentPubInfoService, dIpcTypeService, bladeRegionService, beginDate);
//						dPatentEffectiveInfoService.reMatch2();

						// 同步集成电路
//						iCIntegratedCircuitService.doSyncForIprd(beginDate, endDate);
//						
//						//同步地理标志
//						iCIndicationInfoService.doSyncForIprd(beginDate, endDate);
//						
////						//同步马德里商标
//						cMadridmarkService.doSyncForIprd(beginDate, endDate);
//						
////						//同步商标
//						cTrademarkService.doSyncForIprd(beginDate, endDate);

					}
					if (ruoYiConfig.getLoadPub()) {
//						dPatentPubInfoService.loadToCache();
//						dPatentAuthInfoService.cleanData();
//						dPatentEffectiveInfoService.cleanData();
					}
					
					//test
//					syncTrademarkArea();
//					reMatch3();
					
					syncPatentAdjuest();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}.start();
	}
}
