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

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.dev.swms.server.model.base.Worker;
import com.dev.swms.server.model.inventory.Task;
import com.dev.swms.server.model.inventory.TaskStatus;
import com.dev.swms.server.model.inventory.TaskType;
import com.dev.swms.server.service.receiving.MoveDocManager;
import com.dev.swms.server.service.shipping.PickTicketManager;
import com.dev.swms.server.service.task.TaskManager;
import com.dev.energy.server.service.pojo.DefaultBaseManager;

/**
 * 
 * @author  dev
 * @description 
 * 
 */

@SuppressWarnings("unchecked")
public class DefaultTaskManager extends DefaultBaseManager implements
		TaskManager {
	
	protected final MoveDocManager moveDocManager;
	
	protected final PickTicketManager pickTicketManager;
	
	public DefaultTaskManager(MoveDocManager moveDocManager,PickTicketManager pickTicketManager){
		this.moveDocManager = moveDocManager;
		this.pickTicketManager = pickTicketManager;
	}
	
	protected List<Task> getTasksByPickTicketId(Long pickTicketId){
		return commonDao.findByNamedQuery("getTasksByPickTicket",
				new String[]{"types","pickTicketId"},
				new Object[]{TaskType.MV_PICKTICKET,pickTicketId});
	}
	
	public void taskBack(List<Long> tasks){
		for(Long id : tasks){
			taskBack(id);
		}
	}
	
	public void taskBack(Long taskId){
		Task task  = load(Task.class,taskId);
		task.setOperateTime(null);
		task.setWorker(null);
	}
	
	
	protected Worker getWorkerByUserId(Long userId){
		List<Worker> workers = commonDao.findByNamedQuery("getWorkerByUserId",
				new String[]{"userId"},new Object[]{userId});
		if(workers.isEmpty()){
			throw new NullPointerException("操作人员不存在!");
		}
		return workers.get(0);
	}
	
	public boolean isExistMoveTask(Long userId){
		Map<String,Long> tasks = getTaskByTypeAndWorker(userId);
		for(Long value : tasks.values()){
			if(value != null && value > 0L){
				return true;
			}
		}
		return false;
	}
	
	public Map<String,Long> getTaskByTypeAndWorker(Long userId){
		Map<String,Long> tasks = new HashMap<String, Long>();
		tasks.put("1", getMvASNCountByUserId(userId));
		tasks.put("2", getMvQualityCountByUserId(userId));
		tasks.put("3", getMvProcessCountByUserId(userId));
		tasks.put("4", getMvPickbackCountByUserId(userId));
		tasks.put("5", getMvMoveCountByUserId(userId));
		tasks.put("6", getMvPickticketCountByUserId(userId));
		return tasks;
	}
	
	/**
	 * 收货上架
	 */
	protected Long getMvASNCountByUserId(Long userId){
		List<String> types = new ArrayList<String>();
		types.add(TaskType.MV_ASN);
		types.add(TaskType.MV_CROSS);
		return (Long)commonDao.findByNamedQueryUniqueResult("getTaskCountByTypesAndWorker",
				new String[]{"types","userId"}, new Object[]{types,userId});
	}
	
	/**
	 * 质检上架
	 */
	protected Long getMvQualityCountByUserId(Long userId){
		List<String> types = new ArrayList<String>();
		types.add(TaskType.MV_QUALITY);
		return (Long)commonDao.findByNamedQueryUniqueResult("getTaskCountByTypesAndWorker",
				new String[]{"types","userId"}, new Object[]{types,userId});
	}
	
	/**
	 * 加工上架
	 */
	protected Long getMvProcessCountByUserId(Long userId){
		List<String> types = new ArrayList<String>();
		types.add(TaskType.MV_PROCESS);
		return (Long)commonDao.findByNamedQueryUniqueResult("getTaskCountByTypesAndWorker",
				new String[]{"types","userId"}, new Object[]{types,userId});
	}
	
	/**
	 * 退拣上架
	 */
	protected Long getMvPickbackCountByUserId(Long userId){
		List<String> types = new ArrayList<String>();
		types.add(TaskType.MV_PICKBACK);
		return (Long)commonDao.findByNamedQueryUniqueResult("getTaskCountByTypesAndWorker",
				new String[]{"types","userId"}, new Object[]{types,userId});
	}
	
	/**
	 * 库内移位
	 */
	protected Long getMvMoveCountByUserId(Long userId){
		List<String> types = new ArrayList<String>();
		types.add(TaskType.MV_MOVE);
		types.add(TaskType.MV_SUPPLY);
		return (Long)commonDao.findByNamedQueryUniqueResult("getTaskCountByTypesAndWorker",
				new String[]{"types","userId"}, new Object[]{types,userId});
	}
	
	/**
	 * 拣货下架
	 */
	protected Long getMvPickticketCountByUserId(Long userId){
		List<String> types = new ArrayList<String>();
		types.add(TaskType.MV_PICKTICKET);
		return (Long)commonDao.findByNamedQueryUniqueResult("getTaskCountByTypesAndWorker",
				new String[]{"types","userId"}, new Object[]{types,userId});
	}

	public void assignedTask(Task task) {
		Task dbTask = commonDao.load(Task.class, task.getId());
		dbTask.setWorker(commonDao.load(Worker.class, task.getWorker().getId()));
		dbTask.setOperateTime(new Date());
		commonDao.store(dbTask);
	}

	public void lock(List<Long> tasks) {
		for(Long id :tasks){
			Task task = commonDao.load(Task.class, id);
			if(task.getTaskStatus().equals(TaskStatus.NORMAL)){
				task.setTaskStatus(TaskStatus.FREEZE);
			}
		}
	}
	
	public void unlock(List<Long> tasks) {
		for(Long id :tasks){
			Task task = commonDao.load(Task.class, id);
			if(task.getTaskStatus().equals(TaskStatus.FREEZE)){
				task.setTaskStatus(TaskStatus.NORMAL);
			}
		}
	}
}


