| | |
| | | import com.ruoyi.lims.service.DataInterfaceService; |
| | | import com.ruoyi.lims.service.InterfaceFieldService; |
| | | import com.ruoyi.lims.service.InterfaceLogService; |
| | | import com.ruoyi.lims.service.LimsCleanService; |
| | | import okhttp3.OkHttpClient; |
| | | import okhttp3.Request; |
| | | import okhttp3.Response; |
| | |
| | | private InterfaceFieldService fieldService; |
| | | private InterfaceLogService logService; |
| | | private DataCollectionService collectionService; |
| | | private LimsCleanService cleanService; |
| | | |
| | | private static final ObjectMapper JSON = new ObjectMapper(); |
| | | |
| | |
| | | List<InterfaceField> fields = fieldService.list(fw); |
| | | |
| | | int saved = 0; |
| | | Map<String, Object> ret_clean = null; |
| | | List<String> warnings = new ArrayList<>(); |
| | | try { |
| | | if (fields.isEmpty()) { |
| | |
| | | return AjaxResult.error("映射写入失败:" + shortMsg(e)); |
| | | } |
| | | |
| | | // 采集成功后自动清洗(可用 autoClean=false 关闭) |
| | | if (saved > 0) { |
| | | try { |
| | | java.util.List<Long> newIds = new java.util.ArrayList<>(); |
| | | com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<DataCollection> nw = |
| | | new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<>(); |
| | | nw.eq("clean_status", "pending").orderByDesc("id").last("limit " + saved); |
| | | for (DataCollection x : collectionService.list(nw)) { |
| | | newIds.add(x.getId()); |
| | | } |
| | | if (!newIds.isEmpty()) { |
| | | Map<String, Object> cr = cleanService.cleanBatch(newIds); |
| | | ret_clean = cr; |
| | | } |
| | | } catch (Exception ignore) { |
| | | } |
| | | } |
| | | saveLog(di, "manual", requestDesc, rawJson, true, null, System.currentTimeMillis() - start, saved); |
| | | |
| | | Map<String, Object> ret = new LinkedHashMap<>(); |
| | | ret.put("saved", saved); |
| | | ret.put("rawLength", rawJson == null ? 0 : rawJson.length()); |
| | | ret.put("warnings", warnings); |
| | | if (ret_clean != null) { ret.put("clean", ret_clean); } |
| | | return AjaxResult.success("采集完成,写入 " + saved + " 条", ret); |
| | | } |
| | | |
| | |
| | | return AjaxResult.success(logService.page(page, w)); |
| | | } |
| | | |
| | | /** |
| | | * 供定时任务调用的采集入口(不经 HTTP),返回成功写入条数 |
| | | */ |
| | | public Map<String, Object> collectInternal(Long id, String invokeType) { |
| | | Map<String, Object> r = new LinkedHashMap<>(); |
| | | AjaxResult ar = collect(id); |
| | | Object code = ar.get("code"); |
| | | boolean ok = code != null && "200".equals(String.valueOf(code)); |
| | | r.put("ok", ok); |
| | | r.put("msg", ar.get("msg")); |
| | | if (!ok) { |
| | | return r; |
| | | } |
| | | Object data = ar.get("data"); |
| | | if (data instanceof Map) { |
| | | r.put("saved", ((Map<?, ?>) data).get("saved")); |
| | | r.put("clean", ((Map<?, ?>) data).get("clean")); |
| | | } |
| | | return r; |
| | | } |
| | | /* ==================== helpers ==================== */ |
| | | |
| | | private String buildHttpUrl(DataInterface di) { |
| | |
| | | private static int parseInt(String s, int def) { |
| | | try { return Integer.parseInt(s.trim()); } catch (Exception e) { return def; } |
| | | } |
| | | } |
| | | |
| | | /* ==================== ⑤ 自动采集配置 ==================== */ |
| | | |
| | | /** |
| | | * 开启/关闭自动采集,并设置采集周期(cron) |
| | | */ |
| | | @PostMapping("/autoConfig") |
| | | @ApiOperation("配置自动采集") |
| | | public AjaxResult autoConfig(@RequestBody Map<String, Object> body) { |
| | | Long id = null; |
| | | try { id = Long.valueOf(String.valueOf(body.get("id"))); } catch (Exception ignore) {} |
| | | if (id == null) return AjaxResult.error("id 不能为空"); |
| | | DataInterface di = dataInterfaceservice.getById(id); |
| | | if (di == null) return AjaxResult.error("接口不存在"); |
| | | |
| | | String enabled = body.get("autoEnabled") == null ? "0" : String.valueOf(body.get("autoEnabled")); |
| | | String cron = body.get("collectCron") == null ? null : String.valueOf(body.get("collectCron")); |
| | | if ("1".equals(enabled) && (cron == null || cron.trim().isEmpty())) { |
| | | return AjaxResult.error("开启自动采集必须填写 cron 表达式,如 0 */5 * * * ?(每 5 分钟)"); |
| | | } |
| | | |
| | | DataInterface upd = new DataInterface(); |
| | | upd.setId(id); |
| | | upd.setAutoEnabled(enabled); |
| | | upd.setCollectCron(cron); |
| | | dataInterfaceservice.updateById(upd); |
| | | return AjaxResult.success("1".equals(enabled) ? "已开启自动采集" : "已关闭自动采集"); |
| | | } |
| | | |
| | | /** |
| | | * 查询当前启用自动采集的接口(定时任务用,也可前端展示) |
| | | */ |
| | | @GetMapping("/autoList") |
| | | @ApiOperation("查询启用自动采集的接口") |
| | | public AjaxResult autoList() { |
| | | com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<DataInterface> w = |
| | | new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<>(); |
| | | w.eq("auto_enabled", "1").orderByAsc("id"); |
| | | return AjaxResult.success(dataInterfaceservice.list(w)); |
| | | }} |