package com.zksy.web.controller.drainage;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zksy.base.domain.EquipmentType;
import com.zksy.base.service.EquipmentTypeService;
import com.zksy.common.annotation.Anonymous;
import com.zksy.common.core.domain.AjaxResult;
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;

/**
 * 排水设备类型查询（用于预警类型映射）
 */
@Slf4j
@RestController
@RequestMapping("/api/drainage/equipmentType")
@Api(tags = "排水设备类型查询")
public class DrainageEquipmentTypeController {

    @Autowired
    private EquipmentTypeService equipmentTypeService;

    /**
     * 查询设备类型列表（按 parentTypeId 过滤）
     * 注意：@GetMapping("/list") 必须在 @GetMapping("/{id}") 之前定义
     */
    @Anonymous
    @GetMapping("/list")
    @ApiOperation(value = "查询设备类型列表（按父类型ID过滤）")
    public AjaxResult list(
            @ApiParam("父类型ID（如2为排水子类型）") @RequestParam(required = false) String parentTypeId) {
        LambdaQueryWrapper<EquipmentType> wrapper = new LambdaQueryWrapper<>();
        if (parentTypeId != null && !parentTypeId.isEmpty()) {
            wrapper.eq(EquipmentType::getParentTypeId, parentTypeId);
        }
        wrapper.orderByAsc(EquipmentType::getId);
        List<EquipmentType> list = equipmentTypeService.list(wrapper);
        return AjaxResult.success(list);
    }

    @Anonymous
    @GetMapping("/{id}")
    @ApiOperation(value = "根据ID查询设备类型")
    public AjaxResult getById(@PathVariable String id) {
        EquipmentType entity = equipmentTypeService.getById(id);
        if (entity == null) {
            return AjaxResult.error("该设备类型不存在");
        }
        return AjaxResult.success(entity);
    }
}
