package com.yunzhupaas.flowable.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yunzhupaas.base.service.SuperServiceImpl;
import com.yunzhupaas.flowable.entity.RevokeEntity;
import com.yunzhupaas.flowable.entity.TaskLineEntity;
import com.yunzhupaas.flowable.mapper.TaskLineMapper;
import com.yunzhupaas.flowable.service.RevokeService;
import com.yunzhupaas.flowable.service.TaskLineService;
import com.yunzhupaas.util.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 类的描述
 *
 * @author YUNZHUPAASYUNZHUPAAS开发组
 * @version 5.0.x
 * @since 2024/8/23 17:37
 */
@Service
public class TaskLineServiceImpl extends SuperServiceImpl<TaskLineMapper, TaskLineEntity> implements TaskLineService {
    @Autowired
    private RevokeService revokeService;

    @Override
    public List<TaskLineEntity> getList(String taskId) {
        QueryWrapper<TaskLineEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TaskLineEntity::getTaskId, taskId);
        return this.list(queryWrapper);
    }

    @Override
    public void create(String taskId, Map<String, Boolean> conditionResMap) {
        if (CollectionUtil.isEmpty(conditionResMap)) {
            return;
        }
        List<TaskLineEntity> createList = new ArrayList<>();
        List<TaskLineEntity> updateList = new ArrayList<>();

        List<TaskLineEntity> list = this.getList(taskId);

        if (CollectionUtil.isNotEmpty(list)) {
            conditionResMap.forEach((k, v) -> {
                TaskLineEntity entity = list.stream().filter(e -> ObjectUtil.equals(k, e.getLineKey())).findFirst()
                        .orElse(null);
                if (null != entity) {
                    Boolean b = conditionResMap.get(entity.getLineKey());
                    if (null != b) {
                        entity.setLineValue(String.valueOf(b));
                        updateList.add(entity);
                    }
                } else {
                    entity = new TaskLineEntity();
                    entity.setId(RandomUtil.uuId());
                    entity.setTaskId(taskId);
                    entity.setLineKey(k);
                    entity.setLineValue(String.valueOf(v));
                    createList.add(entity);
                }
            });
        } else {
            conditionResMap.forEach((k, v) -> {
                TaskLineEntity entity = new TaskLineEntity();
                entity.setId(RandomUtil.uuId());
                entity.setTaskId(taskId);
                entity.setLineKey(k);
                entity.setLineValue(String.valueOf(v));
                createList.add(entity);
            });
        }

        if (CollectionUtil.isNotEmpty(createList)) {
            this.saveBatch(createList);
        }
        if (CollectionUtil.isNotEmpty(updateList)) {
            this.updateBatchById(updateList);
        }
    }

    @Override
    public List<String> getLineKeyList(String taskId) {
        List<String> resList = new ArrayList<>();
        RevokeEntity revokeEntity = revokeService.getRevokeTask(taskId);
        if (null != revokeEntity) {
            taskId = revokeEntity.getTaskId();
        }
        List<TaskLineEntity> list = this.getList(taskId);
        Map<String, List<TaskLineEntity>> collect = list.stream()
                .collect(Collectors.groupingBy(TaskLineEntity::getLineKey));
        collect.forEach((k, v) -> {
            List<TaskLineEntity> sortList = v.stream()
                    .sorted(Comparator.comparing(TaskLineEntity::getCreatorTime).reversed())
                    .collect(Collectors.toList());
            boolean bo = Boolean.parseBoolean(sortList.get(0).getLineValue());
            if (bo) {
                resList.add(k);
            }
        });
        return resList;
    }
}
