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

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.excel.ExcelReaderHelper;
import cn.hme.framework.helper.lang.StringHelper;
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.tsy.data.idsse.dsds.mapper.sample.SampleDatasetMapper;
import cn.tsy.data.idsse.dsds.model.sample.SampleDataset;
import cn.tsy.idsse.dsds.helper.DSDSWebHelper;
import cn.tsy.idsse.dsds.service.sample.SampleDatasetService;

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

	@Autowired
	private SampleDatasetMapper mDataset;


	@Autowired
	private SampleDatasetService sDataset;

	@RequestMapping("page.htm")
	public Ajax page(Integer group, Integer limit, Integer start, String query, String order, String type) {
		order = StringHelper.hasValue(order) ? order : "create_time";
		type = StringHelper.hasValue(type) ? type : "DESC";
		Pageable page = this.mDataset.datasetWithPage(group, start, limit, query, order, type);
		return new Ajax(page);
	}

	@RequestMapping("find.htm")
	public Ajax find(int dataset_id) {		
		return new Ajax(this.mDataset.find(dataset_id));
	}
	
	
	@RequestMapping("edit.htm")
	public Ajax edit(SampleDataset dataset,Integer old_dataset_id) {		
		return new Ajax(this.sDataset.editDataset(dataset, old_dataset_id,DSDSWebHelper.getFrameOptr(true)));
	}
	
	@CrossOrigin
	@RequestMapping("upload.htm")
	public Ajax upload(MultipartFile file) {

		DSDSWebHelper.getFrameOptr(true);
		List<List<String>> values = ExcelReaderHelper.readSheet(file, 1, 40);
		if (values.size() == 0)
			throw new BusinessException("导入的文件中无有效数据，请检查！");
		this.sDataset.uploadSample(values, DSDSWebHelper.getFrameOptr(true));
		return new Ajax();
	}
	
//	@RequestMapping("sample/list.htm")
//	public Ajax listSample(int dataset_id) {
//		SampleDataset dataset = this.mDataset.find(dataset_id);
//		return new Ajax(this.sDataset.editDataset(dataset, getFrameOptr()));
//	}
//	
//	
	

	@RequestMapping("delete.htm")
	public Ajax delete(int tsy_id) {

		DSDSWebHelper.getFrameOptr(true);
		this.sDataset.deleteDataset(tsy_id);
		return new Ajax();
	}

	

}
