package com.fjhx.apk.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.apk.mapper.ApkVersionMapper;
import com.fjhx.apk.service.ApkVersionService;
import com.fjhx.entity.apk.ApkVersion;
import com.fjhx.params.apk.ApkVersionVo;
import com.fjhx.utils.WrapperUtil;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2022-10-19
 */
@Service
public class ApkVersionServiceImpl extends ServiceImpl<ApkVersionMapper, ApkVersion> implements ApkVersionService {

    @Override
    public Page<ApkVersion> getPage(Map<String, String> condition) {

        QueryWrapper<ApkVersion> wrapper = Wrappers.query();

        WrapperUtil.init(condition, wrapper)
                .like("type", "type")
                .like("version_name", "versionName")
                .like("apk_name", "apkName")
                .createTimeDesc();

        return page(condition, wrapper);
    }

    @Override
    public void add(ApkVersionVo apkVersionVo) {
        save(apkVersionVo);
    }

    @Override
    public void edit(ApkVersionVo apkVersionVo) {
        updateById(apkVersionVo);
    }

    @Override
    public void delete(ApkVersionVo apkVersionVo) {
        removeById(apkVersionVo.getId());
    }

    @Override
    public ApkVersion getNewestVersion(ApkVersionVo apkVersionVo) {
        LambdaQueryWrapper<ApkVersion> wrapper = Wrappers.<ApkVersion>lambdaQuery()
                .eq(ObjectUtil.isNotEmpty(apkVersionVo.getType()), ApkVersion::getType, apkVersionVo.getType())
                .orderByDesc(ApkVersion::getUploadTime)
                .last("limit 1");

        return getOne(wrapper);
    }

}
