package com.yanzhu.flowable.controller;

import com.yanzhu.common.core.controller.BaseController;
import com.yanzhu.common.core.domain.AjaxResult;
import com.yanzhu.common.core.domain.entity.SysUser;
import com.yanzhu.common.core.page.TableDataInfo;
import com.yanzhu.flowable.service.IFlowBusinessKeyService;
import com.yanzhu.system.domain.flowable.FlowTaskEntity;
import com.yanzhu.system.service.ISysDeptService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
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.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 业务工作流程
 * </p>
 *
 * @author JiangYuQi
 * @date 2024-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();
        //超管查询所有数据
        if(!SysUser.isAdmin(super.getUserId())){
            flowTaskEntity.setDeptAncestors(super.getLoginUser().getUser().getDept().getAncestors());
        }
        return getDataTable(flowBusinessKeyService.selectAllFlowTaskByParams(flowTaskEntity));
    }

    /**
     * 根据条件统计所有流任务
     * @param flowTaskEntity
     * @return
     */
    @GetMapping(value = "/queryCount")
    public AjaxResult queryCount(FlowTaskEntity flowTaskEntity) {
        //超管查询所有数据
        if(!SysUser.isAdmin(super.getUserId())){
            flowTaskEntity.setDeptAncestors(super.getLoginUser().getUser().getDept().getAncestors());
        }
        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();
        //超管查询所有数据
        if(!SysUser.isAdmin(super.getUserId())){
            SysUser sysUser = super.getLoginUser().getUser();
            flowTaskEntity.setDeptAncestors(sysUser.getDept().getAncestors());
            flowTaskEntity.setRoleIds(sysUser.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.setAssigneeId(super.getUserId());
        return getDataTable(flowBusinessKeyService.selectMyFinishedFlowTask(flowTaskEntity));
    }

    /**
     * 查询我的代办任务数量
     * @return AjaxResult 统计结果
     */
    @GetMapping(value = "/findMyTask")
    public AjaxResult findMyTask() {
        FlowTaskEntity flowTaskEntity = new FlowTaskEntity();
        //超管查询所有数据
        if(!SysUser.isAdmin(super.getUserId())){
            SysUser sysUser = super.getLoginUser().getUser();
            flowTaskEntity.setDeptAncestors(sysUser.getDept().getAncestors());
            flowTaskEntity.setRoleIds(sysUser.getRoles().stream().map(role -> role.getRoleId()).collect(Collectors.toList()));
        }
        flowTaskEntity.setActiveName("await");
        List<Map<String, Object>> list = flowBusinessKeyService.selectMyFinishedFlowTask(flowTaskEntity);
        Map<String,Object> data = new HashMap<>();
        if(CollectionUtils.isNotEmpty(list)){
            data.put("todo",list.size());
        }else{
            data.put("todo",0);
        }
        return success(data);
    }


}
