package com.zksy.web.controller.WaterSupply.DeviceLedger;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zksy.base.domain.vo.DeviceGisVO;
import com.zksy.base.domain.vo.DeviceTypeStatisticsVO;
import com.zksy.base.service.WaterSupplyDeviceService;
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.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/waterSupply/device/ledger")
@Api(tags = "供水设备-设备台账管理")
public class DeviceLedgerController {

    @Autowired
    private WaterSupplyDeviceService waterSupplyDeviceService;

    @GetMapping("/typeStatistics")
    @ApiOperation("设备分类统计（树形结构）")
    @PreAuthorize("@ss.hasPermi('waterSupply:device:list')")
    public AjaxResult getTypeStatistics() {
        List<DeviceTypeStatisticsVO> list = waterSupplyDeviceService.getDeviceTypeStatistics();
        return AjaxResult.success(list);
    }

    @GetMapping("/page")
    @ApiOperation("设备台账分页查询")
    @PreAuthorize("@ss.hasPermi('waterSupply:device:list')")
    public AjaxResult getDevicePage(
            @ApiParam("页码") @RequestParam(defaultValue = "1") long pageNum,
            @ApiParam("每页数量") @RequestParam(defaultValue = "10") long pageSize,
            @ApiParam("设备名称") @RequestParam(required = false) String equipmentName,
            @ApiParam("设备编码") @RequestParam(required = false) String equipmentCode,
            @ApiParam("设备类型ID") @RequestParam(required = false) String equipmentTypeId,
            @ApiParam("设备状态") @RequestParam(required = false) Integer status) {
        Page<DeviceGisVO> page = waterSupplyDeviceService.getDeviceLedgerPage(
                pageNum, pageSize, equipmentName, equipmentCode, equipmentTypeId, status);
        return AjaxResult.success(page);
    }

    @GetMapping("/gisPoints")
    @ApiOperation("获取所有设备GIS点位")
    @PreAuthorize("@ss.hasPermi('waterSupply:device:list')")
    public AjaxResult getGisPoints(
            @ApiParam("设备类型ID") @RequestParam(required = false) String equipmentTypeId) {
        List<DeviceGisVO> list = waterSupplyDeviceService.getAllDeviceGisPoints(equipmentTypeId);
        return AjaxResult.success(list);
    }

    @PostMapping("/export")
    @ApiOperation("批量导出设备台账")
    @Log(title = "导出设备台账", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('waterSupply:device:export')")
    public AjaxResult exportDeviceLedger(
            @ApiParam("设备名称") @RequestParam(required = false) String equipmentName,
            @ApiParam("设备编码") @RequestParam(required = false) String equipmentCode,
            @ApiParam("设备类型ID") @RequestParam(required = false) String equipmentTypeId,
            @ApiParam("设备状态") @RequestParam(required = false) Integer status) {
        log.info("导出设备台账：name={}, code={}, typeId={}, status={}",
                equipmentName, equipmentCode, equipmentTypeId, status);
        return AjaxResult.success("导出功能待对接EasyExcel");
    }
}
