package com.chushang.inspection.project.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.IService;
import com.chushang.common.mybatis.page.CommonParam;
import com.chushang.inspection.project.dto.TaskDTO;
import com.chushang.inspection.project.dto.TaskExcelDTO;
import com.chushang.inspection.project.dto.TaskQuery;
import com.chushang.inspection.project.po.PollingTask;
import com.chushang.inspection.project.vo.ConfigVO;
import com.chushang.inspection.project.vo.CountVO;
import com.chushang.inspection.project.vo.TaskVO;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
  * @auther: zhao
  * @date: 2024/6/18 15:39
  */
public interface PollingTaskService extends IService<PollingTask>{


    List<TaskVO> findList(TaskQuery taskQuery);

    List<TaskVO> findTopList(CommonParam commonParam);

    ConfigVO findTaskConfig(Long taskId);

    TaskVO getTaskById(Long taskId);

    Long saveTask(TaskDTO task);

    Long updateTask(TaskDTO task, Long taskId);

    List<TaskExcelDTO> importRead(MultipartFile file);

    void importSave(List<TaskExcelDTO> taskExcel, Long parentId);

    PollingTask getTopTask(Long taskId);

    default String getTaskNameById(Long taskId){
        PollingTask task = getById(taskId);
        return ObjectUtil.isNotEmpty(task) ? task.getName() : "";
    }

    List<CountVO> countByProjectIds(Set<Long> projectIds);

    default Map<Long, Integer> countByProjectIdsMap(Set<Long> projectIds){
        List<CountVO> countVOS = countByProjectIds(projectIds);
        if (CollectionUtil.isNotEmpty(countVOS)){
            return countVOS.stream().collect(Collectors.toMap(
                    CountVO::getProjectId,
                    CountVO::getTotal,
                    Integer::sum
            ));
        }
        return Map.of();
    }

}
