package com.ruoyi.flowable.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.service.ISysUserService;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.ProcessEngines;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysFormMapper;
import com.ruoyi.system.domain.SysForm;
import com.ruoyi.flowable.service.ISysFormService;

/**
 * 流程表单Service业务层处理
 * 
 * @author Tony
 * @date 2021-04-03
 */
@Service
public class SysFormServiceImpl implements ISysFormService 
{
    @Autowired
    private SysFormMapper sysFormMapper;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询流程表单
     * 
     * @param formId 流程表单ID
     * @return 流程表单
     */
    @Override
    public SysForm selectSysFormById(Long formId)
    {
        return sysFormMapper.selectSysFormById(formId);
    }

    /**
     * 查询流程表单列表
     * 
     * @param sysForm 流程表单
     * @return 流程表单
     */
    @Override
    public List<SysForm> selectSysFormList(SysForm sysForm)
    {
        return sysFormMapper.selectSysFormList(sysForm);
    }

    /**
     * 新增流程表单
     * 
     * @param sysForm 流程表单
     * @return 结果
     */
    @Override
    public int insertSysForm(SysForm sysForm)
    {
        sysForm.setCreateTime(DateUtils.getNowDate());
        return sysFormMapper.insertSysForm(sysForm);
    }

    /**
     * 修改流程表单
     * 
     * @param sysForm 流程表单
     * @return 结果
     */
    @Override
    public int updateSysForm(SysForm sysForm)
    {
        sysForm.setUpdateTime(DateUtils.getNowDate());
        return sysFormMapper.updateSysForm(sysForm);
    }

    /**
     * 批量删除流程表单
     * 
     * @param formIds 需要删除的流程表单ID
     * @return 结果
     */
    @Override
    public int deleteSysFormByIds(Long[] formIds)
    {
        return sysFormMapper.deleteSysFormByIds(formIds);
    }

    /**
     * 删除流程表单信息
     * 
     * @param formId 流程表单ID
     * @return 结果
     */
    @Override
    public int deleteSysFormById(Long formId)
    {
        return sysFormMapper.deleteSysFormById(formId);
    }

    @Override
    public void updateToDoNumber() {
        //查询所有进行中的任务
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = engine.getRuntimeService();
        List<ProcessInstance> instances = runtimeService
                .createProcessInstanceQuery()
                .active()
                .list();
        //声明map记录用户和对应的代办数量
        Map<String,Integer> map = new HashMap<>();
        for (ProcessInstance instance : instances) {
           /* TaskQuery query = engine.getTaskService().createTaskQuery()
                    .processInstanceId(instance.getId());
            //获取对应的待处理用户记录代办数量+1
            String assignee = query.singleResult().getAssignee();*/
            List<Task> taskList = engine.getTaskService().createTaskQuery()
                    .processInstanceId(instance.getId())
                    .list();
            // 遍历所有任务，获取每个处理人
            for (Task task : taskList) {
                String assignee = task.getAssignee();
                // 处理每个处理人...
                if(assignee!=null && !"".equals(assignee)){
                    //根据用户id获取用户对象
                    SysUser userInfo= sysUserService.selectUserById(Long.parseLong(assignee));
                    if (map.containsKey(userInfo.getUserName())){
                        map.put(userInfo.getUserName(),map.get(userInfo.getUserName())+1);
                    }else {
                        map.put(userInfo.getUserName(),1);
                    }
                }
            }
        }
        //先清空redis中待办数量
        redisCache.deleteObject("toDoNumber");
        //将对应代办数量更新到redis当中
        if(map.size()>0){
            redisCache.setCacheObject("toDoNumber", map);
        }
    }
}
