package com.ho.flow.controller.process;


import com.ho.common.tools.constant.ContextConstant;
import com.ho.common.tools.constant.RedisKeyConstant;
import com.ho.common.tools.entity.SimpleUser;
import com.ho.common.tools.exception.DataResult;
import com.ho.common.tools.service.RedisService;
import com.ho.common.tools.util.PageResult;
import com.ho.flow.entity.process.ProcessDefinitionDTO;
import com.ho.flow.service.process.service.IProcessDefinitionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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.IOException;

@RequestMapping(ContextConstant.FLOW_CONTEXT + "processDefinition")
@RestController
@Api(tags = "流程定义(绘制流程)")
public class ProcessDefinitionController {

    @Autowired
    private IProcessDefinitionService processDefinitionService;


    @Autowired
    private RedisService redisService;

    /**
     * 获取流程定义集合
     *
     * @param processDefinition
     * @return
     */
    @PostMapping(value = "/list")
    @ApiOperation(value = "分页查询流程定义集合")
    public DataResult<PageResult<ProcessDefinitionDTO>> list(@RequestBody ProcessDefinitionDTO processDefinition,HttpServletRequest request) {
        String token = request.getHeader(RedisKeyConstant.User.ACCESS_TOKEN);
        SimpleUser simpleUser = redisService.getSimpleUserByToken(token);
        //设置租户id
        processDefinition.setTenantId(String.valueOf(simpleUser.getGroupId()));
        return DataResult.success(processDefinitionService.selectProcessDefinitionList(processDefinition));
    }


/*    @ApiOperation(value = "")
    @GetMapping("/getDefinitions/{instanceId}")
    public DataResult getDefinitionsByInstanceId(@PathVariable("instanceId") String instanceId) {
        return DataResult.success(processDefinitionService.getDefinitionsByInstanceId(instanceId));
    }*/

    /**
     * 删除流程定义
     *
     * @param deploymentId
     * @return
     */
    @DeleteMapping(value = "/remove/{deploymentId}")
    @ApiOperation(value = "删除流程定义")
    public DataResult delDefinition(@PathVariable("deploymentId") String deploymentId) {
        processDefinitionService.deleteProcessDefinitionById(deploymentId);
        return DataResult.success();
    }

    /**
     * 上传并部署流程定义
     *
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping(value = "/uploadStreamAndDeployment")
    @ApiOperation(value = "上传并部署流程定义")
    public DataResult uploadStreamAndDeployment(@RequestBody MultipartFile file) throws IOException {
        processDefinitionService.uploadStreamAndDeployment(file);
        return DataResult.success();

    }

    /**
     * 启动挂起流程流程定义
     *
     * @param processDefinition
     * @return
     */
    @PostMapping("/suspendOrActiveApply")
    @ApiOperation(value = "启动挂起流程流程定义")
    public DataResult suspendOrActiveApply(@RequestBody ProcessDefinitionDTO processDefinition) {
        processDefinitionService.suspendOrActiveApply(processDefinition.getId(), processDefinition.getSuspendState());
        return DataResult.success();
    }

    /*    *//**
     * 上传流程流程定义
     *
     * @param multipartFile
     * @return
     * @throws IOException
     *//*
    @PostMapping(value = "/upload")
    @ApiOperation(value = "上传流程流程定义")
    public DataResult upload(@RequestParam("processFile") MultipartFile multipartFile) throws IOException {

        if (multipartFile.isEmpty()) {
            throw new BusinessException(BaseResponseCode.FILE_IS_EMPTY);
        }
        String fileName = processDefinitionService.upload(multipartFile);
        return DataResult.success();
    }*/


    /**
     * 通过stringBPMN添加流程定义
     *
     * @param stringBPMN
     * @return
     */
    @PostMapping(value = "/addDeploymentByString")
    @ApiOperation(value = "新增流程定义")
    public DataResult addDeploymentByString(@RequestBody String stringBPMN, HttpServletRequest request) {
        String token = request.getHeader(RedisKeyConstant.User.ACCESS_TOKEN);
        SimpleUser simpleUser = redisService.getSimpleUserByToken(token);
        processDefinitionService.addDeploymentByString(stringBPMN,String.valueOf(simpleUser.getGroupId()));
        return DataResult.success();

    }


    /**
     * 获取流程定义XML
     *
     * @param response
     * @param deploymentId
     * @param resourceName
     */
    @GetMapping(value = "/getDefinitionXML")
    @ApiOperation(value = "获取流程定义XML")
    public void getProcessDefineXML(HttpServletResponse response,
                                    @RequestParam("deploymentId") String deploymentId,
                                    @RequestParam("resourceName") String resourceName) throws IOException {

        processDefinitionService.getProcessDefineXML(response, deploymentId, resourceName);
    }


    @PostMapping(value = "/initDeploymentByString")
    @ApiOperation(value = "初始化固定流程")
    public DataResult initDeploymentByString() {
        processDefinitionService.initDeploymentByString();
        return DataResult.success();
    }




}
