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

import java.time.LocalDateTime;
import java.util.List;

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

import cn.hme.framework.helper.lang.StringHelper;
import cn.hme.framework.web.helper.FrameAssertHelper;
import cn.tsy.data.idsse.dsds.mapper.rescue.RescueFileMapper;
import cn.tsy.data.idsse.dsds.mapper.rescue.RescueRegMapper;
import cn.tsy.data.idsse.dsds.model.rescue.RescueReg;
import cn.tsy.data.idsse.dsds.model.voyage.VoyageMember;
import cn.tsy.data.idsse.dsds.model.voyage.VoyageNavigation;

@Service
public class RescueRegService {

	@Autowired
	private RescueRegMapper mReg;

	@Autowired
	private RescueFileMapper mFile;

	public void editRescue(RescueReg rescue) {

		// rescue_type
		FrameAssertHelper.isNotEmpty(rescue.getRescue_type(), "应急搜捞类型不能为空！");
		FrameAssertHelper.isNotEmpty(rescue.getRescue_target_type(), "目标类型不能为空！");
		if (rescue.getTsy_id() == null) {
			// 新增，自动生成rescue_code
			// 获取上一个序号
			String sql = "select * from rescue_reg order by tsy_id desc limit 0,1";
			List<RescueReg> regArray = this.mReg.selectListBySql(sql);
			String rescue_code = rescue.getRescue_type() + "_" + rescue.getRescue_target_type() + "_";
			if (regArray.size() == 0) {
				rescue_code = rescue_code + "001";
			} else {
				int seq = Integer.parseInt(regArray.get(0).getRescue_code().split("_")[2])+1;
				rescue_code = rescue_code + StringHelper.leftPadding(seq + "", 3, '0');
			}
			rescue.setRescue_code(rescue_code);
			rescue.setFill_time(LocalDateTime.now());
			rescue.setRescue_status_time(LocalDateTime.now());
			this.mReg.insert(rescue);
		} else {
			this.mReg.update(rescue);
		}
	}

	public void deleteRescue(int rescue_id) {
		this.mFile.deleteByColumn("rescue_id", rescue_id);
		this.mReg.deleteByKey(rescue_id);
	}

}
