package com.iformall.controller;

import com.iformall.annotation.SystemControllerLog;
import com.iformall.common.ResultData;
import com.iformall.domain.po.WxFlowRecord;
import com.iformall.enums.EnumFlowRecordStatus;
import com.iformall.service.WxFlowService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@RestController
@Api(description = "工作流相关接口")
@RequestMapping(value = "workflow")
public class WxFlowAbleController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WxFlowService wxFlowService;
//    @Autowired
//    RuntimeService runtimeService;

    @ApiOperation(value = "启动流程",notes = "{\"businessId\":\"221178539888607232\",\"businessType\":1,\"remark\":\"意见意见。\",\"taskAssignee\":[{\"taskKey\":\"firstTaskUser\",\"assignee\":\"243650055783841792\"},{\"taskKey\":\"secondTaskUser\",\"assignee\":\"\"}],\"variables\":[{\"key\":\"contractType\",\"value\":\"1\"},{\"key\":\"contractNumber\",\"value\":\"1111\"}]}")
    @PostMapping("/start")
    @SystemControllerLog(description = "工作流-启动流程")
    public ResultData start(@RequestBody Map<String, Object> params) {
        logger.debug("[" + getIpAddr() + "] FlowAbleController::start");
        return wxFlowService.start(params,super.getUser().getId(),super.getUser().getName(),super.getTenantId());
    }

    /**
     * 删除流程
     */
//    @GetMapping(value = "/del")
//    public ResultData del(String id,String x) {
//        if("xll1dxx1314".equals(x)) {
//            runtimeService.deleteProcessInstance(id, "驳回");
//            return new ResultData();
//        }else{
//            return new ResultData("fail");
//        }
//    }

    /**
     * 用户代办列表
     */
    @ApiOperation("用户代办列表")
    @GetMapping(value = "/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "flowType", value = "流程类型,1合同流程 2账单流程", dataType = "string", paramType = "query", required = true)
    })
    @SystemControllerLog(description = "工作流-用户代办列表")
    public ResultData list(String flowType,Integer pageNum, Integer pageSize) {
        logger.debug("[" + getIpAddr() + "] FlowAbleController::list");
        return wxFlowService.list(flowType,pageNum,pageSize,super.getUser().getId());
    }

    /**
     * 我的申请列表
     */
    @ApiOperation("我的申请列表")
    @GetMapping(value = "/myApplyList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true),
    })
    @SystemControllerLog(description = "工作流-我的申请列表")
    public ResultData myApplyList(@ModelAttribute WxFlowRecord record, Integer pageNum, Integer pageSize) {
        logger.debug("[" + getIpAddr() + "] FlowAbleController::myApplyList");
        record.setUserId(super.getUser().getId());
        record.setStatus(EnumFlowRecordStatus.NEW.getCode());
        return new ResultData(wxFlowService.listAsPage(record,pageNum,pageSize));
    }

    /**
     * 我的审批列表
     */
    @ApiOperation("我的审批列表")
    @GetMapping(value = "/myHandleList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true),
    })
    @SystemControllerLog(description = "工作流-我的审批列表")
    public ResultData myHandleList(@ModelAttribute WxFlowRecord record, Integer pageNum, Integer pageSize) {
        logger.debug("[" + getIpAddr() + "] FlowAbleController::myHandelList");
        record.setUserId(super.getUser().getId());
        record.setStatus(EnumFlowRecordStatus.NEW.getCode());
        return new ResultData(wxFlowService.myHandelList(record,pageNum,pageSize));
    }


    /**
     * 审批历史
     */
    @ApiOperation("审批历史")
    @GetMapping(value = "/applyHistory")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "businessId", value = "业务id", dataType = "long", paramType = "query", required = true),
    })
    @SystemControllerLog(description = "工作流-审批历史")
    public ResultData applyHistory(Long businessId) {
        logger.debug("[" + getIpAddr() + "] FlowAbleController::applyHistory");
        return wxFlowService.applyHistory(businessId,super.getTenantId());
    }

    /**
     * 审批历史和代办
     */
    @ApiOperation("审批历史和代办")
    @GetMapping(value = "/applyHistoryAndAssignee")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "businessId", value = "业务id", dataType = "long", paramType = "query", required = true),
    })
    @SystemControllerLog(description = "工作流-审批历史和代办")
    public ResultData applyHistoryAndAssignee(Long businessId) {
        logger.debug("[" + getIpAddr() + "] FlowAbleController::applyHistoryAndAssignee");
        return wxFlowService.getTaskStatusList(businessId,super.getTenantId());
    }

    /**
     * 审批
     */
    @ApiOperation(value = "审批",notes = "{\"taskId\":\"\",\"processInstanceId\":\"\",\"remark\":\"\"}")
    @PostMapping(value = "apply")
    public ResultData apply(@RequestBody Map<String, String> params) {
        return wxFlowService.apply(params,super.getUser().getId(),super.getUser().getName(),super.getTenantId());
    }

    /**
     * 驳回
     */
    @ApiOperation(value = "驳回",notes = "{\"taskId\":\"\",\"processInstanceId\":\"\",\"remark\":\"\"}")
    @PostMapping(value = "reject")
    @SystemControllerLog(description = "工作流-驳回")
    public ResultData reject(@RequestBody Map<String, Object> params) {
        return wxFlowService.reject(params,super.getUser().getId(),super.getUser().getName(),super.getUser().getPhone(),super.getTenantId());
    }


    /**
     * 撤回
     */
    @ApiOperation(value = "撤回",notes = "{\"taskId\":\"\",\"processInstanceId\":\"\",\"remark\":\"\"}")
    @PostMapping(value = "setBack")
    @SystemControllerLog(description = "工作流-撤回")
    public ResultData setBack(@RequestBody Map<String, Object> params) {
        return wxFlowService.setBack(params,super.getUser().getId(),super.getUser().getName());
    }

    /**
     * 生成流程图
     *
     */
    @GetMapping(value = "processDiagram")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "businessId", value = "业务id", dataType = "String", paramType = "query", required = true)
    })
    @SystemControllerLog(description = "工作流-生成流程图")
    public void genProcessDiagram(HttpServletResponse httpServletResponse, String businessId) throws Exception {
        wxFlowService.genProcessDiagram(httpServletResponse,businessId);
    }





}