package com.zksy.web.controller.drainage;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zksy.base.domain.*;
import com.zksy.base.domain.vo.EquipmentFullVO;
import com.zksy.base.service.EquipmentBaseService;
import com.zksy.base.service.EquipmentMaintainService;
import com.zksy.base.service.EquipmentStatusService;
import com.zksy.base.service.EquipmentTypeService;
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.service.DrainageEquipmentPointRelService;
import com.zksy.drainage.service.DrainageEquipmentTestService;
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/equipment")
@Api(tags = "排水系统-监测设备管理")
public class DrainageEquipmentController {

    @Autowired
    private EquipmentBaseService equipmentBaseService;

    @Autowired
    private EquipmentTypeService equipmentTypeService;

    @Autowired
    private EquipmentStatusService equipmentStatusService;

    @Autowired
    private EquipmentMaintainService equipmentMaintainService;

    @Autowired
    private DrainageEquipmentTestService equipmentTestService;

    @Autowired
    private DrainageEquipmentPointRelService equipmentPointRelService;

    // ==================== 设备基础信息 ====================

    @Anonymous
    @GetMapping("/base/page")
    @ApiOperation(value = "监测设备分页查询")
    public Page<EquipmentFullVO> basePage(
            @ApiParam("页码") @RequestParam(defaultValue = "1") long pageNum,
            @ApiParam("每页数量") @RequestParam(defaultValue = "10") long pageSize,
            @ApiParam("设备编码") @RequestParam(required = false) String equipmentCode,
            @ApiParam("设备名称") @RequestParam(required = false) String equipmentName,
            @ApiParam("设备型号") @RequestParam(required = false) String equipmentModel,
            @ApiParam("制造商") @RequestParam(required = false) String manufacturer,
            @ApiParam("设备类型ID") @RequestParam(required = false) String equipmentTypeId) {
        return equipmentBaseService.findFullByPage(pageNum, pageSize, equipmentCode, equipmentName, equipmentModel, manufacturer, equipmentTypeId);
    }

    @Anonymous
    @GetMapping("/base/{id}")
    @ApiOperation(value = "查询设备详情")
    public AjaxResult baseGetById(@PathVariable String id) {
        Map<String, Object> detail = equipmentBaseService.getEquipmentDetail(id);
        return AjaxResult.success(detail);
    }

    @Anonymous
    @GetMapping("/base/list")
    @ApiOperation(value = "查询所有监测设备")
    public AjaxResult baseList() {
        return AjaxResult.success(equipmentBaseService.list());
    }

    @Anonymous
    @PostMapping("/base")
    @ApiOperation(value = "新增监测设备")
    @Log(title = "新增监测设备", businessType = BusinessType.INSERT)
    @Transactional
    public AjaxResult baseSave(@RequestBody EquipmentBase entity) {
        boolean saved = equipmentBaseService.saveWithCheck(entity);
        if (saved) {
            return AjaxResult.success("新增成功", entity);
        }
        return AjaxResult.error("新增失败");
    }

    @Anonymous
    @PutMapping("/base")
    @ApiOperation(value = "修改监测设备")
    @Log(title = "修改监测设备", businessType = BusinessType.UPDATE)
    @Transactional
    public AjaxResult baseUpdate(@RequestBody EquipmentBase entity) {
        boolean updated = equipmentBaseService.updateWithCheck(entity);
        return updated ? AjaxResult.success("修改成功") : AjaxResult.error("修改失败");
    }

    @Anonymous
    @DeleteMapping("/base/{id}")
    @ApiOperation(value = "删除监测设备")
    @Log(title = "删除监测设备", businessType = BusinessType.DELETE)
    @Transactional
    public AjaxResult baseDelete(@PathVariable String id) {
        boolean deleted = equipmentBaseService.removeWithCheck(id);
        return deleted ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败");
    }

    @Anonymous
    @DeleteMapping("/base/batch")
    @ApiOperation(value = "批量删除监测设备")
    @Log(title = "批量删除监测设备", businessType = BusinessType.DELETE)
    @Transactional
    public AjaxResult baseDeleteBatch(@RequestBody List<String> ids) {
        boolean deleted = equipmentBaseService.removeBatchWithCheck(ids);
        return deleted ? AjaxResult.success("批量删除成功") : AjaxResult.error("批量删除失败");
    }

    // ==================== 设备类别 ====================

    @Anonymous
    @GetMapping("/type/page")
    @ApiOperation(value = "设备类别分页查询")
    public Page<EquipmentType> typePage(
            @ApiParam("页码") @RequestParam(defaultValue = "1") long pageNum,
            @ApiParam("每页数量") @RequestParam(defaultValue = "10") long pageSize,
            @ApiParam("类别ID") @RequestParam(required = false) String typeId,
            @ApiParam("类别名称") @RequestParam(required = false) String typeName,
            @ApiParam("父类别ID") @RequestParam(required = false) String parentTypeId) {
        return equipmentTypeService.findByPage(pageNum, pageSize, typeId, typeName, parentTypeId);
    }

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

