ApkController.java 3.96 KB
package com.huaheng.mobile.download;

import com.alibaba.fastjson.JSONException;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.common.mapper.ApkMapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/mobile/download")
public class ApkController {

    @Resource
    ApkMapper apkMapper;

    @PostMapping("/getUpdateApkInfo")
    @ApiOperation("获取apk更新信息")
    public AjaxResult getUpdateApkInfo(@RequestBody Map<String, String> param) {
        System.out.println("getUpdateApkInfo pkgName:" + param.get("pkgName"));
        if  (param.get("pkgName") == null)
            throw new JSONException("pkgName不能为空");
        if  (param.get("versionCode") == null)
            throw new JSONException("versionCode不能为空");
        List<ApkInfo>  apkinfos = apkMapper.getApkInfoByPkgName(param.get("pkgName"));
        ApkInfo apkInfo = apkinfos.get(0);
        int maxVersion = Integer.MAX_VALUE;
        String project = param.containsKey("project") ? param.get("project") : null;
        if(project != null) {
            if(project.equals(Constant.PROJECT_XIANGJI)) {
                maxVersion = Constant.PROJECT_XIANGJI_MAX_LEVEL;
            }
        }
        for (ApkInfo apkInfo2 : apkinfos)   //循环找到版本最大值
        {
            if (apkInfo.getVersionCode() < apkInfo2.getVersionCode()) {
                if(apkInfo2.getVersionCode() > maxVersion) {
                    continue;
                }
                apkInfo = apkInfo2;
            }
        }

        int versionCode = Integer.parseInt(param.get("versionCode"));
        System.out.println("getUpdateApkInfo versionCode:" + versionCode);
        System.out.println("apkInfo.getVersionCode():" + apkInfo.getVersionCode());
        if(versionCode >= apkInfo.getVersionCode()) {
           return AjaxResult.error("当前已经是最新版本");
        }

        return AjaxResult.success(apkInfo);
    }

    @PostMapping("/insertApkInfo")
    @ApiOperation("插入apk更新信息")
    public ApkInfo insertApkInfo(@RequestBody Map<String, String> param) {
        System.out.println("insertApkInfo pkgName:" + param.get("pkgName"));
        if  (param.get("pkgName") == null)
            throw new JSONException("pkgName不能为空");
        if  (param.get("versionCode") == null)
            throw new JSONException("versionCode不能为空");
        if  (param.get("versionName") == null)
            throw new JSONException("versionName");
        if  (param.get("url") == null)
            throw new JSONException("url");
        if  (param.get("md5") == null)
            throw new JSONException("md5");
        ApkInfo apkInfo = buildApkInfo(param);
        try {
            apkMapper.insertApkInfo(apkInfo.getPkgName(), apkInfo.getVersionCode(), apkInfo.getVersionName(), apkInfo.getUrl(), apkInfo.getMd5());
        } catch(Exception e) {

        }
        return apkInfo;
    }

    @PostMapping("/clearApkInfo")
    @ApiOperation("删除apk信息")
    public AjaxResult clearApkInfo(@RequestBody Map<String, String> param) {
        System.out.println("clearApkInfo");
        try {
            apkMapper.clearApkInfo();
        } catch(Exception e) {

        }
        return AjaxResult.success("删除apk信息成功");
    }

    private ApkInfo buildApkInfo(Map<String, String> param) {
        ApkInfo apkInfo = new ApkInfo();
        apkInfo.setPkgName(param.get("pkgName"));
        apkInfo.setVersionCode(Integer.parseInt(param.get("versionCode")));
        apkInfo.setVersionName(param.get("versionName"));
        apkInfo.setUrl(param.get("url"));
        apkInfo.setMd5(param.get("md5"));
        return apkInfo;
    }
}