/**
 * Copyright
 * All right reserved.
 * 项目名称:运维系统
 * 创建日期:2022/5/22
 */
package org.springblade.modules.map.task;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.oss.model.BladeFile;
import org.springblade.modules.map.entity.*;
import org.springblade.modules.map.service.*;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;

/***
 * Date:2022/5/22
 * Title: 河道水位抽取
 * Description：
 * @author swp
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@Slf4j
public class DangerAreaLeaveLineImportTask implements Callable<Integer> {

	private BladeFile bladeFile;
	private DangerAreaLeaveLineImportRecordEntity recordEntity;
	private IDangerAreaLeaveLineService leaveLineService;
	private IDangerAreaLeaveLineImportServcie leaveLineImportServcie;
	private IDangerAreaLeaveLinePointService leaveLinePointService;


	public DangerAreaLeaveLineImportTask(BladeFile bladeFile, DangerAreaLeaveLineImportRecordEntity recordEntity, IDangerAreaLeaveLineService leaveLineService,  IDangerAreaLeaveLinePointService leaveLinePointService,IDangerAreaLeaveLineImportServcie leaveLineImportServcie) {
		this.bladeFile = bladeFile;
		this.recordEntity = recordEntity;
		this.leaveLineService = leaveLineService;
		this.leaveLineImportServcie = leaveLineImportServcie;
		this.leaveLinePointService = leaveLinePointService;
	}

	@Override
	public Integer call() {
		try {
			log.info("转移路线导入任务开始执行*********************");
			URL url = new URL(bladeFile.getLink());
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			//设置超时间为3秒
			conn.setConnectTimeout(3 * 1000);
			//防止屏蔽程序抓取而返回403错误
			conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
			//得到输入流
			InputStream inputStream = conn.getInputStream();
			String fileName = bladeFile.getOriginalName();
			if (!fileName.endsWith("geojson")) {
				recordEntity.setImportStatus(2L);
				leaveLineImportServcie.saveOrUpdate(recordEntity);
				return 0;
			}

			JSONObject jsonObject = JSONObject.parseObject(inputStream, JSONObject.class);
			JSONArray featureList = jsonObject.getJSONArray("features");
			for (int j = 0; j < featureList.size(); j++) {
				DangerAreaLeaveLineEntity entity = new DangerAreaLeaveLineEntity();
				JSONObject feature = featureList.getJSONObject(j);
				JSONObject properties = feature.getJSONObject("properties");
				String pid = properties.getString("pid");
				String name = properties.getString("name");
				String adcd = properties.getString("ADCD");
				String address = properties.getString("address");
				String remark = properties.getString("remark");
				entity.setLeaveLinePid(pid);
				entity.setLeaveLineName(name);
				entity.setAdcd(adcd);


				if (null != address && address.length() > 0) {
					entity.setAddress(address);
				}

				if (null != remark && remark.length() > 0) {
					entity.setRemark(remark);
				}

				JSONObject geometry = feature.getJSONObject("geometry");
				JSONArray coordinates = geometry.getJSONArray("coordinates");
				JSONArray locationList = coordinates.getJSONArray(0);
				List<DangerAreaLeaveLinePointEntity> pointEntityList = new ArrayList<>();
				for (int i = 0; i < locationList.size(); i++) {
					JSONArray location = locationList.getJSONArray(i);
					DangerAreaLeaveLinePointEntity pointEntity = new DangerAreaLeaveLinePointEntity();
					pointEntity.setPointLng(location.getDoubleValue(0));
					pointEntity.setPointLat(location.getDoubleValue(1));
					pointEntityList.add(pointEntity);

				}

				if (pointEntityList.size() > 0) {

					entity.setCenterPointLat(pointEntityList.get(0).getPointLat());
					entity.setCenterPointLng(pointEntityList.get(0).getPointLng());
				}


				LambdaQueryWrapper<DangerAreaLeaveLineEntity> wrapper = Wrappers.<DangerAreaLeaveLineEntity>query().lambda();
				wrapper.eq(DangerAreaLeaveLineEntity::getLeaveLinePid, entity.getLeaveLinePid());
				DangerAreaLeaveLineEntity dangerAreaEntity = leaveLineService.getOne(wrapper);
				if (null == dangerAreaEntity) {
					dangerAreaEntity = new DangerAreaLeaveLineEntity();
					dangerAreaEntity.setLeaveLinePid(entity.getLeaveLinePid());
				}
				dangerAreaEntity.setLeaveLineName(entity.getLeaveLineName());
				dangerAreaEntity.setCenterPointLng(entity.getCenterPointLng());
				dangerAreaEntity.setCenterPointLat(entity.getCenterPointLat());
				dangerAreaEntity.setAddress(entity.getAddress());
				dangerAreaEntity.setRemark(entity.getRemark());
				dangerAreaEntity.setAdcd(entity.getAdcd());

				if (leaveLineService.saveOrUpdate(dangerAreaEntity)) {
					for (DangerAreaLeaveLinePointEntity pointEntity : pointEntityList) {
						pointEntity.setLeaveLineId(dangerAreaEntity.getId());

					}
					leaveLinePointService.saveBatch(pointEntityList);
				}
			}


			recordEntity.setImportStatus(1L);
			leaveLineImportServcie.saveOrUpdate(recordEntity);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("导入任务异常 {}", e.getMessage());
			try {
				recordEntity.setImportStatus(2L);
				leaveLineImportServcie.saveOrUpdate(recordEntity);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
		return 0;
	}
}
