package com.ruoyi.web.bst;

import com.ruoyi.bst.fault.domain.Fault;
import com.ruoyi.bst.fault.domain.FaultQuery;
import com.ruoyi.bst.fault.domain.FaultVO;
import com.ruoyi.bst.fault.domain.enums.FaultHandleStatus;
import com.ruoyi.bst.fault.domain.enums.FaultStatus;
import com.ruoyi.bst.fault.service.FaultConverter;
import com.ruoyi.bst.fault.service.FaultService;
import com.ruoyi.bst.fault.service.FaultValidator;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 故障Controller
 *
 * @author ruoyi
 * @date 2025-03-26
 */
@RestController
@RequestMapping("/bst/fault")
public class FaultController extends BaseController
{
    @Autowired
    private FaultService faultService;
    @Autowired
    private FaultConverter faultConverter;
    @Autowired
    private FaultValidator faultValidator;

    /**
     * 查询故障列表
     */
    @PreAuthorize("@ss.hasPermi('bst:fault:list')")
    @GetMapping("/list")
    public TableDataInfo list(FaultQuery query)
    {
        startPage();
        startOrderBy();
        query.setScope(true);
        List<FaultVO> list = faultService.selectFaultList(query);
        return getDataTable(list);
    }

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

    /**
     * 获取故障详细信息
     */
    @PreAuthorize("@ss.hasPermi('bst:fault:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(faultService.selectFaultById(id,true));
    }

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

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

    /**
     * 删除故障信息
     */
    @PreAuthorize("@ss.hasPermi('bst:fault:remove')")
    @Log(title = "故障", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(faultService.deleteFaultByIds(ids));
    }



    /**
     * 处理或者驳回申报信息
     * @param fault
     * @return
     */
    @PreAuthorize("@ss.hasPermi('bst:fault:edit')")
    @Log(title = "故障", businessType = BusinessType.UPDATE)
    @ApiOperation("处理申报信息")
    @PutMapping("/handle")
    public AjaxResult update(@RequestBody Fault fault) {
        if (!faultValidator.canEdit(fault.getId())){
            return AjaxResult.error("您没有权限修改id为" + fault.getId() + "的故障申报");
        }
        faultService.updateFault(faultService.handle(fault));
        return success();
    }

}
