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

import java.io.File;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import cn.hme.framework.helper.date.LocalDateHelper;
import cn.hme.framework.helper.file.FileHelper;
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.jdbc.provider.page.OrderBy;
import cn.hme.framework.jdbc.provider.page.Pageable;
import cn.hme.framework.web.control.Ajax;
import cn.hme.framework.web.control.AjaxControl;
import cn.hme.framework.web.exception.BusinessException;
import cn.hme.framework.web.helper.WebHelper;
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.helper.DSDSGeoCalculator;
import cn.tsy.idsse.dsds.helper.DSDSParamHelper;
import cn.tsy.idsse.dsds.helper.DSDSWebHelper;
import cn.tsy.idsse.dsds.service.voyage.VoyageNavigationService;

@RestController
@RequestMapping("voyage/navigation")
@CrossOrigin
public class VoyageNavigationControl extends AjaxControl {

	@Autowired
	private VoyageRegMapper mReg;
	@Autowired
	private VoyageNavigationMapper mNavigation;

	@Autowired
	private VoyageNavigationService sNav;

	@RequestMapping("page.htm")
	public Ajax page(int voyage_id, Integer group, Integer limit, Integer start, String query, String order, String type) {
		order = StringHelper.hasValue(order) ? order : "nav_time";
		Pageable page = this.mNavigation.navigationWithPage(group, start, limit, query, order, type, voyage_id);
		return new Ajax(page);
	}
	
	@RequestMapping("list.htm")
	public Ajax list(int voyage_id) {
		//return new Ajax(this.mNavigation.selectListByColumnAndSimpleOrder("voyage_id", voyage_id, OrderBy.of("nav_time")));
		return new Ajax(this.mNavigation.listNavigation(voyage_id, DSDSParamHelper.VaoyageNaviagtionTicketMinutes));
	}
	
	@RequestMapping("mileage.htm")
	public Ajax mileage(int voyage_id) {
		Double value = DSDSGeoCalculator.calcuateVoyage(
				this.mNavigation.selectListByColumnAndSimpleOrder("voyage_id", voyage_id, OrderBy.of("nav_time")));
		if(value!=null && value>0) {
			VoyageReg voyage = new VoyageReg();
			voyage.setTsy_id(voyage_id);
			voyage.setVoyage_mileage(value.intValue()+"");
			this.mReg.update(voyage);
			
		}
		return new Ajax();
	}
	
	@RequestMapping("total.htm")
	public Ajax total(int voyage_id) {
		Map<String,Object> value = this.mNavigation.totalNavigation(voyage_id);
		Map<String,Object> mapReturn  = new HashMap<>();
		if(value==null)
			return new Ajax(mapReturn);
		VoyageNavigation start = this.mNavigation.selectList(SqlBuilder.create().addSqlCondition(SqlCondition.of("voyage_id", voyage_id))
				.addSqlCondition(SqlCondition.of("nav_time", value.get("min_time")))).get(0);
		VoyageNavigation end = this.mNavigation.selectList(SqlBuilder.create().addSqlCondition(SqlCondition.of("voyage_id", voyage_id))
				.addSqlCondition(SqlCondition.of("nav_time", value.get("max_time")))).get(0);
		mapReturn.put("start", start);
		mapReturn.put("end", end);
		return new Ajax(mapReturn);
	}

	@CrossOrigin
	@RequestMapping("upload.htm")
	public Ajax upload(int voyage_id, MultipartFile file) {
		DSDSWebHelper.getFrameOptr(true);
		File f = FileHelper.fromUpload(file);
		List<String> array = FileHelper.readFileAsArray(f);
		f.delete();
		List<VoyageNavigation> list = this.sNav.uploadNavigation(voyage_id, array,file.getOriginalFilename(), DSDSWebHelper.getFrameOptr(true));
		if (list.size() == 0)
			throw new BusinessException("未找到有效的导航记录(已忽略相同的记录)！");
		return new Ajax(file.getOriginalFilename());
	}

	@RequestMapping("download.htm")
	public void download(int voyage_id) {
		VoyageReg voyage = this.mReg.find(voyage_id);
		List<VoyageNavigation> array = this.mNavigation.selectListByColumnAndSimpleOrder("voyage_id", voyage_id, OrderBy.of("nav_time"));
		List<String> values = new ArrayList<>();
		for (VoyageNavigation nav : array) {
			values.add(nav.getNav_lon() + " " + nav.getNav_lat() + " " + LocalDateHelper.format(nav.getNav_time()).replace(" ", "-"));
		}
		File tmpFile = FileHelper.createTempFile();
		FileHelper.writeArray(tmpFile, values);
		// TS2-29-1_0918-1007
		DateTimeFormatter ft = DateTimeFormatter.ofPattern("MMdd");
		WebHelper.downloadFile(tmpFile, voyage.getVoyage_name() + "_" + voyage.getVoyage_start_date().format(ft) + "-" + voyage.getVoyage_end_date().format(ft)+".txt", true);
	}

}
