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.SampleStationMapper;
import cn.tsy.data.idsse.dsds.model.sample.SampleDataset;
import cn.tsy.data.idsse.dsds.model.sample.SampleStation;

@Service
public class SampleStationService {
	@Autowired
	private SampleDatasetMapper mDataset;
	@Autowired
	private SampleStationMapper mStation;

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

		return satation;

	}

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

}
