package com.hz.pm.api.dashboard.handle;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.hz.pm.api.dashboard.constant.DashboardConstant;
import com.hz.pm.api.dashboard.model.vo.ProtraitProjectOutputVO;
import com.hz.pm.api.projectlib.model.entity.Project;
import com.hz.pm.api.projectlib.model.entity.ProjectApplication;
import com.hz.pm.api.projectlib.service.IProjectApplicationService;
import com.hz.pm.api.projectlib.service.IProjectService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Classname ApplicationHandler
 * @Description
 * @Date 2023/10/9 14:11
 * @Author PoffyZhang
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class ApplicationHandler {
    private final IProjectApplicationService applicationService;

    private final IProjectService projectService;
    /**
     * 生成项目产出
     * @param projectCode
     * @return
     */
    public Map<String, ProtraitProjectOutputVO> generateOutput(String projectCode) {
        Map<String, ProtraitProjectOutputVO> res = Maps.newHashMap();
        //1.初始化 项目体征
        res.put(DashboardConstant.Protrait.PROJECT_SIGNS,
                generateOutputEntity(DashboardConstant.Protrait.PROJECT_SIGNS));
        //2.查询应用
        Project project = projectService.getProjectByCode(projectCode);
        if(Objects.nonNull(project)){
            List<ProjectApplication> apps = applicationService.list(Wrappers.lambdaQuery(ProjectApplication.class)
                    .eq(ProjectApplication::getProjectVersion, project.getVersion())
                    .eq(ProjectApplication::getProjectCode, projectCode));
            for(ProjectApplication app : apps){
                String appName = StringUtils.isNotBlank(app.getApplicationName()) ? app.getApplicationName() : app.getRelatedExistsApplication();
                res.put(appName,generateOutputEntity(appName));
            }
        }
        return res;
    }

    public List<ProtraitProjectOutputVO> generateIrsApp(String projectCode) {
        List<ProtraitProjectOutputVO> res = Lists.newArrayList();
        //.查询应用
        Project project = projectService.getProjectByCode(projectCode);
        List<ProjectApplication> apps = applicationService.list(Wrappers.lambdaQuery(ProjectApplication.class)
                .eq(ProjectApplication::getProjectVersion, project.getVersion())
                .eq(ProjectApplication::getProjectCode, projectCode));
        for(ProjectApplication app : apps){
            String appName = StringUtils.isNotBlank(app.getApplicationName()) ? app.getApplicationName() : app.getRelatedExistsApplication();
            res.add(generateOutputEntity(appName));
        }
        return res;
    }

    private ProtraitProjectOutputVO generateOutputEntity(String name){
        return ProtraitProjectOutputVO.builder()
                .name(DashboardConstant.Protrait.PROJECT_SIGNS)
                .generateData(new ProtraitProjectOutputVO.GenerateData())
                .componentUsage(new ProtraitProjectOutputVO.ComponentUsage())
                .cloudResourceUsage(new ProtraitProjectOutputVO.CloudResourceUsage())
                .dataUsage(new ProtraitProjectOutputVO.DataUsage())
                .operationReport(new ProtraitProjectOutputVO.OperationReport())
                .build();
    }
}
