package com.njcn.rdms.module.project.dal.mysql.performance;

import com.njcn.rdms.framework.common.pojo.PageResult;
import com.njcn.rdms.framework.mybatis.core.mapper.BaseMapperX;
import com.njcn.rdms.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.njcn.rdms.module.project.controller.admin.performance.vo.PerformanceTemplatePageReqVO;
import com.njcn.rdms.module.project.dal.dataobject.performance.PerformanceTemplateDO;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface PerformanceTemplateMapper extends BaseMapperX<PerformanceTemplateDO> {

    default PerformanceTemplateDO selectCurrent() {
        return selectOne(new LambdaQueryWrapperX<PerformanceTemplateDO>()
                .eq(PerformanceTemplateDO::getActiveFlag, true)
                .orderByDesc(PerformanceTemplateDO::getVersionNo)
                .last("LIMIT 1"));
    }

    default PageResult<PerformanceTemplateDO> selectPage(PerformanceTemplatePageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<PerformanceTemplateDO>()
                .likeIfPresent(PerformanceTemplateDO::getTemplateName, reqVO.getTemplateName())
                .eqIfPresent(PerformanceTemplateDO::getActiveFlag, reqVO.getActiveFlag())
                .orderByDesc(PerformanceTemplateDO::getVersionNo)
                .orderByDesc(PerformanceTemplateDO::getId));
    }

    default Integer selectMaxVersionNo() {
        PerformanceTemplateDO template = selectOne(new LambdaQueryWrapperX<PerformanceTemplateDO>()
                .select(PerformanceTemplateDO::getVersionNo)
                .orderByDesc(PerformanceTemplateDO::getVersionNo)
                .last("LIMIT 1"));
        return template == null || template.getVersionNo() == null ? 0 : template.getVersionNo();
    }
}
