package com.sunxung.factoring.web.sys.flowable;

import javax.servlet.http.HttpServletResponse;

import com.sunxung.factoring.entity.sys.flowable.request.DeployRequest;
import com.sunxung.factoring.entity.sys.request.TimeLimitRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import com.sunxung.factoring.entity.sys.flowable.vo.SearchFlowActivityDefinition;
import com.sunxung.factoring.entity.sys.flowable.vo.SearchProcessDefinition;
import com.sunxung.factoring.service.sys.flowable.FlowActivityDefinitionService;
import com.sunxung.factoring.service.sys.flowable.FlowableService;

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

    private static String PREFIX = "sys/flowable/";
    @Autowired
    private FlowableService flowableService;
    @Autowired
    private FlowActivityDefinitionService flowActDefService;

    @RequestMapping("/list")
    public String list(Model model) {
        return PREFIX + "processDefinitionList";
    }

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

    @RequestMapping("/deploy")
    public String add(Model model) {
        return PREFIX + "processDefinitionDeploy";
    }

    @PostMapping("/do_deploy")
    @ResponseBody
    public Object do_deploy(@RequestBody  @Validated DeployRequest deployRequest) {
        return flowableService.deploy(deployRequest);
    }

    @RequestMapping("/viewImg")
    public void viewImg(HttpServletResponse response, String deploymentId, String resourceName) {
        flowableService.viewImg(response, deploymentId, resourceName);
    }

    @RequestMapping("/viewFlowActDef")
    public String viewFlowActDef(String procDefId, Model model) {
        model.addAttribute("procDefId", procDefId);
        return PREFIX + "flowActDefList";
    }

    @GetMapping("/doListFlowActDef")
    @ResponseBody
    public Object doListFlowActDef(SearchFlowActivityDefinition search) {
        return flowActDefService.findBySearch(search);
    }

    @RequestMapping("/setTimeLimit")
    public String setTimeLimit(Long id, Model model) {
        model.addAttribute("id", id);
        return PREFIX + "flowActDefSetTimeLimit";
    }

    @RequestMapping("/doSetTimeLimit")
    @ResponseBody
    public Object doSetTimeLimit(@RequestBody TimeLimitRequest timeLimitRequest) {
        return flowActDefService.updateTimeLimit(timeLimitRequest.getId(), timeLimitRequest.getTimeLimit(), timeLimitRequest.isWorkingDay());
    }
}