    @Anonymous
    @GetMapping("/type/list")
    @ApiOperation(value = "查询所有设备类别")
    public AjaxResult typeList() {
        return AjaxResult.success(equipmentTypeService.list());
    }

    @Anonymous
    @PostMapping("/type")
    @ApiOperation(value = "新增设备类别")
    @Log(title = "新增设备类别", businessType = BusinessType.INSERT)
    @Transactional
    public AjaxResult typeSave(@RequestBody EquipmentType entity) {
        boolean saved = equipmentTypeService.saveWithCheck(entity);
        return saved ? AjaxResult.success("新增成功") : AjaxResult.error("新增失败");
    }

    @Anonymous
    @PutMapping("/type")
    @ApiOperation(value = "修改设备类别")
    @Log(title = "修改设备类别", businessType = BusinessType.UPDATE)
    @Transactional
    public AjaxResult typeUpdate(@RequestBody EquipmentType entity) {
        boolean updated = equipmentTypeService.updateWithCheck(entity);
        return updated ? AjaxResult.success("修改成功") : AjaxResult.error("修改失败");
    }

    @Anonymous
    @DeleteMapping("/type/{id}")
    @ApiOperation(value = "删除设备类别")
    @Log(title = "删除设备类别", businessType = BusinessType.DELETE)
    @Transactional
    public AjaxResult typeDelete(@PathVariable String id) {
        boolean deleted = equipmentTypeService.removeWithCheck(id);
        return deleted ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败");
    }

    @Anonymous
    @DeleteMapping("/type/batch")
    @ApiOperation(value = "批量删除设备类别")
    @Log(title = "批量删除设备类别", businessType = BusinessType.DELETE)
    @Transactional
    public AjaxResult typeDeleteBatch(@RequestBody List<String> ids) {
        boolean deleted = equipmentTypeService.removeBatchWithCheck(ids);
        return deleted ? AjaxResult.success("批量删除成功") : AjaxResult.error("批量删除失败");
    }

    // ==================== 设备状态 ====================

    @Anonymous
    @GetMapping("/status/page")
    @ApiOperation(value = "设备状态分页查询")
    public Page<EquipmentStatus> statusPage(
            @ApiParam("页码") @RequestParam(defaultValue = "1") long pageNum,
            @ApiParam("每页数量") @RequestParam(defaultValue = "10") long pageSize,
            @ApiParam("状态ID") @RequestParam(required = false) String statusId,
            @ApiParam("设备ID") @RequestParam(required = false) String equipmentId,
            @ApiParam("当前状态") @RequestParam(required = false) Integer currentStatus,
            @ApiParam("报警状态") @RequestParam(required = false) Integer alarmStatus,
            @ApiParam("在线状态") @RequestParam(required = false) Integer onlineStatus,
            @ApiParam("负责人ID") @RequestParam(required = false) Long userId) {
        return equipmentStatusService.findByPage(pageNum, pageSize, statusId, equipmentId, currentStatus, alarmStatus, onlineStatus, userId);
    }

    @Anonymous
    @GetMapping("/status/{id}")
    @ApiOperation(value = "查询设备状态详情")
    public AjaxResult statusGetById(@PathVariable String id) {
        EquipmentStatus entity = equipmentStatusService.getById(id);
        if (entity == null) {
            return AjaxResult.error("该设备状态记录不存在");
        }
        return AjaxResult.success(entity);
    }

    @Anonymous
    @PostMapping("/status")
    @ApiOperation(value = "新增设备状态")
    @Log(title = "新增设备状态", businessType = BusinessType.INSERT)
    @Transactional
    public AjaxResult statusSave(@RequestBody EquipmentStatus entity) {
        boolean saved = equipmentStatusService.saveWithCheck(entity);
        return saved ? AjaxResult.success("新增成功") : AjaxResult.error("新增失败");
    }

    @Anonymous
    @PutMapping("/status")
    @ApiOperation(value = "修改设备状态")
    @Log(title = "修改设备状态", businessType = BusinessType.UPDATE)
    @Transactional
    public AjaxResult statusUpdate(@RequestBody EquipmentStatus entity) {
        boolean updated = equipmentStatusService.updateWithCheck(entity);
        return updated ? AjaxResult.success("修改成功") : AjaxResult.error("修改失败");
    }

    @Anonymous
    @DeleteMapping("/status/{id}")
    @ApiOperation(value = "删除设备状态")
    @Log(title = "删除设备状态", businessType = BusinessType.DELETE)
    @Transactional
    public AjaxResult statusDelete(@PathVariable String id) {
        boolean deleted = equipmentStatusService.removeById(id);
        return deleted ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败");
    }

