package com.hz.pm.api.projectlib.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hz.pm.api.common.model.constant.BizConst;
import com.hz.pm.api.common.statemachine.event.ProjectStatusChangeEvent;
import com.hz.pm.api.projectlib.model.entity.ProjectStatusChange;
import com.hz.pm.api.projectlib.mapper.NdProjectStatusChangeMapper;
import com.hz.pm.api.projectlib.service.INdProjectStatusChangeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Collection;

import static com.hz.pm.api.common.statemachine.event.ProjectStatusChangeEvent.DEPARTMENT_UNITED_REVIEW_PASS;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author CMM
 * @since 2023-02-27
 */
@Service
public class NdProjectStatusChangeServiceImpl extends ServiceImpl<NdProjectStatusChangeMapper, ProjectStatusChange> implements INdProjectStatusChangeService {

    @Override
    public ProjectStatusChange getByProjectStatus(ProjectStatusChangeEvent event, Long projectId) {
        LambdaQueryWrapper<ProjectStatusChange> query = Wrappers.lambdaQuery(ProjectStatusChange.class)
                .eq(ProjectStatusChange::getEvent, event)
                .eq(ProjectStatusChange::getProjectId, projectId)
                .orderByDesc(ProjectStatusChange::getCreateOn)
                .last(BizConst.LIMIT_1);
        return getOne(query);
    }

    @Override
    public ProjectStatusChange getByProjectStatus(ProjectStatusChangeEvent event, Collection<Long> projectIds) {
        LambdaQueryWrapper<ProjectStatusChange> query = Wrappers.lambdaQuery(ProjectStatusChange.class)
                .in(ProjectStatusChange::getProjectId, projectIds)
                .eq(ProjectStatusChange::getEvent, DEPARTMENT_UNITED_REVIEW_PASS)
                .last(BizConst.LIMIT_1);
        return getOne(query);
    }
}
