package com.dev.swms.server.service.base.pojo;

import java.util.Date;
import java.util.List;

import com.dev.swms.server.model.base.Pilot;
import com.dev.swms.server.model.base.Transportation;
import com.dev.swms.server.model.base.TransportationStatus;
import com.dev.swms.server.model.base.TransportationType;
import com.dev.swms.server.service.base.TransportationManager;
import com.dev.energy.server.exception.BusinessException;
import com.dev.energy.server.service.pojo.DefaultBaseManager;

public class DefaultTransportationManager extends DefaultBaseManager implements
		TransportationManager {

	//司机信息维护
	public void storePilot(Pilot pilot) {
		commonDao.store(pilot);
	}

	//司机信息失效
	public void disabledPilot(List<Long> ids) {
		for (Long id : ids) {
			Pilot pilot = this.load(Pilot.class, id);
			pilot.setDisabled(true);
		}
	}

	//司机信息恢复
	public void reUsePilot(List<Long> ids) {
		for (Long id : ids) {
			Pilot pilot = this.load(Pilot.class, id);
			pilot.setDisabled(false);
		}
	}

	//车型信息维护
	public void storeTransportationType(TransportationType type) {
		type.setDisabled(false);
		this.store(type);
	}

	//车型信息失效
	public void disabledTransportationType(List<Long> ids) {
		for (Long id : ids) {
			TransportationType type = this.load(TransportationType.class, id);
			type.setDisabled(true);
		}
	}

	//车型信息恢复
	public void reUseTransportationType(List<Long> ids) {
		for (Long id : ids) {
			TransportationType type = this.load(TransportationType.class, id);
			type.setDisabled(false);
		}
	}

	//车辆信息维护
	public void storeTransportation(Transportation transportation) {
		if ((transportation.getProduceDate() != null && transportation.getProduceDate().after(new Date()))
				|| (transportation.getPurchaseDate() != null && transportation.getPurchaseDate().after(new Date()))) {
			throw new BusinessException(
					"produceDate.or.purchaseDate.can.not.later.than.current.Date");
		}
		if (transportation.getProduceDate() != null && transportation.getPurchaseDate() != null 
				&& transportation.getProduceDate().after(transportation.getPurchaseDate())) {
			throw new BusinessException(
					"produceDate.can.not.later.than.purchaseDate");
		}
		if(transportation.getProduceDate() ==null){
			transportation.setProduceDate(null);
		}
		if (transportation.isNew()) {
			transportation.setStatus(TransportationStatus.ENABLE);
			this.store(transportation);
		}else{
			Transportation vc = commonDao.load(Transportation.class,transportation.getId());
			vc.setOrganization(transportation.getOrganization());
			vc.setType(transportation.getType());
			vc.setCode(transportation.getCode());
			vc.setLicense(transportation.getLicense());
			vc.setEngineNo(transportation.getEngineNo());
			vc.setTeamNo(transportation.getTeamNo());
			vc.setGps(transportation.getGps());
			vc.setBaseFuel(transportation.getBaseFuel());
			vc.setTrailType(transportation.getTrailType());
			vc.setWeight(transportation.getWeight());
			vc.setVolume(transportation.getVolume());
			vc.setProduceDate(transportation.getProduceDate());
			vc.setPurchaseDate(transportation.getPurchaseDate());
			vc.setMasterPilot(transportation.getMasterPilot());
			vc.setMinorPilot(transportation.getMinorPilot());
			vc.setContractType(transportation.getContractType());
			vc.setContractNo(transportation.getContractNo());
			vc.setTotalDistance(transportation.getTotalDistance());
			vc.setDescription(transportation.getDescription());
			vc.setStatus(TransportationStatus.ENABLE);
			this.store(vc);
		}
	}
	
	//车辆信息失效
	public void disabledTransportation(List<Long> ids) {
		for (Long id : ids) {
			Transportation transportation = this.load(Transportation.class, id);
			transportation.setDisabled(true);
		}
	}

	//车辆信息恢复
	public void reUseTransportation(List<Long> ids) {
		for (Long id : ids) {
			Transportation transportation = this.load(Transportation.class, id);
			transportation.setDisabled(false);
		}
	}
}
