package com.ho.flow.controller.process;

import com.ho.common.tools.annotation.LogAnnotation;
import com.ho.common.tools.constant.ContextConstant;
import com.ho.common.tools.exception.DataResult;
import com.ho.common.tools.util.PageResult;
import com.ho.flow.entity.ProcessTemplate;
import com.ho.flow.service.process.service.ProcessTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * @Author yule
 * @Date 2023/8/17 10:51
 * @desc ${}
 */
@RequestMapping(ContextConstant.FLOW_CONTEXT + "processTemplate")
@RestController
@Api(tags = "流程模板")
public class ProcessTemplateController {

    @Autowired
    ProcessTemplateService processTemplateService;

    @PostMapping
    @ApiOperation(value = "分页查询")
    public DataResult<PageResult<ProcessTemplate>> page(@RequestBody @Valid ProcessTemplate processTemplate) {
        PageResult<ProcessTemplate> page = processTemplateService.selectPage(processTemplate);
        return DataResult.success(page);
    }

    @PostMapping("/list")
    @ApiOperation(value = "下拉查询模版")
    public DataResult<List<ProcessTemplate>> list(@RequestBody(required = false) @Valid ProcessTemplate processTemplate) {
        List<ProcessTemplate> list = processTemplateService.selectList(processTemplate);
        return DataResult.success(list);
    }


    @PostMapping("add")
    @ApiOperation(value = "新增流程模板")
    @LogAnnotation(title = "流程模板", action = "新增流程模板")
    public DataResult add(@RequestBody @Valid ProcessTemplate processTemplate) {
        processTemplateService.add(processTemplate);
        return DataResult.success();
    }

    @PostMapping("delete")
    @ApiOperation(value = "删除流程模板")
    @LogAnnotation(title = "流程模板", action = "删除流程模板")
    public DataResult delete(@RequestBody @Valid ProcessTemplate processTemplate) {
        processTemplateService.delete(processTemplate);
        return DataResult.success();
    }

    @PostMapping("edit")
    @ApiOperation(value = "修改流程模板")
    @LogAnnotation(title = "流程模板", action = "修改流程模板")
    public DataResult edit(@RequestBody @Valid ProcessTemplate processTemplate) {
        processTemplateService.edit(processTemplate);
        return DataResult.success();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "查询详情")
    public DataResult<ProcessTemplate> getInfo(@PathVariable Integer id) {
        return  DataResult.success(processTemplateService.getInfo(id));
    }

    @GetMapping("getProcessXML/{id}")
    @ApiOperation(value = "查询流程图")
    public void getBpmn(HttpServletResponse response,@PathVariable Integer id) throws IOException {
        processTemplateService.getBpmn(response,id);
    }

}
