package com.xindazhou.business.dal.mysql.version;

import com.xindazhou.framework.common.pojo.PageResult;
import com.xindazhou.framework.mybatis.core.mapper.BaseMapperX;
import com.xindazhou.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.xindazhou.business.controller.admin.version.vo.VersionPageReqVO;
import com.xindazhou.business.dal.dataobject.version.VersionDO;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface VersionMapper extends BaseMapperX<VersionDO> {

    default PageResult<VersionDO> selectPage(VersionPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<VersionDO>()
                .eqIfPresent(VersionDO::getPlatform, reqVO.getPlatform())
                .likeIfPresent(VersionDO::getVersionName, reqVO.getVersionName())
                .eqIfPresent(VersionDO::getStatus, reqVO.getStatus())
                .orderByDesc(VersionDO::getVersionCode));
    }

    /**
     * 获取指定平台的最新版本（启用状态）
     *
     * @param platform 平台类型：1-安卓，2-鸿蒙，3-iOS
     * @return 最新版本
     */
    default VersionDO selectLatestByPlatform(Integer platform) {
        return selectOne(new LambdaQueryWrapperX<VersionDO>()
                .eq(VersionDO::getPlatform, platform)
                .eq(VersionDO::getStatus, 0) // 0-启用
                .orderByDesc(VersionDO::getVersionCode)
                .last("LIMIT 1"));
    }

}

