package com.bonus.system.dept.service;

import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.system.dept.dao.EightMembersVacancyDao;
import com.bonus.system.dept.entity.EightMembersVacancyVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;

import static com.bonus.common.core.utils.PageUtils.startPage;

/**
 * @className:EightMembersVacancyServiceImpl
 * @author:cwchen
 * @date:2025-02-10-13:40
 * @version:1.0
 * @description:八大员缺员兼职-业务层
 */
@Service(value = "EightMembersVacancyService")
@Slf4j
public class EightMembersVacancyServiceImpl implements EightMembersVacancyService {

    @Resource(name = "EightMembersVacancyDao")
    private EightMembersVacancyDao dao;

    @Resource(name = "testTaskExecutor")
    private ThreadPoolTaskExecutor testTaskExecutor;

    @Override
    public List<EightMembersVacancyVo> getList(EightMembersVacancyVo vo) {
        List<EightMembersVacancyVo> list = new ArrayList<>();
        List<EightMembersVacancyVo> newList = new ArrayList<>();
        List<Future> futureList = new ArrayList<>();
        try {
            list = dao.getList(vo);
            for (EightMembersVacancyVo vacancyVo : list) {
                Future<EightMembersVacancyVo> future = testTaskExecutor.submit(new Callable<EightMembersVacancyVo>() {
                    @Override
                    public EightMembersVacancyVo call() throws Exception {
                        // 查询缺员项目部数量
                        List<Long> numList = dao.getNumByType(vacancyVo.getId(),1);
                        // 查询兼职项目部数量
                        List<Long> numList2  = dao.getNumByType(vacancyVo.getId(),2);
                        vacancyVo.setVacancyDeptNum(CollectionUtils.isEmpty(numList) ? 0 : numList.size());
                        vacancyVo.setPartTimeJobNum(CollectionUtils.isEmpty(numList2) ? 0 : numList2.size());
                        return vacancyVo;
                    }
                });
                futureList.add(future);
            }
            for (Future<EightMembersVacancyVo> future : futureList) {
                EightMembersVacancyVo vacancyVo = future.get();
                newList.add(vacancyVo);
            }
            PageInfo<EightMembersVacancyVo> pageInfo = new PageInfo<>(newList);
        } catch (Exception e) {
            log.error(e.toString(), e);
        }
        return newList;
    }

    @Override
    public List<EightMembersVacancyVo.VacancyDetail> getUnderStaffingList(EightMembersVacancyVo vo) {
        List<EightMembersVacancyVo.VacancyDetail> list = new ArrayList<>();
        try {
            // 查询缺员项目部id
            List<Long> underStaffingIds = dao.getNumByType(vo.getId(), 1);
            startPage();
            if (CollectionUtils.isNotEmpty(underStaffingIds)) {
                list = dao.getStaffingList(underStaffingIds, vo);
            }
        } catch (Exception e) {
            log.error(e.toString(), e);
        }
        return list;
    }

    @Override
    public List<EightMembersVacancyVo.VacancyDetail> getPartTimeList(EightMembersVacancyVo vo) {
        List<EightMembersVacancyVo.VacancyDetail> list = new ArrayList<>();
        try {
            // 查询兼职项目部id
            List<Long> partTimeIds = dao.getNumByType(vo.getId(), 2);
            startPage();
            if (CollectionUtils.isNotEmpty(partTimeIds)) {
                list = dao.getStaffingList(partTimeIds, vo);
            }
        } catch (Exception e) {
            log.error(e.toString(), e);
        }
        return list;
    }

    @Override
    public AjaxResult getRoleList() {
        List<Map<String, Object>> list = new ArrayList<>();
        try {
            list = dao.getRoleList();
        } catch (Exception e) {
            log.error(e.toString(), e);
        }
        return AjaxResult.success(list);
    }
}
