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.EquipmentStatus;
import com.zksy.base.service.EquipmentStatusService;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备状态
 */
@Slf4j
@RestController
@RequestMapping("/EquipmentStatus")
@Api(tags = "设备状态")
public class EquipmentStatusController {

    @Autowired
    private EquipmentStatusService service;

    @GetMapping("/getById/{id}")
    @ApiOperation(value = "设备状态搜索getById")
    @Anonymous
    public AjaxResult getById(@PathVariable String id) {
        EquipmentStatus equipmentStatus = service.getById(id);
        if(equipmentStatus==null){
            return AjaxResult.error("该设备状态不存在");
        }
        return AjaxResult.success(equipmentStatus);
    }

    @GetMapping("/findByPage")
    @ApiOperation(value = "设备状态分页")
    @Anonymous
    public Page findByPage(long pageNum, long pageSize,
                           @RequestParam(required = false) String statusId,
                           @RequestParam(required = false) String equipmentId,
                           @RequestParam(required = false) Integer currentStatus,
                           @RequestParam(required = false) Integer alarmStatus,
                           @RequestParam(required = false) Integer onlineStatus,
                           @RequestParam(required = false) Long userId) throws Exception {
        return service.findByPage(pageNum, pageSize, statusId,equipmentId,currentStatus,alarmStatus,onlineStatus,userId);
    }

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

    @PostMapping("/save")
    @ApiOperation(value = "新增设备状态")
    @Log(title = "新增设备状态", businessType = BusinessType.INSERT)
    public AjaxResult save(@RequestBody EquipmentStatus 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<EquipmentStatus> 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 EquipmentStatus 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.removeById(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.removeByIds(ids);
        return deleted ? AjaxResult.success("删除成功",deleted)
                : AjaxResult.error("删除失败",deleted);
    }
}
