package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.ss.equipment.domain.Equipment;
import com.ruoyi.ss.equipment.domain.EquipmentQuery;
import com.ruoyi.ss.equipment.domain.EquipmentVO;
import com.ruoyi.ss.equipment.domain.unified.UnifiedEquipment;
import com.ruoyi.ss.equipment.domain.unified.UnifiedEquipmentQueryDTO;
import com.ruoyi.ss.equipment.service.IEquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 设施Controller
 *
 * @author qzz
 * @date 2024-12-21
 */
@RestController
@RequestMapping("/system/equipment")
public class EquipmentController extends BaseController
{
    @Autowired
    private IEquipmentService equipmentService;

    /**
     * 查询设施列表
     */
    @PreAuthorize("@ss.hasPermi('system:equipment:list')")
    @GetMapping("/list")
    public TableDataInfo list(EquipmentQuery query)
    {
        startPage();
        startOrderBy();
        List<EquipmentVO> list = equipmentService.selectEquipmentList(query);
        return getDataTable(list);
    }

    /**
     * 查询统一设施列表（视图）
     */
    @PreAuthorize("@ss.hasPermi('system:equipment:list')")
    @GetMapping("/unifiedList")
    public TableDataInfo unifiedList(UnifiedEquipmentQueryDTO query)
    {
        startPage();
        startOrderBy();
        List<UnifiedEquipment> list = equipmentService.selectUnifiedEquipmentList(query);
        return getDataTable(list);
    }

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

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

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

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

    /**
     * 删除设施
     */
    @PreAuthorize("@ss.hasPermi('system:equipment:remove')")
    @Log(title = "设施", businessType = BusinessType.DELETE)
	@DeleteMapping("/{equipmentIds}")
    public AjaxResult remove(@PathVariable Long[] equipmentIds)
    {
        return toAjax(equipmentService.deleteEquipmentByEquipmentIds(equipmentIds));
    }
}
