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

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

import org.springframework.transaction.annotation.Transactional;

import com.dev.energy.server.service.pojo.DefaultBaseManager;
import com.dev.energy.server.web.security.UserHolder;

import com.dev.swms.server.model.count.CommonCode;
import com.dev.swms.server.model.count.Consumables;
import com.dev.swms.server.model.count.Contract;
import com.dev.swms.server.service.count.CommonCodeManager;
import com.dev.swms.server.service.sequence.BussinessCodeManager;



public class DefaultCommonCodeManager extends DefaultBaseManager implements CommonCodeManager{
	
	protected final BussinessCodeManager bussinessCodeManager;
	public DefaultCommonCodeManager(BussinessCodeManager bussinessCodeManager) {
		// TODO 自动生成的构造函数存根
		this.bussinessCodeManager=bussinessCodeManager;
		
	}
	
	
	@Override
	public void storeCommonCode(CommonCode commonCode) {
		commonCode.setOperator(UserHolder.getUser().getName());
		commonCode.setOperateDate(new Date());
		this.commonDao.store(commonCode);
	}
   
	@Override
	public void inActiveCommonCode(List<Long> ids) {
		for (Long id : ids) {
			CommonCode commonCode = this.commonDao.load(CommonCode.class,id);
			commonCode.setOperator(UserHolder.getUser().getName());
			commonCode.setOperateDate(new Date());
			commonCode.setDisabled(true);
		}
	}

	@Override
	public void activeCommonCode(List<Long> ids) {
		for (Long id : ids) {
			CommonCode commonCode = this.commonDao.load(CommonCode.class,id);
			commonCode.setOperator(UserHolder.getUser().getName());
			commonCode.setOperateDate(new Date());
			commonCode.setDisabled(false);
		}
	}
	
	@Transactional
	public void storeContract(Contract contract) {
		if(contract.isNew()){
			contract.setOperator(UserHolder.getUser().getName());
			contract.setOperatorDate(new Date());
			contract.setStatus("OPEN");
			contract.setBeContract(true);
			contract.setDisabled(false); 
			contract.setCode(this.bussinessCodeManager.getCodeForYm("RP",5));
		}
		this.commonDao.store(contract);
		
	}
	
	@Transactional  
	public void inActiveContract(List<Long> ids) {
		for(Long id:ids){
			Contract con = this.commonDao.load(Contract.class,id);
			con.setDisabled(true);
			this.commonDao.store(con);
			
		}
		
		
	}

	@Transactional
	public void activeContract(List<Long> ids) {
		for(Long id:ids){
			Contract con = this.commonDao.load(Contract.class,id);
			con.setDisabled(false);
			this.commonDao.store(con);
			
		}
		
	}
	
	public void saveConsumables(Consumables consumables) {
		if(consumables.isNew()) {
			consumables.setOperator(UserHolder.getUser().getName());
			consumables.setOperatorDate(new Date());
		}
		commonDao.store(consumables);
	}
	public void deleteConsumables(List<Long> ids) {
		for(Long id:ids) {
			Consumables c=commonDao.load(Consumables.class, id);
			commonDao.delete(c);
		}
	}
}
