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

import java.util.List;

import com.dev.swms.server.web.filter.WarehouseHolder;
import com.dev.swms.server.model.fee.FeeRate;
import com.dev.swms.server.model.fee.FeeType;
import com.dev.swms.server.service.base.FeeTypeManager;
import com.dev.energy.server.exception.BusinessException;
import com.dev.energy.server.service.pojo.DefaultBaseManager;

public class DefaultFeeTypeManager extends DefaultBaseManager implements
		FeeTypeManager {

	
	


	public void delete(List<Long> ids) {
		for (Long id : ids) {
			FeeType feeType = this.load(FeeType.class, id);
			feeType.setDisabled(true);
			
			}
		/*// TODO Auto-generated method stub
		String actionName="失效";
		String str1="TMS";
		for (Long id : ids) {
			FeeType feeType = this.load(FeeType.class, id);
			feeType.setDisabled(true);
			if(PlatFormHolder.getPlatForm()==null){
				str1="WMS";
				operationHisManager.storeOperationHis(feeType.getId(), feeType.getCode(),null, null, 
						"计费项目管理", actionName,  null,null, 
						0, 0, 0, 0, feeType.getDescription(),str1);
			}else{
				operationHisManager.storeOperationHis(feeType.getId(), feeType.getCode(),null, null, 
						"计费项目管理", actionName,  null,null, 
						0, 0, 0, 0, feeType.getDescription(),str1);
			}
		}*/

	}

	public void resume(List<Long> ids) {
		for (Long id : ids) {
			FeeType feeType = this.load(FeeType.class, id);
			feeType.setDisabled(false);
			
			}
		/*// TODO Auto-generated method stub
		String actionName="恢复";
		String str1="TMS";
		for (Long id : ids) {
			FeeType feeType = this.load(FeeType.class, id);
			feeType.setDisabled(false);
			if(PlatFormHolder.getPlatForm()==null){
				str1="WMS";
				operationHisManager.storeOperationHis(feeType.getId(), feeType.getCode(),null, null, 
						"计费项目管理", actionName,  null,null, 
						0, 0, 0, 0, feeType.getDescription(),str1);
			}else{
				operationHisManager.storeOperationHis(feeType.getId(), feeType.getCode(),null, null, 
						"计费项目管理", actionName,  null,null, 
						0, 0, 0, 0, feeType.getDescription(),str1);
			}
		}*/
	}

	public void save(FeeType feeType) {
		this.commonDao.store(feeType);
	}
	
	public void saveRate(FeeRate rate) {
		if(rate.isNew()){
			List<FeeRate> FeeRates =  commonDao.findByQuery(" from FeeRate fr where fr.company.id=:id and fr.feeName.name=:name ",
			new String[] { "id","name"},new Object[] {rate.getCompany().getId(),rate.getFeeName().getName()});
			if(FeeRates.size()>0){
				throw new BusinessException(rate.getCompany().getName()+"货主，已新建"+rate.getFeeName().getName()+"!");
			}else{
				this.commonDao.store(rate);
			}
		}else{
			FeeRate fr=this.load(FeeRate.class, rate.getId());
			
			List<FeeRate> FeeRates =  commonDao.findByQuery(" from FeeRate fr where fr.company.id=:id and fr.feeName.name=:name ",
					new String[] { "id","name"},new Object[] {rate.getCompany().getId(),rate.getFeeName().getName()});
			if(FeeRates.size()>0){
				if(fr.getFeeName().getName().equals(rate.getFeeName().getName())){
					fr=FeeRates.get(0);
					fr.setFeeName(rate.getFeeName());
					fr.setType(rate.getType());
					fr.setPrice(rate.getPrice());
					fr.setDescription(rate.getDescription());
					fr.setShipmentMethod(rate.getShipmentMethod());
					fr.setPackType(rate.getPackType());
					fr.setNode(rate.getNode());
					this.commonDao.store(fr);
				}else{
					throw new BusinessException(rate.getCompany().getName()+"货主，已新建"+rate.getFeeName().getName()+"!");
				}
				
			}else{
				
			}
		}
		
	}
	
	public void deleteRate(List<Long> ids){
		for(Long id:ids){
			FeeRate r=commonDao.load(FeeRate.class, id);
			commonDao.delete(r);
		}
	}
	
}
