Merge remote-tracking branch 'origin/main'

This commit is contained in:
zhouz
2026-09-30 08:14:52 +08:00
49 changed files with 2905 additions and 32 deletions

View File

@@ -0,0 +1,131 @@
package cn.code.nl.module.system.controller.admin.appversion;
import cn.code.nl.framework.common.pojo.CommonResult;
import cn.code.nl.framework.common.pojo.PageResult;
import cn.code.nl.module.system.controller.admin.appversion.vo.AppVersionCreateReqVO;
import cn.code.nl.module.system.controller.admin.appversion.vo.AppVersionPageReqVO;
import cn.code.nl.module.system.controller.admin.appversion.vo.AppVersionRespVO;
import cn.code.nl.module.system.controller.admin.appversion.vo.AppVersionUpdateReqVO;
import cn.code.nl.module.system.controller.app.appversion.vo.AppVersionCheckReqVO;
import cn.code.nl.module.system.controller.app.appversion.vo.AppVersionCheckRespVO;
import cn.code.nl.module.system.convert.appversion.AppVersionConvert;
import cn.code.nl.module.system.service.appversion.AppVersionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import static cn.code.nl.framework.common.pojo.CommonResult.success;
/**
* APP 版本管理控制器。
*/
@Tag(name = "管理后台 - APP 版本管理")
@RestController
@RequestMapping("/system/app-version")
@Validated
public class AppVersionController {
/** APP 版本服务。 */
@Resource
private AppVersionService appVersionService;
/** 新增 APP 版本草稿。 */
@PostMapping("/create")
@Operation(summary = "新增 APP 版本草稿")
@PreAuthorize("@ss.hasPermission('system:app-version:create')")
public CommonResult<Long> createAppVersion(@Valid @RequestBody AppVersionCreateReqVO reqVO) {
return success(appVersionService.createAppVersion(reqVO));
}
/** 修改 APP 版本。 */
@PutMapping("/update")
@Operation(summary = "修改 APP 版本")
@PreAuthorize("@ss.hasPermission('system:app-version:update')")
public CommonResult<Boolean> updateAppVersion(@Valid @RequestBody AppVersionUpdateReqVO reqVO) {
appVersionService.updateAppVersion(reqVO);
return success(true);
}
/** 删除 APP 版本。 */
@DeleteMapping("/delete")
@Operation(summary = "删除 APP 版本")
@Parameter(name = "id", description = "版本编号", required = true)
@PreAuthorize("@ss.hasPermission('system:app-version:delete')")
public CommonResult<Boolean> deleteAppVersion(@NotNull(message = "版本编号不能为空") @RequestParam("id") Long id) {
appVersionService.deleteAppVersion(id);
return success(true);
}
/** 查询 APP 版本详情。 */
@GetMapping("/get")
@Operation(summary = "查询 APP 版本详情")
@Parameter(name = "id", description = "版本编号", required = true)
@PreAuthorize("@ss.hasPermission('system:app-version:query')")
public CommonResult<AppVersionRespVO> getAppVersion(
@NotNull(message = "版本编号不能为空") @RequestParam("id") Long id) {
return success(AppVersionConvert.INSTANCE.convert(appVersionService.getAppVersion(id)));
}
/** 根据比较版本号查询 APP 版本详情。 */
@GetMapping("/getByVersion")
@Operation(summary = "根据比较版本号查询 APP 版本详情")
@Parameter(name = "versionCode", description = "比较版本号", required = true)
@PermitAll
public CommonResult<AppVersionRespVO> getAppVersionByVersion(
@NotNull(message = "版本编号不能为空") @RequestParam("versionCode") Long versionCode) {
return success(AppVersionConvert.INSTANCE.convert(appVersionService.getAppVersionByVersion(versionCode)));
}
/** 分页查询 APP 版本。 */
@GetMapping("/page")
@Operation(summary = "分页查询 APP 版本")
@PreAuthorize("@ss.hasPermission('system:app-version:query')")
public CommonResult<PageResult<AppVersionRespVO>> getAppVersionPage(@Valid AppVersionPageReqVO reqVO) {
return success(AppVersionConvert.INSTANCE.convert(appVersionService.getAppVersionPage(reqVO)));
}
/** 上传 APK 并发布 APP 版本。 */
@PostMapping("/publish")
@Operation(summary = "上传 APK 并发布 APP 版本")
@PreAuthorize("@ss.hasPermission('system:app-version:publish')")
public CommonResult<Boolean> publishAppVersion(
@NotNull(message = "版本编号不能为空") @RequestParam("id") Long id,
@NotNull(message = "APK 文件不能为空") @RequestParam("file") MultipartFile file) {
appVersionService.publishAppVersion(id, file);
return success(true);
}
/** 下线 APP 版本。 */
@PutMapping("/offline")
@Operation(summary = "下线 APP 版本")
@PreAuthorize("@ss.hasPermission('system:app-version:publish')")
public CommonResult<Boolean> offlineAppVersion(
@NotNull(message = "版本编号不能为空") @RequestParam("id") Long id) {
appVersionService.offlineAppVersion(id);
return success(true);
}
/** 检查手持端是否需要更新。 */
@GetMapping("/checkUpdate")
@Operation(summary = "检查 APP 更新")
@PermitAll
public CommonResult<AppVersionCheckRespVO> checkUpdate(@Valid AppVersionCheckReqVO reqVO) {
return success(appVersionService.checkUpdate(reqVO));
}
}

