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

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.vos.oa.UpdateTaskAssigneeVO;
import ink.wgink.module.activiti.service.oa.IOaFormReportService;
import ink.wgink.module.activiti.util.oa.OaFormReportUtil;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.result.SuccessResult;
import ink.wgink.pojo.result.SuccessResultList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @ClassName: OAFormReportController
 * @Description: OA表单上报
 * @Author: wanggeng
 * @Date: 2022/3/22 21:55
 * @Version: 1.0
 */
@Api(tags = ISystemConstant.API_TAGS_SYSTEM_PREFIX + "oa表单上报")
@RestController
@RequestMapping(ISystemConstant.API_PREFIX + "/oa-form-report")
public class OaFormReportController extends DefaultBaseController {

    @Autowired
    private IOaFormReportService oaFormReportService;

    @ApiOperation(value = "保存表单（发起流程）", notes = "保存表单（发起流程）接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "processDefinitionId", value = "流程定义ID", paramType = "path"),
            @ApiImplicitParam(name = "formCode", value = "表单编码", paramType = "path"),
            @ApiImplicitParam(name = "formVersion", value = "表单版本", paramType = "path")
    })
    @PostMapping("save/definition-id/{processDefinitionId}/code/{formCode}/version/{formVersion}")
    public synchronized SuccessResult save(@PathVariable("processDefinitionId") String processDefinitionId,
                              @PathVariable("formCode") String formCode,
                              @PathVariable("formVersion") Integer formVersion,
                              @RequestBody Map<String, Object> params) {
        OaFormReportUtil.checkReportBody(params);
        oaFormReportService.save(processDefinitionId, formCode, formVersion, params);
        return new SuccessResult();
    }

    @ApiOperation(value = "更新表单", notes = "更新表单接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskId", value = "任务ID", paramType = "path"),
            @ApiImplicitParam(name = "formCode", value = "表单编码", paramType = "path"),
            @ApiImplicitParam(name = "formVersion", value = "表单版本", paramType = "path"),
            @ApiImplicitParam(name = "uid", value = "表单主键", paramType = "path")
    })
    @PutMapping("update/task-id/{taskId}/code/{formCode}/version/{formVersion}/is-need-claim/{isNeedClaim}/uid/{uid}")
    public synchronized SuccessResult update(@PathVariable("taskId") String taskId,
                                @PathVariable("formCode") String formCode,
                                @PathVariable("formVersion") Integer formVersion,
                                @PathVariable("isNeedClaim") Integer isNeedClaim,
                                @PathVariable("uid") String uid,
                                @RequestBody Map<String, Object> params) {
        if (isNeedClaim != 0 && isNeedClaim != 1) {
            throw new ParamsException("认领参数错误");
        }
        OaFormReportUtil.checkReportBody(params);
        oaFormReportService.update(taskId, formCode, formVersion, isNeedClaim, uid, params);
        return new SuccessResult();
    }

    @ApiOperation(value = "更新回退", notes = "更新回退接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "processInstanceId", value = "流程实例ID", paramType = "path"),
            @ApiImplicitParam(name = "taskId", value = "当前任务ID", paramType = "path"),
            @ApiImplicitParam(name = "nodeId", value = "跳转节点ID", paramType = "path")
    })
    @PutMapping("update-go-back/process-instance-id/{processInstanceId}/task-id/{taskId}/node-id/{nodeId}")
    public synchronized SuccessResult updateGoBack(@PathVariable("processInstanceId") String processInstanceId,
                                                   @PathVariable("taskId") String taskId,
                                                   @PathVariable("nodeId") String nodeId,
                                                   @RequestBody Map<String, String> params) {
        String reason = params.get(IOaFormReportService.KEY_REASON);
        if (StringUtils.isBlank(reason)) {
            throw new ParamsException("回退原因不能为空");
        }
        oaFormReportService.updateGoBack(processInstanceId, taskId, nodeId, reason);
        return new SuccessResult();
    }

    @ApiOperation(value = "更新强制结束", notes = "更新强制结束接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "processInstanceId", value = "流程实例ID", paramType = "path"),
            @ApiImplicitParam(name = "taskId", value = "当前任务ID", paramType = "path")
    })
    @PutMapping("update-forced-end/process-instance-id/{processInstanceId}/task-id/{taskId}")
    public synchronized SuccessResult updateForcedEnd(@PathVariable("processInstanceId") String processInstanceId,
                                                      @PathVariable("taskId") String taskId,
                                                      @RequestBody Map<String, String> params) {
        String reason = params.get(IOaFormReportService.KEY_REASON);
        if (StringUtils.isBlank(reason)) {
            throw new ParamsException("结束原因不能为空");
        }
        oaFormReportService.updateForcedEnd(processInstanceId, taskId, reason);
        return new SuccessResult();
    }

    @ApiOperation(value = "更新代理人", notes = "更新代理人接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "processInstanceId", value = "流程实例ID", paramType = "path"),
            @ApiImplicitParam(name = "taskId", value = "当前任务ID", paramType = "path")
    })
    @PutMapping("update-assignee/process-instance-id/{processInstanceId}/task-id/{taskId}")
    @CheckRequestBodyAnnotation
    public synchronized SuccessResult updateAssignee(@PathVariable("processInstanceId") String processInstanceId,
                                                     @PathVariable("taskId") String taskId,
                                                     @RequestBody UpdateTaskAssigneeVO updateTaskAssigneeVO) {
        oaFormReportService.updateTaskAssignee(processInstanceId, taskId, updateTaskAssigneeVO);
        return new SuccessResult();
    }

    @GetMapping("get/code/{formCode}/version/{formVersion}/uid/{uid}")
    public Map<String, Object> get(@PathVariable("formCode") String formCode, @PathVariable("formVersion") Integer formVersion, @PathVariable("uid") String uid) {
        return oaFormReportService.get(formCode, formVersion, uid);
    }

    @GetMapping("list/code/{formCode}/version/{formVersion}")
    public List<Map<String, Object>> list(@PathVariable("formCode") String formCode, @PathVariable("formVersion") Integer formVersion) {
        Map<String, Object> params = requestParams();
        return oaFormReportService.list(formCode, formVersion, params);
    }

    @GetMapping("listpage/code/{formCode}/version/{formVersion}")
    public SuccessResultList<List<Map<String, Object>>> listPage(@PathVariable("formCode") String formCode, @PathVariable("formVersion") Integer formVersion, ListPage page) {
        Map<String, Object> params = requestParams();
        page.setParams(params);
        return oaFormReportService.listPage(formCode, formVersion, page);
    }
}
