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

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

import com.dev.swms.server.model.base.Structure;
import com.dev.swms.server.model.base.UserGroupWarehouse;
import com.dev.swms.server.model.base.Warehouse;
import com.dev.swms.server.model.base.Worker;
import com.dev.swms.server.model.base.WorkerStructure;
import com.dev.swms.server.model.base.WarehouseArea;
import com.dev.swms.server.service.base.WorkerManager;
import com.dev.swms.server.web.filter.WarehouseHolder;
import com.dev.energy.server.exception.BusinessException;
import com.dev.energy.server.model.Entity;
import com.dev.energy.server.model.EntityFactory;
import com.dev.energy.server.model.security.Group;
import com.dev.energy.server.model.security.User;
import com.dev.energy.server.service.pojo.DefaultBaseManager;
import com.dev.energy.server.util.LocalizedMessage;

@SuppressWarnings("unchecked")
public class DefaultWorkerManager extends DefaultBaseManager implements
		WorkerManager {

	public void disabled(List<Long> ids) {
		for(Long id : ids) {
			Worker worker = this.commonDao.load(Worker.class, id);
			worker.setDisabled(true);
		}	
	}

	public void active(List<Long> ids) {
		for(Long id : ids) {
			Worker worker = this.commonDao.load(Worker.class, id);
			worker.setDisabled(false);
		}		
	}

	public void storeWorker(Worker worker) {
		if (worker.isNew()) {
			worker.setWarehouse(WarehouseHolder.getWarehouse());
			this.store(worker);
		} else {
			Worker dbWorker = commonDao.load(Worker.class, worker.getId());
			dbWorker.setCode(worker.getCode());
			dbWorker.setName(worker.getName());
			if (worker.getZone() != null && worker.getZone().getId() != null 
					&& worker.getZone().getId() != 0) {
				dbWorker.setZone(commonDao.load(WarehouseArea.class, worker.getZone().getId()));
			} else {
				dbWorker.setZone(null);
			}
			dbWorker.setType(worker.getType());
			if (worker.getUser() != null && worker.getUser().getId() != 0) {
				dbWorker.setUser(commonDao.load(User.class, worker.getUser().getId()));
			} else {
				dbWorker.setUser(null);
			}
			dbWorker.setDescription(worker.getDescription());
			commonDao.store(dbWorker);
		}
	}

	public Warehouse getWarhouseByUser(Long userId) {
		List<Warehouse> warehouses = commonDao.findByNamedQuery("getWarehouseByUserId",
				new String[]{"userId"}, new Object[]{userId});
		if(!warehouses.isEmpty()){
			return warehouses.get(0);
		}
		return null;
	}
	
	public boolean isExistWorkerByUser(Long userId){
		return !commonDao.findByNamedQuery("getWorkerByUserId",
				new String[]{"userId"},new Object[]{userId}).isEmpty();
	}
	public List<Warehouse> getWarehousesByAdmin(){
		List<Warehouse> warehouses = new ArrayList<Warehouse>();
		warehouses = this.commonDao.findByQuery("from Warehouse warehouse where warehouse.disabled = false");
		return warehouses;
	}
	public boolean isExistUserByUserId(Long userId){
		String hql = "FROM User user WHERE user.id = :userId AND user.enabled = true " +
		"AND user.locked = false ORDER BY user.id";
		return !commonDao.findByQuery(hql, new String[]{"userId"},new Object[]{userId}).isEmpty();
	}
	
	public List getWarehousesByUser(Long userId) {
		User user = this.commonDao.load(User.class, userId);
		Set<Group> groups = user.getGroups();
		List<Long> groupIds=new ArrayList<Long>();
		if(groups!=null){
			for(Group group:groups){
				groupIds.add(group.getId());
			}
		}
		List<Warehouse> warehouses = commonDao.findByNamedQuery("getWarehouseByUserGroup", 
										new String[]{"groupIds"}, new Object[]{groupIds});
		if(!warehouses.isEmpty()){
//			if(warehouses.size()<=5){
//				return warehouses;
//				}else{
//					return (warehouses.subList(0, 5));
//				}
			return warehouses;
		}
		return null;
	}
	
	public void invokeStructureToWorker(List<Long> cusIds,Long wId) {
		WorkerStructure workerStructure = null;
		if (cusIds.size() == 0 || cusIds == null) {
			throw new BusinessException("请选择记录");
		}
		for (Long cusId : cusIds) {
			workerStructure = getWorkerStructure(cusId, wId);
			this.commonDao.store(workerStructure);
		}
	}
	
	private WorkerStructure getWorkerStructure(Long cusId, Long wId) {
		WorkerStructure workerStructure = (WorkerStructure) commonDao.findByQueryUniqueResult(
						"from WorkerStructure w where w.structure.id=:cusId and w.worker.id=:wId",
						new String[] { "cusId", "wId" }, new Object[] {
								cusId, wId });
		if (workerStructure == null) {
			Structure st = commonDao.load(Structure.class, cusId);
			Worker w = commonDao.load(Worker.class, wId);
			workerStructure=EntityFactory.getEntity(WorkerStructure.class);
			workerStructure.setStructure(st);
			workerStructure.setWorker(w);
			
		}
		return workerStructure;
	}
	
	public void revokeStructureToWorker(List<Long> ids,Long wId) {
		for(Long id:ids){
			WorkerStructure ws=this.commonDao.get(WorkerStructure.class, id);
			this.commonDao.delete(ws);
		}
	}
}
