package ink.wgink.module.activiti.controller.api;

import ink.wgink.annotation.CheckRequestBodyAnnotation;
import ink.wgink.common.base.DefaultBaseController;
import ink.wgink.exceptions.ParamsException;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.module.activiti.pojo.dtos.ModelDTO;
import ink.wgink.module.activiti.pojo.vos.ActivitiVO;
import ink.wgink.module.activiti.service.activiti.IActivitiService;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.result.ErrorResult;
import ink.wgink.pojo.result.SuccessResult;
import ink.wgink.pojo.result.SuccessResultData;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.util.RegexUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: ActivitiController
 * @Description: activiti
 * @Author: WangGeng
 * @Date: 2021/7/11 16:36
 * @Version: 1.0
 **/
@Api(tags = ISystemConstant.API_TAGS_SYSTEM_PREFIX + "流程引擎接口")
@RestController
@RequestMapping(ISystemConstant.API_PREFIX + "/activiti")
public class ActivitiController extends DefaultBaseController {

    @Autowired
    private IActivitiService activitiService;

    @ApiOperation(value = "创建模型", notes = "创建模型接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("create")
    @CheckRequestBodyAnnotation
    public SuccessResultData create(@RequestBody ActivitiVO activitiVO) throws UnsupportedEncodingException {
        if (!RegexUtil.isLetterOrNumber(activitiVO.getModelKey())) {
            throw new ParamsException("模型Key只能是字符和数字");
        }
        return new SuccessResultData<>(activitiService.create(activitiVO));
    }

    @ApiOperation(value = "发布模型", notes = "发布模型接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "modelId", value = "模型ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PutMapping("deploy/{modelId}")
    public SuccessResult deploy(@PathVariable("modelId") String modelId) throws IOException {
        activitiService.saveDeploy(modelId);
        return new SuccessResult();
    }

    @ApiOperation(value = "撤销发布模型", notes = "撤销发布模型接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "modelId", value = "模型ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PutMapping("revoke-deploy/{modelId}")
    public SuccessResult revokeDeploy(@PathVariable("modelId") String modelId) {
        activitiService.revokeDeploy(modelId);
        return new SuccessResult();
    }


    @ApiOperation(value = "模型列表", notes = "模型列表接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list")
    public List<ModelDTO> list(@RequestParam(required = false, name = "modelCategory") String modelCategory) {
        return activitiService.listByCategory(modelCategory);
    }

    @ApiOperation(value = "模型分页列表", notes = "模型分页列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "modelCategory", value = "模型类别", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", dataType = "Integer", defaultValue = "1"),
            @ApiImplicitParam(name = "rows", value = "显示数量", paramType = "query", dataType = "Integer", defaultValue = "20"),
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listpage")
    public SuccessResultList<List<ModelDTO>> listPage(@RequestParam(required = false, name = "modelCategory") String modelCategory, ListPage page) {
        return activitiService.listPageByCategory(modelCategory, page.getPage(), page.getRows());
    }

}
