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

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.dev.swms.server.model.base.Location;
import com.dev.swms.server.model.base.Position;
import com.dev.swms.server.model.base.PositionDetail;
import com.dev.swms.server.service.base.PositionManager;
import com.dev.swms.server.web.filter.WarehouseHolder;
import com.dev.energy.server.exception.BusinessException;
import com.dev.energy.server.service.pojo.DefaultBaseManager;

public class DefaultPositionManager extends DefaultBaseManager implements
		PositionManager {

	public void storePosition(Position position) {
		if (position.isNew()) {
			position.setWarehouse(WarehouseHolder.getWarehouse());
		}
		this.store(position);
	}

	public void addDetailToPosition(Long positionId, PositionDetail detail) {
		Position position = commonDao.load(Position.class,positionId);
		if(detail.isNew()){
			detail.setPosition(position);
			position.addDetail(detail);
		}
		updateLocationPositions(detail, 1);
	}

	public void deleteDetailFromPosition(Long positionId, List<Long> positionDetailIds) {
		Position position = commonDao.load(Position.class,positionId);
		List<PositionDetail> positionDetails = commonDao.loadAll(PositionDetail.class,
				positionDetailIds.toArray(new Long[positionDetailIds.size()]));
		for (PositionDetail detail : positionDetails) {
			position.getDetails().remove(detail);
			updateLocationPositions(detail, -1);
		}
	}

	public void disablePosition(List<Long> positionIds) {
		List<Position> positions = commonDao.loadAll(Position.class,
				positionIds.toArray(new Long[positionIds.size()]));
		for(Position position : positions){
			for (PositionDetail detail : position.getDetails()) {
				updateLocationPositions(detail, -1);
			}

			position.setDisabled(true);
		}
	}

	public void enablePosition(List<Long> positionIds) {
		List<Position> positions = commonDao.loadAll(Position.class,
				positionIds.toArray(new Long[positionIds.size()]));
		for(Position position : positions){
			for (PositionDetail detail : position.getDetails()) {
				updateLocationPositions(detail, 1);
			}

			position.setDisabled(false);
		}
	}

	@SuppressWarnings("unchecked")
	private void updateLocationPositions(String sid,Location loc,int i) {
		if (i == 1) {
			//增加库位定位分类
			if (loc.getPositionIds()==null || loc.getPositionIds().indexOf(sid) == -1) {
				loc.setPositionIds((loc.getPositionIds()==null?"":loc.getPositionIds()) + sid);
			}
		} else {
			//删除库位定位分类
			if (loc.getPositionIds()!=null && loc.getPositionIds().indexOf(sid) >= 0) {
				loc.setPositionIds(StringUtils.replace(loc.getPositionIds(), sid, ""));
			}
		}
		commonDao.store(loc);
	}
	@SuppressWarnings("unchecked")
	private void updateLocationPositions(PositionDetail detail, int i) {
		String hql = "from Location location " +  
		" where location.disabled = false " +
		" and location.type = 'STORAGE' " +
		" and location.zone.id = " + detail.getZone().getId() +
		" and location.aisle >= " + detail.getStartAisle() +
		" and location.aisle <= " + detail.getEndAisle() + 
		" and location.line >= " + detail.getStartLine() +
		" and location.line <= " + detail.getEndLine() +
		" and ((location.row >= " + detail.getStartRow() + " and location.row <= " + detail.getEndRow() + ") " +
		" or (" + detail.getStartRow() + " = 0 and " + detail.getEndRow() + " = 0)) " +
		" and ((location.col >= " + detail.getStartCol() + " and location.col <= " + detail.getEndCol() + ") " +
		" or (" + detail.getStartCol() + " = 0 and " + detail.getEndCol() + " = 0)) " + 
		" order by location.id";
		
		String sid = "<"+detail.getPosition().getId()+">";
		List<Location> locs = commonDao.findByQuery(hql);
		for (Location loc : locs) {
			if (i == 1) {
				//增加库位定位分类
				if (loc.getPositionIds()==null || loc.getPositionIds().indexOf(sid) == -1) {
					loc.setPositionIds((loc.getPositionIds()==null?"":loc.getPositionIds()) + sid);
				}
			} else {
				//删除库位定位分类
				if (loc.getPositionIds()!=null && loc.getPositionIds().indexOf(sid) >= 0) {
					loc.setPositionIds(StringUtils.replace(loc.getPositionIds(), sid, ""));
				}
			}
			commonDao.store(loc);
		}
	}

	@Override
	public void addlocationtoposition(Long pid, List<Long> ids) {
		Position p=commonDao.load(Position.class, pid);
		//List<Long> idss=commonDao.findByQuery("select l.id from Location l where l.type in ('STORAGE','TRANSIT')");
		for(Long id : ids) {
			Location l=commonDao.load(Location.class,id);
			PositionDetail pd=new PositionDetail();
			pd.setStartAisle(l.getAisle());
			pd.setEndAisle(l.getAisle());
			pd.setStartLine(l.getLine());
			pd.setEndLine(l.getLine());
			pd.setStartRow(l.getRow());
			pd.setEndRow(l.getRow());
			pd.setStartCol(l.getCol());
			pd.setEndCol(l.getCol());
			pd.setLocation(l);
			pd.setZone(l.getZone());
			pd.setPosition(p);
			List<PositionDetail> objs =  commonDao.findByQuery(" from PositionDetail pd where pd.location.id=:id and pd.position.id=:pid ",
					new String[] { "id","pid"},new Object[] {l.getId(),p.getId()});
			if(objs.size()>0){
				throw new BusinessException("该库位已经添加");
			}else {
				commonDao.store(pd);
				updateLocationPositions("<"+pid+">",l, 1);
			}
	
			
		}
		
	}

	@Override
	public void removelocationFromPosition(Long pid, List<Long> ids) {
		/*Position p=commonDao.load(Position.class, pid);
		for(Long id:ids){
			PositionDetail l=commonDao.load(PositionDetail.class, id);
			p.getDetails().remove(id);
			updateLocationPositions(l, -1);
			this.delete(l);
		}*/
		Position position = commonDao.load(Position.class,pid);
		List<PositionDetail> positionDetails = commonDao.loadAll(PositionDetail.class,
				ids.toArray(new Long[ids.size()]));
		for (PositionDetail detail : positionDetails) {
			Location l=detail.getLocation();
			position.getDetails().remove(detail);
			updateLocationPositions("<"+pid+">",l, -1);
		}
		
		
		
	}

}
