package cn.iocoder.yudao.module.bpm.api.oa;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oa.BpmOAEvectionDO;
import cn.iocoder.yudao.module.bpm.service.oa.BpmOAEvectionService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * Flowable 流程实例 Api 实现类
 */
@RestController
@Validated
public class BpmOAEvectionApiImpl implements BpmOAEvectionApi {

    @Resource
    private BpmOAEvectionService evectionService;

    @Override
    public CommonResult<Map<String, List<Long>>> getEvectionUserList() {

        LocalDate now = LocalDate.now();

        List<BpmOAEvectionDO> evectionOn = evectionService.getEvectionListByStartTime(now);
        List<BpmOAEvectionDO> evectionOff = evectionService.getEvectionListByEndTime(now.minusDays(1));

        Map<String, List<Long>> result = new HashMap<>();
        result.put("on", evectionOn.stream().flatMap(item -> {
            Set<Long> togetherUserIds = item.getTogetherUserIds();
            List<Long> ids = new ArrayList<>();
            ids.add(item.getUserId());
            if (togetherUserIds != null) {
                ids.addAll(togetherUserIds); // 将 Set 转换为 List 并添加进去
            }
            return ids.stream();
        }).distinct().collect(Collectors.toList()));
        result.put("off", evectionOff.stream().flatMap(item -> {
            Set<Long> togetherUserIds = item.getTogetherUserIds();
            List<Long> ids = new ArrayList<>();
            ids.add(item.getUserId());
            if (togetherUserIds != null) {
                ids.addAll(togetherUserIds); // 将 Set 转换为 List 并添加进去
            }
            return ids.stream();
        }).distinct().collect(Collectors.toList()));

        return success(result);
    }
}
