package com.trade.admin.workflow.controller;

import com.trade.admin.system.controller.BaseController;
import com.trade.admin.workflow.domain.FlowActivityDefinition;
import com.trade.admin.workflow.dto.*;
import com.trade.admin.workflow.service.FlowActivityDefinitionService;
import com.trade.admin.workflow.service.FlowableService;
import com.trade.common.core.domain.ResponseResult;
import com.trade.common.core.page.GridPage;
import org.flowable.engine.repository.Deployment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * @Description:流程定义Controller
 * @Author:wanxiangZhou
 * @E-mail:zhouyu@sunxung.cn
 * @Date:2018年5月23日 上午11:14:27
 */
@RestController
@RequestMapping("/flowable/processDefinition")
public class ProcessDefinitionController extends BaseController {

    @Autowired
    private FlowableService flowableService;
    @Autowired
    private FlowActivityDefinitionService flowActDefService;

    @GetMapping(value = "/doList")
    public Object doList(SearchProcessDefinition search) {
        return flowableService.findLatestProcdef(search);
    }

    @PostMapping("/doDeploy")
    public ResponseResult<Deployment> doDeploy(@RequestBody @Validated DeployRequest deployRequest) {
        Deployment deploy = flowableService.deploy(deployRequest);
        return success(deploy);
    }

    @RequestMapping("/viewImg")
    public ResponseResult<Boolean> viewImg(HttpServletResponse response, String deploymentId, String resourceName) {
        flowableService.viewImg(response, deploymentId, resourceName);
        return success(Boolean.TRUE);
    }

    @GetMapping("/doListFlowActDef")
    public ResponseResult<GridPage<FlowActivityDefinition>> doListFlowActDef(SearchFlowActivityDefinition search) {
        GridPage<FlowActivityDefinition> gridPage = flowActDefService.findBySearch(search);
        return success(gridPage);
    }

    @RequestMapping("/doSetTimeLimit")
    public ResponseResult<Boolean> doSetTimeLimit(@RequestBody TimeLimitRequest timeLimitRequest) {
        Boolean updated = flowActDefService.updateTimeLimit(timeLimitRequest.getId(), timeLimitRequest.getTimeLimit(),
                timeLimitRequest.isWorkingDay());
        return success(updated);
    }
}
