package com.ym.mec.teacher.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

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

import org.snaker.engine.SnakerEngine;
import org.snaker.engine.access.QueryFilter;
import org.snaker.engine.entity.HistoryTask;
import org.snaker.engine.entity.Order;
import org.snaker.engine.entity.Process;
import org.snaker.engine.entity.Task;
import org.snaker.engine.helper.AssertHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.common.controller.BaseController;

@RestController
@RequestMapping(value = "/snaker/order")
@Api(tags = "流程实例")
public class OrderController extends BaseController {

	@Autowired
	private SnakerEngine snakerEngine;

	@Autowired
	private SysUserFeignService SysUserFeignService;

	/**
	 * 抄送实例已读
	 * @param orderId
	 * @param url
	 * @return
	 */
	@PostMapping(value = "setCcread")
	@ApiOperation("抄送实例设置已读")
	public Object ccread(String orderId) {
		SysUser user = SysUserFeignService.queryUserInfo();

		snakerEngine.order().updateCCStatus(orderId, new String[] { user.getId() + "" });
		return succeed();
	}

	@GetMapping(value = "detail")
	@ApiOperation("流程实例进度")
	public Object json(String processId, String orderId) {
		Process process = snakerEngine.process().getProcessById(processId);
		AssertHelper.notNull(process);
		Map<String, Object> jsonMap = new HashMap<String, Object>();

		Order order = snakerEngine.query().getOrder(orderId);
		jsonMap.put("order", order);

		List<Task> tasks = snakerEngine.query().getActiveTasks(new QueryFilter().setOrderId(orderId));
		jsonMap.put("tasks", tasks);

		List<HistoryTask> historyTasks = snakerEngine.query().getHistoryTasks(new QueryFilter().setOrderId(orderId));
		jsonMap.put("historyTasks", historyTasks);
		return jsonMap;
	}

}
