package com.unis.flowable.server.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.unis.common.utils.ResponseWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.github.pagehelper.PageInfo;
import com.unis.flowable.common.vo.DeploymentList;
import com.unis.flowable.server.service.FlowProcessService;

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

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/process")
@Api(value= "FlowProcessController", description = "流程管理")
public class FlowProcessController {
	
	@Autowired
	private FlowProcessService flowProcessService;
	
	@RequestMapping(value="/list",method=RequestMethod.GET)
	@ApiOperation(value="获取流程列表", notes = "分页查询流程列表")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "pageNum", value = "页码", paramType = "query"),
		@ApiImplicitParam(name = "pageSize", value = "每页条数", paramType = "query"),
		@ApiImplicitParam(name = "orgId", value = "组织id,暂时没用，需要开放", paramType = "query")
	})
	public PageInfo<DeploymentList> list(String orgId,Integer pageNum, Integer pageSize) {
		return new PageInfo<DeploymentList>(flowProcessService.processList(orgId,pageNum,pageSize));
	}
	
	@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 = flowProcessService.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;
	}
	
	/**
     * 生成流程图
     *
     * @param processId 任务ID
     */
	
	@RequestMapping(value = "processDiagram/{processId}", method=RequestMethod.GET)
	public Map<String, Object>  processDiagram(@PathVariable String processId) {
        return flowProcessService.processDiagram(processId);
		
	}

	/**
	 * 生成流图 返回base64 编码
	 * @param processId  流程ID
	 * @return
	 */
	@RequestMapping(value = "/findByLcslImage/{processId}", method=RequestMethod.GET)
	@ApiOperation(value="根据流程ID生成流图-------返回base64编码", notes = "参数 processId")
	public  ResponseWrapper findByLcslImage(
			@PathVariable("processId") String processId
	){
		String result=flowProcessService.genProcessDiagram(processId);
		return ResponseWrapper.success(200, "启动成功", result);
	}



	@RequestMapping(value = "/getImage/{processId}", method=RequestMethod.GET)
	@ApiOperation(value="根据流程ID生成流图-------返回base64编码", notes = "参数 processId")
	public  void getImage(
			HttpServletResponse response,
			@PathVariable("processId") String processId
	){
		try {
			flowProcessService.getImage(response,processId);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}



}