View File

@@ -0,0 +1,77 @@
package cn.code.nl.module.system.controller.admin.appversion.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size;
import lombok.Data;
import java.util.List;
/**
* 管理后台 - APP 版本新增请求。
*/
@Schema(description = "管理后台 - APP 版本新增请求")
@Data
public class AppVersionCreateReqVO {
/** 应用标识。 */
@NotBlank(message = "应用标识不能为空")
@Size(max = 64, message = "应用标识不能超过 64 个字符")
@Schema(description = "应用标识", requiredMode = Schema.RequiredMode.REQUIRED)
private String appCode;
/** 运行平台。 */
@NotBlank(message = "运行平台不能为空")
@Pattern(regexp = "android|ios", message = "运行平台只能是 android 或 ios")
@Schema(description = "运行平台", requiredMode = Schema.RequiredMode.REQUIRED)
private String platform;
/** 发布渠道。 */
@NotBlank(message = "发布渠道不能为空")
@Size(max = 64, message = "发布渠道不能超过 64 个字符")
@Schema(description = "发布渠道", requiredMode = Schema.RequiredMode.REQUIRED)
private String channel;
/** 展示版本号。 */
@NotBlank(message = "展示版本号不能为空")
@Size(max = 32, message = "展示版本号不能超过 32 个字符")
@Schema(description = "展示版本号", requiredMode = Schema.RequiredMode.REQUIRED)
private String versionName;
/** 比较版本号。 */
@NotNull(message = "比较版本号不能为空")
@Min(value = 1, message = "比较版本号必须大于 0")
@Schema(description = "比较版本号", requiredMode = Schema.RequiredMode.REQUIRED)
private Long versionCode;
/** 版本标题。 */
@NotBlank(message = "版本标题不能为空")
@Size(max = 128, message = "版本标题不能超过 128 个字符")
@Schema(description = "版本标题", requiredMode = Schema.RequiredMode.REQUIRED)
private String title;
/** 更新摘要。 */
@Size(max = 500, message = "更新摘要不能超过 500 个字符")
@Schema(description = "更新摘要")
private String summary;
/** 更新明细。 */
@NotEmpty(message = "更新明细不能为空")
@Schema(description = "更新明细", requiredMode = Schema.RequiredMode.REQUIRED)
private List<@NotBlank(message = "更新明细项不能为空") @Size(max = 500, message = "单条更新明细不能超过 500 个字符") String> releaseNotes;
/** 是否强制更新。 */
@NotNull(message = "是否强制更新不能为空")
@Schema(description = "是否强制更新", requiredMode = Schema.RequiredMode.REQUIRED)
private Boolean forceUpdate;
/** 最低支持版本号。 */
@Min(value = 1, message = "最低支持版本号必须大于 0")
@Schema(description = "最低支持版本号")
private Long minSupportedCode;
}

View File

@@ -0,0 +1,34 @@
package cn.code.nl.module.system.controller.admin.appversion.vo;
import cn.code.nl.framework.common.pojo.PageParam;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
/**
* 管理后台 - APP 版本分页请求。
*/
@Schema(description = "管理后台 - APP 版本分页请求")
@Data
public class AppVersionPageReqVO extends PageParam {
/** 应用标识。 */
@Schema(description = "应用标识")
private String appCode;
/** 运行平台。 */
@Schema(description = "运行平台")
private String platform;
/** 发布渠道。 */
@Schema(description = "发布渠道")
private String channel;
/** 展示版本号。 */
@Schema(description = "展示版本号")
private String versionName;
/** 版本状态。 */
@Schema(description = "版本状态")
private String appStatus;
}

