package com.corewing.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.corewing.app.dto.CheckVersionRequest;
import com.corewing.app.entity.AppVersion;
import com.corewing.app.mapper.AppVersionMapper;
import com.corewing.app.service.AppVersionService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AppVersionServiceImpl extends ServiceImpl<AppVersionMapper, AppVersion> implements AppVersionService {

    @Override
    public AppVersion getNewAppVersion(CheckVersionRequest checkVersionRequest) {
        int checkVersionNumber = Integer.parseInt(checkVersionRequest.getLocalVersion().replace(".", "") + checkVersionRequest.getLocalBuildNumber());
        LambdaQueryWrapper<AppVersion> wrapper = new LambdaQueryWrapper<>();
        wrapper.gt(AppVersion::getVersionNumber, checkVersionNumber);
        wrapper.eq(AppVersion::getType, checkVersionRequest.getType());
        wrapper.eq(AppVersion::getStatus, 1);
        wrapper.orderByDesc(AppVersion::getVersionNumber, AppVersion::getBuildNumber);
        wrapper.last("limit 1");
        List<AppVersion> list = list(wrapper);
        if (list == null || list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    @Override
    public AppVersion getAppVersion() {
        LambdaQueryWrapper<AppVersion> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AppVersion::getStatus, 1);
        wrapper.eq(AppVersion::getType, "android");
        wrapper.orderByDesc(AppVersion::getVersionNumber, AppVersion::getBuildNumber);
        wrapper.last("limit 1");
        List<AppVersion> list = list(wrapper);
        if (list == null || list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }
}
