package org.springblade.system.service;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.system.mapper.AppVersionMapper;
import org.springblade.system.pojo.entity.AppVersion;
import org.springframework.stereotype.Service;

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

    /**
     * 分页
     */
    public IPage<AppVersion> page(AppVersion modelVo, Query query){
        query.setDescs("create_time");
        IPage<AppVersion> page = this.page(Condition.getPage(query), Condition.getQueryWrapper(modelVo));
        return page;
    }

    /**
     * 详情
     */
    public AppVersion detail(Long id){
        if (ObjectUtil.isEmpty(id)) {
            throw new ServiceException("id不能为空");
        }
        AppVersion model = this.getById(id);
        return model;
    }

    /**
     * 发布版本/编辑
     */
    public AppVersion publishOrEdit(AppVersion modelVo) {
        //版本号没有重复校验
        AppVersion appVersion=this.getOne(new LambdaQueryWrapper<AppVersion>().eq(AppVersion::getUpdateType, modelVo.getUpdateType())
                .eq(AppVersion::getVersion, modelVo.getVersion())
                .eq(AppVersion::getSubVersion, modelVo.getSubVersion())
        );
        if(ObjectUtil.isNotEmpty(appVersion)){
            if(ObjectUtil.isEmpty(modelVo.getId())||(ObjectUtil.isNotEmpty(modelVo.getId())&&!modelVo.getId().equals(appVersion.getId()))){
                throw new ServiceException("该更新类型的版本号重复");
            }
        }
        this.saveOrUpdate(modelVo);
        return modelVo;
    }
}
