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.EquipmentBase;
import com.zksy.base.service.EquipmentBaseService;
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("/EquipmentBase")
@Api(tags = "设备基础信息-物联网感知设备")
public class EquipmentBaseController {

    @Autowired
    private EquipmentBaseService service;

    @GetMapping("/getById/{id}")
    @ApiOperation(value = "设备基础信息搜索getById")
    @Anonymous
    public AjaxResult getById(@PathVariable String id) {
        EquipmentBase equipmentBase = service.getById(id);
        if (equipmentBase == null) {
            return AjaxResult.error("该设备信息不存在");
        }
        return AjaxResult.success(equipmentBase);
    }

    @Anonymous
    @GetMapping("/findByPage")
    @ApiOperation(value = "设备基础信息分页")
    public Page findByPage(long pageNum, long pageSize,
                           @RequestParam(required = false) String equipmentCode,
                           @RequestParam(required = false) String equipmentName,
                           @RequestParam(required = false) String equipmentModel,
                           @RequestParam(required = false) String manufacturer) throws Exception {
        return service.findByPage(pageNum, pageSize, equipmentCode, equipmentName, equipmentModel, manufacturer);
    }

    @Anonymous
    @GetMapping("/getList")
    @ApiOperation(value = "查询所有设备基础信息")
    public AjaxResult getList() throws Exception {
        return AjaxResult.success(service.list());
    }

    @PostMapping("/save")
    @ApiOperation(value = "新增设备基础信息")
    @Log(title = "新增设备基础信息", businessType = BusinessType.INSERT)
    public AjaxResult save(@RequestBody EquipmentBase 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<EquipmentBase> 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 EquipmentBase 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);
    }

    @PutMapping("/updateStatus")
    @ApiOperation(value = "设备状态维护（启用/禁用/维修等）")
    @Log(title = "设备状态维护", businessType = BusinessType.UPDATE)
    @Anonymous
    public AjaxResult updateEquipmentStatus(
            @ApiParam("设备ID") @RequestParam String equipmentId,
            @ApiParam("状态值") @RequestParam Integer currentStatus) {
        boolean updated = service.updateEquipmentStatus(equipmentId, currentStatus);
        return updated ? AjaxResult.success("状态更新成功")
                : AjaxResult.error("状态更新失败");
    }

    @PostMapping("/test")
    @ApiOperation(value = "设备测试验证")
    @Log(title = "设备测试验证", businessType = BusinessType.OTHER)
    @Anonymous
    public AjaxResult testEquipment(
            @ApiParam("设备ID") @RequestParam String equipmentId,
            @ApiParam("测试类型") @RequestParam(required = false) String testType,
            @ApiParam("测试详情") @RequestParam(required = false) String testDetail,
            @ApiParam("测试人") @RequestParam(required = false) String testUser) {
        boolean tested = service.testEquipment(equipmentId, testType, testDetail, testUser);
        return tested ? AjaxResult.success("测试成功")
                : AjaxResult.error("测试失败");
    }

    @GetMapping("/detail/{equipmentId}")
    @ApiOperation(value = "设备详情（含关联信息）")
    @Anonymous
    public AjaxResult getEquipmentDetail(@PathVariable String equipmentId) {
        Map<String, Object> detail = service.getEquipmentDetail(equipmentId);
        return AjaxResult.success(detail);
    }

    @GetMapping("/byPoint/{pointId}")
    @ApiOperation(value = "根据监测点查询设备")
    @Anonymous
    public AjaxResult getEquipmentByPoint(@PathVariable String pointId) {
        List<EquipmentBase> list = service.getEquipmentByPoint(pointId);
        return AjaxResult.success(list);
    }

    @Anonymous
    @GetMapping("/findByTopLevelType")
    @ApiOperation(value = "按顶级设备类型（燃气/排水/供水）查询设备列表")
    public AjaxResult findByTopLevelType(
            @ApiParam("顶级类型名称，如：燃气、排水、供水") @RequestParam String typeName) {
        List<EquipmentBase> list = service.findByTopLevelType(typeName);
        return AjaxResult.success(list);
    }
}
