package org.jeecg.modules.stm.metric.controller;

 
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.jeecg.modules.stm.metric.entity.MetricReport;
import org.jeecg.modules.stm.metric.pojo.MetricQueryCond;
import org.jeecg.modules.stm.metric.service.IMetricReportService;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;



@RestController
@RequestMapping("/metric/dataApproval")
@Slf4j
public class MetricReportApprovalController extends JeecgController<MetricReport, IMetricReportService> {
	@Autowired
	private IMetricReportService metricReportService;

	private final Executor asyncExecutor = Executors.newFixedThreadPool(10);
	@GetMapping(value = "/list")
	public Result<List<Map<String,Object>>> queryMetricReportData(MetricQueryCond metricQueryCond, HttpServletRequest req) {
		try {

			List<Map<String,Object>> list = metricReportService.queryMetricReportData(metricQueryCond);
			return Result.OK(list);
		} catch (Exception e) {
			log.error("查询监测记录失败", e);
			return Result.error("查询监测记录失败");
		}
	}


	@RequestMapping(value = "/batchSaveApproval", method = RequestMethod.POST)
	public Result<String> batchSaveApproval(@RequestBody MetricReport metricReport, HttpServletRequest req) {
		try {
			List<Integer> ids = metricReport.getIds();
			if(ids==null || ids.size()==0){
				return Result.OK("保存成功!");
			}
			Integer appSta = metricReport.getAppSta();
			boolean updated = metricReportService.lambdaUpdate()
			.in(MetricReport::getId, ids)
			.set(MetricReport::getAppSta, appSta)
			.update();
			return Result.OK("保存成功!");
		} catch (Exception e) {
			log.error("", e);
			return Result.error("保存失败: " + e.getMessage());
		}
	}

	@RequestMapping(value = "/saveApproval", method = RequestMethod.POST)
	public Result<String> saveApproval(@RequestBody MetricReport metricReport, HttpServletRequest req) {
		try {
			metricReportService.saveOrUpdate(metricReport);
			return Result.OK("保存成功!");
		} catch (Exception e) {
			log.error("", e);
			return Result.error("保存失败: " + e.getMessage());
		}
	}
}