package com.ym.mec.workfow.controller.flow;

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

import org.apache.commons.lang3.StringUtils;
import org.snaker.engine.SnakerEngine;
import org.snaker.engine.access.Page;
import org.snaker.engine.access.QueryFilter;
import org.snaker.engine.entity.HistoryOrder;
import org.snaker.engine.entity.HistoryTask;
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")
public class OrderController extends BaseController {

	@Autowired
	private SnakerEngine snakerEngine;

	@Autowired
	private SysUserFeignService SysUserFeignService;

	/**
	 * 流程实例查询
	 * @param model
	 * @param page
	 * @return
	 */
	@GetMapping(value = "list")
	public Object order(Page<HistoryOrder> page) {
		return succeed(snakerEngine.query().getHistoryOrders(page, new QueryFilter()));
	}

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

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

	@GetMapping(value = "detail")
	public Object json(String processId, String orderId) {
		Process process = snakerEngine.process().getProcessById(processId);
		AssertHelper.notNull(process);
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		/*ProcessModel model = process.getModel();
		if (model != null) {
			jsonMap.put("process", model);
		}*/

		if (StringUtils.isNotEmpty(orderId)) {
			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;
	}

}
