package org.springblade.modules.app.manager;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wn.common.validator.Validator;
import org.springblade.modules.app.common.dto.AppVersionDTO;
import org.springblade.modules.app.common.query.AppVersionQuery;
import org.springblade.modules.app.common.query.LatestAppVersionQuery;
import org.springblade.modules.app.constant.ErrorCode;
import org.springblade.modules.app.service.IAppVersionService;
import org.springblade.modules.app.utils.ErrorMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author huyinuo
 */
@Slf4j
@Component
public class AppVersionManager {

    @Autowired
    private IAppVersionService appVersionService;

    public Page<AppVersionDTO> pageAppVersion(AppVersionQuery appVersionQuery) {
        Validator.notNull(appVersionQuery, ErrorCode.INPUT_PARAMETERS_NULL);
        return appVersionService.pageAppVersion(appVersionQuery);
    }

    public AppVersionDTO getAppVersion(Long id) {
        Validator.notNull(id, ErrorCode.INPUT_PARAMETERS_NULL);
        return appVersionService.getAppVersion(id);
    }

    public Long addAppVersion(AppVersionDTO appVersionDTO) {
        validatorAppVersion(appVersionDTO);
        return appVersionService.addAppVersion(appVersionDTO);
    }

    /**
    * @describe 检验AppVersionDTO的入参
     */
    private void validatorAppVersion(AppVersionDTO appVersionDTO) {
        log.info("appVersionDTO入参值为：{}", appVersionDTO);
        Validator.notNull(appVersionDTO, ErrorCode.INPUT_PARAMETERS_NULL);
        Validator.notNull(appVersionDTO.getVersionName(), ErrorMsg.NEW("版本名称为空"));
        Validator.notNull(appVersionDTO.getVersionNumber(), ErrorMsg.NEW("版本号为空"));
        Validator.notNull(appVersionDTO.getInstallationPackageUrl(), ErrorMsg.NEW("安装包url为空"));
        Validator.notNull(appVersionDTO.getUpgradeContext(), ErrorMsg.NEW("更新内容为空"));
        Validator.notNull(appVersionDTO.getFileSize(), ErrorMsg.NEW("文件大小为空"));
        Validator.notNull(appVersionDTO.getChannelType(), ErrorMsg.NEW("渠道类型为空"));
    }

    public Boolean updateAppVersion(AppVersionDTO appVersionDTO) {
        validatorAppVersion(appVersionDTO);
        return appVersionService.updateAppVersion(appVersionDTO);
    }

    public AppVersionDTO latestVersion(LatestAppVersionQuery latestAppVersionQuery) {
        return appVersionService.latestAppVersion(latestAppVersionQuery);
    }
}
