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

import java.util.List;
import java.util.Set;


import com.dev.energy.server.service.pojo.DefaultBaseManager;
import com.dev.energy.server.web.security.UserHolder;
import com.dev.swms.server.model.fee.ApplyBill;
import com.dev.swms.server.model.fee.BillFee;

import com.dev.swms.server.service.fee.ApplyBillManager;

public class DefaultApplyBillManager extends DefaultBaseManager implements ApplyBillManager{
	
	/**保存开票信息*/
	@Override
	public void saveApplyBill(ApplyBill applyBill) {
		if(applyBill.isNew()){
			applyBill.setStatus("打开");
			applyBill.setApplicant(UserHolder.getUser().getName());
		}
		commonDao.store(applyBill);
		
	}
	
	/**删除开票信息*/
	@Override
	public void deleteApplyBill(List<Long> ids) {
		for(Long id : ids) {
			ApplyBill applyBill = this.load(ApplyBill.class, id);//开票
			Set<BillFee> billFee=applyBill.getBillFee();//对账
			for(BillFee b : billFee) {
				b=this.load(BillFee.class, b.getId());
				this.delete(b);
			}
			this.delete(applyBill);
		}
		
	}

	@Override
	public void addApplyBillDetail(Long bid, List<Long> ids) {
		ApplyBill applyBill=commonDao.load(ApplyBill.class, bid);
		for(Long id : ids) {
			BillFee billFee=commonDao.load(BillFee.class,id);
			billFee.setApplyBill(applyBill);
			commonDao.store(billFee);	
		}
		
	}

	@Override
	public void removeApplyBillDetail(Long bid, List<Long> ids) {
		ApplyBill applyBill=commonDao.load(ApplyBill.class, bid);
		for(Long id:ids){
			BillFee billFee=commonDao.load(BillFee.class,id);
			billFee.setApplyBill(null);		
			commonDao.store(billFee);	
			
		}
		
	}
	
	
		
				
	
}
