package com.njcn.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.system.mapper.AppVersionMapper;
import com.njcn.system.pojo.param.AppVersionParam;
import com.njcn.system.pojo.po.AppVersion;
import com.njcn.system.pojo.vo.AppVersionVo;
import com.njcn.system.service.IAppVersionService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xy
 * @since 2024-11-21
 */
@Service
public class AppVersionServiceImpl extends ServiceImpl<AppVersionMapper, AppVersion> implements IAppVersionService {

    @Override
    public boolean add(AppVersionParam param) {
        AppVersion appVersion = new AppVersion();
        appVersion.setVersionName(param.getAppVersion());
        appVersion.setPublishTime(LocalDateTime.now());
        appVersion.setSev(param.getSev());
        appVersion.setVersionType(param.getVersionType());
        appVersion.setContent(param.getContent());
        return this.save(appVersion);
    }

    @Override
    public AppVersionVo getLastData(String versionType) {
        AppVersionVo vo = new AppVersionVo();
        LambdaQueryWrapper<AppVersion> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppVersion::getVersionType, versionType);
        queryWrapper.orderByDesc(AppVersion::getPublishTime).last("limit 1");
        AppVersion appVersion = this.getOne(queryWrapper);
        if (Objects.nonNull(appVersion)) {
            BeanUtils.copyProperties(appVersion, vo);
        }
        return vo;
    }

    @Override
    public List<AppVersion> getAllData(String versionType) {
        LambdaQueryWrapper<AppVersion> queryWrapper = new LambdaQueryWrapper<>();
        if (Objects.nonNull(versionType)) {
            queryWrapper.eq(AppVersion::getVersionType, versionType);
        }
        queryWrapper.orderByDesc(AppVersion::getPublishTime);
        return this.list(queryWrapper);
    }
}
