package com.ningdatech.pmapi.projectlib.helper;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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 org.springframework.beans.BeanUtils;

/**
 * <p>
 * ProjectHelper
 * </p>
 *
 * @author WendyYang
 * @since 14:15 2023/2/11
 */
public class ProjectHelper {

    /**
     * 通用项目查询
     *
     * @param req 查询条件
     * @return /
     **/
    public static LambdaQueryWrapper<Project> projectQuery(ProjectListReq req) {
        LambdaQueryWrapper<Project> query = new LambdaQueryWrapper<>();
        query.eq(req.getRegionCode() != null, Project::getAreaCode, req.getRegionCode())
            .eq(req.getUserId() != null, Project::getCreateBy, req.getUserId())
            .like(req.getProjectName() != null, Project::getProjectName, req.getProjectName())
            .eq(req.getProjectType() != null, Project::getProjectType, req.getProjectType())
            .eq(req.getProjectYear() != null, Project::getProjectYear, req.getProjectYear())
            .eq(req.getStage() != null, Project::getStage, req.getStage())
            .eq(req.getStatus() != null, Project::getStatus, req.getStatus())
            // 申报金额
            .ge(req.getDeclareAmountMin() != null, Project::getDeclareAmount, req.getDeclareAmountMin())
            .le(req.getDeclareAmountMax() != null, Project::getDeclareAmount, req.getDeclareAmountMax())
            // 申报时间
            .ge(req.getCreateOnMin() != null, Project::getCreateOn, req.getCreateOnMin())
            .le(req.getCreateOnMax() != null, Project::getCreateOn, req.getCreateOnMax())
            // 批复金额
            .ge(req.getApproveAmountMin() != null, Project::getApprovalAmount, req.getApproveAmountMin())
            .le(req.getApproveAmountMax() != null, Project::getApprovalAmount, req.getApproveAmountMax())

            .like(req.getBuildOrg() != null, Project::getBuildOrgName, req.getBuildOrg())
            .eq(req.getBuildOrgCode() != null, Project::getBuildOrgCode, req.getBuildOrgCode())
            .eq(req.getIsTemporaryAugment() != null, Project::getIsTemporaryAugment, req.getIsTemporaryAugment())
            //状态 阶段 list
            .in(CollUtil.isNotEmpty(req.getStageList()),Project::getStage,req.getStageList())
            .in(CollUtil.isNotEmpty(req.getStatusList()),Project::getStatus,req.getStatusList())
            //实例code
            .in(CollUtil.isNotEmpty(req.getInstCodes()),Project::getInstCode,req.getInstCodes());
        return query;
    }

    /**
     * 转换VO
     * @param app
     * @return
     */
    public static ProjectApplicationVO convertVO(ProjectApplication app) {
        ProjectApplicationVO appVo = new ProjectApplicationVO();
        BeanUtils.copyProperties(app, appVo);
        return appVo;
    }
}
