package com.ruoyi.frequency.appversion.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.code.PublicCommon;
import com.ruoyi.frequency.appversion.entity.AppVersion;
import com.ruoyi.frequency.appversion.mapper.AppVersionMapper;
import com.ruoyi.frequency.appversion.service.AppVersionService;
import com.ruoyi.response.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 版本Service业务层处理
 *
 * @author liwenlong
 * @date 2023-11-27
 */
@Service
public class AppVersionServiceImpl extends ServiceImpl<AppVersionMapper, AppVersion>  implements AppVersionService
{
    @Autowired
    private AppVersionMapper appVersionMapper;

    /**
     * 查询版本
     *
     * @param id 版本主键
     * @return 版本
     */
    @Override
    public AppVersion selectAppVersionById(Long id)
    {
        return appVersionMapper.selectAppVersionById(id);
    }

    /**
     * 查询版本列表
     *
     * @param appVersion 版本
     * @return 版本
     */
    @Override
    public List<AppVersion> selectAppVersionList(AppVersion appVersion)
    {
        return appVersionMapper.selectAppVersionList(appVersion);
    }

    /**
     * 新增版本
     *
     * @param appVersion 版本
     * @return 结果
     */
    @Override
    public int insertAppVersion(AppVersion appVersion)
    {
        appVersion.setCreateData();
        return appVersionMapper.insert(appVersion);
    }

    /**
     * 修改版本
     *
     * @param appVersion 版本
     * @return 结果
     */
    @Override
    public int updateAppVersion(AppVersion appVersion)
    {
        appVersion.setUpdateData();
        return appVersionMapper.updateAppVersion(appVersion);
    }

    /**
     * 批量删除版本
     *
     * @param ids 需要删除的版本主键
     * @return 结果
     */
    @Override
    public int deleteAppVersionByIds(Long[] ids,Integer delFlag)
    {
        this.lambdaUpdate().set(AppVersion::getDelFlag,delFlag).in(AppVersion::getId,ids).update();
        return ids.length;
    }

    @Override
    public ResponseData isUpgrade(Integer appType) {

        AppVersion version = this.getOne(new QueryWrapper<AppVersion>().lambda()
                .eq(AppVersion::getDelFlag, PublicCommon.启用)
                .eq(AppVersion::getAppType, appType)
                .orderByDesc(AppVersion::getVersion)
                .last("limit 1")
        );
        return ResponseData.success(version);
    }
}
