package com.jeeplus.centerservice.service.ccpm.flow;

import cn.hutool.core.util.StrUtil;
import com.jeeplus.centerservice.utils.RestTemplateService;
import com.jeeplus.flowable.model.Flow;
import com.jeeplus.sys.service.dto.UserDTO;
import com.jeeplus.utils.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class FlowRequest {

    @Resource
    private RestTemplateService restTemplateService;

    /**
     * 根据procInsId查询流程流向图
     * @param procInsId
     * @return
     */
    public Object getFlowChart(String procInsId) {
        String token = "";
        Map<String, Object> param = new HashMap<>();
        param.put("procInsId", procInsId);
        Object flowChart = restTemplateService.getCCPM("/a/act/task/getFlowChart", token, param);
        return flowChart;
    }

    /**
     * 根据procInsId查询流程历史
     * @param procInsId
     * @return
     */
    public Object getHistoicFlowList(String procInsId) {
        String token = "";
        Map<String, Object> param = new HashMap<>();
        param.put("procInsId", procInsId);
        Object histoicFlow = restTemplateService.getCCPM("/a/act/task/getHistoicFlowList", token, param);
        return histoicFlow;
    }

    /**
     * 根据待办id及类型  删除待办信息
     * @param notifyId
     * @param type
     * @return
     */
    public Object deleteNotify(String notifyId, String type) {
        String token = "";
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("notifyId", notifyId);
        paramMap.put("type", type);
        Object result = restTemplateService.getCCPM("/a/workprojectnotify/workProjectNotify/deleteNotify", token, paramMap);
        return result;
    }

    /**
     * 查询待办信息
     * @param flow
     * @return
     */
    public Object getNotifyList(Flow flow, String belongProject, UserDTO userDTO) {
        String token = "";
        Map<String, Object> paramMap = new HashMap<>();
        // 设置查询条件
        if (flow.getBeginDate () != null) { // 创建时间
            SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            paramMap.put("startDate", formatter.format(flow.getBeginDate ()));
        }
        if (flow.getEndDate () != null) { // 创建时间
            SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            paramMap.put("endDate", formatter.format(flow.getEndDate ()));
        }
        if (StrUtil.isNotBlank (flow.getTitle ())) { // 标题
            paramMap.put("title", flow.getTitle ());
        }
        if (StringUtils.isNotEmpty(flow.getAssigneeName())) { // 流程发起人
            paramMap.put("userName", flow.getAssigneeName());
        }
        paramMap.put("belongProject", belongProject);
        paramMap.put("size", -1);
        paramMap.put("count", -1);
        Object response = restTemplateService.getCCPMMultithreading("/a/workprojectnotify/workProjectNotify/getList", token, paramMap,userDTO);
        return response;
    }

    /**
     * 查询已办信息
     * @param flow
     * @return
     */
    public Object getHisTaskList(Flow flow, String belongProject) {
        String token = "";
        Map<String, Object> paramMap = new HashMap<>();
        // 设置查询条件
        if (flow.getBeginDate () != null) { // 创建时间
            SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            paramMap.put("startOverDate", formatter.format(flow.getBeginDate ()));
        }
        if (flow.getEndDate () != null) { // 创建时间
            SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            paramMap.put("endOverDate", formatter.format(flow.getEndDate ()));
        }
        if (StrUtil.isNotBlank (flow.getTitle ())) { // 标题
            paramMap.put("title", flow.getTitle ());
        }
        if (StringUtils.isNotEmpty(flow.getAssigneeName())) { // 流程发起人
            paramMap.put("userName", flow.getAssigneeName());
        }
        paramMap.put("belongProject", belongProject);
        Object response = restTemplateService.getCCPM("/a/workprojectnotify/workProjectNotify/backlogListReadAll", token, paramMap);
        return response;
    }

}
