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 index a979a2d..d86778c 100644 --- a/luoo_user/src/main/java/com/luoo/user/dao/AppUpdateDao.java +++ b/luoo_user/src/main/java/com/luoo/user/dao/AppUpdateDao.java @@ -1,11 +1,43 @@ package com.luoo.user.dao; +import com.luoo.user.pojo.AppUpdate; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Sort; +import org.springframework.data.jpa.domain.Specification; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; -import com.luoo.user.pojo.AppUpdate; +import javax.persistence.criteria.Expression; +import javax.persistence.criteria.Predicate; +import java.util.ArrayList; +import java.util.List; public interface AppUpdateDao extends JpaRepository, JpaSpecificationExecutor { - //AppUpdate selectLatestUpdate(String appVersion, String deviceId); + default AppUpdate selectLatestUpdate(String appVersion, String deviceId){ + Specification appUpdateSpecification = createAppUpdateSpecification(appVersion, deviceId); + Sort createTimeDescSort = Sort.by(Sort.Direction.DESC, "createTime"); + Page appUpdatePage = this.findAll(appUpdateSpecification, PageRequest.of(0, 1, createTimeDescSort)); + if(appUpdatePage.getContent().isEmpty()){ + return null; + } + return appUpdatePage.getContent().get(0); + } + + default Specification createAppUpdateSpecification(String appVersion, String deviceId) { + return (root, criteriaQuery, criteriaBuilder) -> { + List predicateList = new ArrayList<>(); + predicateList.add(criteriaBuilder.greaterThan(root.get("version"), appVersion)); + Predicate allPublishPredicate = criteriaBuilder.equal(root.get("status"), 2); + Predicate grayPublishPredicate = criteriaBuilder.equal(root.get("status"), 1); + Expression grayDeviceExpress = criteriaBuilder + .function("find_in_set", Integer.class, criteriaBuilder.literal(deviceId), root.get("grayscaleDevice")); + Predicate grayDevicePredicate = criteriaBuilder.gt(grayDeviceExpress, 0); + Predicate grayScalePredicate = criteriaBuilder.and(grayPublishPredicate, grayDevicePredicate); + predicateList.add(criteriaBuilder.or(allPublishPredicate, grayScalePredicate)); + Predicate[] predicates = new Predicate[predicateList.size()]; + return criteriaBuilder.and(predicateList.toArray(predicates)); + }; + } } 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 index 87d328c..946d028 100644 --- a/luoo_user/src/main/java/com/luoo/user/pojo/AppUpdate.java +++ b/luoo_user/src/main/java/com/luoo/user/pojo/AppUpdate.java @@ -11,9 +11,7 @@ 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; +import javax.persistence.*; @Entity @@ -58,6 +56,7 @@ public class AppUpdate implements Serializable { */ private String grayscaleDevice; + @Transient private String[] updateDescArray; public String[] getUpdateDescArray() { 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 index a9a87d4..26f27a6 100644 --- a/luoo_user/src/main/java/com/luoo/user/service/AppUpdateService.java +++ b/luoo_user/src/main/java/com/luoo/user/service/AppUpdateService.java @@ -151,7 +151,6 @@ public class AppUpdateService { */ public AppUpdate getLatestUpdate(String appVersion, String deviceId) { - //return appUpdateDao.selectLatestUpdate(appVersion, deviceId); - return null; + return appUpdateDao.selectLatestUpdate(appVersion, deviceId); } } diff --git a/luoo_user/src/main/resources/sql/domain.sql b/luoo_user/src/main/resources/sql/domain.sql new file mode 100644 index 0000000..4943f3e --- /dev/null +++ b/luoo_user/src/main/resources/sql/domain.sql @@ -0,0 +1,14 @@ +-- 创建表 +drop table if exists indie_user.tb_app_update; +create table if not exists indie_user.tb_app_update ( + id varchar(100) not null comment 'ID' primary key, + version varchar(10) default '' not null comment '版本号', + update_desc varchar(500) default '' not null comment '更新描述', + update_type tinyint(1) default 0 not null comment '更新类型(0=全更新, 1=局部热更新)', + status tinyint(1) default 1 not null comment '状态(0=未发布, 1=灰度发布, 2=全网发布)', + grayscale_device varchar(4000) default '' not null comment '灰度设备ID集合(英文逗号相隔)', + create_time datetime default CURRENT_TIMESTAMP not null comment '创建时间' +) comment '应用版本更新记录表'; + +create index idx_version on indie_user.tb_app_update (version); +create index idx_create_time on indie_user.tb_app_update (create_time); \ No newline at end of file