package com.ruoyi.sim.controller;

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

import com.ruoyi.sim.service.impl.FaultService;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.sim.domain.Fault;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 故障Controller
 *
 * @author tom
 * @date 2024-12-15
 */
@RestController
@RequestMapping("/sim/fault")
public class FaultController extends BaseController {
    @Autowired
    private FaultService faultService;

    /**
     * 查询故障列表
     */
    @PreAuthorize("@ss.hasPermi('sim:fault:list')")
    @GetMapping("/list")
    public TableDataInfo list(Fault fault) {
        startPage();
        List<Fault> list = faultService.selectFaultList(fault);
        return getDataTable(list);
    }

    /**
     * 导出故障列表
     */
    // @PreAuthorize("@ss.hasPermi('sim:fault:export')")
    @Log(title = "故障", businessType = BusinessType.EXPORT)
    // @PostMapping("/export")
    public void export(HttpServletResponse response, Fault fault) {
        List<Fault> list = faultService.selectFaultList(fault);
        ExcelUtil<Fault> util = new ExcelUtil<Fault>(Fault.class);
        util.exportExcel(response, list, "故障数据");
    }

    /**
     * 获取故障详细信息
     */
    @PreAuthorize("@ss.hasPermi('sim:fault:query')")
    @GetMapping(value = "/{faultId}")
    public AjaxResult getInfo(@PathVariable("faultId") String faultId) {
        return success(faultService.selectFaultByFaultId(faultId));
    }

    /**
     * 新增故障
     */
    // @PreAuthorize("@ss.hasPermi('sim:fault:add')")
    @Log(title = "故障", businessType = BusinessType.INSERT)
    // @PostMapping
    public AjaxResult add(@RequestBody Fault fault) {
        return toAjax(faultService.insertFault(fault));
    }

    /**
     * 修改故障
     */
    // @PreAuthorize("@ss.hasPermi('sim:fault:edit')")
    @Log(title = "故障", businessType = BusinessType.UPDATE)
    // @PutMapping
    public AjaxResult edit(@RequestBody Fault fault) {
        return toAjax(faultService.updateFault(fault));
    }

    /**
     * 删除故障
     */
    // @PreAuthorize("@ss.hasPermi('sim:fault:remove')")
    @Log(title = "故障", businessType = BusinessType.DELETE)
    // @DeleteMapping("/{faultIds}")
    public AjaxResult remove(@PathVariable String[] faultIds) {
        return toAjax(faultService.deleteFaultByFaultIds(faultIds));
    }

    // -------------------------------- tom add  --------------------------------
    private static final Logger logger = LoggerFactory.getLogger(FaultController.class);

    // @PreAuthorize("@ss.hasPermi('sim:fault:listAllTreeViaSimType')")
    @GetMapping("/listAllTreeViaSimType/{simType}")
    @ApiOperation("通过模拟器类型，查询故障列表。")
    public AjaxResult listAllTreeViaSimType(@PathVariable(value = "simType") String simType) {
        return faultService.listAllTreeStyleBySimType(simType);
    }
}
