From c4d802580efc585d21ee0d292138a50cbea8710b Mon Sep 17 00:00:00 2001 From: Gary Date: Mon, 22 Jan 2024 16:03:24 +0800 Subject: [PATCH] 1.add update interface and pseudo implement --- .../user/controller/UpdateController.java | 90 ++++++++++ .../java/com/luoo/user/dao/AppUpdateDao.java | 11 ++ .../luoo/user/dto/response/AppUpdateDTO.java | 29 ++++ .../java/com/luoo/user/pojo/AppUpdate.java | 134 +++++++++++++++ .../luoo/user/service/AppUpdateService.java | 157 ++++++++++++++++++ 5 files changed, 421 insertions(+) create mode 100644 luoo_user/src/main/java/com/luoo/user/controller/UpdateController.java create mode 100644 luoo_user/src/main/java/com/luoo/user/dao/AppUpdateDao.java create mode 100644 luoo_user/src/main/java/com/luoo/user/dto/response/AppUpdateDTO.java create mode 100644 luoo_user/src/main/java/com/luoo/user/pojo/AppUpdate.java create mode 100644 luoo_user/src/main/java/com/luoo/user/service/AppUpdateService.java diff --git a/luoo_user/src/main/java/com/luoo/user/controller/UpdateController.java b/luoo_user/src/main/java/com/luoo/user/controller/UpdateController.java new file mode 100644 index 0000000..04b85e5 --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/controller/UpdateController.java @@ -0,0 +1,90 @@ + +package com.luoo.user.controller; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.BeanUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import com.luoo.user.dto.response.AppUpdateDTO; +import com.luoo.user.pojo.AppUpdate; +import com.luoo.user.service.AppUpdateService; + +import annotation.GlobalInterceptor; +import annotation.VerifyParam; +import api.Result; +import enums.AppUpdateTypeEnum; +import enums.RequestFrequencyTypeEnum; +import io.swagger.annotations.Api; +import util.StringTools; + +import javax.annotation.Resource; +import javax.servlet.http.HttpServletResponse; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.util.Arrays; +@Api(tags = "UpdateController") +@RestController +@RequestMapping("/update") +public class UpdateController { + + private static final Logger logger = LoggerFactory.getLogger(UpdateController.class); + @Autowired + private AppUpdateService appUpdateService; + + @PostMapping("/checkVersion") + @GlobalInterceptor + public Result checkVersion(@RequestParam("appVersion") String appVersion, + @VerifyParam(required = true) @RequestParam("deviceId") String deviceId) { + if (StringTools.isEmpty(appVersion)) { + return Result.success(); + } + AppUpdate appUpdate = appUpdateService.getLatestUpdate(appVersion, deviceId); + if (appUpdate == null) { + return Result.success(); + } + AppUpdateDTO appUpdateDTO = new AppUpdateDTO(); + BeanUtils.copyProperties(appUpdate, appUpdateDTO); + AppUpdateTypeEnum typeEnum = AppUpdateTypeEnum.getByType(appUpdate.getUpdateType()); + appUpdateDTO.setSize(getFileSize(appUpdate.getId() , typeEnum.getSuffix())); + appUpdateDTO.setUpdateList(Arrays.asList(appUpdate.getUpdateDescArray())); + return Result.success(appUpdateDTO); + } + + private Long getFileSize(String id, String suffix) { + return 0L; + } + + @GetMapping("/download/{id}") + @GlobalInterceptor(frequencyType = RequestFrequencyTypeEnum.DAY, requestFrequencyThreshold = 10) + public void download(HttpServletResponse response, @VerifyParam(required = true) @PathVariable Integer id) { + + /* + * OutputStream out = null; FileInputStream in = null; try { AppUpdate appUpdate + * = appUpdateService.getAppUpdateById(id); AppUpdateTypeEnum typeEnum = + * AppUpdateTypeEnum.getByType(appUpdate.getUpdateType()); String fileName = + * appConfig.getAppName() + "." + appUpdate.getVersion() + typeEnum.getSuffix(); + * File file = new File(appConfig.getProjectFolder() + + * Constants.APP_UPDATE_FOLDER + appUpdate.getId() + typeEnum.getSuffix()); if + * (!file.exists()) { return; } + * response.setContentType("application/x-msdownload; charset=UTF-8"); + * response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + * + "\""); response.setContentLengthLong(file.length()); + * + * in = new FileInputStream(file); byte[] byteData = new byte[1024]; out = + * response.getOutputStream(); int len = 0; while ((len = in.read(byteData)) != + * -1) { out.write(byteData, 0, len); } out.flush(); } catch (Exception e) { + * logger.error("读取文件异常", e); } finally { if (out != null) { try { out.close(); + * } catch (IOException e) { logger.error("IO异常", e); } } if (in != null) { try + * { in.close(); } catch (IOException e) { logger.error("IO异常", e); } } } + */ + } +} diff --git a/luoo_user/src/main/java/com/luoo/user/dao/AppUpdateDao.java b/luoo_user/src/main/java/com/luoo/user/dao/AppUpdateDao.java new file mode 100644 index 0000000..a979a2d --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/dao/AppUpdateDao.java @@ -0,0 +1,11 @@ +package com.luoo.user.dao; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.JpaSpecificationExecutor; + +import com.luoo.user.pojo.AppUpdate; + +public interface AppUpdateDao extends JpaRepository, JpaSpecificationExecutor { + + //AppUpdate selectLatestUpdate(String appVersion, String deviceId); +} diff --git a/luoo_user/src/main/java/com/luoo/user/dto/response/AppUpdateDTO.java b/luoo_user/src/main/java/com/luoo/user/dto/response/AppUpdateDTO.java new file mode 100644 index 0000000..f90832d --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/dto/response/AppUpdateDTO.java @@ -0,0 +1,29 @@ +package com.luoo.user.dto.response; + +import java.util.List; + +import io.swagger.annotations.ApiModelProperty; +import lombok.Getter; +import lombok.Setter; + + +@Getter +@Setter +public class AppUpdateDTO{ + @ApiModelProperty(value = "ID") + private String id; + + /** + * 版本号 + */ + @ApiModelProperty(value = "版本号") + private String version; + + /** + * 更新描述 + */ + @ApiModelProperty(value = "更新描述") + private List updateList; + @ApiModelProperty(value = "大小") + private Long size; +} diff --git a/luoo_user/src/main/java/com/luoo/user/pojo/AppUpdate.java b/luoo_user/src/main/java/com/luoo/user/pojo/AppUpdate.java new file mode 100644 index 0000000..87d328c --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/pojo/AppUpdate.java @@ -0,0 +1,134 @@ +package com.luoo.user.pojo; + +import com.fasterxml.jackson.annotation.JsonFormat; + +import enums.DateTimePatternEnum; +import util.DateUtil; +import util.StringTools; + +import org.springframework.format.annotation.DateTimeFormat; + +import java.io.Serializable; +import java.util.Date; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + + +@Entity +@Table(name="tb_app_update") +public class AppUpdate implements Serializable { + private static final long serialVersionUID = 3346259101374338244L; + /** + * ID + */ + @Id + private String id; + + /** + * 版本号 + */ + private String version; + + /** + * 更新描述 + */ + private String updateDesc; + + /** + * 更新类型0:全更新 1:局部热更新 + */ + private Integer updateType; + + /** + * 创建时间 + */ + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") + private Date createTime; + + /** + * 0:未发布 1:灰度发布 2:全网发布 + */ + private Integer status; + + /** + * 灰度设备ID + */ + private String grayscaleDevice; + + private String[] updateDescArray; + + public String[] getUpdateDescArray() { + if (!StringTools.isEmpty(updateDesc)) { + return updateDesc.split("\\|"); + } + return updateDescArray; + } + + public void setUpdateDescArray(String[] updateDescArray) { + this.updateDescArray = updateDescArray; + } + + public void setId(String id) { + this.id = id; + } + + public String getId() { + return this.id; + } + + public void setVersion(String version) { + this.version = version; + } + + public String getVersion() { + return this.version; + } + + public void setUpdateDesc(String updateDesc) { + this.updateDesc = updateDesc; + } + + public String getUpdateDesc() { + return this.updateDesc; + } + + public void setUpdateType(Integer updateType) { + this.updateType = updateType; + } + + public Integer getUpdateType() { + return this.updateType; + } + + public void setCreateTime(Date createTime) { + this.createTime = createTime; + } + + public Date getCreateTime() { + return this.createTime; + } + + public void setStatus(Integer status) { + this.status = status; + } + + public Integer getStatus() { + return this.status; + } + + public void setGrayscaleDevice(String grayscaleDevice) { + this.grayscaleDevice = grayscaleDevice; + } + + public String getGrayscaleDevice() { + return this.grayscaleDevice; + } + + @Override + public String toString() { + return "自增ID:" + (id == null ? "空" : id) + ",版本号:" + (version == null ? "空" : version) + ",更新描述:" + (updateDesc == null ? "空" : updateDesc) + ",更新类型0:全更新 1:局部热更新:" + (updateType == null ? "空" : updateType) + ",创建时间:" + (createTime == null ? "空" : DateUtil.format(createTime, DateTimePatternEnum.YYYY_MM_DD_HH_MM_SS.getPattern())) + ",0:未发布 1:灰度发布 2:全网发布:" + (status == null ? "空" : status) + ",灰度设备ID:" + (grayscaleDevice == null ? "空" : grayscaleDevice); + } +} diff --git a/luoo_user/src/main/java/com/luoo/user/service/AppUpdateService.java b/luoo_user/src/main/java/com/luoo/user/service/AppUpdateService.java new file mode 100644 index 0000000..a9a87d4 --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/service/AppUpdateService.java @@ -0,0 +1,157 @@ +package com.luoo.user.service; + +import java.io.File; +import java.io.IOException; +import java.util.Date; +import java.util.List; + +import javax.annotation.Resource; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.multipart.MultipartFile; + +import com.luoo.user.dao.AppUpdateDao; +import com.luoo.user.pojo.AppUpdate; + +@Service +public class AppUpdateService { + + @Autowired + private AppUpdateDao appUpdateDao; + + /** + * 根据条件查询列表 + */ + /* + * @Override public List findListByParam(AppUpdateQuery param) { + * return this.appUpdateDao.selectList(param); } + * + *//** + * 根据条件查询列表 + */ + /* + * @Override public Integer findCountByParam(AppUpdateQuery param) { return + * this.appUpdateDao.selectCount(param); } + * + *//** + * 分页查询方法 + */ + /* + * @Override public PaginationResultVO findListByPage(AppUpdateQuery + * param) { int count = this.findCountByParam(param); int pageSize = + * param.getPageSize() == null ? PageSize.SIZE15.getSize() : + * param.getPageSize(); + * + * SimplePage page = new SimplePage(param.getPageNo(), count, pageSize); + * param.setSimplePage(page); List list = + * this.findListByParam(param); PaginationResultVO result = new + * PaginationResultVO(count, page.getPageSize(), page.getPageNo(), + * page.getPageTotal(), list); return result; } + * + *//** + * 新增 + */ + /* + * @Override public Integer add(AppUpdate bean) { return + * this.appUpdateDao.insert(bean); } + * + *//** + * 批量新增 + */ + /* + * @Override public Integer addBatch(List listBean) { if (listBean == + * null || listBean.isEmpty()) { return 0; } return + * this.appUpdateDao.insertBatch(listBean); } + * + *//** + * 批量新增或者修改 + */ + /* + * @Override public Integer addOrUpdateBatch(List listBean) { if + * (listBean == null || listBean.isEmpty()) { return 0; } return + * this.appUpdateDao.insertOrUpdateBatch(listBean); } + * + *//** + * 多条件更新 + */ + /* + * @Override public Integer updateByParam(AppUpdate bean, AppUpdateQuery param) + * { StringTools.checkParam(param); return this.appUpdateDao.updateByParam(bean, + * param); } + * + *//** + * 多条件删除 + */ + /* + * @Override public Integer deleteByParam(AppUpdateQuery param) { + * StringTools.checkParam(param); return this.appUpdateDao.deleteByParam(param); + * } + * + *//** + * 根据Id获取对象 + */ + /* + * @Override public AppUpdate getAppUpdateById(Integer id) { return + * this.appUpdateDao.selectById(id); } + * + *//** + * 根据Id修改 + */ + + /* + * @Override public Integer updateAppUpdateById(AppUpdate bean, Integer id) { + * return this.appUpdateDao.updateById(bean, id); } + * + *//** + * 根据Id删除 + *//* + * @Override public Integer deleteAppUpdateById(Integer id) { return + * this.appUpdateDao.deleteById(id); } + * + * @Override + * + * @Transactional(rollbackFor = Exception.class) public void + * saveUpdate(AppUpdate appUpdate, MultipartFile file) { AppUpdateQuery + * updateQuery = new AppUpdateQuery(); updateQuery.setOrderBy("id desc"); + * updateQuery.setSimplePage(new SimplePage(0, 1)); List + * appUpdateList = appUpdateDao.selectList(updateQuery); if + * (!appUpdateList.isEmpty()) { AppUpdate latest = appUpdateList.get(0); Long + * dbVerion = Long.parseLong(latest.getVersion().replace(".", "")); Long + * currentVersion = Long.parseLong(appUpdate.getVersion().replace(".", "")); if + * (appUpdate.getId() == null && currentVersion <= dbVerion) { throw new + * BusinessException("当前版本必须大于历史版本"); } if (appUpdate.getId() != null && + * currentVersion <= dbVerion && + * !appUpdate.getVersion().equals(latest.getVersion())) { throw new + * BusinessException("当前版本必须大于历史版本"); } } if (appUpdate.getId() == null) { + * appUpdate.setCreateTime(new Date()); + * appUpdate.setStatus(AppUpdateSatusEnum.INIT.getStatus()); + * appUpdateDao.insert(appUpdate); } else { appUpdate.setStatus(null); + * appUpdate.setGrayscaleDevice(null); appUpdateDao.updateById(appUpdate, + * appUpdate.getId()); } if (file != null) { File folder = new + * File(appConfig.getProjectFolder() + Constants.APP_UPDATE_FOLDER); if + * (!folder.exists()) { folder.mkdirs(); } AppUpdateTypeEnum typeEnum = + * AppUpdateTypeEnum.getByType(appUpdate.getUpdateType()); try { + * file.transferTo(new File(folder.getAbsolutePath() + "/" + appUpdate.getId() + + * typeEnum.getSuffix())); } catch (IOException e) { throw new + * BusinessException("更新App失败"); } } } + * + * @Override public void postUpdate(Integer id, Integer status, String + * grayscaleDevice) { AppUpdateSatusEnum satusEnum = + * AppUpdateSatusEnum.getByStatus(status); if (status == null) { throw new + * BusinessException(ResponseCodeEnum.CODE_600); } if + * (AppUpdateSatusEnum.GRAYSCALE == satusEnum && + * StringTools.isEmpty(grayscaleDevice)) { throw new + * BusinessException(ResponseCodeEnum.CODE_600); } if + * (AppUpdateSatusEnum.GRAYSCALE != satusEnum) { grayscaleDevice = ""; } + * AppUpdate update = new AppUpdate(); update.setStatus(status); + * update.setGrayscaleDevice(grayscaleDevice); appUpdateDao.updateById(update, + * id); } + */ + + public AppUpdate getLatestUpdate(String appVersion, String deviceId) { + //return appUpdateDao.selectLatestUpdate(appVersion, deviceId); + return null; + } +}