package cn.tsy.idsse.dsds.service.sample;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.hme.framework.helper.date.LocalDateHelper;
import cn.hme.framework.helper.lang.StringHelper;
import cn.hme.framework.jdbc.provider.builder.SqlBuilder;
import cn.hme.framework.jdbc.provider.builder.SqlCondition;
import cn.hme.framework.web.exception.BusinessException;
import cn.hme.framework.web.helper.FrameAssertHelper;
import cn.tsy.data.idsse.dsds.mapper.sample.SampleDatasetMapper;
import cn.tsy.data.idsse.dsds.mapper.sample.SampleLineMapper;
import cn.tsy.data.idsse.dsds.mapper.sample.SampleLineTrackMapper;
import cn.tsy.data.idsse.dsds.model.sample.SampleDataset;
import cn.tsy.data.idsse.dsds.model.sample.SampleLine;
import cn.tsy.data.idsse.dsds.model.sample.SampleLineTrack;

@Service
public class SampleLineTrackService {
	@Autowired
	private SampleLineTrackMapper mTrack;
	@Autowired
	private SampleLineMapper mLine;
	@Autowired
	private SampleDatasetMapper mDataset;

	public List<SampleLineTrack> uploadTrack(int dataset_id, List<String> array, String file_name) {
		FrameAssertHelper.hasValue(array, "测线轨迹文件为空，请检查！");
		SampleDataset ds = this.mDataset.find(dataset_id);
		FrameAssertHelper.isNotNull(ds, "未找到指定数据集，请检查！");
		this.mTrack.deleteByColumn("dataset_id", dataset_id);
		int index = 1;

		List<SampleLineTrack> navArray = new ArrayList<>();
		for (int i = 0; i < array.size(); i++) {
			if (array.get(i).trim().length() == 0) {
				this.update(file_name,ds, index, navArray);
				break;
			}
			String[] values = StringHelper.splitWithSpace(array.get(i));
			if (values[0].equals(">>")) {
				this.update(file_name, ds, index, navArray);
				navArray = new ArrayList<>();
				index++;
				continue;
			}
			if (values.length < 3) {
				throwError(i, "列不足，至少需要3列");
			}

			// 110.565232 17.370000 2023-05-20-13:55:27
			String lon = values[0];
			String lat = values[1];
			String time = values[2];
			// 经纬度的范围包括经度范围0°-180°和纬度范围0°-90°
			try {
				double d = Double.parseDouble(lon);
				if (d > 180)
					throwError(i, "经度[" + lon + "]不能大于 180°");
				if (d < 0)
					throwError(i, "经度[" + lon + "]不能小于 0°");
			} catch (Exception e) {
				throwError(i, "经度[" + lon + "]格式错误");
			}

			try {
				double d = Double.parseDouble(lat);
				if (d > 90)
					throwError(i, "纬度[" + lat + "]不能大于 90°");
				if (d < 0)
					throwError(i, "纬度[" + lat + "]不能小于 0°");
			} catch (Exception e) {
				throwError(i, "纬度[" + lat + "]格式错误");
			}
			// 109.49100 18.23400 2023-09-18-21:58:35
			String[] vs = time.split("-");
			if (vs.length != 4)
				throwError(i, "时间[" + time + "]格式错误，样例 2023-09-18-21:58:35");
			LocalDateTime curTime = LocalDateHelper.parseDateTime(vs[0] + "-" + vs[1] + "-" + vs[2] + " " + vs[3]);
			if (curTime == null)
				throwError(i, "时间[" + time + "]格式错误，样例 2023-09-18-21:58:35");
			// 判断通过

			// 判断时间是否重复了
//			List<SampleLineTrack> vArray = this.mTrack.selectList(SqlBuilder.create().addSqlCondition(SqlCondition.of("line_id", line_id)).addSqlCondition(SqlCondition.of("track_time", curTime)));
//			if (vArray.size() == 0) {
			SampleLineTrack nav = new SampleLineTrack();
			nav.setTrack_lat(lat);
			nav.setTrack_lon(lon);
			nav.setTrack_time(curTime);
			navArray.add(nav);
//			}
		}
		
		return navArray;
	}

	public void update(String file_name, SampleDataset ds, int index, List<SampleLineTrack> navArray) {
		if (navArray.size() == 0)
			return;
		// 查找是否有对应的测线
		String sn = StringHelper.leftPadding(index + "", 2, '0');
		List<SampleLine> lineArray = this.mLine.selectList(SqlBuilder.create().addSqlCondition(SqlCondition.of("dataset_id", ds.getTsy_id())).addSqlCondition(SqlCondition.of("sample_seq", sn)));
		if (lineArray.size() > 0) {
			for (SampleLineTrack nav : navArray) {
				nav.setDataset_id(ds.getTsy_id());
				nav.setTrack_index(sn);
				nav.setLine_id(lineArray.get(0).getTsy_id());
				this.mTrack.insert(nav);
			}
			SampleLine line = new SampleLine();
			line.setTsy_id(lineArray.get(0).getTsy_id());
			line.setTrack_file_name(file_name);
			this.mLine.update(line);

		}
		navArray = new ArrayList<>();
	}

	public void throwError(int row, String msg) {
		throw new BusinessException("测线轨迹文件导入失败->第[" + (row + 1) + "]行:" + msg);

	}

}
