package com.zksy.web.controller.drainage;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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 com.zksy.drainage.domain.WaterloggingPoint;
import com.zksy.drainage.service.WaterloggingPointService;
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.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

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

/**
 * 易积水点管理
 */
@Slf4j
@RestController
@RequestMapping("/api/drainage/waterlogging")
@Api(tags = "易积水点管理")
public class WaterloggingPointController {

    @Autowired
    private WaterloggingPointService service;

    @Anonymous
    @GetMapping("/page")
    @ApiOperation(value = "易积水点分页查询")
    public Page<WaterloggingPoint> page(
            @ApiParam("页码") @RequestParam(defaultValue = "1") long pageNum,
            @ApiParam("每页数量") @RequestParam(defaultValue = "10") long pageSize,
            @ApiParam("监测点名称") @RequestParam(required = false) String name,
            @ApiParam("行政区域") @RequestParam(required = false) String area) {
        return service.findByPage(pageNum, pageSize, name, area);
    }

    @Anonymous
    @GetMapping("/{id}")
    @ApiOperation(value = "查询易积水点详情")
    public AjaxResult getById(@PathVariable String id) {
        WaterloggingPoint entity = service.getById(id);
        if (entity == null) {
            return AjaxResult.error("该易积水点不存在");
        }
        return AjaxResult.success(entity);
    }

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

    @Anonymous
    @PostMapping
    @ApiOperation(value = "新增易积水点")
    @Log(title = "新增易积水点", businessType = BusinessType.INSERT)
    @Transactional
    public AjaxResult save(@RequestBody WaterloggingPoint entity) {
        boolean saved = service.saveWithCheck(entity);
        return saved ? AjaxResult.success("新增成功") : AjaxResult.error("新增失败");
    }

    @Anonymous
    @PutMapping
    @ApiOperation(value = "修改易积水点")
    @Log(title = "修改易积水点", businessType = BusinessType.UPDATE)
    @Transactional
    public AjaxResult update(@RequestBody WaterloggingPoint entity) {
        boolean updated = service.updateWithCheck(entity);
        return updated ? AjaxResult.success("修改成功") : AjaxResult.error("修改失败");
    }

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

    @Anonymous
    @DeleteMapping("/batch")
    @ApiOperation(value = "批量删除易积水点")
    @Log(title = "批量删除易积水点", businessType = BusinessType.DELETE)
    @Transactional
    public AjaxResult deleteBatch(@RequestBody List<String> ids) {
        boolean deleted = service.removeBatchWithCheck(ids);
        return deleted ? AjaxResult.success("批量删除成功") : AjaxResult.error("批量删除失败");
    }

    @Anonymous
    @PostMapping("/bindEquipment")
    @ApiOperation(value = "绑定设备到易积水点")
    public AjaxResult bindEquipment(@RequestBody Map<String, Object> params) {
        String partitionId = (String) params.get("partitionId");
        @SuppressWarnings("unchecked")
        List<String> equipmentIds = (List<String>) params.get("equipmentIds");
        if (partitionId == null || equipmentIds == null || equipmentIds.isEmpty()) {
            return AjaxResult.error("参数不完整");
        }
        boolean success = service.bindEquipment(partitionId, equipmentIds);
        return success ? AjaxResult.success() : AjaxResult.error("绑定失败");
    }
}
