package com.ruoyi.flowable.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.SysRoleEnum;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.flowable.service.IFlowBusinessKeyService;
import com.ruoyi.system.domain.FlowTaskEntity;
import com.ruoyi.system.service.ISysDeptService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.stream.Collectors;

/**
 * <p>
 * 业务工作流程自定义
 * </p>
 *
 * @author JiangYuQi
 * @date 2021-04-03
 */
@Slf4j
@Api(tags = "流程定义")
@RestController
@RequestMapping("/flowable/businessKey")
public class FlowBusinessKeyController extends BaseController {

    @Autowired
    private ISysDeptService sysDeptService;

    @Autowired
    private IFlowBusinessKeyService flowBusinessKeyService;

    /**
     * 根据条件查询所有流任务
     * @param flowTaskEntity
     * @return
     */
    @GetMapping(value = "/allList")
    public TableDataInfo allList(FlowTaskEntity flowTaskEntity) {
        startPage();
        flowTaskEntity.setNowRole(Convert.toStr(getUserFirstRole()));
        if(SysRoleEnum.ZGS.getCode().equals(flowTaskEntity.getNowRole())){
            flowTaskEntity.setNowDept(Convert.toStr(sysDeptService.getZGSDeptId(getDeptId())));
        }else{
            flowTaskEntity.setNowDept(Convert.toStr(getDeptId()));
        }
        flowTaskEntity.setNowUser(Convert.toStr(getUserId()));
        return getDataTable(flowBusinessKeyService.selectAllFlowTaskByParams(flowTaskEntity));
    }

    /**
     * 根据条件统计所有流任务
     * @param flowTaskEntity
     * @return
     */
    @GetMapping(value = "/queryCount")
    public AjaxResult queryCount(FlowTaskEntity flowTaskEntity) {
        flowTaskEntity.setNowRole(Convert.toStr(getUserFirstRole()));
        if(SysRoleEnum.ZGS.getCode().equals(flowTaskEntity.getNowRole())){
            flowTaskEntity.setNowDept(Convert.toStr(sysDeptService.getZGSDeptId(getDeptId())));
        }else{
            flowTaskEntity.setNowDept(Convert.toStr(getDeptId()));
        }
        flowTaskEntity.setNowUser(Convert.toStr(getUserId()));
        return success(flowBusinessKeyService.quueryCount(flowTaskEntity));
    }

    /**
     * 根据流程Id查询操作日志
     * @param procInsId
     * @return
     */
    @GetMapping(value = "/findCommentByProcInsId")
    public AjaxResult findCommentByProcInsId(String procInsId) {
        return success(flowBusinessKeyService.selectCommentByProcInsId(procInsId));
    }

    /**
     * 根据流程Id查询表单数据
     * @param procInsId
     * @return
     */
    @GetMapping(value = "/findFormDatasByProcInsId")
    public AjaxResult findFormDatasByProcInsId(String procInsId) {
        return success(flowBusinessKeyService.selectFormDatasByProcInsId(procInsId));
    }

    /**
     * 根据条件查询我的代办任务
     * @param flowTaskEntity
     * @return
     */
    @GetMapping(value = "/myAwaitFlowTaskList")
    public TableDataInfo myAwaitFlowTaskList(FlowTaskEntity flowTaskEntity) {
        startPage();
        flowTaskEntity.setNowRole(Convert.toStr(getUserFirstRole()));
        if(SysRoleEnum.ZGS.getCode().equals(flowTaskEntity.getNowRole())){
            flowTaskEntity.setNowDept(Convert.toStr(sysDeptService.getZGSDeptId(getDeptId())));
        }else{
            flowTaskEntity.setNowDept(Convert.toStr(getDeptId()));
        }
        flowTaskEntity.setNowUser(Convert.toStr(SecurityUtils.getUserId()));
        flowTaskEntity.setRoleIds(SecurityUtils.getLoginUser().getUser().getRoles().stream().map(role -> role.getRoleId()).collect(Collectors.toList()));
        return getDataTable(flowBusinessKeyService.selectMyAwaitFlowTask(flowTaskEntity));
    }

    /**
     * 根据条件查询我的已办任务
     * @param flowTaskEntity
     * @return
     */
    @GetMapping(value = "/myFinishedFlowTaskList")
    public TableDataInfo myFinishedFlowTaskList(FlowTaskEntity flowTaskEntity) {
        startPage();
        flowTaskEntity.setNowUser(Convert.toStr(SecurityUtils.getUserId()));
        return getDataTable(flowBusinessKeyService.selectMyFinishedFlowTask(flowTaskEntity));
    }
}
