package org.jeecg.modules.activiti.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.activiti.engine.repository.Model;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.activiti.service.ReProcdefService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;

/**
 * 流程管理
 *
 * @author yao
 * @date 2019/06/13
 */
@RestController
@RequestMapping("act/reprocdef")
public class ReProcdefController {
    @Autowired
    private ReProcdefService reProcdefService;

    /**
     * 分页查询
     *
     * @param req
     *            查询参数
     * @return RestResponse
     */
    @GetMapping("/list")
    public Result list(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                       @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                       HttpServletRequest req) {
        IPage page = reProcdefService.queryPage( pageNo,pageSize,req);
        return Result.OK(page);
    }

    /**
     * 获取激活的流程
     *
     * @return RestResponse
     */
    @GetMapping("/list/active")
    public Result listActive(HttpServletRequest request) {
        return Result.OK(reProcdefService.listActive(request));
    }

    /**
     * 读取资源，通过部署ID
     *
     * @param id
     *            流程定义ID
     * @param proInsId
     *            流程实例ID
     * @param resType
     *            资源类型(xml|image)
     * @param response
     *            响应
     * @throws Exception
     *             读写流异常
     */
    @RequestMapping("/read")
    public void resourceRead(String id, String proInsId, String resType, HttpServletResponse response)
        throws Exception {
        InputStream resourceAsStream = reProcdefService.resourceRead(id, proInsId, resType);
        byte[] b = new byte[1024];
        int len = -1;
        int lenEnd = 1024;
        while ((len = resourceAsStream.read(b, 0, lenEnd)) != -1) {
            response.getOutputStream().write(b, 0, len);
        }
    }

    /**
     * 部署流程文件
     *
     * @param file
     *            file
     * @return RestResponse
     */
    @RequestMapping("/deploy")
    public Result deploy(MultipartFile file,HttpServletRequest request) {
        String exportDir = this.getClass().getResource("/").getPath();
        String fileName = file.getOriginalFilename();
        String msg = "";
        if (StringUtils.isBlank(fileName)) {
            throw new JeecgBootException("请选择要部署的流程文件");
        } else {
            try {
                msg = reProcdefService.deploy(exportDir, file, request);
            } catch (Exception e) {
                return Result.error(e.getMessage());
            }
        }
        return Result.OK(msg);
    }

    /**
     * 转为模型
     *
     * @param id
     *            id
     * @return RestResponse
     */
    @GetMapping("/convertToModel")
    public Result convertToModel(String id,HttpServletRequest request) {
        Model model = null;
        try {
             model = reProcdefService.convertToModel(id,request);

        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.OK(model);
    }

    /**
     * 启动流程实例，通过processDefinitionId
     *
     * @param processDefinitionId
     *            processDefinitionId
     * @return RestResponse
     */
    @RequestMapping("/startProcessInstanceById")
    public Result startProcessInstanceById(String processDefinitionId) {
        try {
            reProcdefService.startProcessInstanceById(processDefinitionId);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.OK();
    }

    /**
     * 激活 / 挂起
     *
     * @param state
     * @param id
     * @return
     */
    @GetMapping("/update")
    public Result update(String id,int state) {
        String msg = reProcdefService.updateState(state, id);
        return Result.OK(msg);
    }

    /**
     * 批量删除部署的流程，级联删除流程实例
     *
     * @param ids
     *            流程部署标识
     * @return RestResponse
     */
    @RequestMapping("/deleteBatchByIds")
    public Result deleteBatchByIds(@RequestParam("ids[]") String[] ids) {
        reProcdefService.deleteBatch(ids);
        return Result.OK();
    }

    /**
     * 删除部署的流程，级联删除流程实例
     *
     * @param deploymentId
     *            流程部署标识
     * @return RestResponse
     */
    @DeleteMapping("/deleteByDeploymentId")
    public Result deleteByDeploymentId(String deploymentId) {
        reProcdefService.deleteByDeploymentId(deploymentId);
        return Result.OK();
    }

}
