package cn.newfeifan.mall.module.bpm.service.candidate.sourceInfoProcessor;

import cn.newfeifan.mall.framework.common.util.collection.SetUtils;
import cn.newfeifan.mall.module.bpm.controller.admin.candidate.vo.BpmTaskCandidateRuleVO;
import cn.newfeifan.mall.module.bpm.enums.definition.BpmTaskAssignRuleTypeEnum;
import cn.newfeifan.mall.module.bpm.service.candidate.BpmCandidateSourceInfo;
import cn.newfeifan.mall.module.bpm.service.candidate.BpmCandidateSourceInfoProcessor;
import cn.newfeifan.mall.module.system.api.dept.PostApi;
import cn.newfeifan.mall.module.system.api.user.AdminUserApi;
import cn.newfeifan.mall.module.system.api.user.dto.AdminUserRespDTO;
import org.flowable.engine.delegate.DelegateExecution;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

import static cn.newfeifan.mall.framework.common.util.collection.CollectionUtils.convertSet;

public class BpmCandidatePostApiSourceInfoProcessor implements BpmCandidateSourceInfoProcessor {
    @Resource
    private PostApi api;
    @Resource
    private AdminUserApi adminUserApi;

    @Override
    public Set<Integer> getSupportedTypes() {
        return SetUtils.asSet(BpmTaskAssignRuleTypeEnum.POST.getType());
    }

    @Override
    public void validRuleOptions(Integer type, Set<Long> options) {
        api.validPostList(options);
    }

    @Override
    public Set<Long> doProcess(BpmCandidateSourceInfo request, BpmTaskCandidateRuleVO rule, DelegateExecution delegateExecution) {
        List<AdminUserRespDTO> users = adminUserApi.getUserListByPostIds(rule.getOptions());
        return convertSet(users, AdminUserRespDTO::getId);
    }
}