View File

@@ -0,0 +1,59 @@
package cn.code.nl.module.system.controller.admin.appversion.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.time.LocalDateTime;
import java.util.List;
/**
* 管理后台 - APP 版本响应。
*/
@Schema(description = "管理后台 - APP 版本响应")
@Data
public class AppVersionRespVO {
/** 主键。 */
private Long id;
/** 应用标识。 */
private String appCode;
/** 运行平台。 */
private String platform;
/** 发布渠道。 */
private String channel;
/** 展示版本号。 */
private String versionName;
/** 比较版本号。 */
private Long versionCode;
/** 版本标题。 */
private String title;
/** 更新摘要。 */
private String summary;
/** 更新明细。 */
private List<String> releaseNotes;
/** 安装包类型。 */
private String packageType;
/** 安装包地址。 */
private String packageUrl;
/** 安装包字节数。 */
private Long packageSize;
/** 安装包 SHA-256。 */
private String packageSha256;
/** 是否强制更新。 */
private Boolean forceUpdate;
/** 最低支持版本号。 */
private Long minSupportedCode;
/** 版本状态。 */
private String appStatus;
/** 发布时间。 */
private LocalDateTime publishTime;
/** 创建者。 */
private String creator;
/** 创建时间。 */
private LocalDateTime createTime;
/** 更新者。 */
private String updater;
/** 更新时间。 */
private LocalDateTime updateTime;
}

View File

@@ -0,0 +1,19 @@
package cn.code.nl.module.system.controller.admin.appversion.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
/**
* 管理后台 - APP 版本修改请求。
*/
@Schema(description = "管理后台 - APP 版本修改请求")
@Data
public class AppVersionUpdateReqVO extends AppVersionCreateReqVO {
/** 主键。 */
@NotNull(message = "版本编号不能为空")
@Schema(description = "版本编号", requiredMode = Schema.RequiredMode.REQUIRED)
private Long id;
}

View File

@@ -0,0 +1,42 @@
package cn.code.nl.module.system.controller.app.appversion;
import cn.code.nl.framework.common.pojo.CommonResult;
import cn.code.nl.framework.tenant.core.aop.TenantIgnore;
import cn.code.nl.module.system.controller.app.appversion.vo.AppVersionCheckReqVO;
import cn.code.nl.module.system.controller.app.appversion.vo.AppVersionCheckRespVO;
import cn.code.nl.module.system.service.appversion.AppVersionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import static cn.code.nl.framework.common.pojo.CommonResult.success;
/**
* 手持端版本检查接口。
*/
@Tag(name = "用户 App - 版本检查")
@RestController
@RequestMapping("/system/app-version")
@Validated
@TenantIgnore
public class AppVersionCheckController {
/** APP 版本服务。 */
@Resource
private AppVersionService appVersionService;
/** 检查手持端是否需要更新。 */
@GetMapping("/checkUpdate")
@Operation(summary = "检查 APP 更新")
@PermitAll
public CommonResult<AppVersionCheckRespVO> checkUpdate(@Valid AppVersionCheckReqVO reqVO) {
return success(appVersionService.checkUpdate(reqVO));
}
}

View File

@@ -0,0 +1,38 @@
package cn.code.nl.module.system.controller.app.appversion.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size;
import lombok.Data;
/**
* 手持端检查更新请求。
*/
@Schema(description = "用户 App - 检查更新请求")
@Data
public class AppVersionCheckReqVO {
/** 应用标识。 */
@NotBlank(message = "应用标识不能为空")
@Size(max = 64, message = "应用标识不能超过 64 个字符")
private String appCode;
/** 运行平台。 */
@NotBlank(message = "运行平台不能为空")
@Pattern(regexp = "android|ios", message = "运行平台只能是 android 或 ios")
private String platform;
/** 发布渠道。 */
@NotBlank(message = "发布渠道不能为空")
@Size(max = 64, message = "发布渠道不能超过 64 个字符")
private String channel = "default";
/** 手持端当前比较版本号。 */
@NotNull(message = "当前版本号不能为空")
@Min(value = 1, message = "当前版本号必须大于 0")
private Long currentVersionCode;
}

