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

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 cn.hme.framework.helper.lang.StringHelper;
import cn.hme.framework.jdbc.provider.SqlBuilderHelper;
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.tsy.data.idsse.dsds.mapper.sample.SampleLineMapper;
import cn.tsy.data.idsse.dsds.model.sample.SampleLine;
import cn.tsy.idsse.dsds.helper.DSDSWebHelper;
import cn.tsy.idsse.dsds.service.sample.SampleLineService;

@RestController
@RequestMapping("sample/line")
@CrossOrigin
public class SampleLineControl extends AjaxControl {

	@Autowired
	private SampleLineMapper mLine;
	@Autowired
	private SampleLineService sLine;

	
	@RequestMapping("page.htm")
	public Ajax page(Integer group, Integer limit, Integer start, String query, String order, String type) {
		order = StringHelper.hasValue(order) ? order : "sample_name";
		type = StringHelper.hasValue(type) ? type : "DESC";
		Pageable page = this.mLine.pageRecord(group,start, limit,  SqlBuilderHelper.makePageSqlBuilder(query, order, type,
				"sample_name","funding_org","sample_tag_name","sample_tag_value","sample_remark"));
		return new Ajax(page);
	}
	
	
//	@RequestMapping("list.htm")
//	public Ajax list(int dataset_id) {
//		return new Ajax(this.mLine.selectListByColumnAndSimpleOrder("dataset_id", dataset_id, OrderBy.of("create_time", DirectionEnum.DESC)));
//	}
	
	@RequestMapping("list.htm")
	public Ajax list(int dataset_id,String order,String type) {
		if(StringHelper.isEmpty(order))
			order = "sample_name";
		if(StringHelper.isEmpty(type))
			type = "DESC";		
		return new Ajax(this.mLine.selectListByColumnAndSimpleOrder("dataset_id", dataset_id,OrderBy.of(order, type) ));
	}
	
	@RequestMapping("edit.htm")
	public Ajax edit(int dataset_id, SampleLine line) {
		this.sLine.editLine(dataset_id, line, DSDSWebHelper.getFrameOptr(true));
		return new Ajax(line);
	}
	

	@RequestMapping("find.htm")
	public Ajax find(int tsy_id) {
		return new Ajax(this.mLine.find(tsy_id));
	}
	
	@RequestMapping("delete.htm")
	public Ajax delete(int tsy_id) {
		DSDSWebHelper.getFrameOptr(true);
		this.sLine.deleteLine(tsy_id);
		return new Ajax();
	}

	
}
