/**
 * Copyright
 * All right reserved.
 * 项目名称:
 * 创建日期:2022/5/22
 */
package org.springblade.etl;


import lombok.extern.slf4j.Slf4j;
import org.springblade.etl.entity.EtlAdCdEntity;
import org.springblade.etl.service.IEtlAdCdService;
import org.springblade.modules.baseinfo.region.whole.entity.BaseRegionInfoEntity;
import org.springblade.modules.baseinfo.region.whole.service.IBaseRegionInfoService;

import java.util.List;
import java.util.concurrent.Callable;

/***
 * Date:2022/5/22
 * Title: 行政区划数据抽取
 * Description：
 * @author swp
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@Slf4j
public class EtlRegionDataTask implements Callable<Integer> {


	private IEtlAdCdService etlAdCdService;
	private IBaseRegionInfoService baseRegionInfoService;


	public EtlRegionDataTask(IEtlAdCdService etlAdCdService, IBaseRegionInfoService baseRegionInfoService) {
		this.etlAdCdService = etlAdCdService;
		this.baseRegionInfoService = baseRegionInfoService;
	}

	private void crateChildRegion(BaseRegionInfoEntity root, String ancestors){
		EtlAdCdEntity etlAdCdEntity = new EtlAdCdEntity();
		etlAdCdEntity.setSuperioradcd(root.getAdcd());
		List<EtlAdCdEntity> list =etlAdCdService.selectList(etlAdCdEntity);
		for (EtlAdCdEntity entity:list){
			BaseRegionInfoEntity baseRegionInfoEntity= new BaseRegionInfoEntity();
			baseRegionInfoEntity.setAdcd(entity.getAdcd());
			baseRegionInfoEntity.setAdnm(entity.getAdnm());
			baseRegionInfoEntity.setParentCode(root.getAdcd());
			baseRegionInfoEntity.setAncestors(ancestors+","+root.getAdcd());
			baseRegionInfoEntity.setAddType(entity.getAddtype());
			baseRegionInfoEntity.setLgtd(entity.getLgtd());
			baseRegionInfoEntity.setLttd(entity.getLttd());
			baseRegionInfoService.save(baseRegionInfoEntity);
			crateChildRegion(baseRegionInfoEntity,baseRegionInfoEntity.getAncestors());
		}
	}


	@Override
	public Integer call() {
		try {
			log.info("行政区划数据抽取开始**************");
			EtlAdCdEntity etlAdCdEntity = new EtlAdCdEntity();
			etlAdCdEntity.setAdcd("150000000000000");
			List<EtlAdCdEntity> list =etlAdCdService.selectList(etlAdCdEntity);
			for (EtlAdCdEntity entity:list){
				BaseRegionInfoEntity baseRegionInfoEntity= new BaseRegionInfoEntity();
				baseRegionInfoEntity.setAdcd(entity.getAdcd());
				baseRegionInfoEntity.setAdnm(entity.getAdnm());
				baseRegionInfoEntity.setParentCode("00");
				baseRegionInfoEntity.setAncestors("00");
				baseRegionInfoEntity.setAddType(entity.getAddtype());
				baseRegionInfoEntity.setLgtd(entity.getLgtd());
				baseRegionInfoEntity.setLttd(entity.getLttd());
				baseRegionInfoService.save(baseRegionInfoEntity);
				crateChildRegion(baseRegionInfoEntity,baseRegionInfoEntity.getAncestors());
			}

			log.info("行政区划数据抽取完成**************");
		} catch (Exception e) {
			log.error("实时数据统计任务异常 {}", e.getMessage());
		}
		return 0;
	}
}
