package com.ningdatech.pmapi.performance.convert;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;

import com.ningdatech.pmapi.performance.model.entity.PerformanceAppraisalApplication;
import com.ningdatech.pmapi.performance.model.entity.PerformanceAppraisalProject;
import com.ningdatech.pmapi.performance.model.vo.PerformanceAppraisalApplicationVO;
import com.ningdatech.pmapi.performance.model.vo.PerformanceAppraisalProjectVO;
import com.ningdatech.pmapi.projectlib.enumeration.ProjectTypeEnum;
import com.ningdatech.pmapi.projectlib.model.entity.Project;
import com.ningdatech.pmapi.projectlib.model.entity.ProjectApplication;

import cn.hutool.core.collection.CollUtil;

/**
 * @Classname PerformanceAppraisalConverer
 * @Description
 * @Date 2023/6/19 15:31
 * @Author PoffyZhang
 */
public class PerformanceAppraisalConveter {

    public static List<PerformanceAppraisalProjectVO> convertProjects(
            List<PerformanceAppraisalProject> projects, Map<String, Project> map) {
        if(CollUtil.isNotEmpty(projects) && CollUtil.isNotEmpty(map)){
            return projects.stream().map(p -> {
                PerformanceAppraisalProjectVO vo = new PerformanceAppraisalProjectVO();
                vo.setIsReAppraisal(p.getIsReAppraisal());
                vo.setCanSelfAppraisal(checkCanSelfAppraisal(p));
                vo.setProjectId(p.getProjectId());
                if(map.containsKey(p.getProjectCode())){
                    Project project = map.get(p.getProjectCode());
                    vo.setProjectCode(project.getProjectCode());
                    vo.setProjectName(project.getProjectName());
                    vo.setProjectType(project.getProjectType());
                    vo.setProjectTypeName(getProjectTypeName(project.getProjectType()));
                    vo.setBuildOrgName(project.getBuildOrgName());
                    vo.setDeclareAmount(project.getDeclareAmount());
                    vo.setProjectYear(project.getProjectYear());
                }
                return vo;
            }).collect(Collectors.toList());
        }
        return Collections.emptyList();
    }

    public static List<PerformanceAppraisalApplicationVO> convertApplications(
            List<PerformanceAppraisalApplication> applications, Map<Long,ProjectApplication> map) {
        if(CollUtil.isNotEmpty(applications) && CollUtil.isNotEmpty(map)){
            return applications.stream().map(a -> {
                PerformanceAppraisalApplicationVO vo = new PerformanceAppraisalApplicationVO();
                vo.setAppId(a.getApplicationId());
                if(map.containsKey(a.getApplicationId())){
                    ProjectApplication application = map.get(a.getApplicationId());
                    vo.setApplicationName(StringUtils.isNotBlank(application.getApplicationName())?
                            application.getApplicationName() : application.getAccountAppName());
                    vo.setApplicationType(application.getApplicationType());
                    vo.setBizDomain(application.getBizDomain());
                    vo.setBuildOrgName(application.getBuildOrgName());
                    vo.setPublishSide(application.getPublishSide());
                }
                return vo;
            }).collect(Collectors.toList());
        }
        return Collections.emptyList();
    }

    public static String getProjectTypeName(Integer projectType) {
        if (Objects.nonNull(projectType)) {
            return ProjectTypeEnum.getDesc(projectType);
        }
        return StringUtils.EMPTY;
    }

    /**
     * 是否可以自评
     * @param appraisalProject
     * @return
     */
    public static Boolean checkCanSelfAppraisal(PerformanceAppraisalProject appraisalProject) {
        if (Objects.nonNull(appraisalProject.getSelfAppraisalStart())
                && Objects.nonNull(appraisalProject.getSelfAppraisalEnd())) {
            if (LocalDateTime.now().compareTo(appraisalProject.getSelfAppraisalStart()) > 0 &&
                    LocalDateTime.now().compareTo(appraisalProject.getSelfAppraisalEnd()) < 0) {
                return Boolean.TRUE;
            }
        }
        return Boolean.FALSE;
    }
}