    // ==================== 设备维护记录 ====================

    @Anonymous
    @GetMapping("/maintain/page")
    @ApiOperation(value = "设备维护记录分页查询")
    public Page<EquipmentMaintain> maintainPage(
            @ApiParam("页码") @RequestParam(defaultValue = "1") long pageNum,
            @ApiParam("每页数量") @RequestParam(defaultValue = "10") long pageSize,
            @ApiParam("维护记录ID") @RequestParam(required = false) String maintainId,
            @ApiParam("设备ID") @RequestParam(required = false) String equipmentId,
            @ApiParam("维护类型") @RequestParam(required = false) String maintainType,
            @ApiParam("维护人") @RequestParam(required = false) String maintainPerson) {
        return equipmentMaintainService.findByPage(pageNum, pageSize, maintainId, equipmentId, maintainType, maintainPerson);
    }

    @Anonymous
    @GetMapping("/maintain/{id}")
    @ApiOperation(value = "查询设备维护记录详情")
    public AjaxResult maintainGetById(@PathVariable String id) {
        EquipmentMaintain entity = equipmentMaintainService.getById(id);
        if (entity == null) {
            return AjaxResult.error("该维护记录不存在");
        }
        return AjaxResult.success(entity);
    }

    @Anonymous
    @PostMapping("/maintain")
    @ApiOperation(value = "新增设备维护记录")
    @Log(title = "新增设备维护记录", businessType = BusinessType.INSERT)
    @Transactional
    public AjaxResult maintainSave(@RequestBody EquipmentMaintain entity) {
        boolean saved = equipmentMaintainService.saveWithCheck(entity);
        return saved ? AjaxResult.success("新增成功") : AjaxResult.error("新增失败");
    }

    @Anonymous
    @PutMapping("/maintain")
    @ApiOperation(value = "修改设备维护记录")
    @Log(title = "修改设备维护记录", businessType = BusinessType.UPDATE)
    @Transactional
    public AjaxResult maintainUpdate(@RequestBody EquipmentMaintain entity) {
        boolean updated = equipmentMaintainService.updateWithCheck(entity);
        return updated ? AjaxResult.success("修改成功") : AjaxResult.error("修改失败");
    }

    @Anonymous
    @DeleteMapping("/maintain/{id}")
    @ApiOperation(value = "删除设备维护记录")
    @Log(title = "删除设备维护记录", businessType = BusinessType.DELETE)
    @Transactional
    public AjaxResult maintainDelete(@PathVariable String id) {
        boolean deleted = equipmentMaintainService.removeById(id);
        return deleted ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败");
    }

    @Anonymous
    @DeleteMapping("/maintain/batch")
    @ApiOperation(value = "批量删除设备维护记录")
    @Log(title = "批量删除设备维护记录", businessType = BusinessType.DELETE)
    @Transactional
    public AjaxResult maintainDeleteBatch(@RequestBody List<String> ids) {
        boolean deleted = equipmentMaintainService.removeByIds(ids);
        return deleted ? AjaxResult.success("批量删除成功") : AjaxResult.error("批量删除失败");
    }

    // ==================== 设备测试记录 ====================

    @Anonymous
    @GetMapping("/test/page")
    @ApiOperation(value = "设备测试记录分页查询")
    public Page<EquipmentTest> testPage(
            @ApiParam("页码") @RequestParam(defaultValue = "1") long pageNum,
            @ApiParam("每页数量") @RequestParam(defaultValue = "10") long pageSize,
            @ApiParam("设备ID") @RequestParam(required = false) String equipmentId,
            @ApiParam("测试类型") @RequestParam(required = false) String testType,
            @ApiParam("测试结果") @RequestParam(required = false) String testResult) {
        return equipmentTestService.findByPage(pageNum, pageSize, equipmentId, testType, testResult);
    }

    @Anonymous
    @GetMapping("/test/{id}")
    @ApiOperation(value = "查询设备测试记录详情")
    public AjaxResult testGetById(@PathVariable String id) {
        EquipmentTest entity = equipmentTestService.getById(id);
        if (entity == null) {
            return AjaxResult.error("该测试记录不存在");
        }
        return AjaxResult.success(entity);
    }

    @Anonymous
    @PostMapping("/test")
    @ApiOperation(value = "新增设备测试记录")
    @Log(title = "新增设备测试记录", businessType = BusinessType.INSERT)
    @Transactional
    public AjaxResult testSave(@RequestBody EquipmentTest entity) {
        boolean saved = equipmentTestService.saveWithCheck(entity);
        return saved ? AjaxResult.success("新增成功") : AjaxResult.error("新增失败");
    }

