package com.ygtx.gxt.controller;

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.ygtx.common.annotation.Log;
import com.ygtx.common.core.controller.BaseController;
import com.ygtx.common.core.domain.AjaxResult;
import com.ygtx.common.enums.BusinessType;
import com.ygtx.gxt.domain.GxtFaultCodes;
import com.ygtx.gxt.service.IGxtFaultCodesService;
import com.ygtx.common.utils.poi.ExcelUtil;
import com.ygtx.common.core.page.TableDataInfo;

/**
 * 故障代码管理Controller
 * 
 * @author ouyj
 * @date 2025-10-29
 */
@RestController
@RequestMapping("/gxt/faultCodes")
public class GxtFaultCodesController extends BaseController
{
    @Autowired
    private IGxtFaultCodesService gxtFaultCodesService;

    /**
     * 查询故障代码管理列表
     */
    @PreAuthorize("@ss.hasPermi('gxt:faultCodes:list')")
    @GetMapping("/list")
    public TableDataInfo list(GxtFaultCodes gxtFaultCodes)
    {
        startPage();
        List<GxtFaultCodes> list = gxtFaultCodesService.selectGxtFaultCodesList(gxtFaultCodes);
        return getDataTable(list);
    }

    /**
     * 导出故障代码管理列表
     */
    @PreAuthorize("@ss.hasPermi('gxt:faultCodes:export')")
    @Log(title = "故障代码管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GxtFaultCodes gxtFaultCodes)
    {
        List<GxtFaultCodes> list = gxtFaultCodesService.selectGxtFaultCodesList(gxtFaultCodes);
        ExcelUtil<GxtFaultCodes> util = new ExcelUtil<GxtFaultCodes>(GxtFaultCodes.class);
        util.exportExcel(response, list, "故障代码管理数据");
    }

    /**
     * 获取故障代码管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('gxt:faultCodes:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(gxtFaultCodesService.selectGxtFaultCodesById(id));
    }

    /**
     * 新增故障代码管理
     */
    @PreAuthorize("@ss.hasPermi('gxt:faultCodes:add')")
    @Log(title = "故障代码管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GxtFaultCodes gxtFaultCodes)
    {
        if (!gxtFaultCodesService.checkFaultCodeUnique(gxtFaultCodes)) {
            return error("新增故障代码'" + gxtFaultCodes.getFaultCode() + "'失败，故障代码已存在");
        }
        return toAjax(gxtFaultCodesService.insertGxtFaultCodes(gxtFaultCodes));
    }

    /**
     * 修改故障代码管理
     */
    @PreAuthorize("@ss.hasPermi('gxt:faultCodes:edit')")
    @Log(title = "故障代码管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GxtFaultCodes gxtFaultCodes)
    {
        if (!gxtFaultCodesService.checkFaultCodeUnique(gxtFaultCodes)) {
            return error("修改故障代码'" + gxtFaultCodes.getFaultCode() + "'失败，故障代码已存在");
        }
        return toAjax(gxtFaultCodesService.updateGxtFaultCodes(gxtFaultCodes));
    }

    /**
     * 删除故障代码管理
     */
    @PreAuthorize("@ss.hasPermi('gxt:faultCodes:remove')")
    @Log(title = "故障代码管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(gxtFaultCodesService.deleteGxtFaultCodesByIds(ids));
    }
}