package com.ym.mec.teacher.controller;

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

import org.snaker.engine.SnakerEngine;
import org.snaker.engine.access.Page;
import org.snaker.engine.access.QueryFilter;
import org.snaker.engine.core.AccessService;
import org.snaker.engine.entity.HistoryOrder;
import org.snaker.engine.entity.WorkItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
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;

/**
 * Snaker流程引擎常用Controller
 * @author yuqs
 * @since 0.1
 */
@RestController
@RequestMapping(value = "/snaker/task")
@Api(tags = "流程任务")
public class TaskController extends BaseController {

	@Autowired
	private SnakerEngine snakerEngine;

	@Autowired
	private SysUserFeignService SysUserFeignService;

	/**
	 * 查询我发起的任务列表
	 * @param model
	 * @return
	 */
	@GetMapping(value = "queryMyCreatedList")
	@ApiOperation("查询我发起的任务列表")
	public Object queryMyCreatedList(Page<HistoryOrder> page) {
		SysUser user = SysUserFeignService.queryUserInfo();
		if (user == null) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}

		snakerEngine.query().getHistoryOrders(page, new QueryFilter().setOperator(user.getId() + ""));
		return succeed(page);
	}

	/**
	 * 查询我待办任务列表
	 * @param model
	 * @return
	 */
	@GetMapping(value = "queryWaitList")
	@ApiOperation("查询我待办任务列表")
	public Object queryWaitList(Page<WorkItem> page) {
		SysUser user = SysUserFeignService.queryUserInfo();
		if (user == null) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}

		snakerEngine.query().getWorkItems(page, new QueryFilter().setOperator(user.getId() + ""));
		return succeed(page);
	}

	/**
	 * 查询我已办任务列表
	 * @param model
	 * @return
	 */
	@GetMapping(value = "queryProcessedList")
	@ApiOperation("查询我已办任务列表")
	public Object queryProcessedList(Page<WorkItem> page) {
		SysUser user = SysUserFeignService.queryUserInfo();
		if (user == null) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}

		snakerEngine.query().getHistoryWorkItems(page, new QueryFilter().setOperator(user.getId() + ""));
		return succeed(page);
	}

	/**
	 * 抄送我的已读列表
	 * @param model
	 * @return
	 */
	@GetMapping(value = "queryCCReadList")
	@ApiOperation("抄送我的已读列表")
	public Object queryCCReadList(Page<HistoryOrder> page) {
		SysUser user = SysUserFeignService.queryUserInfo();
		if (user == null) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}

		snakerEngine.query().getCCWorks(page, new QueryFilter().setOperator(user.getId() + "").setState(AccessService.STATE_FINISH));
		return succeed(page);
	}

	/**
	 * 抄送我的未读列表
	 * @param model
	 * @return
	 */
	@GetMapping(value = "queryCCUnReadList")
	@ApiOperation("抄送我的未读列表")
	public Object queryCCUnReadList(Page<HistoryOrder> page) {
		SysUser user = SysUserFeignService.queryUserInfo();
		if (user == null) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}

		snakerEngine.query().getCCWorks(page, new QueryFilter().setOperator(user.getId() + "").setState(AccessService.STATE_ACTIVE));
		return succeed(page);
	}

}
