package com.gyee.frame.service.impl.ticket;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gyee.frame.model.ticket.FlowAction;
import com.gyee.frame.model.ticket.Wfaction;
import com.gyee.frame.mapper.ticket.WfactionMapper;
import com.gyee.frame.model.ticket.Wfprocess;
import com.gyee.frame.model.ticket.Wftaskassign;
import com.gyee.frame.service.ticket.WfactionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gyee.frame.service.ticket.WfprocessService;
import com.gyee.frame.service.ticket.WftaskassignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenmh
 * @since 2020-07-21
 */
@Service
public class WfactionServiceImpl extends ServiceImpl<WfactionMapper, Wfaction> implements WfactionService {

    @Autowired
    WftaskassignService wftaskassignService;

    @Override
    public Map<String, Object> listAction(int status, double startNodeId, double version, String wfName) {
        /**
         * 返回结果 -- map
         */
        Map<String, Object> map = new HashMap<>();
        List<FlowAction> list = new ArrayList<>();

        // 流程未启动
        if (status == 0){
            Wfaction wfaction = baseMapper.initAction(wfName);
            Wftaskassign wftaskassign = wftaskassignService.taskAssign(wfaction.getEndnodeid(), wfaction.getWfrevision(), wfName);
            FlowAction action = new FlowAction();
            action.setDescription(wfaction.getDescription());
            action.setActionType(wfaction.getActiontype());
            action.setNextNodeId(String.valueOf(wfaction.getEndnodeid()));
            action.setRole(wftaskassign.getWfrole());
            action.setChecked(false);
            action.setFilter("Y");
            list.add(action);
        }

        // 流程已启动
        if (status == 2){
            QueryWrapper<Wfaction> wrapper = new QueryWrapper<>();
            wrapper.eq("STARTNODEID", startNodeId);
            wrapper.eq("WFREVISION", version);
            wrapper.eq("WFNAME", wfName);
            wrapper.orderByAsc("ENDNODEID");
            wrapper.in("ACTIONTYPE", "空操作", "自定义类");

            List<Wfaction> wfactions = baseMapper.selectList(wrapper);

            if (wfactions == null)
                return null;

            /**
             * 查询绑定的人员组信息
             */
            List<Wftaskassign> wftaskassigns = wftaskassignService.listTaskAssign(version, wfName);
            Map<Double, String> taskMap = wftaskassigns.stream().collect(Collectors.toMap(Wftaskassign::getNodeid, Wftaskassign::getWfrole));

            double temp = 0.0;
            for (Wfaction wfaction : wfactions) {
                FlowAction action = new FlowAction();
                if (wfaction.getActiontype().equals("空操作")) {
                    action.setActionId(wfaction.getId());
                    action.setDescription(wfaction.getDescription());
                    action.setActionType(wfaction.getActiontype());
                    action.setNextNodeId(String.valueOf(wfaction.getEndnodeid()));
                    action.setRole(taskMap.get(wfaction.getEndnodeid()));
                    action.setChecked(false);
                    action.setFilter("N");
                    /**
                     * 是否为结束节点
                     */
                    if (wfaction.getAction().contains("完成") || wfaction.getAction().contains("取消此操作")
                            || wfaction.getAction().contains("关闭") || wfaction.getAction().contains("终结")){
                        action.setIsEndNode("Y");
                    }
                    list.add(action);
                }
                /**
                 * 过滤重复项  判定是否需要手动选择人员
                 */
                if (temp == wfaction.getEndnodeid()) {
                    action = list.get(list.size() - 1);
                    action.setFilter("Y");
                }

                temp = wfaction.getEndnodeid();
            }
        }

        map.put("action", list);

        return map;
    }

    @Override
    public List<Wfaction> getAction(double nodeId, double version, String wfName) {
        QueryWrapper<Wfaction> wrapper = new QueryWrapper<>();
        wrapper.eq("STARTNODEID", nodeId);
        wrapper.eq("WFREVISION", version);
        wrapper.eq("WFNAME", wfName);
        wrapper.eq("ACTIONTYPE", "空操作");

        List<Wfaction> wfactions = baseMapper.selectList(wrapper);

        return wfactions;
    }

    @Override
    public Wfaction initAction(String wfName) {
        return baseMapper.initAction(wfName);
    }

    @Override
    public Wfaction getActionStatus(double startNodeId, double endNodeId) {
        QueryWrapper<Wfaction> wrapper = new QueryWrapper<>();
        wrapper.eq("STARTNODEID", startNodeId);
        wrapper.eq("ENDNODEID", endNodeId);
        wrapper.eq("ACTIONTYPE", "更改状态");

        Wfaction wfaction = baseMapper.selectOne(wrapper);

        return wfaction;
    }
}
