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

import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.hme.data.model.frame.optr.FrameOptr;
import cn.hme.framework.helper.lang.StringHelper;
import cn.hme.framework.jdbc.provider.builder.SqlQuery;
import cn.tsy.data.idsse.dsds.mapper.sample.SampleDatasetMapper;
import cn.tsy.data.idsse.dsds.mapper.sample.SampleLineMapper;
import cn.tsy.data.idsse.dsds.model.sample.SampleDataset;
import cn.tsy.data.idsse.dsds.model.sample.SampleLine;

@Service
public class SampleLineService {
	@Autowired
	private SampleDatasetMapper mDataset;
	@Autowired
	private SampleLineMapper mLine;

	/***********************
	 * 增加或者修改
	 * 
	 */
	public SampleLine editLine(int dataset_id, SampleLine line, FrameOptr optr) {
		SampleDataset dataset = this.mDataset.find(dataset_id);
		//
		if (line.getTsy_id() == null) {
			// 计算序号
			Integer total = this.mLine.selectCount(SqlQuery.create().eq("dataset_id", dataset_id));
			String seq = "01";
			if (total != null && total > 0)
				seq = StringHelper.leftPadding((total + 1)+"", 2, '0');
			line.setDataset_id(dataset_id);
			line.setDataset_name(dataset.getDataset_name());
			line.setSample_seq(seq);
			line.setSample_name(dataset.getDataset_name()+"_"+seq);
			line.setCreate_time(LocalDateTime.now());
			this.mLine.insert(line);
		}else 
			this.mLine.update(line);

		return line;

	}

	/****************
	 * 删除数据样品
	 * 
	 */
	public void deleteLine(int line_id) {
		/****************
		 * @todo 判断是否能实际删除
		 */
		this.mLine.deleteByKey(line_id);
	}

}
