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

import java.io.File;
import java.util.List;

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.file.FileHelper;
import cn.hme.framework.web.control.Ajax;
import cn.hme.framework.web.control.AjaxControl;
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.idsse.dsds.helper.DSDSWebHelper;
import cn.tsy.idsse.dsds.service.sample.SampleDatasetTrackService;

@RestController
@RequestMapping("sample/diving")
@CrossOrigin
public class SampleDivingControl extends AjaxControl {


	@Autowired
	private SampleDatasetMapper mDataset;
	@Autowired
	private SampleDatasetTrackMapper mTrack;

	@Autowired
	private SampleDatasetTrackService sTrack;



	@CrossOrigin
	@RequestMapping("upload.htm")
	public Ajax upload( MultipartFile file) {
		DSDSWebHelper.getFrameOptr(true);
		File f = FileHelper.fromUpload(file);
		List<String> array = FileHelper.readFileAsArray(f);
		f.delete();
		List<SampleDataset> arrayDataset = this.mDataset.selectListByColumn("diving_sn", file.getOriginalFilename().replace(".txt", ""));
		
		if(arrayDataset.size()==0)
			return new Ajax();
		this.sTrack.uploadTrack(arrayDataset.get(0).getTsy_id(), array, file.getOriginalFilename(), DSDSWebHelper.getFrameOptr(true));
	
		return new Ajax(file.getOriginalFilename());
	}

	

}
