package com.yanzhu.flowable.service.impl;

import com.yanzhu.common.core.text.Convert;
import com.yanzhu.flowable.domain.my.FlowTaskEntity;
import com.yanzhu.flowable.mapper.FlowBusinessKeyMapper;
import com.yanzhu.flowable.service.IFlowBusinessKeyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 业务工作流程自定义
 *
 * @author Tony
 * @date 2021-04-03
 */
@Service
@Slf4j
public class FlowBusinessKeyServiceImpl implements IFlowBusinessKeyService {

    @Resource
    private FlowBusinessKeyMapper flowBusinessKeyMapper;

    /**
     * 根据条件查询所有流任务
     * @param flowTaskEntity
     * @return
     */
    @Override
    public List<FlowTaskEntity> selectAllFlowTaskByParams(FlowTaskEntity flowTaskEntity) {
        return flowBusinessKeyMapper.selectAllFlowTaskByParams(flowTaskEntity);
    }

    /**
     * 根据条件统计所有流任务
     * @param flowTaskEntity
     * @return
     */
    @Override
    public Map<String,Object> quueryCount(FlowTaskEntity flowTaskEntity) {
        flowTaskEntity.setActiveTags("await");
        int awaitSize = flowBusinessKeyMapper.selectAllFlowTaskByParams(flowTaskEntity).size();
        flowTaskEntity.setActiveTags("finished");
        int finishedSize = flowBusinessKeyMapper.selectAllFlowTaskByParams(flowTaskEntity).size();
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("await",awaitSize);
        dataMap.put("finished",finishedSize);
        return dataMap;
    }

    /**
     * 根据条件流程Id查询操作日志
     * @param procInsId
     * @return
     */
    @Override
    public List<Map<String, Object>> selectCommentByProcInsId(String procInsId) {
        return flowBusinessKeyMapper.selectCommentByProcInsId(procInsId);
    }

    /**
     * 根据流程Id查询表单数据
     * @param procInsId
     * @return
     */
    @Override
    public Map<String, Object> selectFormDatasByProcInsId(String procInsId) {
        Map<String, Object> dataMap = new HashMap<>();
        List<Map<String, Object>> list = flowBusinessKeyMapper.selectFormDatasByProcInsId(procInsId);
        Iterator<Map<String, Object>> it = list.iterator();
        while (it.hasNext()) {
            Map<String, Object> map = it.next();
            dataMap.put(Convert.toStr(map.get("name")), map.get("text"));
        }
        return dataMap;
    }

    /**
     * 查询我的代办任务
     * @param flowTaskEntity
     * @return
     */
    @Override
    public List<Map<String, Object>> selectMyAwaitFlowTask(FlowTaskEntity flowTaskEntity){
        return flowBusinessKeyMapper.selectMyAwaitFlowTask(flowTaskEntity);
    }

    /**
     * 查询我的已办任务
     * @param flowTaskEntity
     * @return
     */
    @Override
    public List<Map<String, Object>> selectMyFinishedFlowTask(FlowTaskEntity flowTaskEntity) {
        return flowBusinessKeyMapper.selectMyFinishedFlowTask(flowTaskEntity);
    }

}
