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

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
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.base.Line;
import com.dev.swms.server.model.base.Store;
import com.dev.swms.server.service.base.LineManager;

public class DefaultLineManager extends DefaultBaseManager implements LineManager{
	
	//添加路线
	@Override
	public void saveLine(Line l) {
		l.setBeDisabled(false);//默认否
		l.setCreatDate(new Date());
		l.setCreatPeople(UserHolder.getUser().getName());
		commonDao.store(l);
		
	}
	
	//删除路线
	@Override
	public void deleteLine(List<Long> ids) {
		for(Long id :ids) {
			Line l = this.load(Line.class, id);
			this.delete(l);
		}
		
	}
	
	//失效
	@Override
	public void lineInvalid(List<Long> ids) {
		for(Long id :ids) {
			Line l = this.load(Line.class, id);
			l.setBeDisabled(true);
		}
		
	}

	//恢复
	@Override
	public void linerestore(List<Long> ids) {
		for(Long id :ids) {
			Line l = this.load(Line.class, id);
			l.setBeDisabled(false);
		}
		
	}

	//线路维护（添加门店到线路）
	@Override
	public void addLineStore(Long lid,List<Long> ids) {
		Line l=commonDao.load(Line.class, lid);
		for(Long id : ids) {
			Store store=commonDao.load(Store.class,id);
//			store.setCreatPeople(UserHolder.getUser().getName());
//			store.setCreatDate(new Date());	
			store.setLine(l);
			commonDao.store(store);
			
			
			
		}
		
		
		
		
	}
	
	@Override
	public void deleteLineStore(Long lid, List<Long> ids) {
		Line l=commonDao.load(Line.class, lid);
		for(Long id:ids){
			Store s=commonDao.load(Store.class, id);
			s.setLine(null);
			commonDao.store(s);
			
		}
		
	}

	//修改顺序号
	@Override
	public void updatesequenceNo(Long lid, Map stores) {
		Iterator it = stores.entrySet().iterator();
		while(it.hasNext()) {
		 	Map.Entry entry = (Map.Entry) it.next();	
			Long sid=(Long) entry.getKey();
			List value=  (List) entry.getValue();
			Integer sequenceNo = Integer.valueOf(String.valueOf(value.get(0)));
			Integer sequenceNo1 = Integer.valueOf(String.valueOf(value.get(1)));
			Integer sequenceNo2 = Integer.valueOf(String.valueOf(value.get(2)));
			Store store = commonDao.load(Store.class, sid);
			store.setSequenceNo(sequenceNo);
			store.setSequenceNo1(sequenceNo1);
			store.setSequenceNo2(sequenceNo2);
			commonDao.store(store);
		}
		
	}

}
