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.topology.service.IItemTopologyService;
import com.pm.common.core.page.TableDataInfo;

/**
 * 项目拓扑Controller
 *
 * @author pm
 * @date 2025-04-24
 */
@RestController
@RequestMapping("/device/topology")
public class ItemTopologyController extends BaseController
{
    @Autowired
    private IItemTopologyService itemTopologyService;

    /**
     * 查询项目拓扑列表
     */
    @PreAuthorize("@ss.hasPermi('device:topology:list')")
    @GetMapping("/list")
    public TableDataInfo list()
    {
        PageData pd = this.getPageData();
        startPage();
        List<PageData> list = itemTopologyService.selectItemTopologyList(pd);
        return getDataTable(list);
    }

    /**
     * 导出项目拓扑列表
     */
    @PreAuthorize("@ss.hasPermi('device:topology:export')")
    @Log(title = "项目拓扑", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response)
    {
        try {
            PageData pd = this.getPageData();
            List<PageData> list = itemTopologyService.selectItemTopologyList(pd);
            String filename = "项目拓扑数据";
            String[] titles = {
                "项目id,itemId",
                "父项目id,parentId",
                "祖级列表,ancestors",
                "项目名称,itemName",
                "显示顺序,orderNum",
                "部门状态（0正常 1停用）,status",
                "删除标志（0代表存在 2代表删除）,delFlag",
                "项目类型,itemType",
                "设备ids,deviceId"
            };
            ExcelUtilPageData.exportXLSX(response, null, null, filename, titles, list);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 获取项目拓扑详细信息
     */
    @PreAuthorize("@ss.hasPermi('device:topology:query')")
    @GetMapping(value = "/{itemId}")
    public AjaxResult getInfo(@PathVariable("itemId") String itemId)
    {
        return success(itemTopologyService.selectItemTopologyByItemId(itemId));
    }

    /**
     * 新增项目拓扑
     */
    @PreAuthorize("@ss.hasPermi('device:topology:add')")
    @Log(title = "项目拓扑", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PageData pd)
    {
        return toAjax(itemTopologyService.insertItemTopology(pd));
    }

    /**
     * 修改项目拓扑
     */
    @PreAuthorize("@ss.hasPermi('device:topology:edit')")
    @Log(title = "项目拓扑", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PageData pd)
    {
        return toAjax(itemTopologyService.updateItemTopology(pd));
    }

    /**
     * 删除项目拓扑
     */
    @PreAuthorize("@ss.hasPermi('device:topology:remove')")
    @Log(title = "项目拓扑", businessType = BusinessType.DELETE)
	@DeleteMapping("/{itemIds}")
    public AjaxResult remove(@PathVariable Long[] itemIds)
    {
        return toAjax(itemTopologyService.deleteItemTopologyByItemIds(itemIds));
    }
}
