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


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springblade.core.excel.listener.DataListener;
import org.springblade.core.oss.model.BladeFile;
import org.springblade.core.tool.utils.ConcurrentDateFormat;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.baseinfo.rtu.entity.RtuInfoEntity;
import org.springblade.modules.baseinfo.rtu.service.IRtuBaseInfoService;
import org.springblade.modules.manage.entity.RtuImportRecordDetailEntity;
import org.springblade.modules.manage.entity.RtuImportRecordEntity;
import org.springblade.modules.manage.entity.RtuInfoTableDataEntity;
import org.springblade.modules.manage.service.IRtuImportRecordDetailService;
import org.springblade.modules.manage.service.IRtuImportRecordService;
import org.springblade.modules.system.entity.DictBiz;
import org.springblade.modules.system.service.IDictBizService;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;
import java.util.concurrent.Callable;

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

	private BladeFile bladeFile;
	private RtuImportRecordEntity recordEntity;
	private IRtuBaseInfoService rtuBaseInfoService;
	private IDictBizService dictBizService;
	private IRtuImportRecordService rtuImportRecordService;
	private IRtuImportRecordDetailService rtuImportRecordDetailService;


	public RtuImportTask(BladeFile bladeFile, RtuImportRecordEntity recordEntity, IRtuBaseInfoService rtuBaseInfoService, IRtuImportRecordService rtuImportRecordService, IRtuImportRecordDetailService rtuImportRecordDetailService,IDictBizService dictBizService) {
		this.bladeFile = bladeFile;
		this.recordEntity = recordEntity;
		this.rtuBaseInfoService = rtuBaseInfoService;
		this.rtuImportRecordService = rtuImportRecordService;
		this.rtuImportRecordDetailService = rtuImportRecordDetailService;
		this.dictBizService =dictBizService;
	}

	private String getLng(String lngText) {
		ArrayList<String> textArr = new ArrayList<>();
		textArr.add("");
		for (int l = 0; l < lngText.length(); l++) {
			String subtext = lngText.substring(l, l + 1);
			if (Func.isNumeric(subtext)) {
				String str = textArr.get(textArr.size() - 1);
				str += subtext;
				textArr.set(textArr.size() - 1, str);
			} else {
				textArr.add("");
			}
		}
		ArrayList<String> tmp = new ArrayList<>();
		for (String text : textArr) {
			if (text.length() > 0) {
				tmp.add(text);
			}
		}
		if (tmp.size() >= 4) {
			//度分秒，秒有小数点
			String v = tmp.get(2) + "." + tmp.get(3);
			double lat = Double.parseDouble(v);
			lat = lat / 60.0;
			v = tmp.get(1);
			lat = (Double.parseDouble(v) + lat) / 60.0;
			v = tmp.get(0);
			lat = Double.parseDouble(v) + lat;
			return String.format("%.06f", lat);
		} else if (tmp.size() == 3) {
			//度分秒
			String v = tmp.get(2);
			double lat = Double.parseDouble(v);
			lat = lat / 60.0;
			v = tmp.get(1);
			lat = (Double.parseDouble(v) + lat) / 60.0;
			v = tmp.get(0);
			lat = Double.parseDouble(v) + lat;
			return String.format("%.06f", lat);
		} else if (tmp.size() == 2) {
			//度
			String v = tmp.get(0) + "." + tmp.get(1);
			double lat = Double.parseDouble(v);
			return String.format("%.06f", lat);
		}
		return null;
	}

	private String getLat(String latText) {
		ArrayList<String> textArr = new ArrayList<>();
		textArr.add("");
		for (int l = 0; l < latText.length(); l++) {
			String subtext = latText.substring(l, l + 1);
			if (Func.isNumeric(subtext)) {
				String str = textArr.get(textArr.size() - 1);
				str += subtext;
				textArr.set(textArr.size() - 1, str);
			} else {
				textArr.add("");
			}
		}
		ArrayList<String> tmp = new ArrayList<>();
		for (String text : textArr) {
			if (text.length() > 0) {
				tmp.add(text);
			}
		}
		if (tmp.size() >= 4) {
			//度分秒，秒有小数点
			String v = tmp.get(2) + "." + tmp.get(3);
			double lat = Double.parseDouble(v);
			lat = lat / 60.0;
			v = tmp.get(1);
			lat = (Double.parseDouble(v) + lat) / 60.0;
			v = tmp.get(0);
			lat = Double.parseDouble(v) + lat;
			return String.format("%.06f", lat);
		} else if (tmp.size() == 3) {
			//度分秒
			String v = tmp.get(2);
			double lat = Double.parseDouble(v);
			lat = lat / 60.0;
			v = tmp.get(1);
			lat = (Double.parseDouble(v) + lat) / 60.0;
			v = tmp.get(0);
			lat = Double.parseDouble(v) + lat;
			return String.format("%.06f", lat);
		} else if (tmp.size() == 2) {
			//度
			String v = tmp.get(0) + "." + tmp.get(1);
			double lat = Double.parseDouble(v);
			return String.format("%.06f", lat);
		}
		return null;
	}

	private Date getDate(String dateText) {
		ArrayList<String> textArr = new ArrayList<>();
		textArr.add("");
		for (int l = 0; l < dateText.length(); l++) {
			String subtext = dateText.substring(l, l + 1);
			if (Func.isNumeric(subtext)) {
				String str = textArr.get(textArr.size() - 1);
				str += subtext;
				textArr.set(textArr.size() - 1, str);
			} else {
				textArr.add("");
			}
		}
		ArrayList<String> tmp = new ArrayList<>();
		for (String text : textArr) {
			if (text.length() > 0) {
				tmp.add(text);
			}
		}
		if (tmp.size() >= 3) {
			String v = tmp.get(0) + "-" + StringUtils.leftPad(tmp.get(1), 2, "0") + "-" + StringUtils.leftPad(tmp.get(2), 2, "0");
			return Func.parse(v, ConcurrentDateFormat.of("yyyy-MM-dd"));
		} else if (tmp.size() == 2) {
			String v = tmp.get(0) + "-" + StringUtils.leftPad(tmp.get(1), 2, "0") + "-01";
			return Func.parse(v, ConcurrentDateFormat.of("yyyy-MM-dd"));
		}
		return null;
	}

	private void copyEntity(RtuInfoEntity target, RtuInfoEntity source) {
		if (null != source.getRtuName()) {
			target.setRtuName(source.getRtuName());
		}
		if (null != source.getAdCity()) {
			target.setAdCity(source.getAdCity());
		}
		if (null != source.getAdDist()) {
			target.setAdDist(source.getAdDist());
		}
		if (null != source.getIsRain()) {
			target.setIsRain(source.getIsRain());
		}
		if (null != source.getIsRiver()) {
			target.setIsRiver(source.getIsRiver());
		}
		if (null != source.getIsRes()) {
			target.setIsRes(source.getIsRes());
		}
		if (null != source.getIsGround()) {
			target.setIsGround(source.getIsGround());
		}
		if (null != source.getIsVideo()) {
			target.setIsVideo(source.getIsVideo());
		}
		if (null != source.getRtuKinds()) {
			target.setRtuKinds(source.getRtuKinds());
		}
		if (null != source.getLocationDesc()) {
			target.setLocationDesc(source.getLocationDesc());
		}
		if (null != source.getLng()) {
			target.setLng(source.getLng());
		}
		if (null != source.getLat()) {
			target.setLat(source.getLat());
		}
		if (null != source.getManageCompany()) {
			target.setManageCompany(source.getManageCompany());
		}
		if (null != source.getRtuBrand()) {
			target.setRtuBrand(source.getRtuBrand());
		}
		if (null != source.getRtuModel()) {
			target.setRtuModel(source.getRtuModel());
		}
		if (null != source.getRtuReplaceDate()) {
			target.setRtuReplaceDate(source.getRtuReplaceDate());
		}
		if (null != source.getDatagramToQu()) {
			target.setDatagramToQu(source.getDatagramToQu());
		}
		if (null != source.getDatagramToShi()) {
			target.setDatagramToShi(source.getDatagramToShi());
		}
		if (null != source.getDatagramToSheng()) {
			target.setDatagramToSheng(source.getDatagramToSheng());
		}
		if (null != source.getRainSensorBrand()) {
			target.setRainSensorBrand(source.getRainSensorBrand());
		}
		if (null != source.getRainSensorModel()) {
			target.setRainSensorModel(source.getRainSensorModel());
		}
		if (null != source.getRainSensorReplaceDate()) {
			target.setRainSensorReplaceDate(source.getRainSensorReplaceDate());
		}
		if (null != source.getWaterSensorBrand()) {
			target.setWaterSensorBrand(source.getWaterSensorBrand());
		}
		if (null != source.getWaterSensorModel()) {
			target.setWaterSensorModel(source.getWaterSensorModel());
		}
		if (null != source.getWaterSensorReplaceDate()) {
			target.setWaterSensorReplaceDate(source.getWaterSensorReplaceDate());
		}
		if (null != source.getWaterSensorType()) {
			target.setWaterSensorType(source.getWaterSensorType());
		}
		if (null != source.getGroundSensorBrand()) {
			target.setGroundSensorBrand(source.getGroundSensorBrand());
		}
		if (null != source.getGroundSensorModel()) {
			target.setGroundSensorModel(source.getGroundSensorModel());
		}
		if (null != source.getGroundSensorReplaceDate()) {
			target.setGroundSensorReplaceDate(source.getGroundSensorReplaceDate());
		}
		if (null != source.getGroundSensorType()) {
			target.setGroundSensorType(source.getGroundSensorType());
		}
		if (null != source.getBatteryModel()) {
			target.setBatteryModel(source.getBatteryModel());
		}
		if (null != source.getBatteryReplaceDate()) {
			target.setBatteryReplaceDate(source.getBatteryReplaceDate());
		}
		if (null != source.getSunPowerModel()) {
			target.setSunPowerModel(source.getSunPowerModel());
		}
		if (null != source.getSunPowerControllerBrand()) {
			target.setSunPowerControllerBrand(source.getSunPowerControllerBrand());
		}
		if (null != source.getSunPowerControllerModel()) {
			target.setSunPowerControllerModel(source.getSunPowerControllerModel());
		}
		if (null != source.getNetworkSimId()) {
			target.setNetworkSimId(source.getNetworkSimId());
		}
		if (null != source.getNetworkPayer()) {
			target.setNetworkPayer(source.getNetworkPayer());
		}
		if (null != source.getNetworkPayEndDate()) {
			target.setNetworkPayEndDate(source.getNetworkPayEndDate());
		}
		if (null != source.getSatelliteModel()) {
			target.setSatelliteModel(source.getSatelliteModel());
		}
		if (null != source.getIsSatellite()) {
			target.setIsSatellite(source.getIsSatellite());
		}
		if (null != source.getRemark()) {
			target.setRemark(source.getRemark());
		}
		if (null != source.getAdCode()) {
			target.setAdCode(source.getAdCode());
		}
		if (null != source.getAdName()) {
			target.setAdName(source.getAdName());
		}
	}

	@Override
	public Integer call() {
		try {
			log.info("RTU导入任务开始执行*********************");
			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("xlsx")) {
				recordEntity.setImportStatus(2L);
				rtuImportRecordService.saveOrUpdate(recordEntity);
				return 0;
			}
			DataListener<RtuInfoTableDataEntity> dataListener = new DataListener<>();
			InputStream inputStreambuff = new BufferedInputStream(inputStream);
			ExcelReaderBuilder builder = EasyExcel.read(inputStreambuff, RtuInfoTableDataEntity.class, dataListener);
			((ExcelReaderSheetBuilder) builder.sheet(0).headRowNumber(3)).doRead();
			List<RtuInfoTableDataEntity> list = dataListener.getDataList();
			if (null == list) {
				recordEntity.setImportStatus(2L);
				rtuImportRecordService.saveOrUpdate(recordEntity);
				return 0;
			}
			//检查数据
			boolean dataErrorCheck = false;
			String errorInfo = "";
			for (RtuInfoTableDataEntity dataEntity : list) {
				log.info("data {}", Func.toJson(dataEntity));
				if (null != dataEntity.getRtuCode()) {
					String rtuCode = dataEntity.getRtuCode().trim();
					if (rtuCode.length() > 0) {
						if (!isLetterDigit(rtuCode)) {
							errorInfo = "测站编码字符格式错误：" + rtuCode;
							dataErrorCheck = true;
							break;
						}
					} else {
						errorInfo = "测站编码不能为空";
						dataErrorCheck = true;
						break;
					}
				} else {
					errorInfo = "测站编码不能为空";
					dataErrorCheck = true;
					break;
				}
				if (null != dataEntity.getRtuName()) {
					String rtuName = dataEntity.getRtuName().trim();
					if (rtuName.length() == 0) {
						errorInfo = "测站名称不能为空";
						dataErrorCheck = true;
						break;
					}
				} else {
					errorInfo = "测站名称不能为空";
					dataErrorCheck = true;
					break;
				}
				if (null != dataEntity.getCity()) {
					String city = dataEntity.getCity().trim();
					if (city.length() == 0) {
						errorInfo = "盟市不能为空";
						dataErrorCheck = true;
						break;
					}
				} else {
					errorInfo = "盟市不能为空";
					dataErrorCheck = true;
					break;
				}
				if (null != dataEntity.getDist()) {
					String dist = dataEntity.getDist().trim();
					if (dist.length() == 0) {
						errorInfo = "旗县区不能为空";
						dataErrorCheck = true;
						break;
					}
				} else {
					errorInfo = "旗县区不能为空";
					dataErrorCheck = true;
					break;
				}
				if (null != dataEntity.getRtuKind()) {
					String rtuKind = dataEntity.getRtuKind().trim();
					if (rtuKind.length() == 0) {
						errorInfo = "站点类型不能为空";
						dataErrorCheck = true;
						break;
					}
				} else {
					errorInfo = "站点类型不能为空";
					dataErrorCheck = true;
					break;
				}
//				if (null != dataEntity.getManageCompany()) {
//					String company = dataEntity.getManageCompany().trim();
//					if (company.length() == 0) {
//						dataErrorCheck = true;
//						break;
//					}
//				} else {
//					dataErrorCheck = true;
//					break;
//				}

				//查找行政区划
				RtuInfoEntity rtuInfoEntity = new RtuInfoEntity();
				if (null != dataEntity.getCity() && dataEntity.getCity().trim().length() > 0) {
					rtuInfoEntity.setAdCity(dataEntity.getCity().trim());
				}
				if (null != dataEntity.getDist() && dataEntity.getDist().trim().length() > 0) {
					rtuInfoEntity.setAdDist(dataEntity.getDist().trim());
				}
				RtuInfoEntity adcd = rtuBaseInfoService.getAdcd(rtuInfoEntity);
				if (adcd == null || adcd.getAdCode() == null) {
					errorInfo = "无法匹配到一个行政区划: " + rtuInfoEntity.getAdCity() + "/" + rtuInfoEntity.getAdDist();
					dataErrorCheck = true;
					break;
				}
			}
			//数据异常
			if (dataErrorCheck) {
				recordEntity.setImportStatus(2L);
				recordEntity.setRemark(errorInfo);
				rtuImportRecordService.saveOrUpdate(recordEntity);
				return 0;
			}

			//处理数据
			for (RtuInfoTableDataEntity dataEntity : list) {
				String rtuCode = dataEntity.getRtuCode().trim();
//				if (rtuCode.contains("40512880")){
//					log.info("40512880");
//				}
				if (rtuCode.length() < 8) {
					rtuCode = StringUtils.leftPad(rtuCode, 8, "0");
				}
				RtuInfoEntity rtuInfoEntity = new RtuInfoEntity();
				rtuInfoEntity.setRtuCode(rtuCode);
				if (null != dataEntity.getCity() && dataEntity.getCity().trim().length() > 0) {
					rtuInfoEntity.setAdCity(dataEntity.getCity().trim());
				}
				if (null != dataEntity.getDist() && dataEntity.getDist().trim().length() > 0) {
					rtuInfoEntity.setAdDist(dataEntity.getDist().trim());
				}
				if (null != dataEntity.getRtuName() && dataEntity.getRtuName().trim().length() > 0) {
					rtuInfoEntity.setRtuName(dataEntity.getRtuName().trim());
				}
				rtuInfoEntity.setIsRain(0);
				rtuInfoEntity.setIsRiver(0);
				rtuInfoEntity.setIsRes(0);
				rtuInfoEntity.setIsGround(0);
				rtuInfoEntity.setIsVideo(0);

				if (null != dataEntity.getRtuKind()) {
					String kinds = dataEntity.getRtuKind().trim();
					kinds = kinds.replaceAll(" ", "");
					kinds = kinds.replaceAll("\\s*", "");
					if (kinds.length() > 0) {
						List<DictBiz> bizs = dictBizService.getList("rtu_kind");
						StringBuffer kindList = new StringBuffer();
						if (kinds.contains("水位") || kinds.contains("河道") || kinds.contains("水库")) {
							if (kinds.contains("河道")){
								rtuInfoEntity.setIsRiver(1);
								for (DictBiz biz : bizs) {
									if ("river".equals(biz.getDictKey())) {
										if (kindList.length() > 0) {
											kindList.append(",");
										}
										kindList.append(Func.toStr(biz.getId()));
										break;
									}
								}
							}else{
								rtuInfoEntity.setIsRes(1);
								for (DictBiz biz : bizs) {
									if ("rsvr".equals(biz.getDictKey())) {
										if (kindList.length() > 0) {
											kindList.append(",");
										}
										kindList.append(Func.toStr(biz.getId()));
										break;
									}
								}
							}
						}


						if (kinds.contains("雨量")) {
							rtuInfoEntity.setIsRain(1);
							for (DictBiz biz : bizs) {
								if ("rain".equals(biz.getDictKey())) {
									if (kindList.length() > 0) {
										kindList.append(",");
									}
									kindList.append(Func.toStr(biz.getId()));
									break;
								}
							}
						}
						if (kinds.contains("墒情")) {
							rtuInfoEntity.setIsGround(1);
							for (DictBiz biz : bizs) {
								if ("ground".equals(biz.getDictKey())) {
									if (kindList.length() > 0) {
										kindList.append(",");
									}
									kindList.append(Func.toStr(biz.getId()));
									break;
								}
							}
						}
						if (kinds.contains("视频") || kinds.contains("图像")) {
							rtuInfoEntity.setIsVideo(1);
						}
						rtuInfoEntity.setRtuKinds(kindList.toString());
					}
				}
				if (null != dataEntity.getLocationDesc() && dataEntity.getLocationDesc().trim().length() > 0) {
					rtuInfoEntity.setLocationDesc(dataEntity.getLocationDesc().trim());
				}
				if (null != dataEntity.getLng() && null != dataEntity.getLat()) {
					String lngText = dataEntity.getLng().trim();
					String latText = dataEntity.getLat().trim();
					if (lngText.length() > 0 && latText.length() > 0) {
						String lng = getLng(lngText);
						String lat = getLat(latText);
						if (null != lng && null != lat) {
							rtuInfoEntity.setLng(lng);
							rtuInfoEntity.setLat(lat);
						}
					}
				}
				if (null != dataEntity.getManageCompany() && dataEntity.getManageCompany().trim().length() > 0) {
					rtuInfoEntity.setManageCompany(dataEntity.getManageCompany().trim());
				}
				if (null != dataEntity.getRtuBrand() && dataEntity.getRtuBrand().trim().length() > 0) {
					rtuInfoEntity.setRtuBrand(dataEntity.getRtuBrand().trim());
				}
				if (null != dataEntity.getRtuModel() && dataEntity.getRtuModel().trim().length() > 0) {
					rtuInfoEntity.setRtuModel(dataEntity.getRtuModel().trim());
				}
				if (null != dataEntity.getRtuReplaceDate() && dataEntity.getRtuReplaceDate().trim().length() > 0) {
					Date dt = getDate(dataEntity.getRtuReplaceDate().trim());
					rtuInfoEntity.setRtuReplaceDate(dt);
				}
				if (null != dataEntity.getToSheng() && dataEntity.getToSheng().trim().length() > 0) {
					if ("是".equals(dataEntity.getToSheng().trim())) {
						rtuInfoEntity.setDatagramToSheng(1);
					} else {
						rtuInfoEntity.setDatagramToSheng(0);
					}
				}
				if (null != dataEntity.getToShi() && dataEntity.getToShi().trim().length() > 0) {
					if ("是".equals(dataEntity.getToShi().trim())) {
						rtuInfoEntity.setDatagramToShi(1);
					} else {
						rtuInfoEntity.setDatagramToShi(0);
					}
				}
				if (null != dataEntity.getToQu() && dataEntity.getToQu().trim().length() > 0) {
					if ("是".equals(dataEntity.getToQu().trim())) {
						rtuInfoEntity.setDatagramToQu(1);
					} else {
						rtuInfoEntity.setDatagramToQu(0);
					}
				}
				if (null != dataEntity.getRainSensorBrand() && dataEntity.getRainSensorBrand().trim().length() > 0) {
					rtuInfoEntity.setRainSensorBrand(dataEntity.getRainSensorBrand().trim());
				}
				if (null != dataEntity.getRainSensorModel() && dataEntity.getRainSensorModel().trim().length() > 0) {
					rtuInfoEntity.setRainSensorModel(dataEntity.getRainSensorModel().trim());
				}
				if (null != dataEntity.getRainSensorReplaceDate() && dataEntity.getRainSensorReplaceDate().trim().length() > 0) {
					Date dt = getDate(dataEntity.getRainSensorReplaceDate().trim());
					rtuInfoEntity.setRainSensorReplaceDate(dt);
				}
				if (null != dataEntity.getWaterSensorType() && dataEntity.getWaterSensorType().trim().length() > 0) {
					List<DictBiz> bizs = dictBizService.getList("water_level_sensor_type");
					for (DictBiz biz : bizs) {
						if (dataEntity.getWaterSensorType().trim().contains(biz.getDictValue())) {
							rtuInfoEntity.setWaterSensorType(biz.getId());
							break;
						}
					}
				}
				if (null != dataEntity.getWaterSensorBrand() && dataEntity.getWaterSensorBrand().trim().length() > 0) {
					rtuInfoEntity.setWaterSensorBrand(dataEntity.getWaterSensorBrand().trim());
				}
				if (null != dataEntity.getWaterSensorModel() && dataEntity.getWaterSensorModel().trim().length() > 0) {
					rtuInfoEntity.setWaterSensorModel(dataEntity.getWaterSensorModel().trim());
				}
				if (null != dataEntity.getWaterSensorReplaceDate() && dataEntity.getWaterSensorReplaceDate().trim().length() > 0) {
					Date dt = getDate(dataEntity.getWaterSensorReplaceDate().trim());
					rtuInfoEntity.setWaterSensorReplaceDate(dt);
				}
				if (null != dataEntity.getGroundSensorType() && dataEntity.getGroundSensorType().trim().length() > 0) {
					List<DictBiz> bizs = dictBizService.getList("ground_water_sensor_type");
					for (DictBiz biz : bizs) {
						if (dataEntity.getGroundSensorType().trim().contains(biz.getDictValue())) {
							rtuInfoEntity.setGroundSensorType(biz.getId());
							break;
						}
					}
				}
				if (null != dataEntity.getGroundSensorBrand() && dataEntity.getGroundSensorBrand().trim().length() > 0) {
					rtuInfoEntity.setGroundSensorBrand(dataEntity.getGroundSensorBrand().trim());
				}
				if (null != dataEntity.getGroundSensorModel() && dataEntity.getGroundSensorModel().trim().length() > 0) {
					rtuInfoEntity.setGroundSensorModel(dataEntity.getGroundSensorModel().trim());
				}
				if (null != dataEntity.getGroundSensorReplaceDate() && dataEntity.getGroundSensorReplaceDate().trim().length() > 0) {
					Date dt = getDate(dataEntity.getGroundSensorReplaceDate().trim());
					rtuInfoEntity.setGroundSensorReplaceDate(dt);
				}
				if (null != dataEntity.getBatteryModel() && dataEntity.getBatteryModel().trim().length() > 0) {
					rtuInfoEntity.setBatteryModel(dataEntity.getBatteryModel().trim());
				}
				if (null != dataEntity.getBatteryReplaceDate() && dataEntity.getBatteryReplaceDate().trim().length() > 0) {
					Date dt = getDate(dataEntity.getBatteryReplaceDate().trim());
					rtuInfoEntity.setBatteryReplaceDate(dt);
				}
				if (null != dataEntity.getSunModel() && dataEntity.getSunModel().trim().length() > 0) {
					rtuInfoEntity.setSunPowerModel(dataEntity.getSunModel().trim());
				}
				if (null != dataEntity.getSunPowerControllerBrand() && dataEntity.getSunPowerControllerBrand().trim().length() > 0) {
					rtuInfoEntity.setSunPowerControllerBrand(dataEntity.getSunPowerControllerBrand().trim());
				}
				if (null != dataEntity.getSunPowerControllerModel() && dataEntity.getSunPowerControllerModel().trim().length() > 0) {
					rtuInfoEntity.setSunPowerControllerModel(dataEntity.getSunPowerControllerModel().trim());
				}
				if (null != dataEntity.getSimId() && dataEntity.getSimId().trim().length() > 0) {
					rtuInfoEntity.setNetworkSimId(dataEntity.getSimId().trim());
				}
				if (null != dataEntity.getSimPay() && dataEntity.getSimPay().trim().length() > 0) {
					rtuInfoEntity.setNetworkPayer(dataEntity.getSimPay().trim());
				}
				if (null != dataEntity.getSimOverdate() && dataEntity.getSimOverdate().trim().length() > 0) {
					Date dt = getDate(dataEntity.getSimOverdate().trim());
					rtuInfoEntity.setNetworkPayEndDate(dt);
				}
				if (null != dataEntity.getSatellite() && dataEntity.getSatellite().trim().length() > 0) {
					if ("是".equals(dataEntity.getSatellite().trim())) {
						rtuInfoEntity.setIsSatellite(1);
					} else {
						rtuInfoEntity.setIsSatellite(0);
					}
				}
				if (null != dataEntity.getSatelliteModel() && dataEntity.getSatelliteModel().trim().length() > 0) {
					List<DictBiz> bizs = dictBizService.getList("satellite_model");
					for (DictBiz biz : bizs) {
						if (dataEntity.getSatelliteModel().trim().contains(biz.getDictValue())) {
							rtuInfoEntity.setSatelliteModel(biz.getId());
							break;
						}
					}
				}
				if (null != dataEntity.getRemark() && dataEntity.getRemark().trim().length() > 0) {
					rtuInfoEntity.setRemark(dataEntity.getRemark().trim());
				}
				//查找行政区划
				RtuInfoEntity adcd = rtuBaseInfoService.getAdcd(rtuInfoEntity);
				if (adcd != null && adcd.getAdName() != null) {
					rtuInfoEntity.setAdName(adcd.getAdName());
				}
				if (adcd != null && adcd.getAdCode() != null) {
					rtuInfoEntity.setAdCode(adcd.getAdCode());
				}
				System.out.println("save record********************");
				if (recordEntity.getSaveType() == 0) {
					//新增
					RtuInfoEntity entity = rtuBaseInfoService.rtuInfoByRtuCode(rtuInfoEntity.getRtuCode());
					if (null == entity) {
						rtuBaseInfoService.save(rtuInfoEntity);
						RtuImportRecordDetailEntity detailEntity = new RtuImportRecordDetailEntity();
						detailEntity.setRecordId(recordEntity.getId());
						detailEntity.setRtuCode(rtuInfoEntity.getRtuCode());
						detailEntity.setRtuName(rtuInfoEntity.getRtuName());
						detailEntity.setSaveType(0);
						rtuImportRecordDetailService.save(detailEntity);
					}
				} else if (recordEntity.getSaveType() == 1) {
					//覆盖
					RtuInfoEntity entity = rtuBaseInfoService.rtuInfoByRtuCode(rtuInfoEntity.getRtuCode());
					if (null != entity) {
						//更新列
						copyEntity(entity, rtuInfoEntity);
						entity.setUpdateTime(new Date());
						rtuBaseInfoService.updateById(entity);
						RtuImportRecordDetailEntity detailEntity = new RtuImportRecordDetailEntity();
						detailEntity.setRecordId(recordEntity.getId());
						detailEntity.setRtuCode(rtuInfoEntity.getRtuCode());
						detailEntity.setRtuName(rtuInfoEntity.getRtuName());
						detailEntity.setSaveType(1);
						rtuImportRecordDetailService.save(detailEntity);
					}
				} else if (recordEntity.getSaveType() == 2) {
					//新增或修改
					RtuImportRecordDetailEntity detailEntity = new RtuImportRecordDetailEntity();
					detailEntity.setRecordId(recordEntity.getId());
					detailEntity.setRtuCode(rtuInfoEntity.getRtuCode());
					detailEntity.setRtuName(rtuInfoEntity.getRtuName());
					RtuInfoEntity entity = rtuBaseInfoService.rtuInfoByRtuCode(rtuInfoEntity.getRtuCode());
					if (null != entity) {
						detailEntity.setSaveType(1);
						//更新列
						copyEntity(entity, rtuInfoEntity);
						entity.setUpdateTime(new Date());
						rtuBaseInfoService.updateById(entity);
					} else {
						rtuBaseInfoService.save(rtuInfoEntity);
						detailEntity.setSaveType(0);
					}
					rtuImportRecordDetailService.save(detailEntity);
				}
			}
			recordEntity.setImportStatus(1L);
			rtuImportRecordService.saveOrUpdate(recordEntity);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("RTU导入任务异常 {}", e.getMessage());
			try {
				recordEntity.setImportStatus(2L);
				rtuImportRecordService.saveOrUpdate(recordEntity);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
		return 0;
	}

	private static boolean isLetterDigit
		(String str) {
		String regex = "^[a-z0-9A-Z]+$";
		return str.matches(regex);
	}

	private Double tranformPos
		(String lng) {
		lng = lng
			.trim()
			.replace("g", "");
		if (lng.indexOf("o") >= 0 || lng.indexOf("°") >= 0 || lng.indexOf("°") >= 0 || lng.indexOf("°") >= 0) {
			lng = lng
				.trim()
				.replace("o", ":")
				.replace("°", ":")
				.replace("°", ":")
				.replace("°", ":")
				.replace("′", ":")
				.replace("'", ":")
				.replace("\"", "")
				.replace("″", "")
				.replace(".", ":");

			String[] lntArr = lng.split(":");
			Double result = 0D;
			if (lntArr.length == 1) {
				String[] lntArr2 = new String[3];
				lntArr2[0] = lntArr[0];
				lntArr2[1] = "0";
				lntArr2[2] = "0";
				lntArr = lntArr2;
			} else if (lntArr.length == 2) {
				String[] lntArr2 = new String[3];
				lntArr2[0] = lntArr[0];
				lntArr2[1] = lntArr[1];
				lntArr2[2] = "0";
				lntArr = lntArr2;
			}
			for (int i = lntArr.length; i > 0; i--) {
				String str = lntArr[i - 1];
				if (null != str) {
					str = str.trim();
					if (null != str && str.length() > 0) {
						double v = Double.parseDouble(str);
						if (i == 1) {
							result = v + result;
						} else {
							result = (result + v) / 60;
						}
					} else {
						double v = 0.0;
						if (i == 1) {
							result = v + result;
						} else {
							result = (result + v) / 60;
						}
					}
				} else {
					double v = 0.0;
					if (i == 1) {
						result = v + result;
					} else {
						result = (result + v) / 60;
					}
				}
			}
			return result;
		} else {
			String lng2 = lng;
			lng2 = lng2
				.trim()
				.replace(".", ":");
			String[] vs = lng2.split(":");
			if (vs.length > 1) {
				lng = lng
					.trim()
					.replace("o", ":")
					.replace("°", ":")
					.replace("°", ":")
					.replace("°", ":")
					.replace("′", ":")
					.replace("'", ":")
					.replace("\"", "")
					.replace("″", "")
					.replace(".", ":");

				String[] lntArr = lng.split(":");
				Double result = 0D;
				if (lntArr.length == 1) {
					String[] lntArr2 = new String[3];
					lntArr2[0] = lntArr[0];
					lntArr2[1] = "0";
					lntArr2[2] = "0";
					lntArr = lntArr2;
				} else if (lntArr.length == 2) {
					String[] lntArr2 = new String[3];
					lntArr2[0] = lntArr[0];
					lntArr2[1] = lntArr[1];
					lntArr2[2] = "0";
					lntArr = lntArr2;
				}
				for (int i = lntArr.length; i > 0; i--) {
					String str = lntArr[i - 1];
					if (null != str) {
						str = str.trim();
						if (null != str && str.length() > 0) {
							double v = Double.parseDouble(str);
							if (i == 1) {
								result = v + result;
							} else {
								result = (result + v) / 60;
							}
						} else {
							double v = 0.0;
							if (i == 1) {
								result = v + result;
							} else {
								result = (result + v) / 60;
							}
						}
					} else {
						double v = 0.0;
						if (i == 1) {
							result = v + result;
						} else {
							result = (result + v) / 60;
						}
					}
				}
				return result;
			} else {
				double v = Double.parseDouble(lng);
				return v;
			}

		}

	}
}
