package com.zy.asrs.controller; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.baomidou.mybatisplus.plugins.Page; import com.core.annotations.ManagerAuth; import com.core.common.Cools; import com.core.common.DateUtils; import com.core.common.R; import com.core.controller.AbstractBaseController; import com.zy.asrs.entity.ApkBuildTask; import com.zy.asrs.service.ApkBuildTaskService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; /** * APK打包任务Controller */ @RestController public class ApkBuildTaskController extends AbstractBaseController { @Autowired private ApkBuildTaskService apkBuildTaskService; /** * 查询单个任务 */ @RequestMapping(value = "/apkBuildTask/{id}/auth") @ManagerAuth public R get(@PathVariable("id") Long id) { return R.ok(apkBuildTaskService.selectById(id)); } /** * 分页查询任务列表 */ @RequestMapping(value = "/apkBuildTask/list/auth") @ManagerAuth public R list(@RequestParam(defaultValue = "1") Integer curr, @RequestParam(defaultValue = "10") Integer limit, @RequestParam(required = false) String orderByField, @RequestParam(required = false) String orderByType, @RequestParam Map param) { excludeTrash(param); EntityWrapper wrapper = new EntityWrapper<>(); convert(param, wrapper); if (!Cools.isEmpty(orderByField)) { wrapper.orderBy(orderByField, "asc".equalsIgnoreCase(orderByType)); } else { wrapper.orderBy("id", false); } return R.ok(apkBuildTaskService.selectPage(new Page<>(curr, limit), wrapper)); } /** * 搜索参数转换 */ private void convert(Map map, EntityWrapper wrapper) { for (Map.Entry entry : map.entrySet()) { String key = entry.getKey(); Object value = entry.getValue(); if (value == null || "".equals(value.toString())) { continue; } if (key.endsWith(">")) { wrapper.ge(Cools.deleteChar(key), DateUtils.convert(String.valueOf(value))); } else if (key.endsWith("<")) { wrapper.le(Cools.deleteChar(key), DateUtils.convert(String.valueOf(value))); } else if ("status".equals(key)) { wrapper.eq(key, value); } else { wrapper.like(key, String.valueOf(value)); } } } /** * 触发打包任务 */ @RequestMapping(value = "/apkBuildTask/build/auth", method = RequestMethod.POST) @ManagerAuth public R triggerBuild(@RequestBody JSONObject param) { try { String buildType = param.getString("buildType"); String repoAlias = param.getString("repoAlias"); String branch = param.getString("branch"); if (Cools.isEmpty(buildType)) { buildType = "release"; } if (Cools.isEmpty(repoAlias)) { return R.error("仓库别名不能为空"); } if (Cools.isEmpty(branch)) { branch = "master"; } ApkBuildTask task = apkBuildTaskService.triggerBuild(buildType, repoAlias, branch); return R.ok(task); } catch (Exception e) { return R.error("打包任务创建失败: " + e.getMessage()); } } /** * 刷新指定任务状态 */ @RequestMapping(value = "/apkBuildTask/refresh/{id}/auth", method = RequestMethod.POST) @ManagerAuth public R refreshStatus(@PathVariable("id") Long id) { try { ApkBuildTask task = apkBuildTaskService.refreshStatus(id); return R.ok(task); } catch (Exception e) { return R.error("刷新状态失败: " + e.getMessage()); } } /** * 刷新所有进行中的任务状态 */ @RequestMapping(value = "/apkBuildTask/refreshAll/auth", method = RequestMethod.POST) @ManagerAuth public R refreshAllPendingTasks() { try { int count = apkBuildTaskService.refreshAllPendingTasks(); // 返回所有任务列表 EntityWrapper wrapper = new EntityWrapper<>(); wrapper.orderBy("id", false); List tasks = apkBuildTaskService.selectList(wrapper); Map result = new HashMap<>(); result.put("tasks", tasks); result.put("refreshedCount", count); return R.ok(result); } catch (Exception e) { return R.error("刷新状态失败: " + e.getMessage()); } } /** * 下载APK到服务器 */ @RequestMapping(value = "/apkBuildTask/download/{id}/auth", method = RequestMethod.POST) @ManagerAuth public R downloadApk(@PathVariable("id") Long id) { try { String localPath = apkBuildTaskService.downloadApk(id); Map result = new HashMap<>(); result.put("apkPath", localPath); return R.ok(result); } catch (Exception e) { return R.error("下载APK失败: " + e.getMessage()); } } /** * 通过ADB安装APK到指定设备 */ @RequestMapping(value = "/apkBuildTask/install/{id}/auth", method = RequestMethod.POST) @ManagerAuth public R installApk(@PathVariable("id") Long id, @RequestBody JSONObject param) { try { String deviceIp = param.getString("deviceIp"); if (Cools.isEmpty(deviceIp)) { return R.error("设备IP不能为空"); } String installResult = apkBuildTaskService.installApk(id, deviceIp); Map result = new HashMap<>(); result.put("result", installResult); return R.ok(result); } catch (Exception e) { return R.error("安装APK失败: " + e.getMessage()); } } /** * 批量安装APK到多台设备 */ @RequestMapping(value = "/apkBuildTask/installBatch/{id}/auth", method = RequestMethod.POST) @ManagerAuth public R installApkBatch(@PathVariable("id") Long id, @RequestBody JSONObject param) { try { List deviceIps = param.getJSONArray("deviceIps").toJavaList(String.class); if (deviceIps == null || deviceIps.isEmpty()) { return R.error("设备IP列表不能为空"); } List results = apkBuildTaskService.installApkToMultipleDevices(id, deviceIps); return R.ok(results); } catch (Exception e) { return R.error("批量安装APK失败: " + e.getMessage()); } } /** * 删除任务记录 */ @RequestMapping(value = "/apkBuildTask/delete/auth", method = RequestMethod.POST) @ManagerAuth public R delete(Integer[] ids) { if (ids == null || ids.length == 0) { return R.error(); } apkBuildTaskService.deleteBatchIds(Arrays.asList(ids)); return R.ok(); } /** * 查询进行中的任务 */ @RequestMapping(value = "/apkBuildTask/pending/auth") @ManagerAuth public R getPendingTasks() { List tasks = apkBuildTaskService.getPendingTasks(); return R.ok(tasks); } }