package com.pm.web.controller.device;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
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.pm.common.annotation.Log;
import com.pm.common.core.controller.BaseController;
import com.pm.common.core.domain.AjaxResult;
import com.pm.common.enums.BusinessType;
import com.pm.common.config.PageData;
import com.pm.common.utils.poi.ExcelUtilPageData;
import com.pm.device.service.IIbmsDeviceService;
import com.pm.common.utils.poi.ExcelUtil;
import com.pm.common.core.page.TableDataInfo;

/**
 * 设备基础信息Controller
 *
 * @author lxf
 * @date 2025-04-29
 */
@RestController
@RequestMapping("/device/basic")
public class IbmsDeviceController extends BaseController
{
    @Autowired
    private IIbmsDeviceService ibmsDeviceService;

    /**
     * 查询设备基础信息列表
     */
    @PreAuthorize("@ss.hasPermi('device:basic:list')")
    @GetMapping("/list")
    public TableDataInfo list()
    {
        PageData pd = this.getPageData();
        startPage();
        List<PageData> list = ibmsDeviceService.selectIbmsDeviceList(pd);
        return getDataTable(list);
    }

    /**
     * 导出设备基础信息列表
     */
    @PreAuthorize("@ss.hasPermi('device:basic:export')")
    @Log(title = "设备基础信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response)
    {
        try {
            PageData pd = this.getPageData();
            List<PageData> list = ibmsDeviceService.selectIbmsDeviceList(pd);
            String filename = "设备基础信息数据";
            String[] titles = {
                    "设备ID,id",
                    "设备编码,deviceCode",
                    "设备名称,deviceName",
                    "设备类型(空调/新风/配电等),deviceType",
                    "子系统类型,subsystemType",
                    "所属空间ID,spaceId",
                    "所属空间名称,spaceName",
                    "品牌,brand",
                    "型号,model",
                    "序列号,serialNumber",
                    "生产日期,manufactureDate",
                    "安装日期,installDate",
                    "保修期(月),warrantyPeriod",
                    "状态(0-禁用,1-正常,2-故障),status",
                    "是否在线,isOnline",
                    "设备描述,description"
            };
            ExcelUtilPageData.exportXLSX(response, null, null, filename, titles, list);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 获取设备基础信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('device:basic:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(ibmsDeviceService.selectIbmsDeviceById(id));
    }

    /**
     * 新增设备基础信息
     */
    @PreAuthorize("@ss.hasPermi('device:basic:add')")
    @Log(title = "设备基础信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PageData pd)
    {
        return toAjax(ibmsDeviceService.insertIbmsDevice(pd));
    }

    /**
     * 修改设备基础信息
     */
    @PreAuthorize("@ss.hasPermi('device:basic:edit')")
    @Log(title = "设备基础信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PageData pd)
    {
        return toAjax(ibmsDeviceService.updateIbmsDevice(pd));
    }

    /**
     * 删除设备基础信息
     */
    @PreAuthorize("@ss.hasPermi('device:basic:remove')")
    @Log(title = "设备基础信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ibmsDeviceService.deleteIbmsDeviceByIds(ids));
    }
}
