版本管理后端II

功能:

  1. 获取版本更新列表
  2. 创建发布更新版本
  3. 删除发布版本
  4. 发布新版本
  5. 检查更新

SQL准备

CREATE TABLE `app_update` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增id',
  `version` varchar(10) DEFAULT NULL COMMENT '版本号',
  `update_desc` varchar(500) DEFAULT NULL COMMENT '更新描述',
  `create_time` datetime DEFAULT NULL COMMENT '创建时间',
  `status` tinyint(1) DEFAULT NULL COMMENT '0:未发布 1:灰度发布 2:全网发布',
  `grayscale_uid` varchar(1000) DEFAULT NULL COMMENT '灰度uid',
  `file_type` tinyint(1) DEFAULT NULL COMMENT '文件类型0:本地文件 1:外链',
  `outer_link` varchar(200) DEFAULT NULL COMMENT '外链地址',
  PRIMARY KEY (`id`),
  UNIQUE KEY `idx_key` (`version`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COMMENT='app发布更新版本表';

AppUpdate的po

public class AppUpdate implements Serializable {


    /**
     * 自增ID
     */
    private Integer id;

    /**
     * 版本号
     */
    private String version;

    /**
     * 更新描述
     */
    private String updateDesc;

    /**
     * 创建时间
     */
    @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;

    /**
     * 灰度uid
     */
    private String grayscaleUid;

    /**
     * 文件类型0:本地文件 1:外链
     */
    private Integer fileType;

    /**
     * 外链地址
     */
    private String outerLink;

    private String[] updateDescArray;

    //更新内容数组,因为有多条更新内容,get时候创建数据获取内容既可
    public String[] getUpdateDescArray() {
        if (!StringTools.isEmpty(updateDesc)) {
            return updateDesc.split("\\|");
        }
        return updateDescArray;
    }

    public void setUpdateDescArray(String[] updateDescArray) {
        this.updateDescArray = updateDescArray;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer 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 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 setGrayscaleUid(String grayscaleUid) {
        this.grayscaleUid = grayscaleUid;
    }

    public String getGrayscaleUid() {
        return this.grayscaleUid;
    }

    public void setFileType(Integer fileType) {
        this.fileType = fileType;
    }

    public Integer getFileType() {
        return this.fileType;
    }

    public void setOuterLink(String outerLink) {
        this.outerLink = outerLink;
    }

    public String getOuterLink() {
        return this.outerLink;
    }

    @Override
    public String toString() {
        return "自增ID:" + (id == null ? "空" : id) + ",版本号:" + (version == null ? "空" : version) + ",更新描述:" + (updateDesc == null ? "空" : updateDesc) + ",创建时间:" + (createTime == null ? "空" : DateUtils.format(createTime, DateTimePatternEnum.YYYY_MM_DD_HH_MM_SS.getPattern())) + ",0:未发布 1:灰度发布 2:全网发布:" + (status == null ? "空" : status) + ",灰度uid:" + (grayscaleUid == null ? "空" : grayscaleUid) + ",文件类型0:本地文件 1:外链:" + (fileType == null ? "空" : fileType) + ",外链地址:" + (outerLink == null ? "空" : outerLink);
    }
}

AppUpdateQuery的query

public class AppUpdateQuery extends BaseQuery {


    /**
     * 自增ID
     */
    private Integer id;

    /**
     * 版本号
     */
    private String version;

    private String versionFuzzy;

    /**
     * 更新描述
     */
    private String updateDesc;

    private String updateDescFuzzy;

    /**
     * 创建时间
     */
    private String createTime;

    private String createTimeStart;

    private String createTimeEnd;

    /**
     * 0:未发布 1:灰度发布 2:全网发布
     */
    private Integer status;

    /**
     * 灰度uid
     */
    private String grayscaleUid;

    private String grayscaleUidFuzzy;

    /**
     * 文件类型0:本地文件 1:外链
     */
    private Integer fileType;

    /**
     * 外链地址
     */
    private String outerLink;

    private String outerLinkFuzzy;


    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getId() {
        return this.id;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getVersion() {
        return this.version;
    }

    public void setVersionFuzzy(String versionFuzzy) {
        this.versionFuzzy = versionFuzzy;
    }

    public String getVersionFuzzy() {
        return this.versionFuzzy;
    }

    public void setUpdateDesc(String updateDesc) {
        this.updateDesc = updateDesc;
    }

    public String getUpdateDesc() {
        return this.updateDesc;
    }

    public void setUpdateDescFuzzy(String updateDescFuzzy) {
        this.updateDescFuzzy = updateDescFuzzy;
    }

    public String getUpdateDescFuzzy() {
        return this.updateDescFuzzy;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }

    public String getCreateTime() {
        return this.createTime;
    }

    public void setCreateTimeStart(String createTimeStart) {
        this.createTimeStart = createTimeStart;
    }

    public String getCreateTimeStart() {
        return this.createTimeStart;
    }

    public void setCreateTimeEnd(String createTimeEnd) {
        this.createTimeEnd = createTimeEnd;
    }

    public String getCreateTimeEnd() {
        return this.createTimeEnd;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public Integer getStatus() {
        return this.status;
    }

    public void setGrayscaleUid(String grayscaleUid) {
        this.grayscaleUid = grayscaleUid;
    }

    public String getGrayscaleUid() {
        return this.grayscaleUid;
    }

    public void setGrayscaleUidFuzzy(String grayscaleUidFuzzy) {
        this.grayscaleUidFuzzy = grayscaleUidFuzzy;
    }

    public String getGrayscaleUidFuzzy() {
        return this.grayscaleUidFuzzy;
    }

    public void setFileType(Integer fileType) {
        this.fileType = fileType;
    }

    public Integer getFileType() {
        return this.fileType;
    }

    public void setOuterLink(String outerLink) {
        this.outerLink = outerLink;
    }

    public String getOuterLink() {
        return this.outerLink;
    }

    public void setOuterLinkFuzzy(String outerLinkFuzzy) {
        this.outerLinkFuzzy = outerLinkFuzzy;
    }

    public String getOuterLinkFuzzy() {
        return this.outerLinkFuzzy;
    }

}

mapper接口AppUpdateMapper

public interface AppUpdateMapper<T, P> extends BaseMapper<T, P> {

    /**
     * 根据Id更新
     */
    Integer updateById(@Param("bean") T t, @Param("id") Integer id);


    /**
     * 根据Id删除
     */
    Integer deleteById(@Param("id") Integer id);


    /**
     * 根据Id获取对象
     */
    T selectById(@Param("id") Integer id);


    /**
     * 根据Version更新
     */
    Integer updateByVersion(@Param("bean") T t, @Param("version") String version);


    /**
     * 根据Version删除
     */
    Integer deleteByVersion(@Param("version") String version);


    /**
     * 根据Version获取对象
     */
    T selectByVersion(@Param("version") String version);

    T selectLatestUpdate(@Param("version") String appVersion, @Param("uid") String uid);
}

xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
      "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xh.mapper.AppUpdateMapper">
<!--实体映射-->
   <resultMap id="base_result_map" type="com.xh.entity.po.AppUpdate">
      <!-- 自增id-->
      <id column="id" property="id"/>
      <!-- 版本号-->
      <result column="version" property="version"/>
      <!-- 更新描述-->
      <result column="update_desc" property="updateDesc"/>
      <!-- 创建时间-->
      <result column="create_time" property="createTime"/>
      <!-- 0:未发布 1:灰度发布 2:全网发布-->
      <result column="status" property="status"/>
      <!-- 灰度uid-->
      <result column="grayscale_uid" property="grayscaleUid"/>
      <!-- 文件类型0:本地文件 1:外链-->
      <result column="file_type" property="fileType"/>
      <!-- 外链地址-->
      <result column="outer_link" property="outerLink"/>
   </resultMap>
   <!--通用查询结果列-->
   <sql id="base_column_list">
      id,version,update_desc,create_time,status,grayscale_uid,file_type,outer_link
   </sql>

   <!--基础查询条件-->
   <sql id="base_query_condition">
      <if test="query.id != null">
         and id = #{query.id}
      </if>
      <if test="query.version != null and query.version!=''">
         and version = #{query.version}
      </if>
      <if test="query.updateDesc != null and query.updateDesc!=''">
         and update_desc = #{query.updateDesc}
      </if>
      <if test="query.createTime != null">
         and create_time = #{query.createTime}
      </if>
      <if test="query.status != null">
         and status = #{query.status}
      </if>
      <if test="query.grayscaleUid != null and query.grayscaleUid!=''">
         and grayscale_uid = #{query.grayscaleUid}
      </if>
      <if test="query.fileType != null">
         and file_type = #{query.fileType}
      </if>
      <if test="query.outerLink != null and query.outerLink!=''">
         and outer_link = #{query.outerLink}
      </if>
   </sql>

   <!--扩展的查询条件-->
   <sql id="base_query_condition_extend">
      <if test="query.versionFuzzy != null and query.versionFuzzy !=''">
          and version like concat('%',#{query.versionFuzzy},'%')
      </if>
      <if test="query.updateDescFuzzy != null and query.updateDescFuzzy !=''">
          and update_desc like concat('%',#{query.updateDescFuzzy},'%')
      </if>
      <if test="query.createTimeStart != null and query.createTimeStart !=''">
          <![CDATA[ and   create_time >= str_to_date(#{query.createTimeStart}, '%Y-%m-%d') ]]>
      </if>
      <if test="query.createTimeEnd != null and query.createTimeEnd !=''">
          <![CDATA[ and   create_time < date_sub(str_to_date(#{query.createTimeEnd}, '%Y-%m-%d'), interval - 1 day ) ]]>
      </if>
      <if test="query.grayscaleUidFuzzy != null and query.grayscaleUidFuzzy !=''">
          and grayscale_uid like concat('%',#{query.grayscaleUidFuzzy},'%')
      </if>
      <if test="query.outerLinkFuzzy != null and query.outerLinkFuzzy !=''">
          and outer_link like concat('%',#{query.outerLinkFuzzy},'%')
      </if>
   </sql>

   <!--扩展的查询条件-->
   <sql id="query_condition">
      <where>
         <include refid="base_query_condition"/>
         <include refid="base_query_condition_extend"/>
      </where>
   </sql>

   <!--查询列表-->
   <select id="selectList" resultMap="base_result_map">
      SELECT <include refid="base_column_list"/> FROM app_udate <include refid="query_condition"/>
      <if test="query.orderBy!=null">order by ${query.orderBy}</if>
      <if test="query.simplePage!=null">limit #{query.simplePage.start},#{query.simplePage.end}</if>
   </select>

   <!--查询数量-->
   <select id="selectCount" resultType="java.lang.Integer">
      SELECT count(1) FROM app_udate <include refid="query_condition"/>
   </select>

   <!--插入 (匹配有值的字段)-->
    <insert id="insert" parameterType="com.xh.entity.po.AppUpdate">
      <selectKey keyProperty="bean.id" resultType="Integer" order="AFTER">
         SELECT LAST_INSERT_ID()
      </selectKey>
      INSERT INTO app_udate
      <trim prefix="(" suffix=")" suffixOverrides=",">
         <if test="bean.id != null">
            id,
         </if>
         <if test="bean.version != null">
            version,
         </if>
         <if test="bean.updateDesc != null">
            update_desc,
         </if>
         <if test="bean.createTime != null">
            create_time,
         </if>
         <if test="bean.status != null">
            status,
         </if>
         <if test="bean.grayscaleUid != null">
            grayscale_uid,
         </if>
         <if test="bean.fileType != null">
            file_type,
         </if>
         <if test="bean.outerLink != null">
            outer_link,
         </if>
      </trim>
      <trim prefix="values (" suffix=")" suffixOverrides=",">
         <if test="bean.id!=null">
            #{bean.id},
         </if>
         <if test="bean.version!=null">
            #{bean.version},
         </if>
         <if test="bean.updateDesc!=null">
            #{bean.updateDesc},
         </if>
         <if test="bean.createTime!=null">
            #{bean.createTime},
         </if>
         <if test="bean.status!=null">
            #{bean.status},
         </if>
         <if test="bean.grayscaleUid!=null">
            #{bean.grayscaleUid},
         </if>
         <if test="bean.fileType!=null">
            #{bean.fileType},
         </if>
         <if test="bean.outerLink!=null">
            #{bean.outerLink},
         </if>
      </trim>
   </insert>
   <!--插入或者更新(匹配有值的字段)-->
    <insert id="insertOrUpdate" parameterType="com.xh.entity.po.AppUpdate">
      INSERT INTO app_udate
      <trim prefix="(" suffix=")" suffixOverrides=",">
         <if test="bean.id != null">
            id,
         </if>
         <if test="bean.version != null">
            version,
         </if>
         <if test="bean.updateDesc != null">
            update_desc,
         </if>
         <if test="bean.createTime != null">
            create_time,
         </if>
         <if test="bean.status != null">
            status,
         </if>
         <if test="bean.grayscaleUid != null">
            grayscale_uid,
         </if>
         <if test="bean.fileType != null">
            file_type,
         </if>
         <if test="bean.outerLink != null">
            outer_link,
         </if>
      </trim>
      <trim prefix="values (" suffix=")" suffixOverrides=",">
         <if test="bean.id!=null">
            #{bean.id},
         </if>
         <if test="bean.version!=null">
            #{bean.version},
         </if>
         <if test="bean.updateDesc!=null">
            #{bean.updateDesc},
         </if>
         <if test="bean.createTime!=null">
            #{bean.createTime},
         </if>
         <if test="bean.status!=null">
            #{bean.status},
         </if>
         <if test="bean.grayscaleUid!=null">
            #{bean.grayscaleUid},
         </if>
         <if test="bean.fileType!=null">
            #{bean.fileType},
         </if>
         <if test="bean.outerLink!=null">
            #{bean.outerLink},
         </if>
      </trim>
      on DUPLICATE key update
      <trim prefix="" suffix="" suffixOverrides=",">
         <if test="bean.version!=null">
            version = VALUES(version),
         </if>
         <if test="bean.updateDesc!=null">
            update_desc = VALUES(update_desc),
         </if>
         <if test="bean.createTime!=null">
            create_time = VALUES(create_time),
         </if>
         <if test="bean.status!=null">
            status = VALUES(status),
         </if>
         <if test="bean.grayscaleUid!=null">
            grayscale_uid = VALUES(grayscale_uid),
         </if>
         <if test="bean.fileType!=null">
            file_type = VALUES(file_type),
         </if>
         <if test="bean.outerLink!=null">
            outer_link = VALUES(outer_link),
         </if>
      </trim>
   </insert>
   <!--添加(批量插入)-->
   <insert id="insertBatch" parameterType="com.xh.entity.po.AppUpdate">
      INSERT INTO app_udate(version,update_desc,create_time,status,grayscale_uid,file_type,outer_link)values
      <foreach collection="list" item="item" separator=",">
         (#{item.version},#{item.updateDesc},#{item.createTime},#{item.status},#{item.grayscaleUid},#{item.fileType},#{item.outerLink})
      </foreach>
   </insert>

   <!--批量插入或更新 (批量插入)-->
   <insert id="insertOrUpdateBatch" parameterType="com.xh.entity.po.AppUpdate">
      INSERT INTO app_udate(version,update_desc,create_time,status,grayscale_uid,file_type,outer_link)values
      <foreach collection="list" item="item" separator=",">
         (#{item.version},#{item.updateDesc},#{item.createTime},#{item.status},#{item.grayscaleUid},#{item.fileType},#{item.outerLink})
      </foreach>
      on DUPLICATE key update id = VALUES(id),version = VALUES(version),update_desc = VALUES(update_desc),create_time = VALUES(create_time),status = VALUES(status),grayscale_uid = VALUES(grayscale_uid),file_type = VALUES(file_type),outer_link = VALUES(outer_link)
   </insert>
   <!--根据Id查询-->
   <select id="selectById" resultMap="base_result_map">
      select <include refid="base_column_list"/>  from app_udate where id=#{id}
   </select>

   <!--根据Id更新-->
   <update id="updateById" parameterType="com.xh.entity.po.AppUpdate">
      update app_udate
      <set>
         <if test="bean.id != null">
            id = #{bean.id},
         </if>
         <if test="bean.version != null">
            version = #{bean.version},
         </if>
         <if test="bean.updateDesc != null">
            update_desc = #{bean.updateDesc},
         </if>
         <if test="bean.createTime != null">
            create_time = #{bean.createTime},
         </if>
         <if test="bean.status != null">
            status = #{bean.status},
         </if>
         <if test="bean.grayscaleUid != null">
            grayscale_uid = #{bean.grayscaleUid},
         </if>
         <if test="bean.fileType != null">
            file_type = #{bean.fileType},
         </if>
         <if test="bean.outerLink != null">
            outer_link = #{bean.outerLink},
         </if>
      </set>
      where id=#{id}
   </update>

   <!--根据Id删除-->
   <delete id="deleteById">
      delete from app_udate where id=#{id}
   </delete>


</mapper>

枚举

  • 发布枚举类型

    public enum AppUpdateStatusEnum {
        INIT(0, "未发布"), GRAYSCALE(1, "灰度发布"), ALL(2, "全网发布");
    
        private Integer status;
        private String description;
    
        AppUpdateStatusEnum(int status, String description) {
            this.status = status;
            this.description = description;
        }
    
        public Integer getStatus() {
            return status;
        }
    
        public String getDescription() {
            return description;
        }
    
        public static AppUpdateStatusEnum getByStatus(Integer status) {
            for (AppUpdateStatusEnum at : AppUpdateStatusEnum.values()) {
                if (at.status.equals(status)) {
                    return at;
                }
            }
            return null;
        }
    }

  • 上传版本类型枚举

public enum AppUpdateFileTypeEnum {
    LOCAL(0, "本地"), OUTER_LINK(1, "外链");

    private Integer type;
    private String description;

    AppUpdateFileTypeEnum(int type, String description) {
        this.type = type;
        this.description = description;
    }

    public Integer getType() {
        return type;
    }

    public String getDescription() {
        return description;
    }

    public static AppUpdateFileTypeEnum getByType(Integer type) {
        for (AppUpdateFileTypeEnum at : AppUpdateFileTypeEnum.values()) {
            if (at.type.equals(type)) {
                return at;
            }
        }
        return null;
    }
}

Constants常量定义

public class Constants {

    //存储文件版本地址
    public static final String APP_UPDATE_FOLDER = "/app/";

    public static final String APP_EXE_SUFFIX  = ".exe";
}

获取版本更新列表

  • 接口

/**
 * 分页查询
 */
PaginationResultVo<AppUpdate> findListByPage(AppUpdateQuery param);
  • 实现

/**
 * 根据条件查询列表
 */
@Override
public List<AppUpdate> findListByParam(AppUpdateQuery param) {
    return this.appUpdateMapper.selectList(param);
 }

@Override
public PaginationResultVo<AppUpdate> 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<AppUpdate> list = this.findListByParam(param);
    PaginationResultVo<AppUpdate> result = new PaginationResultVo(count, page.getPageSize(), page.getPageNo(), page.getPageTotal(), list);
    return result;
}
  • 调用

/**
 * 获取更新列表
 * @param
 * @return
 */
@RequestMapping("/loadUpdateList")
@GlobalInterceptor(checkAdmin = true)
public ResponseVo loadUpdateList(AppUpdateQuery query){
    query.setOrderBy("id desc");
    //分页查询
    return getSuccessResponseVo(appUpdateService.findListByPage(query));
}

创建发布更新版本

思路:

  1. 发布更新版本有两个方法分别是本地文件、外部链接
  2. 判断文件类型是否为空、更新版本对象的自增长是否为空,不为空则直接从数据库获取id,再判断发布状态是否是未发布才可以进行版本内容修改
  3. version倒序排拿到最新版本,取第一条数据
  4. 判断发布的版本号是否存在,并且输入的版本号要大于最新版本
  5. 如果版本存在拿到第一条数据,再根据版本号切割是例:1.0 2.0 根据.切割(需切割数据库的version和传入的version)进行比较
  6. 判断一(版本id是否存在,并且版本不能小于数据库的历史版本)、判断二(如果id版本存在并且版本大于历史版本,传入版本对象的id是否等于第一条数据的id)、判断三(获取传入的版本对象的版本,判断版本在数据库是否存在)
  7. 如果版本对象的自增id为空,封装版本创建时间、默认发布状态,增加到数据库中;否则将发布状态置空(默认不能修改)以及灰度uid置空(默认不能修改)再修改数据库
  8. 如果传入的是版本文件,创建文件名,如果文件名为空则,创建文件将加文件名后缀.exe
  • 接口

//发布更新版本
void saveUpdate(AppUpdate appUpdate , MultipartFile file) throws BusinessException, IOException;
  • 实现

/**
 * 发布更新
 * @param appUpdate 版本对象
 * @param file 文件
 */
@Override
public void saveUpdate(AppUpdate appUpdate, MultipartFile file) throws BusinessException, IOException {
    //文件类型 : 文件/外部链接
    AppUpdateFileTypeEnum fileTypeEnum = AppUpdateFileTypeEnum.getByType(appUpdate.getFileType());
    if (fileTypeEnum == null){
        throw new BusinessException(ResponseCodeEnum.CODE_600);
    }
    if (appUpdate.getId() != null) {
        AppUpdate dbInfo = this.getAppUpdateById(appUpdate.getId());
        //未发布才可以修改
        if (!AppUpdateStatusEnum.INIT.getStatus().equals(dbInfo.getStatus())) {
            throw new BusinessException(ResponseCodeEnum.CODE_600);
        }
    }
    //判断发布的版本号是否存在,并且输入的版本号要大于最新版本
    AppUpdateQuery updateQuery = new AppUpdateQuery();
    updateQuery.setOrderBy("id desc");//version倒序排拿到最新版本
    updateQuery.setSimplePage(new SimplePage(0,1));//取第一条
    List<AppUpdate> appUpdateList = appUpdateMapper.selectList(updateQuery);
    if (!appUpdateList.isEmpty()){
        AppUpdate lastest = appUpdateList.get(0);//拿到第一条数据
        //版本号是1.0 2.0 根据.切割
        //数据库的version
        Long dbVersion = Long.parseLong(lastest.getVersion().replace(".",""));
        //传入的version
        Long currentVersion = Long.parseLong(appUpdate.getVersion().replace(".",""));
        //进行比较
        if (appUpdate.getId() == null && currentVersion <= dbVersion) {
            throw new BusinessException("当前版本必须大于历史版本");
        }
        if (appUpdate.getId() != null && currentVersion >= dbVersion && !appUpdate.getId().equals(lastest.getId())) {
            throw new BusinessException("当前版本必须大于历史版本");
        }
        //判断版本在数据库是否存在
        AppUpdate versionDb = appUpdateMapper.selectByVersion(appUpdate.getVersion());
        if (appUpdate.getId() != null && versionDb != null && !versionDb.getId().equals(appUpdate.getId())) {
            throw new BusinessException("版本号已存在");
        }
    }
    if (appUpdate.getId() == null){
        appUpdate.setCreateTime(new Date());
        //默认未发布
        appUpdate.setStatus(AppUpdateStatusEnum.INIT.getStatus());
        appUpdateMapper.insert(appUpdate);
    }else {
        appUpdate.setStatus(null);//不能修改发布状态
        appUpdate.setGrayscaleUid(null);//不能修改灰度uid
        appUpdateMapper.updateById(appUpdate,appUpdate.getId());
    }
    if (file != null){
        File folder = new File(appConfig.getProjectFolder() + Constants.APP_UPDATE_FOLDER);
        if (!folder.exists()){
            folder.mkdirs();
        }
        //文件名后缀.exe
        file.transferTo(new File(folder.getAbsolutePath() + "/" + appUpdate.getId() + Constants.APP_EXE_SUFFIX));
    }
}
  • 调用

/**
 *发布更新版本
 * @param id
 * @param version 版本号
 * @param updateDesc 更新内容
 * @param fileType 文件类型
 * @param outerLink 外部链接(非必填)
 * @param file 文件(非必填)
 * @return
 */
@RequestMapping("/saveUpdate")
@GlobalInterceptor(checkAdmin = true)
public ResponseVo saveUpdate(Integer id,
                                 @NotEmpty String version,
                                 @NotEmpty Integer fileType,
                                 @NotEmpty String updateDesc,
                                 String outerLink,
                                 MultipartFile file) throws BusinessException, IOException {
    AppUpdate appUpdate = new AppUpdate();
    appUpdate.setId(id);
    appUpdate.setVersion(version);
    appUpdate.setUpdateDesc(updateDesc);
    appUpdate.setFileType(fileType);
    appUpdate.setOuterLink(outerLink);
    appUpdateService.saveUpdate(appUpdate,file);
    return getSuccessResponseVo(null);
}

删除发布版本

  • 接口

/**
 * 根据Id删除
 */
Integer deleteAppUpdateById(Integer id) throws BusinessException;
  • 实现

/**
 * 根据Id删除
 */
@Override
public Integer deleteAppUpdateById(Integer id) throws BusinessException {
    AppUpdate dbInfo = this.getAppUpdateById(id);
    if (!AppUpdateStatusEnum.INIT.getStatus().equals(dbInfo.getStatus())) {
        throw new BusinessException(ResponseCodeEnum.CODE_600);
    }
    return this.appUpdateMapper.deleteById(id);
}
  • 调用

//删除发布
@RequestMapping("/delUpdate")
@GlobalInterceptor(checkAdmin = true)
public ResponseVo delUpdate(@NotNull Integer id) throws BusinessException {
    appUpdateService.deleteAppUpdateById(id);
    return getSuccessResponseVo(null);
}

发布新版本

  • 接口

    //发布更新
    void postUpdate(Integer id, Integer status, String grayscaleUid) throws BusinessException;
  • 实现

    /**
     * 发布新版本
     * @param id
     * @param status 状态
     * @param grayscaleUid 灰色用户id
     */
    @Override
    public void postUpdate(Integer id, Integer status, String grayscaleUid) throws BusinessException {
        //获取发布状态枚举
        AppUpdateStatusEnum statusEnum = AppUpdateStatusEnum.getByStatus(status);
        if (status == null) {
            throw new BusinessException(ResponseCodeEnum.CODE_600);
        }
        //即使为灰度发布状态但是灰度用户uid不存在也抛出异常
        if (AppUpdateStatusEnum.GRAYSCALE == statusEnum && StringTools.isEmpty(grayscaleUid)) {
            throw new BusinessException(ResponseCodeEnum.CODE_600);
        }
        //不等于灰度状态,则将灰度用户uid置空
        if (AppUpdateStatusEnum.GRAYSCALE != statusEnum) {
            grayscaleUid = "";//全网发布
        }
        //封装状态和灰度用户
        //更新数据库
        AppUpdate update = new AppUpdate();
        update.setStatus(status);
        update.setGrayscaleUid(grayscaleUid);
        appUpdateMapper.updateById(update,id);
    }
  • 调用

/**
 * 发布新版本上线
 * @param id
 * @param status 状态
 * @param grayscaleUid 灰度用户
 * @return
 * @throws BusinessException
 */
@RequestMapping("/postUpdate")
@GlobalInterceptor(checkAdmin = true)
public ResponseVo postUpdate(@NotNull Integer id,@NotNull Integer status,String grayscaleUid) throws BusinessException {
    appUpdateService.postUpdate(id,status,grayscaleUid);
    return getSuccessResponseVo(null);
}

检查更新

  • 接口AppUpdateService

AppUpdate getLatestUpdate(String appVersion, String uid);
  • 调用

//检查更新
@Override
public AppUpdate getLatestUpdate(String appVersion, String uid) {
    //查询数据库中最新的版本号
    //判断最新的版本号发布是否是灰度发布
    //如果是灰度发布判断用户的uid是否是灰度的uid,如果是显示最新版本号,不是则显示最新公开发布的版本号
    return appUpdateMapper.selectLatestUpdate(appVersion,uid);
}

AppUpdateMapper

//检查更新
T selectLatestUpdate(@Param("version") String appVersion, @Param("uid") String uid);

AppUpdateMapper.xml

<!--  1.查询数据库中最新的版本号
    2.判断最新的版本号发布是否是灰度发布
    3.如果是灰度发布判断用户的uid是否是灰度的uid,如果是显示最新版本号,不是则显示最新公开发布的版本号  -->
<select id="selectLatestUpdate" resultMap="base_result_map">
    select
    <include refid="base_column_list"></include>
    from app_update where version>#{version} and (status = 2 or (status=1 and find_in_set(#{uid},grayscale_uid)))
    order by id desc limit 0,1
</select>

SQL语句

SELECT *
FROM
	app_update 
WHERE
	version>"1.0.3" and (status = 2 or (status=1 and find_in_set("1231313123",grayscale_uid)))
	
ORDER BY
	id DESC 
	LIMIT 0,1
  • 实现

@RestController("updateController")
@RequestMapping("/update")
public class UpdateController extends ABaseController {
    @Resource
    private AppUpdateService appUpdateService;

    @Resource
    private AppConfig appConfig;
    /**
     * 检查更新
     * @param appVersion 版本号
     * @param uid 用户id 用于判断是否是灰度用户
     * @return
     */
    @RequestMapping("/checkVersion")
    @GlobalInterceptor(checkAdmin = true)
    public ResponseVo checkVersion(String appVersion,String uid){
        //判断版本号是否为空
        if (StringTools.isEmpty(appVersion)){
            return getSuccessResponseVo(null);
        }
        //查看当前最新版本
        AppUpdate appUpdate =  appUpdateService.getLatestUpdate(appVersion,uid);
        if (appUpdate == null) {//如果没有最新版本直接返回
            return getSuccessResponseVo(null);
        }
        //封装vo返回前端
        AppUpdateVo updateVo = CopyTools.copy(appUpdate,AppUpdateVo.class);
        //版本文件
        if (AppUpdateFileTypeEnum.LOCAL.getType().equals(appUpdate.getFileType())){
            //获取文件名
            File file = new File(appConfig.getProjectFolder() + Constants.APP_UPDATE_FOLDER + appUpdate.getId() + Constants.APP_EXE_SUFFIX);
            updateVo.setSize(file.length());
        }else {//版本外部链接
            updateVo.setSize(0L);
        }
        updateVo.setUpdateList(Arrays.asList(appUpdate.getUpdateDescArray()));
        //文件名
        String fileName = Constants.APP_NAME + appUpdate.getVersion() + Constants.APP_EXE_SUFFIX;
        updateVo.setFileName(fileName);
        return getSuccessResponseVo(updateVo);
    }


}
  • 12
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java后端修改fastjson版本可以通过以下步骤进行: 1.了解当前使用的fastjson版本:查看项目中的依赖管理文件(如maven的pom.xml文件)或者构建工具中fastjson的引入方式,确定当前使用的fastjson版本号。 2.选择目标版本:根据项目需求和fastjson的发布历史,选择要升级的目标版本。可以参考fastjson的官方文档或者查看fastjson的发布版本列表,了解各个版本之间的变化和修复内容,选择适合的版本号。 3.更新依赖信息:将项目中fastjson的依赖声明更新为目标版本。如果使用maven管理项目,可以在pom.xml文件中修改fastjson的版本号,或者使用构建工具提供的命令进行版本更新操作。如果是手动管理jar包,可以将旧版本的fastjson jar包替换为目标版本的jar包。 4.解决依赖冲突(可选):如果升级后与其他依赖产生冲突,需要解决依赖冲突问题。可以通过更新其他依赖的版本或者使用dependencyManagement等方式解决冲突。 5.测试和验证:在升级后,进行全面的测试和验证,确保修改后的fastjson版本在项目中能够正常运行,并且没有引入新的问题或者兼容性问题。 6.处理相关代码改动(可选):如果新版本中存在不兼容的API变动,需要根据fastjson的版本更新说明或者官方文档,在项目代码中进行相应的调整和修改,以适应新版本的使用方式。 7.文档和沟通:在修改完成后,及时更新项目相关文档或者通知其他相关人员,以确保大家都了解fastjson版本的变更,并能够及时适应和应对。 总之,Java后端修改fastjson版本需要了解当前的版本、选择目标版本、更新依赖信息、解决依赖冲突、测试和验证,以及处理相关代码改动等一系列操作。通过以上步骤,可以顺利地完成fastjson版本的修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值