package org.stm.metric.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.stm.metric.entity.MetricMonitorSetHight;
import org.stm.metric.service.IMetricMonitorSetHightService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;


@Api(tags="metric_monitor_set_hight")
@RestController
@RequestMapping("/metric/metricMonitorSetHight")
@Slf4j
public class MetricMonitorSetHightController extends JeecgController<MetricMonitorSetHight, IMetricMonitorSetHightService> {
	@Autowired
	private IMetricMonitorSetHightService metricMonitorSetHightService;
	
	@GetMapping(value = "/list")
	public Result<IPage<MetricMonitorSetHight>> queryPageList(MetricMonitorSetHight metricMonitorSetHight,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<MetricMonitorSetHight> queryWrapper = QueryGenerator.initQueryWrapper(metricMonitorSetHight, req.getParameterMap());
		Page<MetricMonitorSetHight> page = new Page<MetricMonitorSetHight>(pageNo, pageSize);
		IPage<MetricMonitorSetHight> pageList = metricMonitorSetHightService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody MetricMonitorSetHight metricMonitorSetHight) {
		MetricMonitorSetHight maxMetric = getMaxCodeRecord();

		if(maxMetric==null) {
			metricMonitorSetHight.setMtrcNo("10000");
		} else {
			Integer nextCode = Integer.parseInt(maxMetric.getMtrcNo())+1;
			metricMonitorSetHight.setMtrcNo(nextCode.toString());
		}

		metricMonitorSetHightService.save(metricMonitorSetHight);
		return Result.OK("添加成功！");
	}

	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody MetricMonitorSetHight metricMonitorSetHight) {
		metricMonitorSetHightService.updateById(metricMonitorSetHight);
		return Result.OK("编辑成功!");
	}

	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		metricMonitorSetHightService.removeById(id);
		return Result.OK("删除成功!");
	}

	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.metricMonitorSetHightService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	

	@GetMapping(value = "/queryById")
	public Result<MetricMonitorSetHight> queryById(@RequestParam(name="id",required=true) String id) {
		MetricMonitorSetHight metricMonitorSetHight = metricMonitorSetHightService.getById(id);
		if(metricMonitorSetHight==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(metricMonitorSetHight);
	}

    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, MetricMonitorSetHight metricMonitorSetHight) {
        return super.exportXls(request, metricMonitorSetHight, MetricMonitorSetHight.class, "metric_monitor_set_hight");
    }

    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, MetricMonitorSetHight.class);
    }

	private MetricMonitorSetHight getMaxCodeRecord() {
		LambdaQueryWrapper<MetricMonitorSetHight> wrapper = new LambdaQueryWrapper<>();
		wrapper.orderByDesc(MetricMonitorSetHight::getMtrcNo)  // 使用Lambda表达式
				.last("LIMIT 1");

		return metricMonitorSetHightService.getOne(wrapper);
	}

}
