package com.zksy.web.controller.gasbasic;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zksy.base.domain.MonitorPoint;
import com.zksy.base.dto.MonitorPointPageInDTO;
import com.zksy.base.service.MonitorPointService;
import com.zksy.common.annotation.Anonymous;
import com.zksy.common.annotation.Log;
import com.zksy.common.core.domain.AjaxResult;
import com.zksy.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api/monitor-point")
@Api(tags = "监测点管理")
public class GasMonitorPointController {

    @Autowired
    private MonitorPointService service;

    @GetMapping("/findByPage")
    @ApiOperation("监测点分页查询")
    @Anonymous
    public AjaxResult findByPage(MonitorPointPageInDTO dto) {
        Page<MonitorPoint> page = service.queryByPage(dto);
        return AjaxResult.success(page);
    }

    @GetMapping("/getById/{id}")
    @ApiOperation("根据ID查询监测点")
    @Anonymous
    public AjaxResult getById(@PathVariable String id) {
        MonitorPoint entity = service.getById(id);
        return entity != null ? AjaxResult.success(entity) : AjaxResult.error("监测点信息不存在");
    }

    @PostMapping("/save")
    @ApiOperation("新增监测点")
    @Log(title = "新增监测点", businessType = BusinessType.INSERT)
    public AjaxResult save(@RequestBody MonitorPoint entity) {
        boolean saved = service.saveWithCheck(entity);
        return saved ? AjaxResult.success("新增成功", saved) : AjaxResult.error("新增失败");
    }

    @PutMapping("/updateById")
    @ApiOperation("修改监测点")
    @Log(title = "修改监测点", businessType = BusinessType.UPDATE)
    public AjaxResult updateById(@RequestBody MonitorPoint entity) {
        boolean updated = service.updateWithCheck(entity);
        return updated ? AjaxResult.success("修改成功", updated) : AjaxResult.error("修改失败");
    }

    @DeleteMapping("/deleteById")
    @ApiOperation("删除监测点")
    @Log(title = "删除监测点", businessType = BusinessType.DELETE)
    public AjaxResult deleteById(@RequestParam String id) {
        boolean deleted = service.removeWithCheck(id);
        return deleted ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败");
    }
}
