package com.hz.pm.api.irs.manage;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
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.hz.pm.api.common.enumeration.CommonEnum;
import com.hz.pm.api.projectlib.model.enumeration.ProjectStatusEnum;
import com.hz.pm.api.projectlib.model.entity.Project;
import com.hz.pm.api.projectlib.model.req.ProjectListReq;
import com.hz.pm.api.projectlib.model.vo.ProjectLibListItemVO;
import com.hz.pm.api.projectlib.service.IProjectService;
import com.hz.pm.api.user.security.model.UserInfoDetails;
import com.hz.pm.api.user.util.LoginUserUtil;
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.Objects;
import java.util.stream.Collectors;

/**
 * @Classname IrsProjectManage
 * @Description
 * @Date 2023/8/15 11:43
 * @Author PoffyZhang
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class IrsProjectManage {

    private final IProjectService projectService;

    /**
     * IRS档案 项目列表
     *
     * @param req
     * @return
     */
    public PageVo<ProjectLibListItemVO> list(ProjectListReq req) {
        Page<Project> page = req.page();
        UserInfoDetails user = LoginUserUtil.loginUserDetail();
        LambdaQueryWrapper<Project> wrapper = Wrappers.lambdaQuery(Project.class)
                .eq(StringUtils.isNotBlank(req.getProjectType()), Project::getProjectType, req.getProjectType())
                .eq(Objects.nonNull(req.getStatus()), Project::getStatus, req.getStatus())
                .eq(Objects.nonNull(req.getStage()), Project::getStage, req.getStage())
                .ge(Objects.nonNull(req.getApproveAmountMin()), Project::getApprovalAmount, req.getApproveAmountMin())
                .le(Objects.nonNull(req.getApproveAmountMax()), Project::getApprovalAmount, req.getApproveAmountMax())
                .eq(Objects.nonNull(req.getProjectYear()), Project::getProjectYear, req.getProjectYear())
                .ge(Objects.nonNull(req.getCreateOnMin()), Project::getCreateOn, req.getCreateOnMin())
                .le(Objects.nonNull(req.getCreateOnMax()), Project::getCreateOn, req.getCreateOnMax())
                .like(StringUtils.isNotBlank(req.getProjectName()), Project::getProjectName, req.getProjectName())
                .like(StringUtils.isNotBlank(req.getBuildOrgName()), Project::getBuildOrgName, req.getBuildOrgName())
                .gt(Project::getStatus, ProjectStatusEnum.UNDER_CONSTRUCTION.getCode())
                .eq(Project::getNewest, Boolean.TRUE)
                .eq(Project::getIncludeApplication, CommonEnum.YES.getCode())
                .orderByDesc(Project::getUpdateOn);
        buildPermission(wrapper, user);
        projectService.page(page, wrapper);

        if (CollUtil.isEmpty(page.getRecords())) {
            return PageVo.empty();
        }
        List<ProjectLibListItemVO> res = page.getRecords().stream()
                .map(p -> BeanUtil.copyProperties(p, ProjectLibListItemVO.class))
                .collect(Collectors.toList());
        return PageVo.of(res, page.getTotal());
    }

    private void buildPermission(LambdaQueryWrapper<Project> wrapper, UserInfoDetails user) {
        if (user.getSuperAdmin() || user.getRegionAdmin()) {
            log.info("超管 都可看 项目档案列表");
        } else {
            wrapper.eq(Project::getBuildOrgCode, user.getMhUnitIdStr());
        }
    }
}
