/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 运维系统
 * 创建日期:2022/8/22
 */
package org.springblade.modules.baseinfo.region.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.mysql.cj.log.Log;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.constant.BusinessConstant;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.baseinfo.org.entity.DeptRegionEntity;
import org.springblade.modules.baseinfo.org.service.IDeptRegionService;
import org.springblade.modules.baseinfo.region.dto.RegionInfoDTO;
import org.springblade.modules.baseinfo.region.entity.RegionInfoEntity;
import org.springblade.modules.baseinfo.region.entity.RegionTreeNodeEntity;
import org.springblade.modules.baseinfo.region.service.IBaseInfoRegionService;
import org.springblade.modules.baseinfo.region.service.IOrgRegionInfoService;
import org.springblade.modules.baseinfo.region.vo.RegionInfoVO;
import org.springblade.modules.baseinfo.region.wrapper.RegionInfoWrapper;
import org.springblade.modules.etl.entity.EtlAdCdEntity;
import org.springblade.modules.etl.service.IEtlAdCdService;
import org.springblade.modules.system.entity.Dept;
import org.springblade.modules.system.service.IDeptService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;


/***
 * Date:2022/8/22
 * Title:行政区划管理
 * Description：对本文件的详细描述，原则上不能少于30字
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@RestController
@Slf4j
@RequestMapping("galaxy-business/baseinfo/region")
@RequiredArgsConstructor
@Api(value = "行政区划管理", tags = "行政区划管理")
public class BaseInfoRegionController extends BladeController {
	private final IBaseInfoRegionService baseInfoRegionService;
	private final IDeptService deptService;
	private final IEtlAdCdService etlAdCdService;
	private final IDeptRegionService deptRegionService;

	@Value("${region-ver-config.ver}")
	private String regionVer;

	@Value("${region-ver-config.original-ver}")
	private String originalRgionVer;

	@Resource
	private RedisTemplate redisTemplate;

	/**
	 * 排除行政区划
	 *
	 * @param dto
	 * @return
	 */
	@PostMapping("/exclude")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "修改", notes = "传入 RegionInfoDTO")
	public R excludeRegion(@RequestBody RegionInfoDTO dto) {
		this.baseInfoRegionService.excludeRegion(dto);
		this.baseInfoRegionService.updateAdminRegionCache();
		this.baseInfoRegionService.updateDeptRegionCache(dto.getDeptId());
		return R.status(true);
	}

	/**
	 * 重置行政区划
	 *
	 * @param dto
	 * @return
	 */
	@PostMapping("/reset")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "修改", notes = "传入 RegionInfoDTO")
	public R resetRegion(@RequestBody RegionInfoDTO dto) {
		this.baseInfoRegionService.resetRegion(dto);
		this.baseInfoRegionService.updateAdminRegionCache();
		this.baseInfoRegionService.updateDeptRegionCache(dto.getDeptId());
		return R.status(true);
	}


	@GetMapping("/children/list")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "分页")
	public R<List<RegionInfoEntity>> childrenList() {
		BladeUser user = AuthUtil.getUser();
		if (BusinessConstant.ROLE_SYS_ADMIN.equals(user.getRoleName())) {
			LambdaQueryWrapper<RegionInfoEntity> wrapper = Wrappers.<RegionInfoEntity>query().lambda();
			wrapper.eq(RegionInfoEntity::getIsDeleted, 0);
			wrapper.eq(RegionInfoEntity::getParentCode, BusinessConstant.REGION_NM_ADCODE);
			List<RegionInfoEntity> list = baseInfoRegionService.list(wrapper);
			return R.data(list);
		} else if (BusinessConstant.ROLE_ORG_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_COMPANY_ADMIN.equals(user.getRoleName())) {
			LambdaQueryWrapper<DeptRegionEntity> deptRegionEntityLambdaQueryWrapper = Wrappers.<DeptRegionEntity>query().lambda();
			deptRegionEntityLambdaQueryWrapper.eq(DeptRegionEntity::getIsDeleted, 0);
			deptRegionEntityLambdaQueryWrapper.eq(DeptRegionEntity::getDeptId, Func.toLong(user.getDeptId()));
			DeptRegionEntity deptRegionEntity = deptRegionService.getOne(deptRegionEntityLambdaQueryWrapper);
			LambdaQueryWrapper<RegionInfoEntity> regionInfoEntityLambdaQueryWrapper = Wrappers.<RegionInfoEntity>query().lambda();
			regionInfoEntityLambdaQueryWrapper.eq(RegionInfoEntity::getIsDeleted, 0);
			regionInfoEntityLambdaQueryWrapper.eq(RegionInfoEntity::getParentCode, deptRegionEntity.getAdcd());
			List<RegionInfoEntity> list = baseInfoRegionService.list(regionInfoEntityLambdaQueryWrapper);
			return R.data(list);
		}
		return R.data(null);
	}

	@GetMapping("/children/list/{parentCode}")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "分页")
	public R<List<RegionInfoEntity>> childrenList(@PathVariable(name = "parentCode") String parentCode) {
		LambdaQueryWrapper<RegionInfoEntity> wrapper = Wrappers.<RegionInfoEntity>query().lambda();
		wrapper.eq(RegionInfoEntity::getIsDeleted, 0);
		wrapper.eq(RegionInfoEntity::getParentCode, parentCode);
		List<RegionInfoEntity> list = baseInfoRegionService.list(wrapper);
		return R.data(list);
	}

	@GetMapping("/original/tree")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "树")
	public R<List<RegionTreeNodeEntity>> regionTree() {
		ValueOperations<String, String> opsHash = redisTemplate.opsForValue();
		List<RegionTreeNodeEntity> nodeEntities = new LinkedList<>();
		BladeUser user = AuthUtil.getUser();
		if (BusinessConstant.ROLE_SYS_ADMIN.equals(user.getRoleName())) {
			boolean updateRegion = false;
			String redisRegionVer = opsHash.get("region.original.ver.admin");
			if (null == redisRegionVer || redisRegionVer.length() == 0 || !redisRegionVer.equals(this.originalRgionVer)) {
				updateRegion = true;
			} else {
				String regionJson = opsHash.get("region.original.admin");
				if (null != regionJson) {
					nodeEntities = JSON.parseObject(regionJson, new TypeReference<List<RegionTreeNodeEntity>>() {
					});
				} else {
					updateRegion = true;
				}
			}
			if (updateRegion) {
				LambdaQueryWrapper<RegionInfoEntity> queryWrapper = Wrappers.<RegionInfoEntity>query().lambda();
				queryWrapper.eq(RegionInfoEntity::getParentCode, BusinessConstant.REGION_ROOT_ADCODE);
				List<RegionInfoEntity> list = baseInfoRegionService.list(queryWrapper);
				for (RegionInfoEntity entity : list) {
					RegionTreeNodeEntity nodeEntity = new RegionTreeNodeEntity();
					nodeEntity.setId(entity.getAdcd());
					nodeEntity.setValue(entity.getAdcd());
					nodeEntity.setTitle(entity.getAdnm());
					nodeEntity.setText(entity.getAdnm());
					nodeEntity.setKey(entity.getAdcd());
					nodeEntity.setParentId(entity.getParentCode());
					if (getOriginalTreeChildren(nodeEntity, entity)) {
						if (nodeEntity.getChildren() != null && nodeEntity.getChildren().size() > 0) {
							nodeEntity.setHasChildren(true);
						} else {
							nodeEntity.setHasChildren(false);
						}
					}
					nodeEntities.add(nodeEntity);
				}
				opsHash.set("region.original.admin", JSON.toJSONString(nodeEntities));
				opsHash.set("region.original.ver.admin", this.originalRgionVer);
			}
		} else {
			boolean updateRegion = false;
			String redisRegionVer = opsHash.get("region.original.ver." + user.getDeptId());
			if (null == redisRegionVer || redisRegionVer.length() == 0 || !redisRegionVer.equals(this.originalRgionVer)) {
				updateRegion = true;
			} else {
				String regionJson = opsHash.get("region.original." + user.getDeptId());
				if (null != regionJson) {
					nodeEntities = JSON.parseObject(regionJson, new TypeReference<List<RegionTreeNodeEntity>>() {
					});
				} else {
					updateRegion = true;
				}
			}
			if (updateRegion) {
				LambdaQueryWrapper<DeptRegionEntity> deptRegionEntityLambdaQueryWrapper = Wrappers.<DeptRegionEntity>query().lambda();
				deptRegionEntityLambdaQueryWrapper.eq(DeptRegionEntity::getDeptId, Func.toLong(user.getDeptId()));
				DeptRegionEntity deptRegionEntity = deptRegionService.getOne(deptRegionEntityLambdaQueryWrapper);
				LambdaQueryWrapper<RegionInfoEntity> queryWrapper = Wrappers.<RegionInfoEntity>query().lambda();
				queryWrapper.eq(RegionInfoEntity::getAdcd, deptRegionEntity.getAdcd());
				List<RegionInfoEntity> list = baseInfoRegionService.list(queryWrapper);
				for (RegionInfoEntity entity : list) {
					RegionTreeNodeEntity nodeEntity = new RegionTreeNodeEntity();
					nodeEntity.setId(entity.getAdcd());
					nodeEntity.setValue(entity.getAdcd());
					nodeEntity.setTitle(entity.getAdnm());
					nodeEntity.setText(entity.getAdnm());
					nodeEntity.setKey(entity.getAdcd());
					nodeEntity.setParentId(entity.getParentCode());
					if (getOriginalTreeChildren(nodeEntity, entity)) {
						if (nodeEntity.getChildren() != null && nodeEntity.getChildren().size() > 0) {
							nodeEntity.setHasChildren(true);
						} else {
							nodeEntity.setHasChildren(false);
						}
					}
					nodeEntities.add(nodeEntity);
				}
				opsHash.set("region.original." + user.getDeptId(), JSON.toJSONString(nodeEntities));
				opsHash.set("region.original.ver." + user.getDeptId(), this.originalRgionVer);
			}
		}
		return R.data(nodeEntities);
	}


	@GetMapping("/original/tree/{adcd}")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "树")
	public R<List<RegionTreeNodeEntity>> regionTree(@PathVariable(name = "adcd") String adcd) {
		List<RegionTreeNodeEntity> nodeEntities = new LinkedList<>();
		LambdaQueryWrapper<RegionInfoEntity> queryWrapper = Wrappers.<RegionInfoEntity>query().lambda();
		queryWrapper.eq(RegionInfoEntity::getAdcd, adcd);
		List<RegionInfoEntity> list = baseInfoRegionService.list(queryWrapper);
		for (RegionInfoEntity entity : list) {
			RegionTreeNodeEntity nodeEntity = new RegionTreeNodeEntity();
			nodeEntity.setId(entity.getAdcd());
			nodeEntity.setValue(entity.getAdcd());
			nodeEntity.setTitle(entity.getAdnm());
			nodeEntity.setText(entity.getAdnm());
			nodeEntity.setKey(entity.getAdcd());
			nodeEntity.setParentId(entity.getParentCode());
			if (getOriginalTreeChildren(nodeEntity, entity)) {
				if (nodeEntity.getChildren() != null && nodeEntity.getChildren().size() > 0) {
					nodeEntity.setHasChildren(true);
				} else {
					nodeEntity.setHasChildren(false);
				}
			}
			nodeEntities.add(nodeEntity);
		}
		return R.data(nodeEntities);
	}

	private boolean getOriginalTreeChildren(RegionTreeNodeEntity nodeEntity, RegionInfoEntity regionInfoEntity) {
		LambdaQueryWrapper<RegionInfoEntity> queryWrapper = Wrappers.<RegionInfoEntity>query().lambda();
		queryWrapper.eq(RegionInfoEntity::getParentCode, regionInfoEntity.getAdcd());
		List<RegionInfoEntity> list = baseInfoRegionService.list(queryWrapper);
		if (null != list && list.size() > 0) {
			List<RegionTreeNodeEntity> nodeEntities = new LinkedList<>();
			for (RegionInfoEntity entity : list) {
				RegionTreeNodeEntity node = new RegionTreeNodeEntity();
				node.setId(entity.getAdcd());
				node.setValue(entity.getAdcd());
				node.setTitle(entity.getAdnm());
				node.setText(entity.getAdnm());
				node.setKey(entity.getAdcd());
				node.setParentId(entity.getParentCode());
				if (getOriginalTreeChildren(node, entity)) {
					if (node.getChildren() != null && node.getChildren().size() > 0) {
						node.setHasChildren(true);
					} else {
						node.setHasChildren(false);
					}
				}
				nodeEntities.add(node);
			}
			nodeEntity.setChildren(nodeEntities);
		}
		return true;
	}

	@GetMapping("/tree")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "树")
	public R<List<RegionTreeNodeEntity>> virtuallyRegionTree() {
		ValueOperations<String, String> opsHash = redisTemplate.opsForValue();
		List<RegionTreeNodeEntity> nodeEntities = new LinkedList<>();
		BladeUser user = AuthUtil.getUser();
		if (BusinessConstant.ROLE_SYS_ADMIN.equals(user.getRoleName())) {
			String redisRegionVer = opsHash.get("region.ver.admin");
			boolean updateRegion = false;
			if (null == redisRegionVer || redisRegionVer.length() == 0 || !redisRegionVer.equals(this.regionVer)) {
				updateRegion = true;
			} else {
				String regionJson = opsHash.get("region.admin");
				if (null != regionJson) {
					nodeEntities = JSON.parseObject(regionJson, new TypeReference<List<RegionTreeNodeEntity>>() {
					});
				} else {
					updateRegion = true;
				}
			}
			if (updateRegion) {
				//根节点
				LambdaQueryWrapper<RegionInfoEntity> queryWrapper = Wrappers.<RegionInfoEntity>query().lambda();
				queryWrapper.eq(RegionInfoEntity::getAdcd, BusinessConstant.REGION_NM_ADCODE);
				RegionInfoEntity nmRegion = baseInfoRegionService.getOne(queryWrapper);
				RegionTreeNodeEntity nmEntity = new RegionTreeNodeEntity();
				nmEntity.setId(nmRegion.getAdcd());
				nmEntity.setValue(nmRegion.getAdcd());
				nmEntity.setTitle(nmRegion.getAdnm());
				nmEntity.setText(nmRegion.getAdnm());
				nmEntity.setKey(nmRegion.getAdcd());
				nmEntity.setExclude(0);
				nmEntity.setParentId(nmRegion.getParentCode());
				//查找子节点
				LambdaQueryWrapper<Dept> deptqueryWrapper = Wrappers.<Dept>query().lambda();
				deptqueryWrapper.eq(Dept::getParentId, Func.toLong(user.getDeptId()));
				List<Dept> deptList = deptService.list(deptqueryWrapper);
				if (null != deptList) {
					List<RegionTreeNodeEntity> subNodeEntities = new LinkedList<>();
					for (Dept dept : deptList) {
						LambdaQueryWrapper<DeptRegionEntity> deptRegionQueryWrapper = Wrappers.<DeptRegionEntity>query().lambda();
						deptRegionQueryWrapper.eq(DeptRegionEntity::getDeptId, dept.getId());
						DeptRegionEntity deptRegionEntity = deptRegionService.getOne(deptRegionQueryWrapper);
						if (null != deptRegionEntity) {
							LambdaQueryWrapper<RegionInfoEntity> nodeQueryWrapper = Wrappers.<RegionInfoEntity>query().lambda();
							nodeQueryWrapper.eq(RegionInfoEntity::getAdcd, deptRegionEntity.getAdcd());
							RegionInfoEntity nodeRegion = baseInfoRegionService.getOne(nodeQueryWrapper);
							if (null != nodeRegion) {
								RegionTreeNodeEntity node = new RegionTreeNodeEntity();
								node.setId(nodeRegion.getAdcd());
								node.setValue(nodeRegion.getAdcd());
								node.setTitle(nodeRegion.getAdnm());
								node.setText(nodeRegion.getAdnm());
								node.setKey(nodeRegion.getAdcd());
								int excludeCode = 0;
								if (nodeRegion.getParentCode().equals(BusinessConstant.REGION_NM_ADCODE)) {
									excludeCode = 1;
								}
								node.setExclude(excludeCode);
								node.setParentId(nodeRegion.getParentCode());
								if (getChildren(node, nodeRegion, excludeCode)) {
									if (node.getChildren() != null && node.getChildren().size() > 0) {
										node.setHasChildren(true);
									} else {
										node.setHasChildren(false);
									}
								}
								subNodeEntities.add(node);
							}
						}
					}
					nmEntity.setChildren(subNodeEntities);
				}
				if (nmEntity.getChildren() != null && nmEntity.getChildren().size() > 0) {
					nmEntity.setHasChildren(true);
				} else {
					nmEntity.setHasChildren(false);
				}
				nodeEntities.add(nmEntity);
				//更新缓存
				opsHash.set("region.admin", JSON.toJSONString(nodeEntities));
				opsHash.set("region.ver.admin", this.regionVer);
			}
		} else {
			String redisRegionVer = opsHash.get("region.ver." + user.getDeptId());
			boolean updateRegion = false;
			if (null == redisRegionVer || redisRegionVer.length() == 0 || !redisRegionVer.equals(this.regionVer)) {
				updateRegion = true;
			} else {
				String regionJson = opsHash.get("region." + user.getDeptId());
				if (null != regionJson) {
					nodeEntities = JSON.parseObject(regionJson, new TypeReference<List<RegionTreeNodeEntity>>() {
					});
				} else {
					updateRegion = true;
				}
			}
			if (updateRegion) {
				//根节点
				LambdaQueryWrapper<DeptRegionEntity> deptRegionEntityLambdaQueryWrapper = Wrappers.<DeptRegionEntity>query().lambda();
				deptRegionEntityLambdaQueryWrapper.eq(DeptRegionEntity::getDeptId, Func.toLong(user.getDeptId()));
				DeptRegionEntity rootDeptRegionEntity = deptRegionService.getOne(deptRegionEntityLambdaQueryWrapper);
				if (null != rootDeptRegionEntity) {
					LambdaQueryWrapper<RegionInfoEntity> queryWrapper = Wrappers.<RegionInfoEntity>query().lambda();
					queryWrapper.eq(RegionInfoEntity::getAdcd, rootDeptRegionEntity.getAdcd());
					RegionInfoEntity orgRegion = baseInfoRegionService.getOne(queryWrapper);
					int excludeCode = 0;
					if (orgRegion.getParentCode().equals(BusinessConstant.REGION_NM_ADCODE)) {
						excludeCode = 1;
					}
					RegionTreeNodeEntity orgEntity = new RegionTreeNodeEntity();
					orgEntity.setId(orgRegion.getAdcd());
					orgEntity.setValue(orgRegion.getAdcd());
					orgEntity.setTitle(orgRegion.getAdnm());
					orgEntity.setText(orgRegion.getAdnm());
					orgEntity.setKey(orgRegion.getAdcd());
					orgEntity.setExclude(excludeCode);
					orgEntity.setParentId(orgRegion.getParentCode());
					//查找子节点
					if (getChildren(orgEntity, orgRegion, excludeCode)) {
						if (orgEntity.getChildren() != null && orgEntity.getChildren().size() > 0) {
							orgEntity.setHasChildren(true);
						} else {
							orgEntity.setHasChildren(false);
						}
					}
					nodeEntities.add(orgEntity);
					opsHash.set("region." + user.getDeptId(), JSON.toJSONString(nodeEntities));
					opsHash.set("region.ver." + user.getDeptId(), this.regionVer);
				}
			}
		}
		return R.data(nodeEntities);
	}

	private boolean getChildren(RegionTreeNodeEntity nodeEntity, RegionInfoEntity regionInfoEntity, int exclude) {
		LambdaQueryWrapper<RegionInfoEntity> queryWrapper = Wrappers.<RegionInfoEntity>query().lambda();
		queryWrapper.eq(RegionInfoEntity::getParentCode, regionInfoEntity.getAdcd());
		if (exclude == 1) {
			queryWrapper.eq(RegionInfoEntity::getExcludeAd, 0);
		}
		List<RegionInfoEntity> list = baseInfoRegionService.list(queryWrapper);
		if (null != list && list.size() > 0) {
			List<RegionTreeNodeEntity> nodeEntities = new LinkedList<>();
			for (RegionInfoEntity entity : list) {
				RegionTreeNodeEntity node = new RegionTreeNodeEntity();
				node.setId(entity.getAdcd());
				node.setValue(entity.getAdcd());
				node.setTitle(entity.getAdnm());
				node.setText(entity.getAdnm());
				node.setKey(entity.getAdcd());
				node.setExclude(exclude);
				node.setParentId(entity.getParentCode());
				if (getChildren(node, entity, exclude)) {
					if (node.getChildren() != null && node.getChildren().size() > 0) {
						node.setHasChildren(true);
					} else {
						node.setHasChildren(false);
					}
				}
				nodeEntities.add(node);
			}
			nodeEntity.setChildren(nodeEntities);
		}
		return true;
	}

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入RegionInfoEntity")
	public R<RegionInfoVO> detail(RegionInfoDTO regionInfoDTO) {
		regionInfoDTO.setAdcd(regionInfoDTO.getCode());
		regionInfoDTO.setIsDeleted(0);
		RegionInfoEntity detail = baseInfoRegionService.getOne(Condition.getQueryWrapper(regionInfoDTO));
		return R.data(RegionInfoWrapper.build().entityVO(detail));
	}

	/**
	 * 从宏图数据导入
	 */
	@PostMapping("/import")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "regionLevel", value = "行政区划层级", paramType = "query", dataType = "int"),
		@ApiImplicitParam(name = "isCovered", value = "是否覆盖", paramType = "query", dataType = "int"),
	})
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "导入", notes = "传入RegionInfoDTO")
	public R importSubmit(@RequestBody RegionInfoDTO regionInfoDTO) {
		if (regionInfoDTO.getIsCovered() == 1) {
			LambdaUpdateWrapper<RegionInfoEntity> updateWrapper = Wrappers.<RegionInfoEntity>update().lambda();
			updateWrapper.ne(RegionInfoEntity::getAdcd, "00");
			updateWrapper.eq(RegionInfoEntity::getIsDeleted, 0);
			baseInfoRegionService.remove(updateWrapper);
		}
		EtlAdCdEntity dto = new EtlAdCdEntity();
		dto.setSuperioradcd("0");
		List<EtlAdCdEntity> list = etlAdCdService.selectList(dto);
		if (null != list && list.size() == 1) {
			String code = list.get(0).getAdcd();
			LambdaQueryWrapper<RegionInfoEntity> requestWrapper = Wrappers.<RegionInfoEntity>query().lambda();
			requestWrapper.eq(RegionInfoEntity::getAdcd, code);
			requestWrapper.eq(RegionInfoEntity::getIsDeleted, 0);
			RegionInfoEntity regionInfoEntity = baseInfoRegionService.getOne(requestWrapper);
			if (null != regionInfoEntity) {
				regionInfoEntity.setAdnm(list.get(0).getAdnm());
				baseInfoRegionService.updateById(regionInfoEntity);
			} else {
				regionInfoEntity = new RegionInfoEntity();
				regionInfoEntity.setAdcd(code);
				regionInfoEntity.setAdnm(list.get(0).getAdnm());
				regionInfoEntity.setParentCode("00");
				regionInfoEntity.setAncestors("00");
				regionInfoEntity.setStatus(1);
				regionInfoEntity.setIsDeleted(0);
				baseInfoRegionService.save(regionInfoEntity);
			}
			this.importAdcd(regionInfoDTO.getRegionLevel(), 2, code, "00," + code);
		}
		return R.status(true);
	}

	private boolean importAdcd(int level, int currLevel, String parentCode, String ancestors) {
		if (currLevel > level) {
			return true;
		}
		EtlAdCdEntity dto = new EtlAdCdEntity();
		dto.setSuperioradcd(parentCode);
		List<EtlAdCdEntity> list = etlAdCdService.selectList(dto);
		if (null != list && list.size() > 0) {
			for (EtlAdCdEntity adCdEntity : list) {
				LambdaQueryWrapper<RegionInfoEntity> requestWrapper = Wrappers.<RegionInfoEntity>query().lambda();
				requestWrapper.eq(RegionInfoEntity::getAdcd, adCdEntity.getAdcd());
				requestWrapper.eq(RegionInfoEntity::getIsDeleted, 0);
				RegionInfoEntity regionInfoEntity = baseInfoRegionService.getOne(requestWrapper);
				if (null != regionInfoEntity) {
					regionInfoEntity.setAdnm(adCdEntity.getAdnm());
					baseInfoRegionService.updateById(regionInfoEntity);
				} else {
					regionInfoEntity = new RegionInfoEntity();
					regionInfoEntity.setAdcd(adCdEntity.getAdcd());
					regionInfoEntity.setAdnm(adCdEntity.getAdnm());
					regionInfoEntity.setParentCode(parentCode);
					regionInfoEntity.setAncestors(ancestors);
					regionInfoEntity.setStatus(1);
					regionInfoEntity.setIsDeleted(0);
					baseInfoRegionService.save(regionInfoEntity);
				}
				importAdcd(level, currLevel + 1, adCdEntity.getAdcd(), ancestors + "," + adCdEntity.getAdcd());
			}
		}
		return true;
	}

}