    @Anonymous
    @PutMapping("/test")
    @ApiOperation(value = "修改设备测试记录")
    @Log(title = "修改设备测试记录", businessType = BusinessType.UPDATE)
    @Transactional
    public AjaxResult testUpdate(@RequestBody EquipmentTest entity) {
        boolean updated = equipmentTestService.updateWithCheck(entity);
        return updated ? AjaxResult.success("修改成功") : AjaxResult.error("修改失败");
    }

    @Anonymous
    @DeleteMapping("/test/{id}")
    @ApiOperation(value = "删除设备测试记录")
    @Log(title = "删除设备测试记录", businessType = BusinessType.DELETE)
    @Transactional
    public AjaxResult testDelete(@PathVariable String id) {
        boolean deleted = equipmentTestService.removeWithCheck(id);
        return deleted ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败");
    }

    @Anonymous
    @DeleteMapping("/test/batch")
    @ApiOperation(value = "批量删除设备测试记录")
    @Log(title = "批量删除设备测试记录", businessType = BusinessType.DELETE)
    @Transactional
    public AjaxResult testDeleteBatch(@RequestBody List<String> ids) {
        boolean deleted = equipmentTestService.removeBatchWithCheck(ids);
        return deleted ? AjaxResult.success("批量删除成功") : AjaxResult.error("批量删除失败");
    }

    // ==================== 设备-监控点关联 ====================

    @Anonymous
    @GetMapping("/pointRel/page")
    @ApiOperation(value = "设备-监控点关联分页查询")
    public Page<EquipmentPointRel> pointRelPage(
            @ApiParam("页码") @RequestParam(defaultValue = "1") long pageNum,
            @ApiParam("每页数量") @RequestParam(defaultValue = "10") long pageSize,
            @ApiParam("设备ID") @RequestParam(required = false) String equipmentId,
            @ApiParam("监测点ID") @RequestParam(required = false) String pointId) {
        return equipmentPointRelService.findByPage(pageNum, pageSize, equipmentId, pointId);
    }

    @Anonymous
    @GetMapping("/pointRel/byEquipment/{equipmentId}")
    @ApiOperation(value = "根据设备ID查询关联监测点")
    public AjaxResult pointRelByEquipment(@PathVariable String equipmentId) {
        return AjaxResult.success(equipmentPointRelService.getByEquipmentId(equipmentId));
    }

    @Anonymous
    @GetMapping("/pointRel/byPoint/{pointId}")
    @ApiOperation(value = "根据监测点ID查询关联设备")
    public AjaxResult pointRelByPoint(@PathVariable String pointId) {
        return AjaxResult.success(equipmentPointRelService.getByPointId(pointId));
    }

    @Anonymous
    @PostMapping("/pointRel")
    @ApiOperation(value = "新增设备-监控点关联")
    @Log(title = "新增设备-监控点关联", businessType = BusinessType.INSERT)
    @Transactional
    public AjaxResult pointRelSave(@RequestBody EquipmentPointRel entity) {
        boolean saved = equipmentPointRelService.saveWithCheck(entity);
        return saved ? AjaxResult.success("新增成功") : AjaxResult.error("新增失败");
    }

    @Anonymous
    @PostMapping("/pointRel/batch")
    @ApiOperation(value = "批量新增设备-监控点关联")
    @Log(title = "批量新增设备-监控点关联", businessType = BusinessType.INSERT)
    @Transactional
    public AjaxResult pointRelSaveBatch(@RequestBody List<EquipmentPointRel> entityList) {
        if (CollectionUtils.isEmpty(entityList)) {
            return AjaxResult.error("批量新增失败：关联列表不能为空");
        }
        boolean saved = equipmentPointRelService.saveBatchWithCheck(entityList);
        return saved ? AjaxResult.success("批量新增成功") : AjaxResult.error("批量新增失败");
    }

    @Anonymous
    @DeleteMapping("/pointRel/{id}")
    @ApiOperation(value = "删除设备-监控点关联")
    @Log(title = "删除设备-监控点关联", businessType = BusinessType.DELETE)
    @Transactional
    public AjaxResult pointRelDelete(@PathVariable String id) {
        boolean deleted = equipmentPointRelService.removeWithCheck(id);
        return deleted ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败");
    }

    @Anonymous
    @DeleteMapping("/pointRel/batch")
    @ApiOperation(value = "批量删除设备-监控点关联")
    @Log(title = "批量删除设备-监控点关联", businessType = BusinessType.DELETE)
    @Transactional
    public AjaxResult pointRelDeleteBatch(@RequestBody List<String> ids) {
        boolean deleted = equipmentPointRelService.removeBatchWithCheck(ids);
        return deleted ? AjaxResult.success("批量删除成功") : AjaxResult.error("批量删除失败");
    }
}
