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

import java.io.File;
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.jdbc.provider.builder.SqlBuilder;
import cn.hme.framework.jdbc.provider.builder.SqlCondition;
import cn.hme.framework.jdbc.provider.page.OrderBy;
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.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;
import cn.tsy.idsse.dsds.helper.DSDSParamHelper;
import cn.tsy.idsse.dsds.helper.DSDSWebHelper;
import cn.tsy.idsse.dsds.service.sample.SampleDatasetTrackService;

@RestController
@RequestMapping("sample/dataset/track")
@CrossOrigin
public class SampleDatasetTrackControl extends AjaxControl {

	@Autowired
	private SampleDatasetMapper mData;
	@Autowired
	private SampleDatasetTrackMapper mTrack;

	@Autowired
	private SampleDatasetTrackService sTrack;

	@RequestMapping("total.htm")
	public Ajax total(int dataset_id) {
		Map<String,Object> value = this.mTrack.totalTrack(dataset_id);
		Map<String,Object> mapReturn  = new HashMap<>();
		if(value==null)
			return new Ajax(mapReturn);
		SampleDatasetTrack start = this.mTrack.selectList(SqlBuilder.create().addSqlCondition(SqlCondition.of("dataset_id", dataset_id))
				.addSqlCondition(SqlCondition.of("track_time", value.get("min_time")))).get(0);
		SampleDatasetTrack end = this.mTrack.selectList(SqlBuilder.create().addSqlCondition(SqlCondition.of("dataset_id", dataset_id))
				.addSqlCondition(SqlCondition.of("track_time", value.get("max_time")))).get(0);
		mapReturn.put("start", start);
		mapReturn.put("end", end);
		return new Ajax(mapReturn);
	}
	

	@RequestMapping("list.htm")
	public Ajax list(int dataset_id) {
		//return new Ajax(this.mTrack.selectListByColumnAndSimpleOrder("dataset_id", dataset_id, OrderBy.of("track_time")));
		return new Ajax(this.mTrack.listTrack(dataset_id, DSDSParamHelper.DatasetNaviagtionTicketMinutes));
	}

	@CrossOrigin
	@RequestMapping("upload.htm")
	public Ajax upload(int dataset_id, MultipartFile file) {

		DSDSWebHelper.getFrameOptr(true);
		File f = FileHelper.fromUpload(file);
		List<String> array = FileHelper.readFileAsArray(f);
		f.delete();
		List<SampleDatasetTrack> list = this.sTrack.uploadTrack(dataset_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 dataset_id) {
		SampleDataset dataset = this.mData.find(dataset_id);
		List<SampleDatasetTrack> array = this.mTrack.selectListByColumnAndSimpleOrder("dataset_id", dataset_id, OrderBy.of("track_time"));
		List<String> values = new ArrayList<>();
		for (SampleDatasetTrack nav : array) {
			values.add(nav.getTrack_lon() + " " + nav.getTrack_lat() + " " + LocalDateHelper.format(nav.getTrack_time()).replace(" ", "-"));
		}
		File tmpFile = FileHelper.createTempFile();
		FileHelper.writeArray(tmpFile, values);
		// TS2-29-1_0918-1007
		WebHelper.downloadFile(tmpFile, dataset.getDiving_track_file(), true);
	}

}