View File

@@ -0,0 +1,52 @@
package cn.code.nl.module.system.controller.app.appversion.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.time.LocalDateTime;
import java.util.List;
/**
* 手持端检查更新响应。
*/
@Schema(description = "用户 App - 检查更新响应")
@Data
public class AppVersionCheckRespVO {
/** 是否存在可用更新。 */
private Boolean hasUpdate;
/** 当前版本是否必须更新。 */
private Boolean forceUpdate;
/** 最新版本名称。 */
private String versionName;
/** 最新比较版本号。 */
private Long versionCode;
/** 版本标题。 */
private String title;
/** 更新摘要。 */
private String summary;
/** 更新明细。 */
private List<String> releaseNotes;
/** 安装包类型。 */
private String packageType;
/** 安装包下载地址。 */
private String packageUrl;
/** 安装包字节数。 */
private Long packageSize;
/** 安装包 SHA-256。 */
private String packageSha256;
/** 发布时间。 */
private LocalDateTime publishTime;
}

View File

@@ -0,0 +1,33 @@
package cn.code.nl.module.system.convert.appversion;
import cn.code.nl.framework.common.pojo.PageResult;
import cn.code.nl.module.system.controller.admin.appversion.vo.AppVersionCreateReqVO;
import cn.code.nl.module.system.controller.admin.appversion.vo.AppVersionRespVO;
import cn.code.nl.module.system.controller.admin.appversion.vo.AppVersionUpdateReqVO;
import cn.code.nl.module.system.dal.dataobject.appversion.AppVersionDO;
import org.mapstruct.Mapper;
import org.mapstruct.ReportingPolicy;
import org.mapstruct.factory.Mappers;
/**
* APP 版本对象转换器。
*/
@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface AppVersionConvert {
/** 转换器实例。 */
AppVersionConvert INSTANCE = Mappers.getMapper(AppVersionConvert.class);
/** 将新增请求转换为数据对象。 */
AppVersionDO convert(AppVersionCreateReqVO reqVO);
/** 将修改请求转换为数据对象。 */
AppVersionDO convert(AppVersionUpdateReqVO reqVO);
/** 将数据对象转换为响应对象。 */
AppVersionRespVO convert(AppVersionDO appVersion);
/** 将分页数据转换为分页响应。 */
PageResult<AppVersionRespVO> convert(PageResult<AppVersionDO> pageResult);
}

View File

@@ -0,0 +1,73 @@
package cn.code.nl.module.system.dal.dataobject.appversion;
import cn.code.nl.framework.mybatis.core.dataobject.BaseDO;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import lombok.Data;
import java.time.LocalDateTime;
import java.util.List;
/**
* APP 版本数据对象。
*/
@TableName(value = "system_app_version", autoResultMap = true)
@Data
public class AppVersionDO extends BaseDO {
/** 主键。 */
@TableId
private Long id;
/** 应用标识。 */
private String appCode;
/** 运行平台。 */
private String platform;
/** 发布渠道。 */
private String channel;
/** 展示版本号。 */
private String versionName;
/** 比较版本号。 */
private Long versionCode;
/** 版本标题。 */
private String title;
/** 更新摘要。 */
private String summary;
/** 更新明细。 */
@TableField(typeHandler = JacksonTypeHandler.class)
private List<String> releaseNotes;
/** 安装包类型。 */
private String packageType;
/** 安装包地址。 */
private String packageUrl;
/** 安装包字节数。 */
private Long packageSize;
/** 安装包 SHA-256。 */
private String packageSha256;
/** 是否强制更新。 */
private Boolean forceUpdate;
/** 最低支持版本号。 */
private Long minSupportedCode;
/** 版本状态。 */
private String appStatus;
/** 发布时间。 */
private LocalDateTime publishTime;
}

View File

