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.SampleLineMapper;
import cn.tsy.data.idsse.dsds.mapper.sample.SampleRegMapper;
import cn.tsy.data.idsse.dsds.mapper.sample.SampleStationMapper;
import cn.tsy.data.idsse.dsds.model.sample.SampleLine;
import cn.tsy.data.idsse.dsds.model.sample.SampleReg;
import cn.tsy.data.idsse.dsds.model.sample.SampleStation;
import cn.tsy.idsse.dsds.helper.DSDSWebHelper;
import cn.tsy.idsse.dsds.service.sample.SampleRegService;

@RestController
@RequestMapping("sample/reg")
@CrossOrigin
public class SampleRegControl extends AjaxControl {

	@Autowired
	private SampleRegMapper mSample;
	@Autowired
	private SampleLineMapper mLine;
	@Autowired
	private SampleStationMapper mStation;


	@Autowired
	private SampleRegService sSample;

	@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.mSample.sampleWithPage(group, start, limit, query, order, type);
		return new Ajax(page);
	}

	@RequestMapping("edit.htm")
	public Ajax edit(SampleReg sample, SampleStation satation, SampleLine line) {
		this.sSample.editSample(sample, satation, line, DSDSWebHelper.getFrameOptr(true));
		return new Ajax(sample);
	}
	

	@RequestMapping("line/find.htm")
	public Ajax findLine(int sample_id) {
		return new Ajax(this.mLine.findWithColumn("sample_id",sample_id));
	}
	
	@RequestMapping("station/find.htm")
	public Ajax findStation(int sample_id) {
		return new Ajax(this.mStation.findWithColumn("sample_id",sample_id));
	}

	@RequestMapping("delete.htm")
	public Ajax delete(int tsy_id) {
		DSDSWebHelper.getFrameOptr(true);
		this.sSample.deleteSample(tsy_id);
		return new Ajax();
	}

	@CrossOrigin
	@RequestMapping("upload.htm")
	public Ajax upload(MultipartFile file) {
		DSDSWebHelper.getFrameOptr(true);
		List<List<String>> values = ExcelReaderHelper.readSheet(file, 2, 32);
		if (values.size() == 0)
			throw new BusinessException("导入的文件中无有效数据，请检查！");
		return new Ajax();
	}

}
