package com.yanzhu.flowable.controller;

import com.yanzhu.common.core.web.controller.BaseController;
import com.yanzhu.common.core.web.domain.AjaxResult;
import com.yanzhu.common.core.web.page.TableDataInfo;
import com.yanzhu.common.security.utils.SecurityUtils;
import com.yanzhu.flowable.domain.my.FlowTaskEntity;
import com.yanzhu.flowable.service.IFlowBusinessKeyService;
import com.yanzhu.system.api.domain.SysUser;
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("/businessKey")
public class FlowBusinessKeyController extends BaseController {

    @Autowired
    private IFlowBusinessKeyService flowBusinessKeyService;

    /**
     * 根据条件查询所有流任务
     * @param flowTaskEntity
     * @return
     */
    @GetMapping(value = "/allList")
    public TableDataInfo allList(FlowTaskEntity flowTaskEntity) {
        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();
        //超管查询所有数据
        if(!SecurityUtils.isAdmin(sysUser.getUserId())){
            flowTaskEntity.setDeptAncestors(sysUser.getDept().getAncestors());
        }
        startPage();
        return getDataTable(flowBusinessKeyService.selectAllFlowTaskByParams(flowTaskEntity));
    }

    /**
     * 根据条件统计所有流任务
     * @param flowTaskEntity
     * @return
     */
    @GetMapping(value = "/queryCount")
    public AjaxResult queryCount(FlowTaskEntity flowTaskEntity) {
        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();
        //超管查询所有数据
        if(!SecurityUtils.isAdmin(sysUser.getUserId())){
            flowTaskEntity.setDeptAncestors(sysUser.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) {
        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();
        //超管查询所有数据
        if(!SecurityUtils.isAdmin(sysUser.getUserId())){
            flowTaskEntity.setAssigneeId(sysUser.getUserId());
            flowTaskEntity.setDeptAncestors(sysUser.getDept().getAncestors()+","+sysUser.getDeptId());
            flowTaskEntity.setRoleIds(sysUser.getRoles().stream().map(role -> role.getRoleId()).collect(Collectors.toList()));
        }
        startPage();
        return getDataTable(flowBusinessKeyService.selectMyAwaitFlowTask(flowTaskEntity));
    }

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

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


}