@@ -0,0 +1,44 @@
package cn.code.nl.module.system.dal.mysql.appversion;
import cn.code.nl.framework.common.pojo.PageResult;
import cn.code.nl.framework.mybatis.core.mapper.BaseMapperX;
import cn.code.nl.framework.mybatis.core.util.MyBatisUtils;
import cn.code.nl.module.system.controller.admin.appversion.vo.AppVersionPageReqVO;
import cn.code.nl.module.system.dal.dataobject.appversion.AppVersionDO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
/**
* APP 版本 Mapper。
*/
@Mapper
public interface AppVersionMapper extends BaseMapperX<AppVersionDO> {
/** 分页查询 APP 版本。 */
default PageResult<AppVersionDO> selectPage(AppVersionPageReqVO reqVO) {
Page<AppVersionDO> page = selectPage(MyBatisUtils.buildPage(reqVO), reqVO);
return new PageResult<>(page.getRecords(), page.getTotal());
}
/** 执行 APP 版本分页查询。 */
Page<AppVersionDO> selectPage(Page<AppVersionDO> page, @Param("reqVO") AppVersionPageReqVO reqVO);
/** 按主键查询 APP 版本。 */
AppVersionDO selectAppVersionById(@Param("id") Long id);
/** 按版本唯一键查询 APP 版本。 */
AppVersionDO selectByVersion(@Param("appCode") String appCode,
@Param("platform") String platform,
@Param("channel") String channel,
@Param("versionCode") Long versionCode);
/** 根据比较版本号查询 APP 版本。 */
AppVersionDO selectByVersionCode(@Param("versionCode") Long versionCode);
/** 查询指定应用、平台、渠道的最新已发布版本。 */
AppVersionDO selectLatestPublished(@Param("appCode") String appCode,
@Param("platform") String platform,
@Param("channel") String channel);
}

View File

@@ -0,0 +1,12 @@
package cn.code.nl.module.system.enums.appversion;
/**
* APP 版本状态。
*/
public enum AppVersionStatusEnum {
DRAFT,
PUBLISHED,
OFFLINE
}

View File

@@ -0,0 +1,44 @@
package cn.code.nl.module.system.service.appversion;
import cn.code.nl.framework.common.pojo.PageResult;
import cn.code.nl.module.system.controller.admin.appversion.vo.AppVersionCreateReqVO;
import cn.code.nl.module.system.controller.admin.appversion.vo.AppVersionPageReqVO;
import cn.code.nl.module.system.controller.admin.appversion.vo.AppVersionUpdateReqVO;
import cn.code.nl.module.system.controller.app.appversion.vo.AppVersionCheckReqVO;
import cn.code.nl.module.system.controller.app.appversion.vo.AppVersionCheckRespVO;
import cn.code.nl.module.system.dal.dataobject.appversion.AppVersionDO;
import jakarta.validation.constraints.NotNull;
import org.springframework.web.multipart.MultipartFile;
/**
* APP 版本服务。
*/
public interface AppVersionService {
/** 新增 APP 版本草稿。 */
Long createAppVersion(AppVersionCreateReqVO reqVO);
/** 修改 APP 版本。 */
void updateAppVersion(AppVersionUpdateReqVO reqVO);
/** 删除 APP 版本。 */
void deleteAppVersion(Long id);
/** 查询 APP 版本详情。 */
AppVersionDO getAppVersion(Long id);
/** 分页查询 APP 版本。 */
PageResult<AppVersionDO> getAppVersionPage(AppVersionPageReqVO reqVO);
/** 上传 APK 并发布 APP 版本。 */
void publishAppVersion(Long id, MultipartFile file);
/** 下线 APP 版本。 */
void offlineAppVersion(Long id);
/** 检查手持端是否需要更新。 */
AppVersionCheckRespVO checkUpdate(AppVersionCheckReqVO reqVO);
/** 根据比较版本号查询 APP 版本。 */
AppVersionDO getAppVersionByVersion(@NotNull(message = "版本编号不能为空") Long versionCode);
}

View File

