package com.ningdatech.pmapi.workbench.manage;

import com.ningdatech.pmapi.common.helper.UserInfoHelper;
import com.ningdatech.pmapi.projectdeclared.manage.DeclaredProjectManage;
import com.ningdatech.pmapi.projectdeclared.manage.DefaultDeclaredProjectManage;
import com.ningdatech.pmapi.projectlib.manage.ProjectLibManage;
import com.ningdatech.pmapi.projectlib.model.req.ProjectListReq;
import com.ningdatech.pmapi.projectlib.model.vo.ProjectLibListItemVO;
import com.ningdatech.pmapi.sys.manage.NoticeManage;
import com.ningdatech.pmapi.sys.model.req.NoticeListReq;
import com.ningdatech.pmapi.todocenter.manage.TodoCenterManage;
import com.ningdatech.pmapi.todocenter.model.req.ToBeProcessedReq;
import com.ningdatech.pmapi.todocenter.model.vo.TodoCenterStatisticsVO;
import com.ningdatech.pmapi.user.security.auth.model.UserFullInfoDTO;
import com.ningdatech.pmapi.user.security.auth.model.UserInfoDetails;
import com.ningdatech.pmapi.user.util.LoginUserUtil;
import com.ningdatech.pmapi.workbench.converter.WorkbenchConverter;
import com.ningdatech.pmapi.workbench.model.vo.WorkbenchVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ForkJoinPool;
import java.util.stream.Collectors;

/**
 * @Classname WorkbenchManage
 * @Description
 * @Date 2023/3/20 10:56
 * @Author PoffyZhang
 */
@Component
@AllArgsConstructor
public class WorkbenchManage {

    private final TodoCenterManage todoCenterManage;

    private final DefaultDeclaredProjectManage defaultDeclaredProjectManage;

    private final DeclaredProjectManage declaredProjectManage;

    private final NoticeManage noticeManage;

    private final UserInfoHelper userInfoHelper;

    private final ProjectLibManage projectLibManage;

    public WorkbenchVO getWorkbenchData(Integer year) {
        WorkbenchVO res = new WorkbenchVO();
        Long userId = LoginUserUtil.getUserId();
        UserFullInfoDTO user = userInfoHelper.getUserFullInfo(userId);

        CompletableFuture.allOf(
                CompletableFuture.runAsync(() -> {
                    //1.待办中心数据
                    TodoCenterStatisticsVO statisticsVO = todoCenterManage.todoCenterStatistics(user.getEmployeeCode());
                    ToBeProcessedReq toBeProcessedReq = new ToBeProcessedReq();
                    toBeProcessedReq.setPageNumber(1);
                    toBeProcessedReq.setPageSize(5);
                    toBeProcessedReq.setLoginUserId(userId);
                    toBeProcessedReq.setEmployeeCode(user.getEmployeeCode());
                    statisticsVO.setTodoList(todoCenterManage.todoProjectList(toBeProcessedReq).getRecords()
                            .stream().map(WorkbenchConverter::convert).collect(Collectors.toList()));
                    res.setTodoCerter(statisticsVO);
                }, ForkJoinPool.commonPool()),
                CompletableFuture.runAsync(() -> {
                    //2.项目统计数据
                    res.setOrgDeclared(WorkbenchConverter.convert(defaultDeclaredProjectManage.declaredProjectOrgStatistics(year, user)));
                    if (userInfoHelper.isSuperOrRegionAdmin(user.getUserId())) {
                        res.setRegionDeclared(WorkbenchConverter.convert(defaultDeclaredProjectManage.declaredProjectRegionStatistics(year, user)));
                    }
                }, ForkJoinPool.commonPool()),
                CompletableFuture.runAsync(() -> {
                    //3.所有公告按类型分
                    NoticeListReq noticeListReq = new NoticeListReq();
                    noticeListReq.setPageNumber(1);
                    noticeListReq.setPageSize(1000);
                    res.setNoticeList(noticeManage.listToMapByManager(noticeListReq));
                }, ForkJoinPool.commonPool())
        ).join();

        //这个不能放入子线程
        ProjectListReq projectListReq = new ProjectListReq();
        projectListReq.setPageNumber(1);
        projectListReq.setPageSize(5);
        projectListReq.setProjectYear(year);
        projectListReq.setBuildOrgCode(user.getEmpPosUnitCode());
        res.setProjects(new ArrayList<>(projectLibManage.projectLibListWithPermission(projectListReq, user).getRecords()));
        return res;
    }
}
