package com.zksy.web.controller.base;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zksy.base.domain.MonitorPoint;
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 io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/MonitorPoint")
@Api(tags = "关键监测点管理")
public class MonitorPointController {

    @Autowired
    private MonitorPointService service;

    @GetMapping("/getById/{id}")
    @ApiOperation(value = "获取监测点详情")
    @Anonymous
    public AjaxResult getById(@PathVariable String id) {
        MonitorPoint entity = service.getById(id);
        if (entity == null) {
            return AjaxResult.error("该监测点信息不存在");
        }
        return AjaxResult.success(entity);
    }

    @Anonymous
    @GetMapping("/findByPage")
    @ApiOperation(value = "监测点分页查询")
    public Page findByPage(
            @ApiParam("页码") @RequestParam(defaultValue = "1") long pageNum,
            @ApiParam("每页数量") @RequestParam(defaultValue = "10") long pageSize,
            @ApiParam("监测点编码") @RequestParam(required = false) String pointCode,
            @ApiParam("监测点名称") @RequestParam(required = false) String pointName,
            @ApiParam("监测点类型") @RequestParam(required = false) String pointType,
            @ApiParam("状态") @RequestParam(required = false) String status) {
        return service.findByPage(pageNum, pageSize, pointCode, pointName, pointType, status);
    }

    @Anonymous
    @GetMapping("/getList")
    @ApiOperation(value = "查询所有监测点")
    public AjaxResult getList() {
        return AjaxResult.success(service.list());
    }

    @Anonymous
    @GetMapping("/byNetwork/{networkId}")
    @ApiOperation(value = "根据管网设施查询监测点")
    public AjaxResult getPointsByNetwork(@PathVariable String networkId) {
        List<MonitorPoint> list = service.getPointsByNetwork(networkId);
        return AjaxResult.success(list);
    }

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

    @PostMapping("/saveBatch")
    @ApiOperation(value = "监测点批量新增")
    @Log(title = "批量新增监测点", businessType = BusinessType.INSERT)
    public AjaxResult saveBatch(@RequestBody List<MonitorPoint> entityList) {
        if (CollectionUtils.isEmpty(entityList)) {
            return AjaxResult.error("批量新增失败：列表不能为空");
        }
        boolean saved = service.saveBatchWithCheck(entityList);
        return saved ? AjaxResult.success("新增成功", saved)
                : AjaxResult.error("新增失败", saved);
    }

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

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

    @DeleteMapping("/deleteBatchById")
    @ApiOperation(value = "监测点批量删除")
    @Log(title = "监测点批量删除", businessType = BusinessType.DELETE)
    public AjaxResult deleteBatchById(@RequestParam List<String> ids) {
        boolean deleted = service.removeBatchWithCheck(ids);
        return deleted ? AjaxResult.success("删除成功", deleted)
                : AjaxResult.error("删除失败", deleted);
    }

    @PostMapping("/bindEquipment")
    @ApiOperation(value = "监测点关联设备")
    @Log(title = "监测点关联设备", businessType = BusinessType.UPDATE)
    @Anonymous
    public AjaxResult bindEquipment(
            @ApiParam("监测点ID") @RequestParam String pointId,
            @ApiParam("设备ID列表") @RequestParam List<String> equipmentIds) {
        boolean bound = service.bindEquipment(pointId, equipmentIds);
        return bound ? AjaxResult.success("关联成功")
                : AjaxResult.error("关联失败");
    }

    @PostMapping("/unbindEquipment")
    @ApiOperation(value = "监测点取消关联设备")
    @Log(title = "监测点取消关联设备", businessType = BusinessType.UPDATE)
    @Anonymous
    public AjaxResult unbindEquipment(
            @ApiParam("监测点ID") @RequestParam String pointId,
            @ApiParam("设备ID列表") @RequestParam List<String> equipmentIds) {
        boolean unbound = service.unbindEquipment(pointId, equipmentIds);
        return unbound ? AjaxResult.success("取消关联成功")
                : AjaxResult.error("取消关联失败");
    }

    @GetMapping("/pointDetail/{pointId}")
    @ApiOperation(value = "监测点详情（含关联设备和管网）")
    @Anonymous
    public AjaxResult getPointDetail(@PathVariable String pointId) {
        Map<String, Object> detail = service.getPointDetail(pointId);
        return AjaxResult.success(detail);
    }
}
