package com.iailab.module.model.api; import com.iailab.common.utils.DateUtils; import com.iailab.framework.common.pojo.CommonResult; import com.iailab.module.data.api.IFeignDataApi; import com.iailab.module.mcs.service.StModelResultService; import com.iailab.module.model.dto.RunSetDTO; import com.iailab.module.model.handler.ModelHandler; import io.swagger.v3.oas.annotations.tags.Tag; import org.apache.commons.lang3.StringUtils; import javax.annotation.Resource; import org.springframework.util.CollectionUtils; import org.springframework.web.bind.annotation.*; import java.util.*; /** * scada接口数据 * * @author lirm * @date 2024/4/9 * @since 1.0 */ @RestController @RequestMapping("api/model") @Tag(name = "数据接口") public class ApiModelController { @Resource private IFeignDataApi iFeignDataApi; @Resource private StModelResultService stModelResultService; @Resource private ModelHandler modelHandler; private static String mFx1315SwitchInName = "1315/1给定"; private static String mFx1308SwitchInName = "1308/1给定"; private static String mFx1301AshInName = "1308/1给定"; @PostMapping("execute") public CommonResult execute(@RequestParam Map params, @RequestBody List sampleDataList) { Map result = new HashMap<>(); try { String appKey = "admin"; String modelCode = (String)params.get("modelCode"); if (StringUtils.isBlank(modelCode)) { return new CommonResult().setMsg("modelCode不能为空!"); } if (CollectionUtils.isEmpty(sampleDataList)) { return new CommonResult().setMsg("参数不能为空!"); } Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.MILLISECOND, 0); result = modelHandler.run(modelCode, calendar.getTime(), sampleDataList, appKey); return new CommonResult>().setData(result); } catch (Exception ex) { return new CommonResult().setMsg(ex.getMessage()); } } @PostMapping("run") public CommonResult run(@RequestParam Map params, @RequestBody List sampleDataList) { Map result = new HashMap<>(); try { String modelCode = (String)params.get("modelCode"); if (StringUtils.isBlank(modelCode)) { return new CommonResult().setMsg("modelCode不能为空!"); } if (CollectionUtils.isEmpty(sampleDataList)) { return new CommonResult().setMsg("参数不能为空!"); } Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.MILLISECOND, 0); result = modelHandler.run(modelCode, sampleDataList); return new CommonResult>().setData(result); } catch (Exception ex) { return new CommonResult().setMsg(ex.getMessage()); } } @PostMapping("run-set") public CommonResult runSet(@RequestParam Map params, @RequestBody RunSetDTO dto) { Map result = new HashMap<>(); try { List sampleDataList = dto.getDataList(); String modelCode = (String)params.get("modelCode"); if (StringUtils.isBlank(modelCode)) { return new CommonResult().setMsg("modelCode不能为空!"); } if (CollectionUtils.isEmpty(sampleDataList)) { return new CommonResult().setMsg("参数不能为空!"); } Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.MILLISECOND, 0); result = modelHandler.run(modelCode, sampleDataList, dto.getSettings()); return new CommonResult>().setData(result); } catch (Exception ex) { return new CommonResult().setMsg(ex.getMessage()); } } @GetMapping("model-result/{modelCode}") public CommonResult> getResultByModelCode(@PathVariable("modelCode") String modelCode) { Map data = new HashMap<>(); Map modelParams = new HashMap<>(1); try { modelParams.put("modelCode", modelCode); data = stModelResultService.getLastResultMap(modelParams); if (CollectionUtils.isEmpty(data)) { return new CommonResult<>(); } } catch (Exception ex) { ex.printStackTrace(); } return new CommonResult>().setData(data); } @GetMapping("model-date-result") public CommonResult> getTimeResult(@RequestParam Map params) { Map data = new HashMap<>(); try { data = stModelResultService.getResultByCodeDate(params); if (CollectionUtils.isEmpty(data)) { return new CommonResult<>(); } } catch (Exception ex) { ex.printStackTrace(); } return new CommonResult>().setData(data); } @GetMapping("model-result/list") public CommonResult>> geModelResultList(@RequestParam Map params) { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.MILLISECOND, 0); calendar.set(Calendar.SECOND, 0); if (params.get("lastHour") != null) { calendar.add(Calendar.HOUR_OF_DAY, Integer.parseInt(params.get("lastHour").toString()) * -1); } Date startDate = calendar.getTime(); Date endDate = new Date(); if ((params.get("startDate") != null && StringUtils.isNotBlank(params.get("startDate").toString()))) { String ts = params.get("startDate").toString(); if (ts.length() < 12) { ts = ts + " 00:00:00"; } startDate = DateUtils.parse(ts, DateUtils.DATE_TIME_PATTERN); } if ((params.get("endDate") != null && StringUtils.isNotBlank(params.get("endDate").toString()))) { String ts = params.get("endDate").toString(); if (ts.length() < 12) { ts = ts + " 23:59:59"; } endDate = DateUtils.parse(ts, DateUtils.DATE_TIME_PATTERN); } params.put("startDate", startDate); params.put("endDate", endDate); List> list = stModelResultService.getList(params); return new CommonResult>>().setData(list); } }