@@ -0,0 +1,216 @@
package cn.code.nl.module.system.service.appversion;
import cn.code.nl.framework.common.pojo.CommonResult;
import cn.code.nl.framework.common.pojo.PageResult;
import cn.code.nl.module.infra.api.file.FileApi;
import cn.code.nl.module.infra.api.file.dto.FileCreateReqDTO;
import cn.code.nl.module.system.controller.admin.appversion.vo.AppVersionCreateReqVO;
import cn.code.nl.module.system.controller.admin.appversion.vo.AppVersionPageReqVO;
import cn.code.nl.module.system.controller.admin.appversion.vo.AppVersionUpdateReqVO;
import cn.code.nl.module.system.controller.app.appversion.vo.AppVersionCheckReqVO;
import cn.code.nl.module.system.controller.app.appversion.vo.AppVersionCheckRespVO;
import cn.code.nl.module.system.convert.appversion.AppVersionConvert;
import cn.code.nl.module.system.dal.dataobject.appversion.AppVersionDO;
import cn.code.nl.module.system.dal.mysql.appversion.AppVersionMapper;
import cn.code.nl.module.system.enums.appversion.AppVersionStatusEnum;
import cn.hutool.core.io.FileUtil;
import cn.hutool.crypto.digest.DigestUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.time.LocalDateTime;
import static cn.code.nl.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.code.nl.module.system.enums.ErrorCodeConstants.APP_VERSION_EXISTS;
import static cn.code.nl.module.system.enums.ErrorCodeConstants.APP_VERSION_DOWNLOAD_URL_FAILED;
import static cn.code.nl.module.system.enums.ErrorCodeConstants.APP_VERSION_FILE_INVALID;
import static cn.code.nl.module.system.enums.ErrorCodeConstants.APP_VERSION_NOT_FOUND;
import static cn.code.nl.module.system.enums.ErrorCodeConstants.APP_VERSION_STATUS_INVALID;
import static cn.code.nl.module.system.enums.ErrorCodeConstants.APP_VERSION_UPLOAD_FAILED;
/**
* APP 版本服务实现。
*/
@Service
@Slf4j
public class AppVersionServiceImpl implements AppVersionService {
/** APP 版本 Mapper。 */
@Resource
private AppVersionMapper appVersionMapper;
/** 文件服务 API。 */
@Resource
private FileApi fileApi;
/** 新增 APP 版本草稿。 */
@Override
public Long createAppVersion(AppVersionCreateReqVO reqVO) {
validateVersionUnique(reqVO.getAppCode(), reqVO.getPlatform(), reqVO.getChannel(), reqVO.getVersionCode(), null);
AppVersionDO appVersion = AppVersionConvert.INSTANCE.convert(reqVO);
appVersion.setPackageType("apk");
appVersion.setPackageUrl("");
appVersion.setAppStatus(AppVersionStatusEnum.DRAFT.name().toLowerCase());
appVersionMapper.insert(appVersion);
return appVersion.getId();
}
/** 修改 APP 版本。 */
@Override
public void updateAppVersion(AppVersionUpdateReqVO reqVO) {
validateAppVersionExists(reqVO.getId());
validateVersionUnique(reqVO.getAppCode(), reqVO.getPlatform(), reqVO.getChannel(), reqVO.getVersionCode(), reqVO.getId());
AppVersionDO update = AppVersionConvert.INSTANCE.convert(reqVO);
update.setPackageType(null);
update.setPackageUrl(null);
update.setPackageSize(null);
update.setPackageSha256(null);
update.setAppStatus(null);
update.setPublishTime(null);
appVersionMapper.updateById(update);
}
/** 删除 APP 版本。 */
@Override
public void deleteAppVersion(Long id) {
validateAppVersionExists(id);
appVersionMapper.deleteById(id);
}
/** 查询 APP 版本详情。 */
@Override
public AppVersionDO getAppVersion(Long id) {
return validateAppVersionExists(id);
}
/** 分页查询 APP 版本。 */
@Override
public PageResult<AppVersionDO> getAppVersionPage(AppVersionPageReqVO reqVO) {
return appVersionMapper.selectPage(reqVO);
}
/** 上传 APK 并发布 APP 版本。 */
@Override
@Transactional(rollbackFor = Exception.class)
public void publishAppVersion(Long id, MultipartFile file) {
AppVersionDO appVersion = validateAppVersionExists(id);
if (AppVersionStatusEnum.PUBLISHED.name().equalsIgnoreCase(appVersion.getAppStatus())) {
throw exception(APP_VERSION_STATUS_INVALID);
}
if (file.isEmpty() || !"apk".equalsIgnoreCase(FileUtil.extName(file.getOriginalFilename()))) {
throw exception(APP_VERSION_FILE_INVALID);
}
byte[] content;
try {
content = file.getBytes();
} catch (IOException ex) {
log.error("读取 APK 文件失败,版本编号={},文件名={}", id, file.getOriginalFilename(), ex);
throw exception(APP_VERSION_UPLOAD_FAILED, "读取文件失败");
}
FileCreateReqDTO fileReq = new FileCreateReqDTO();
fileReq.setContent(content);
fileReq.setName(file.getOriginalFilename());
fileReq.setDirectory("app-version/" + appVersion.getAppCode() + "/" + appVersion.getPlatform());
fileReq.setType(file.getContentType());
CommonResult<String> fileResult = fileApi.createFile(fileReq);
if (!fileResult.isSuccess()) {
log.error("上传 APK 失败,版本编号={},文件名={},文件服务返回={}", id, file.getOriginalFilename(), fileResult);
throw exception(APP_VERSION_UPLOAD_FAILED, fileResult.getMsg());
}
AppVersionDO update = new AppVersionDO();
update.setId(id);
update.setPackageType("apk");
update.setPackageUrl(fileResult.getData());
update.setPackageSize(file.getSize());
update.setPackageSha256(DigestUtil.sha256Hex(content));
update.setAppStatus(AppVersionStatusEnum.PUBLISHED.name().toLowerCase());
update.setPublishTime(LocalDateTime.now());
appVersionMapper.updateById(update);
}
/** 下线 APP 版本。 */
@Override
public void offlineAppVersion(Long id) {
AppVersionDO appVersion = validateAppVersionExists(id);
if (!AppVersionStatusEnum.PUBLISHED.name().equalsIgnoreCase(appVersion.getAppStatus())) {
throw exception(APP_VERSION_STATUS_INVALID);
}
AppVersionDO update = new AppVersionDO();
update.setId(id);
update.setAppStatus(AppVersionStatusEnum.OFFLINE.name().toLowerCase());
appVersionMapper.updateById(update);
}
/** 检查手持端是否需要更新,并返回可用安装包地址。 */
@Override
public AppVersionCheckRespVO checkUpdate(AppVersionCheckReqVO reqVO) {
AppVersionDO latest = appVersionMapper.selectLatestPublished(
reqVO.getAppCode(), reqVO.getPlatform(), reqVO.getChannel());
AppVersionCheckRespVO response = new AppVersionCheckRespVO();
if (latest == null || latest.getVersionCode() <= reqVO.getCurrentVersionCode()) {
response.setHasUpdate(false);
response.setForceUpdate(false);
return response;
}
response.setHasUpdate(true);
response.setForceUpdate(latest.getForceUpdate()
|| (latest.getMinSupportedCode() != null
&& reqVO.getCurrentVersionCode() < latest.getMinSupportedCode()));
response.setVersionName(latest.getVersionName());
response.setVersionCode(latest.getVersionCode());
response.setTitle(latest.getTitle());
response.setSummary(latest.getSummary());
response.setReleaseNotes(latest.getReleaseNotes());
response.setPackageType(latest.getPackageType());
response.setPackageSize(latest.getPackageSize());
response.setPackageSha256(latest.getPackageSha256());
response.setPublishTime(latest.getPublishTime());
String packageUrl = latest.getPackageUrl();
if (packageUrl.contains("X-Amz-Signature=")) {
CommonResult<String> urlResult = fileApi.presignGetUrl(packageUrl, null);
if (!urlResult.isSuccess()) {
throw exception(APP_VERSION_DOWNLOAD_URL_FAILED, urlResult.getMsg());
}
packageUrl = urlResult.getData();
}
response.setPackageUrl(packageUrl);
return response;
}
/** 根据比较版本号查询 APP 版本。 */
@Override
public AppVersionDO getAppVersionByVersion(Long versionCode) {
AppVersionDO appVersion = appVersionMapper.selectByVersionCode(versionCode);
if (appVersion == null) {
throw exception(APP_VERSION_NOT_FOUND);
}
return appVersion;
}
/** 校验版本是否存在。 */
private AppVersionDO validateAppVersionExists(Long id) {
AppVersionDO appVersion = appVersionMapper.selectAppVersionById(id);
if (appVersion == null) {
throw exception(APP_VERSION_NOT_FOUND);
}
return appVersion;
}
/** 校验版本唯一键是否重复。 */
private void validateVersionUnique(String appCode, String platform, String channel, Long versionCode, Long excludeId) {
AppVersionDO existed = appVersionMapper.selectByVersion(appCode, platform, channel, versionCode);
if (existed != null && !existed.getId().equals(excludeId)) {
throw exception(APP_VERSION_EXISTS);
}
}
}