package org.eco.als.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.eco.als.domain.bo.FaultDiagnosisResultBo;
import org.eco.als.domain.vo.FaultDiagnosisResultVo;
import org.eco.als.service.IFaultDiagnosisResultService;
import org.eco.common.core.core.domain.CommonResult;
import org.eco.common.core.core.page.PageResult;
import org.eco.common.excel.utils.ExcelUtil;
import org.eco.common.log.annotation.Log;
import org.eco.common.log.enums.BusinessType;
import org.eco.common.web.annotation.RepeatSubmit;
import org.eco.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 故障诊断结果Controller
 *
 * @author wgk
 * @date 2024-11-25
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/als/faultDiagnosisResult")
public class FaultDiagnosisResultController extends BaseController {
    @Resource
    private IFaultDiagnosisResultService faultDiagnosisResultService;

    /**
     * 查询故障诊断结果列表
     */
    @SaCheckPermission("als:faultDiagnosisResult:list")
    @GetMapping("/list")
    public CommonResult<PageResult<FaultDiagnosisResultVo>> list(FaultDiagnosisResultBo faultDiagnosisResultBo) {
        return CommonResult.success(faultDiagnosisResultService.selectPage(faultDiagnosisResultBo));
    }

    /**
     * 导出故障诊断结果列表
     */
    @SaCheckPermission("als:faultDiagnosisResult:export")
    @Log(title = "故障诊断结果", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FaultDiagnosisResultBo faultDiagnosisResultBo) {
        List<FaultDiagnosisResultVo> list = faultDiagnosisResultService.selectList(faultDiagnosisResultBo);
        ExcelUtil.exportExcel(list, "故障诊断结果", FaultDiagnosisResultVo.class, response);
    }

    /**
     * 获取故障诊断结果详细信息
     */
    @SaCheckPermission("als:faultDiagnosisResult:query")
    @GetMapping(value = "/{id}")
    public CommonResult<FaultDiagnosisResultVo> getInfo(@PathVariable Long id) {
        return CommonResult.success(faultDiagnosisResultService.selectById(id));
    }

    /**
     * 新增故障诊断结果
     */
    @SaCheckPermission("als:faultDiagnosisResult:add")
    @Log(title = "故障诊断结果", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public CommonResult<Void> add(@Validated @RequestBody FaultDiagnosisResultBo faultDiagnosisResultBo) {
        boolean inserted = faultDiagnosisResultService.insert(faultDiagnosisResultBo);
        if (!inserted) {
            return CommonResult.fail("新增故障诊断结果记录失败！");
        }
        return CommonResult.success();
    }

    /**
     * 修改故障诊断结果
     */
    @SaCheckPermission("als:faultDiagnosisResult:edit")
    @Log(title = "故障诊断结果", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping
    public CommonResult<Void> edit(@Validated @RequestBody FaultDiagnosisResultBo faultDiagnosisResultBo) {
        boolean updated = faultDiagnosisResultService.update(faultDiagnosisResultBo);
        if (!updated) {
            return CommonResult.fail("修改故障诊断结果记录失败!");
        }
        return CommonResult.success();
    }

    /**
     * 删除故障诊断结果
     */
    @SaCheckPermission("als:faultDiagnosisResult:remove")
    @Log(title = "故障诊断结果", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public CommonResult<Void> remove(@PathVariable Long[] ids) {
        boolean deleted = faultDiagnosisResultService.deleteByIds(ids);
        if (!deleted) {
            return CommonResult.fail("删除故障诊断结果记录失败!");
        }
        return CommonResult.success();
    }
}
