package com.unis.flowable.server.controller;

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

import org.flowable.ui.modeler.model.ModelRepresentation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.unis.flowable.server.service.FlowModelService;

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

@RestController
@RequestMapping("/flowmodel")
@Api(value= "FlowModelController", description = "模型管理")
public class FlowModelController {
	
	@Autowired
    private FlowModelService flowModelService;

	@RequestMapping(value="/list",method=RequestMethod.GET)
	@ApiOperation(value="查询模型列表", notes = "查询模型列表")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "pageNum", value = "当前页", paramType = "query"),
		@ApiImplicitParam(name = "pageSize", value = "页面大小", paramType = "query")
	})
	public PageInfo<ModelRepresentation> list(Integer pageNum, Integer pageSize) {
		
		PageInfo<ModelRepresentation> pageInfo =new PageInfo<ModelRepresentation>(flowModelService.modelList("test",pageNum,pageSize));
		return pageInfo;
	}
	
	/**
	 * 创建模型
	 * @param name流程图的名字
	 * @param key 流程启动的key值
	 * @param description 流程描述
	 * @param category 流程图
	 * @return
	 */
	@RequestMapping(value = "create", method = RequestMethod.POST)
	@ApiOperation(value="创建模型", notes = "创建模型")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "name", value = "模型名称", paramType = "query"),
		@ApiImplicitParam(name = "key", value = "模型key", paramType = "query"),
		@ApiImplicitParam(name = "description", value = "模型描述", paramType = "query")
	})
	public Map<String, Object> create(String name, String key, String description) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		try {
			ModelRepresentation modelRepresentation = new ModelRepresentation();
			modelRepresentation.setName(name);
			modelRepresentation.setKey(key);
			modelRepresentation.setDescription(description);
			modelRepresentation.setModelType(0);
			org.flowable.ui.modeler.domain.Model modelData = flowModelService.create(modelRepresentation);
			if(modelData == null) {
				modelMap.put("message", "创建失败，key已存在");
				modelMap.put("code", 400);
				return modelMap;
			}
			modelMap.put("data", modelData.getId());
			modelMap.put("message", "创建成功!");
			modelMap.put("code", 200);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return modelMap;
	}
	
	@RequestMapping(value = "deploy", method = RequestMethod.POST)
	@ApiOperation(value="部署模型", notes = "部署模型")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id", value = "模型id", paramType = "query")
	})
	public Map<String, Object> deploy(String id) {
		int message = flowModelService.deploy(id);
		Map<String, Object> modelMap = new HashMap<String, Object>();
		if(message==0) {
			modelMap.put("message", "部署失败!");
			modelMap.put("code", 500);
		} else {
			modelMap.put("message", "部署成功");
			modelMap.put("code", 200);
		}
		return modelMap;
	}
	
	@RequestMapping(value = "delete", method = RequestMethod.GET)
	@ApiOperation(value="删除模型", notes = "删除模型")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id", value = "模型id", paramType = "query")
	})
	public Map<String, Object> delete(String id) {
		int message = flowModelService.delete(id);
		Map<String, Object> modelMap = new HashMap<String, Object>();
		if(message==0) {
			modelMap.put("message", "删除失败!");
			modelMap.put("code", 500);
		} else {
			modelMap.put("message", "删除成功");
			modelMap.put("code", 200);
		}
		return modelMap;
	}
}
