package com.blueland.ebpm.rest;

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

import com.blueland.ebpm.util.RestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.blueland.bpm.common.page.Page;
import com.blueland.bpm.engine.core.model.ExceptionErrorCode;
import com.blueland.ebpm.exception.RestException;
import com.blueland.unitework.model.UniteworkTask;
import com.blueland.unitework.service.UniteworkService;

@RestController
@RequestMapping("/unitework")
public class UniteworkController {

	@Autowired
	private UniteworkService uniteworkService;

	@RequestMapping(value = "/myrecordlist", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public Page<UniteworkTask> queryUniteworkList(@RequestBody UniteworkTask task) {
		if (task.getPageSize() == 0)
			task.setPageNumber(10);
		if (RestUtils.isEmpty(task.getRecUserId()))
			throw new RestException(ExceptionErrorCode.B2001.name(),	"recUserId is empty");
		if (RestUtils.isEmpty(task.getStatus()))
			throw new RestException(ExceptionErrorCode.B2001.name(),	"status is empty");
		return uniteworkService.queryTask(task);
	}
	
	@RequestMapping(value = "/myYibanlist", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public Page<UniteworkTask> myYibanlist(@RequestBody UniteworkTask task) {
		if (task.getPageSize() == 0)
			task.setPageNumber(10);
		if (RestUtils.isEmpty(task.getRecUserId()))
			throw new RestException(ExceptionErrorCode.B2001.name(),	"recUserId is empty");
	/*	if (RestUtils.isEmpty(task.getStatus()))
			throw new RestException(ExceptionErrorCode.B2001.name(),	"status is empty");*/
		return uniteworkService.queryYibanTask(task);
	}
	
	@RequestMapping(value = "/queryUnitework", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public List<UniteworkTask> queryUnitework(@RequestBody UniteworkTask task) {
		if (task.getPageSize() == 0)
			task.setPageNumber(10);
		if (RestUtils.isEmpty(task.getProcInstId()))
			throw new RestException(ExceptionErrorCode.B2001.name(),	"procInstId is empty");
		return uniteworkService.findByParams(task);
	}


	@RequestMapping(value = "/myrecordcount", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public Map<String, Object> queryUniteworkCount(@RequestBody UniteworkTask task) {
		if (RestUtils.isEmpty(task.getRecUserId()))
			throw new RestException(ExceptionErrorCode.B2001.name(),	"recUserId is empty");
		if (RestUtils.isEmpty(task.getStatus()))
			throw new RestException(ExceptionErrorCode.B2001.name(),	"status is empty");
		int count = uniteworkService.getTaskCount(task);
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("count", count);
		return result;
	}
	
	@RequestMapping(value="/query/procinstid", produces = "application/json;charset=UTF-8")
	public List<UniteworkTask> queryByProcInstId(@RequestParam String procInstId){
		return uniteworkService.queryByProcInstId(procInstId);
	}
	
	
	@RequestMapping(value="/query/procinstid/actdefid",  produces = "application/json;charset=UTF-8")
	public List<UniteworkTask> queryByProcInstIdActDefId(@RequestParam String procInstId, @RequestParam String curActDefId){
		return uniteworkService.queryByProcInstIdActDefId(procInstId, curActDefId);
	}
	
	@RequestMapping(value = "/{procInstId}/{sourceActId}/{targerActId}/isYiBanTask", produces = "application/json; charset=UTF-8")
	public boolean isYiBanTask(@PathVariable String procInstId,@PathVariable String sourceActId,@PathVariable String targerActId){
		if(StringUtils.isEmpty(procInstId)||StringUtils.isEmpty(sourceActId)||StringUtils.isEmpty(targerActId)){
			return false;
		}
		UniteworkTask task=new UniteworkTask();
		task.setCurActDefId(targerActId);
		task.setPrevActDefId(sourceActId);
		task.setProcInstId(procInstId);
		task.setStatus(UniteworkTask.TASK_TYPE_YIBAN);
		List<UniteworkTask> yiBanTasks=uniteworkService.findByParams(task);
		if(yiBanTasks!=null&&!yiBanTasks.isEmpty()){
			return true;
		}else{
			return false;
		}
	}

	@RequestMapping(value = "/{procInstId}/{recUserId}/isYBTask", produces = "application/json; charset=UTF-8")
	public boolean isYiBanTask(@PathVariable String procInstId, @PathVariable String recUserId){
		if(StringUtils.isEmpty(procInstId) || StringUtils.isEmpty(recUserId)){
			return false;
		}
		UniteworkTask task=new UniteworkTask();
		task.setProcInstId(procInstId);
		task.setRecUserId(recUserId);
		List<UniteworkTask> yiBanTasks = uniteworkService.findByParams(task);
		if(yiBanTasks!=null && !yiBanTasks.isEmpty()){
			return true;
		}else{
			return false;
		}
	}
	
	
	@RequestMapping(value = "/{procInstId}/{sourceActId}/{targerActId}/isYiBanTaskByTopId", produces = "application/json; charset=UTF-8")
	public boolean isYiBanTaskByTopId(@PathVariable String procInstId,@PathVariable String sourceActId,@PathVariable String targerActId){
		if(StringUtils.isEmpty(procInstId)||StringUtils.isEmpty(sourceActId)||StringUtils.isEmpty(targerActId)){
			return false;
		}
		UniteworkTask task=new UniteworkTask();
		task.setCurActDefId(targerActId);
		task.setPrevActDefId(sourceActId);
		task.setTopProcInstId(procInstId);
		task.setStatus(UniteworkTask.TASK_TYPE_YIBAN);
		List<UniteworkTask> yiBanTasks=uniteworkService.findByParams(task);
		if(yiBanTasks!=null&&!yiBanTasks.isEmpty()){
			return true;
		}else{
			return false;
		}
	}
	
	

	@RequestMapping(value = "/syncreadwork", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public Map<String, Object> syncReadWork(@RequestBody UniteworkTask task) {
		String type = task.getStatus();
		if (type.equals(UniteworkTask.TASK_TYPE_READ)) {
			if (RestUtils.isEmpty(task.getRecUserId())) {
				throw new RestException(ExceptionErrorCode.B2001.name(),	"recUserId is null");
			}
			if (RestUtils.isEmpty(task.getId())) {
				throw new RestException(ExceptionErrorCode.B2001.name(),	"id is null");
			}
			uniteworkService.insertRead(task);
		}else if(type.equals(UniteworkTask.TASK_TYPE_READED)) {
			uniteworkService.changeToRead(task.getId());			
		}else if(type.equals(UniteworkTask.TASK_TYPE_DEL)) {
			uniteworkService.deleteRead(task.getId());
		}
		
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("result", "OK");
		return result;
	}

	/**
	 * 待办变已办，供未接入流程中心的第三方应用调用
	 * @param task
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/changeTaskStatus", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public Map<String, Object> changeTaskStatus(@RequestBody UniteworkTask task) {
		if (RestUtils.isEmpty(task.getId())) {
			throw new RestException(ExceptionErrorCode.B2001.name(),	"id is null");
		}
		uniteworkService.changeToYiban(task.getId(),task.getDealTime());
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("result", "OK");
		return result;
	}
}
