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

import java.time.LocalDate;
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.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.VoyageMemberMapper;
import cn.tsy.data.idsse.dsds.model.voyage.VoyageMember;
import cn.tsy.idsse.dsds.constant.VoyageLogType;

@Service
public class VoyageMemberService {
	@Autowired
	private VoyageMemberMapper mMember;
	@Autowired
	private VoyageLogMapper mLog;

	public List<VoyageMember> uploadMember(int voyage_id, List<List<String>> values, FrameOptr optr) {
		FrameAssertHelper.hasValue(values, "参航人员数量为空，请检查！");
		LocalDate startDate = LocalDateHelper.parseDate("1900-01-01");
		this.mMember.deleteByColumn("voyage_id", voyage_id);
		List<VoyageMember> memberArray = new ArrayList<>();
		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;

			VoyageMember m = new VoyageMember();
			m.setMember_name(list.get(0));
			m.setMember_sex(list.get(1));
			String date= list.get(2);
			if(date!=null) {
				// 判断是不是日期
				
				try {
					//System.out.println(LocalDateHelper.format(startDate.plusDays(Integer.parseInt(date))));
					m.setMember_birthday(LocalDateHelper.format(startDate.plusDays(Integer.parseInt(date))));
				}catch(Exception e) {					
				}
				if(m.getMember_birthday()==null)
					m.setMember_birthday(date);
			}
			m.setMember_unit(list.get(3));
			m.setMember_dept(list.get(4));
//			m.setShip_room_addr(list.get(5));
//			m.setMember_tel(list.get(6));
//			m.setMember_cert(list.get(7));
//			m.setBank_name(list.get(8));
//			m.setBank_card(list.get(9));
//			m.setBank_addr(list.get(10));
			m.setMember_remark(list.get(5));
			m.setVoyage_id(voyage_id);

			this.mMember.insert(m);
			memberArray.add(m);
		}
		if (memberArray.size() > 0)
			VoyageRegService.logVoyage(mLog, voyage_id, optr, VoyageLogType.UploadMember, "成功导入[" + memberArray.size() + "]条参航人员");
		return memberArray;
	}

	public void throwError(int row, String msg) {
		throw new BusinessException("航次参航人员导入失败->第[" + (row + 1) + "]行:" + msg);

	}
	
	
	public void editMember(VoyageMember member) {
		if(member.getTsy_id()==null)
			this.mMember.insert(member);
		else this.mMember.update(member);
	}
	
	public void deleteMember(int member_id) {
		this.mMember.deleteByKey(member_id);
	}

}
