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.data.model.frame.optr.FrameOptr;
import cn.hme.framework.helper.date.LocalDateHelper;
import cn.hme.framework.helper.lang.StringHelper;
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.SampleDatasetTrackMapper;
import cn.tsy.data.idsse.dsds.model.sample.SampleDataset;
import cn.tsy.data.idsse.dsds.model.sample.SampleDatasetTrack;

@Service
public class SampleDatasetTrackService {
	@Autowired
	private SampleDatasetTrackMapper mTrack;
	@Autowired
	private SampleDatasetMapper mDataset;

	public List<SampleDatasetTrack> uploadTrack(int dataset_id, List<String> array, String file_name, FrameOptr optr) {
		FrameAssertHelper.hasValue(array, "潜次轨迹文件为空，请检查！");
		List<SampleDatasetTrack> navArray = new ArrayList<>();
		SampleDataset dataset = this.mDataset.find(dataset_id);
		FrameAssertHelper.isNotNull(dataset, "未找到指定数据集，请检查！");
		this.mTrack.deleteByColumn("dataset_id", dataset_id);
		for (int i = 0; i < array.size(); i++) {
			if (array.get(i).trim().length() == 0)
				break;
			String[] values = StringHelper.splitWithSpace(array.get(i));
			if (values.length < 3) {
				throwError(i, "列不足，至少需要3列");
			}

			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");
			// 判断通过
			SampleDatasetTrack nav = new SampleDatasetTrack();
			nav.setTrack_lat(lat);
			nav.setTrack_lon(lon);
			nav.setTrack_time(curTime);
			nav.setDataset_id(dataset_id);
			// 判断时间是否重复了
//			List<SampleDatasetTrack> vArray = this.mTrack.selectList(SqlBuilder.create().addSqlCondition(SqlCondition.of("dataset_id", dataset_id)).addSqlCondition(SqlCondition.of("track_time", curTime)));
//			if (vArray.size() == 0) {
			this.mTrack.insert(nav);
			navArray.add(nav);
//			}
		}
		if (navArray.size() > 0) {
			SampleDataset ds = new SampleDataset();
			ds.setTsy_id(dataset_id);
			ds.setDiving_track_file(file_name);
			this.mDataset.update(ds);

		}
		return navArray;
	}

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

	}

}
