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

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

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

import cn.hme.data.model.frame.optr.FrameOptr;
import cn.hme.framework.constant.FrameStatus;
import cn.hme.framework.helper.date.LocalDateHelper;
import cn.hme.framework.web.exception.BusinessException;
import cn.hme.framework.web.helper.FrameAssertHelper;
import cn.tsy.data.idsse.dsds.mapper.voyage.VoyageLogMapper;
import cn.tsy.data.idsse.dsds.mapper.voyage.VoyageRegMapper;
import cn.tsy.data.idsse.dsds.model.voyage.VoyageLog;
import cn.tsy.data.idsse.dsds.model.voyage.VoyageMember;
import cn.tsy.data.idsse.dsds.model.voyage.VoyageNavigation;
import cn.tsy.data.idsse.dsds.model.voyage.VoyageReg;
import cn.tsy.idsse.dsds.constant.VoyageLogType;
import cn.tsy.idsse.dsds.constant.VoyageStatus;

@Service
public class VoyageRegService {
	@Autowired
	private VoyageRegMapper mReg;
	@Autowired
	private VoyageLogMapper mLog;

	/***********************
	 * 增加或者修改
	 */
	public void editVoyage(VoyageReg voyage, FrameOptr optr) {
		FrameAssertHelper.isNotEmpty(voyage.getVoyage_name(), "航次名称不允许为空！");
		FrameAssertHelper.isNotNull(voyage.getVoyage_start_date(), "航次离港日期不允许为空！");
		FrameAssertHelper.isNotNull(voyage.getVoyage_end_date(), "航次返岗日期不允许为空！");
		if (voyage.getVoyage_end_date().isBefore(voyage.getVoyage_start_date()))
			throw new BusinessException("航次返港日期不能小于离港日期！");
		// 判断类型是否存在
		List<VoyageReg> list = this.mReg.selectListByColumn("voyage_name", voyage.getVoyage_name());
		if (voyage.getTsy_id() != null) {
			if (list.size() > 0 && list.get(0).getTsy_id() - voyage.getTsy_id() != 0)
				throw new BusinessException("航次名称【" + voyage.getVoyage_name() + "】已经存在，请检查！");
			this.mReg.update(voyage);
		} else {
			FrameAssertHelper.isEmpty(list, "航次名称名称【" + voyage.getVoyage_name() + "】已经存在，请检查！");
			voyage.setCreate_time(LocalDateTime.now());
			voyage.setVoyage_status(VoyageStatus.Plan);
			voyage.setVoyage_status_time(LocalDateTime.now());
			this.mReg.insert(voyage);
			/***************
			 * 写航次日志
			 */
			logVoyage(this.mLog, voyage.getTsy_id(), optr, VoyageLogType.Create, voyage.getTsy_id() + "->" + voyage.getVoyage_name());

		}
	}
	
	// 航次名称	科考船舶	涉密航次	航次任务简述	开始时间	结束时间	航次首席	航次资助机构	资助编号
	public List<VoyageReg> uploadVoyage(List<List<String>> values) {
		List<VoyageReg> array = new ArrayList<VoyageReg>();
		for (int i = 0; i < values.size(); i++) {
			List<String> list = values.get(i);
			if (list.get(0) == null || list.get(0).trim().length() == 0)
				break;
			String voyage_name = list.get(0);
			String ship_name = list.get(1);
			String secure_flag = list.get(2);
			String remark = list.get(3);
			String start_date = list.get(4);
			String end_date = list.get(5);
			String voyage_officer = list.get(6);
			String funding_org = list.get(7);
			String funding_code = list.get(8);
			List<VoyageReg> vArray = this.mReg.selectListByColumn("voyage_name", voyage_name);
			VoyageReg reg = null;

			if(vArray.size()>0)
				reg = vArray.get(0);
			else
				reg = new VoyageReg();
			reg.setCreate_time(LocalDateTime.now());
			reg.setFunding_code(funding_code);
			reg.setFunding_org(funding_org);
			reg.setSerect_status("是".equals(secure_flag)?FrameStatus.Active.toString():FrameStatus.InActive.toString());
			reg.setShip_name(ship_name);
			reg.setVoyage_end_date(LocalDateHelper.parseDate(end_date));
			reg.setVoyage_start_date(LocalDateHelper.parseDate(start_date));
			reg.setVoyage_name(voyage_name);
			reg.setVoyage_officer(voyage_officer);
			reg.setVoyage_remark(remark);
			reg.setVoyage_status("Plan");
			reg.setVoyage_status_time(LocalDateTime.now());
			if(reg.getTsy_id()==null)
			this.mReg.insert(reg);
			else
				this.mReg.update(reg);
			array.add(reg);
			
		}
		return array;
	}

	/****************
	 * 删除航次信息
	 * 
	 */
	public void deleteVoyage(int voyage_id) {
		/****************
		 * @todo 判断是否能实际删除
		 */
		this.mLog.deleteByColumn("voyage_id", voyage_id);
		this.mLog.deleteOtherByColumn(VoyageNavigation.class, "voyage_id", voyage_id);
		this.mLog.deleteOtherByColumn(VoyageMember.class, "voyage_id", voyage_id);
		this.mReg.deleteByKey(voyage_id);
	}

	public static VoyageLog logVoyage(VoyageLogMapper mLog, int voyage_id, FrameOptr optr, String log_type, String log_value) {
		VoyageLog log = new VoyageLog();
		log.setLog_type(log_type);
		log.setLog_value(log_value);
		log.setLogin_name(optr.getLogin_name());
		log.setOptr_id(optr.getTsy_id());
		log.setOptr_name(optr.getOptr_name());
		log.setVoyage_id(voyage_id);
		mLog.insert(log);
		return log;
	}
}
