package com.ningdatech.pmapi.projectlib.manage;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ningdatech.basic.model.PageVo;
import com.ningdatech.basic.util.CollUtils;
import com.ningdatech.pmapi.projectlib.helper.ProjectHelper;
import com.ningdatech.pmapi.projectlib.model.entity.Project;
import com.ningdatech.pmapi.projectlib.model.entity.ProjectApplication;
import com.ningdatech.pmapi.projectlib.model.req.ProjectListReq;
import com.ningdatech.pmapi.projectlib.model.vo.ProjectApplicationVO;
import com.ningdatech.pmapi.projectlib.model.vo.ProjectDetailVO;
import com.ningdatech.pmapi.projectlib.model.vo.ProjectLibListItemVO;
import com.ningdatech.pmapi.projectlib.service.IProjectApplicationService;
import com.ningdatech.pmapi.projectlib.service.IProjectService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * <p>
 * ProjectLibManage
 * </p>
 *
 * @author WendyYang
 * @since 14:19 2023/2/1
 */
@Component
@RequiredArgsConstructor
public class ProjectLibManage {

    private final IProjectService projectService;

    private final IProjectApplicationService applicationService;

    public PageVo<ProjectLibListItemVO> projectLibList(ProjectListReq req) {
        LambdaQueryWrapper<Project> query = ProjectHelper.projectQuery(req);
        Page<Project> page = projectService.page(req.page(), query);
        long total;
        if ((total = page.getTotal()) == 0) {
            return PageVo.empty();
        }
        List<ProjectLibListItemVO> records = CollUtils.convert(page.getRecords(), w -> {
            ProjectLibListItemVO item = new ProjectLibListItemVO();
            item.setId(w.getId());
            item.setProjectName(w.getProjectName());
            item.setCreateOn(w.getCreateOn());
            item.setDeclaredAmount(w.getDeclareAmount());
            item.setStage(w.getStage());
            item.setStatus(w.getStatus());
            item.setProjectType(w.getProjectType());
            item.setProjectYear(w.getProjectYear());
            item.setBuildOrg(w.getBuildOrgName());
            item.setBizDomain(w.getBizDomain());
            return item;
        });
        return PageVo.of(records, total);
    }

    /**
     *
     * @param projectId 项目详情
     * @return com.ningdatech.pmapi.projectlib.model.entity.vo.ProjectDetailVO
     * @author ZPF
     * @since 2023/02/11 11:15
     */
    public ProjectDetailVO getProjectDetail(Long projectId) {
        // 查询项目申报信息
        Project projectInfo = projectService.getById(projectId);
        if(Objects.isNull(projectInfo)){
            return null;
        }
        ProjectDetailVO vo = new ProjectDetailVO();
        BeanUtils.copyProperties(projectInfo, vo);
        vo.buildDynamicForm(projectInfo.getDynamicForm());
        // 查询应用
        if(Objects.nonNull(vo)){
            List<ProjectApplication> applications = applicationService.list(Wrappers.lambdaQuery(ProjectApplication.class)
                    .eq(ProjectApplication::getProjectId, vo.getId()));
            Optional.ofNullable(applications).ifPresent(apps ->
                    vo.setProjectApplications(CollUtils.convert(apps,
                            app-> {
                                ProjectApplicationVO appVo = new ProjectApplicationVO();
                                BeanUtils.copyProperties(app, appVo);
                                return appVo;
                            }
                   )
            ));
        }
        return vo;
    }

}
