package com.muyu.enterpise.controller;

import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.core.web.page.TableDataInfo;
import com.muyu.common.security.annotation.RequiresPermissions;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.domain.SysCarFault;
import com.muyu.enterpise.service.ISysCarFaultService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 车辆故障Controller
 * @author Yang鹏
 * @date 2024-09-18
 */
@RestController
@RequestMapping("/fault")
@Tag(name = "查询车辆故障模块",description = "查询车辆故障模块")
public class SysCarFaultController extends BaseController
{
    @Resource
    private ISysCarFaultService sysCarFaultService;

    /**
     * 查询车辆故障列表
     */
    @GetMapping("/list")
    public Result<TableDataInfo<SysCarFault>> list(SysCarFault sysCarFault)
    {
        startPage();
        List<SysCarFault> list = sysCarFaultService.selectSysCarFaultList(sysCarFault);
        return getDataTable(list);
    }

    /**
     * 导出车辆故障列表
     */
    @RequiresPermissions("syscarfault:fault:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysCarFault sysCarFault)
    {
        List<SysCarFault> list = sysCarFaultService.selectSysCarFaultList(sysCarFault);
        ExcelUtil<SysCarFault> util = new ExcelUtil<SysCarFault>(SysCarFault.class);
        util.exportExcel(response, list, "车辆故障数据");
    }

    /**
     * 获取车辆故障详细信息
     */
    @RequiresPermissions("syscarfault:fault:query")
    @GetMapping(value = "/{id}")
    public Result<List<SysCarFault>> getInfo(@PathVariable("id") Long id)
    {
        return success(sysCarFaultService.selectSysCarFaultById(id));
    }

    /**
     * 新增车辆故障
     */
    @RequiresPermissions("syscarfault:fault:add")
    @PostMapping
    public Result<Integer> add(
            @Validated @RequestBody SysCarFault sysCarFault)
    {
        if (sysCarFaultService.checkIdUnique(sysCarFault)) {
            return error("新增 车辆故障 '" + sysCarFault + "'失败，车辆故障已存在");
        }
        sysCarFault.setCreateBy(SecurityUtils.getUsername());
        return toAjax(sysCarFaultService.save(sysCarFault));
    }

    /**
     * 修改车辆故障
     */
    @RequiresPermissions("syscarfault:fault:edit")
    @PutMapping
    public Result<Integer> edit(
            @Validated @RequestBody SysCarFault sysCarFault)
    {
        if (!sysCarFaultService.checkIdUnique(sysCarFault)) {
            return error("修改 车辆故障 '" + sysCarFault + "'失败，车辆故障不存在");
        }
        sysCarFault.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(sysCarFaultService.updateById(sysCarFault));
    }

    /**
     * 删除车辆故障
     */
    @RequiresPermissions("syscarfault:fault:remove")
	@DeleteMapping("/{ids}")
    public Result<Integer> remove(@PathVariable("ids") Long[] ids)
    {
        sysCarFaultService.removeBatchByIds(Arrays.asList(ids));
        return success();
    }
}
