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

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.voyage.VoyageLogMapper;
import cn.tsy.data.idsse.dsds.mapper.voyage.VoyageNavigationMapper;
import cn.tsy.data.idsse.dsds.mapper.voyage.VoyageRegMapper;
import cn.tsy.data.idsse.dsds.model.voyage.VoyageNavigation;
import cn.tsy.data.idsse.dsds.model.voyage.VoyageReg;
import cn.tsy.idsse.dsds.constant.VoyageLogType;
import cn.tsy.idsse.dsds.helper.DSDSGeoCalculator;

@Service
public class VoyageNavigationService {
	@Autowired
	private VoyageNavigationMapper mNav;
	@Autowired
	private VoyageRegMapper mVoyage;
	@Autowired
	private VoyageLogMapper mLog;

	public List<VoyageNavigation> uploadNavigation(int voyage_id, List<String> array,String file_name, FrameOptr optr) {
		FrameAssertHelper.hasValue(array, "航次导航文件为空，请检查！");
		List<VoyageNavigation> navArray = new ArrayList<>();
		this.mNav.deleteByColumn("voyage_id", voyage_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].replace(".000", ""));
			if (curTime == null)
				throwError(i, "时间[" + time + "]格式错误，样例 2023-09-18-21:58:35");
			// 判断通过
			VoyageNavigation nav = new VoyageNavigation();
			nav.setNav_lat(lat);
			nav.setNav_lon(lon);
			nav.setNav_time(curTime);
			nav.setVoyage_id(voyage_id);
			// 判断时间是否重复了
//			List<VoyageNavigation> vArray = this.mNav.selectList(SqlBuilder.create().addSqlCondition(SqlCondition.of("voyage_id", voyage_id)).addSqlCondition(SqlCondition.of("nav_time", curTime)));
//			if (vArray.size() == 0) {
				this.mNav.insert(nav);
				navArray.add(nav);
//			}
		}
		if (navArray.size() > 0) {
			VoyageReg voyage = new VoyageReg();
			voyage.setTsy_id(voyage_id);
			voyage.setNavigation_file_name(file_name);
			// 计算海里数目
			Double dValue = DSDSGeoCalculator.calcuateVoyage(navArray);
			voyage.setVoyage_mileage(dValue.intValue()+"");
			this.mVoyage.update(voyage);
			
			VoyageRegService.logVoyage(mLog, voyage_id, optr, VoyageLogType.UploadNavigation, "成功导入[" + navArray.size() + "]条导航记录");
		
		}
			return navArray;
	}

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

	}

}
