package com.dcs.hnyz.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.dcs.hnyz.domain.vo.EquipmentFormVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dcs.common.annotation.Log;
import com.dcs.common.core.controller.BaseController;
import com.dcs.common.core.domain.AjaxResult;
import com.dcs.common.enums.BusinessType;
import com.dcs.hnyz.domain.Equipment;
import com.dcs.hnyz.service.IEquipmentService;
import com.dcs.common.utils.poi.ExcelUtil;
import com.dcs.common.core.page.TableDataInfo;

/**
 * 设备管理Controller
 * 
 * @author hmy
 * @date 2025-04-22
 */
@Api("设备管理")
@RestController
@RequestMapping("/hnyz/equipment")
public class EquipmentController extends BaseController
{
    @Autowired
    private IEquipmentService equipmentService;

    /**
     * 查询设备管理列表
     */
    @PreAuthorize("@ss.hasPermi('hnyz:equipment:list')")
    @GetMapping("/list")
    @ApiOperation("查询设备管理列表")
    public TableDataInfo list(@ApiParam("设备对象") Equipment equipment)
    {
        startPage();
        List<EquipmentFormVO> list = equipmentService.selectEquipmentForm(equipment);
        return getDataTable(list);
    }

    /**
     * 导出设备管理列表
     */
    @PreAuthorize("@ss.hasPermi('hnyz:equipment:export')")
    @Log(title = "设备管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Equipment equipment)
    {
        List<Equipment> list = equipmentService.selectEquipmentList(equipment);
        ExcelUtil<Equipment> util = new ExcelUtil<Equipment>(Equipment.class);
        util.exportExcel(response, list, "设备管理数据");
    }

    /**
     * 获取设备管理详细信息
     */
    @ApiOperation("获取设备管理详细信息")
    @PreAuthorize("@ss.hasPermi('hnyz:equipment:query')")
    @GetMapping(value = "/{equipmentId}")
    public AjaxResult getInfo(@ApiParam("设备id") @PathVariable("equipmentId") Long equipmentId)
    {
        return success(equipmentService.selectEquipmentByEquipmentId(equipmentId));
    }

    /**
     * 新增设备管理
     */
    @PreAuthorize("@ss.hasPermi('hnyz:equipment:add')")
    @Log(title = "设备管理", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增设备管理")
    public AjaxResult add(@RequestBody Equipment equipment)
    {
        return toAjax(equipmentService.insertEquipment(equipment));
    }

    /**
     * 修改设备管理
     */
    @ApiOperation("修改设备管理")
    @PreAuthorize("@ss.hasPermi('hnyz:equipment:edit')")
    @Log(title = "设备管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Equipment equipment)
    {
        return toAjax(equipmentService.updateEquipment(equipment));
    }

    /**
     * 删除设备管理
     */
    @ApiOperation("删除设备管理")
    @PreAuthorize("@ss.hasPermi('hnyz:equipment:remove')")
    @Log(title = "设备管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{equipmentIds}")
    public AjaxResult remove(@ApiParam("设备id数组") @PathVariable Long[] equipmentIds)
    {
        return toAjax(equipmentService.deleteEquipmentByEquipmentIds(equipmentIds));
    }

    /**
     * 根据流程获取设备列表（用于下拉框显示）
     */
    @ApiOperation("根据流程获取设备列表（用于下拉框显示）")
    @GetMapping("/getEquipmentList")
    public AjaxResult getEquipmentList(@ApiParam("设备对象") Equipment equipment){
        List<Equipment> list = equipmentService.selectEquipmentList(equipment);
        return AjaxResult.success(list);
    }

    /**
     * 修改启用状态
     */
    @PreAuthorize("@ss.hasPermi('hnyz:equipment:edit')")
    @Log(title = "设备管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult updateStatus(@RequestBody Equipment equipment){
        return toAjax(equipmentService.updateEquipment(equipment));
    }

    /**
     * 获取数据图表页面所需设备名list（树状）
     * @Param dataType 数据类型 bool,data,string
     */
    @PostMapping("/getEquipmentTreeByDataType")
    @ApiOperation(value = "获取数据图表页面所需设备名list（树状）")
    public AjaxResult getEquipmentNameList(@ApiParam("数据类型 bool,data,string") String dataType) {
        return success(equipmentService.getEquipmentNameList(dataType));
    }

    /**
     * 获取传感器相关的设备类型列表
     */
    @GetMapping ("/getEquipmentSensorTree")
    @ApiOperation(value = "获取数据图表页面所需设备名list（树状）")
    public AjaxResult getEquipmentSensorTree() {
        return success(equipmentService.getEquipmentSensorTree());
    }


}
