/* Copyright 2012-2013 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ym.mec.workfow.controller.flow;

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.Surrogate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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.common.controller.BaseController;

/**
 * 委托授权
 * @author yuqs
 * @since 0.1
 */
@RestController
@RequestMapping(value = "/snaker/surrogate")
public class SurrogateController extends BaseController {

	@Autowired
	private SnakerEngine snakerEngine;

	@GetMapping(value = "list")
	public Object list(Page<Surrogate> page) {
		return succeed(snakerEngine.manager().getSurrogate(page, new QueryFilter()));
	}

	@GetMapping(value = "view/{id}")
	public Object view(@PathVariable("id") String id) {
		return succeed(snakerEngine.manager().getSurrogate(id));
	}

	@PostMapping(value = "update")
	public Object update(Surrogate surrogate) {
		surrogate.setSdate(surrogate.getSdate() + " 00:00:00");
		surrogate.setEdate(surrogate.getEdate() + " 23:59:59");
		if (surrogate.getState() == null) {
			surrogate.setState(AccessService.STATE_ACTIVE);
		}
		snakerEngine.manager().saveOrUpdate(surrogate);
		return succeed();
	}

	@RequestMapping(value = "delete/{id}")
	public Object delete(@PathVariable("id") String id) {
		snakerEngine.manager().deleteSurrogate(id);
		return succeed();
	}
}
