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.EngineeringFacility;
import com.zksy.drainage.service.EngineeringFacilityService;
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/facility")
@Api(tags = "工程设施基础数据管理")
public class EngineeringFacilityController {

    @Autowired
    private EngineeringFacilityService service;

    @Anonymous
    @GetMapping("/page")
    @ApiOperation(value = "工程设施分页查询")
    public Page<EngineeringFacility> 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,
            @ApiParam("设施类型") @RequestParam(required = false) String facilityType) {
        return service.findByPage(pageNum, pageSize, name, area, facilityType);
    }

    @Anonymous
    @GetMapping("/{id}")
    @ApiOperation(value = "查询工程设施详情")
    public AjaxResult getById(@PathVariable String id) {
        EngineeringFacility 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 EngineeringFacility entity) {
        boolean saved = service.saveEntity(entity);
        return saved ? AjaxResult.success("新增成功") : AjaxResult.error("新增失败");
    }

    @Anonymous
    @PutMapping
    @ApiOperation(value = "修改工程设施")
    @Log(title = "修改工程设施", businessType = BusinessType.UPDATE)
    @Transactional
    public AjaxResult update(@RequestBody EngineeringFacility entity) {
        boolean updated = service.updateEntity(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.removeEntity(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.removeBatchEntity(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("绑定失败");
    }

    @Anonymous
    @PostMapping("/bindPumpStation")
    @ApiOperation(value = "工程设施关联泵站")
    public AjaxResult bindPumpStation(@RequestBody Map<String, Object> params) {
        String facilityId = (String) params.get("partitionId");
        @SuppressWarnings("unchecked")
        List<String> pumpStationIds = (List<String>) params.get("equipmentIds");
        if (facilityId == null || pumpStationIds == null || pumpStationIds.isEmpty()) {
            return AjaxResult.error("参数不完整");
        }
        // 复用bindEquipment，直接存储泵站ID到equipmentId字段，不做设备表校验
        boolean success = service.bindEquipment(facilityId, pumpStationIds);
        return success ? AjaxResult.success("关联成功") : AjaxResult.error("关联失败");
    }
}
