package com.ruoyi.web.bst;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.bst.areaJoin.domain.enums.AreaJoinPermission;
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.FaultVerifyDTO;
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.enums.LogBizType;
import com.ruoyi.common.utils.poi.ExcelUtil;

import io.swagger.annotations.ApiOperation;

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

    @Autowired
    private FaultValidator faultValidator;

    /**
     * 查询故障列表
     */
    @PreAuthorize("@ss.hasPermi('bst:fault:list')")
    @GetMapping("/list")
    public TableDataInfo list(FaultQuery query)
    {
        startPage();
        startOrderBy();
        query.setScope(true);
        query.addAreaPermission(AreaJoinPermission.FAULT_VIEW.getCode());
        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)
    {
        query.addAreaPermission(AreaJoinPermission.FAULT_VIEW.getCode());
        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, bizIdName = "arg0", bizType = LogBizType.FAULT)
    @PostMapping
    public AjaxResult add(@RequestBody Fault fault)
    {
        return toAjax(faultService.insertFault(fault));
    }

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

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



    /**
     * 审核待处理故障申报
     * @param dto
     * @return
     */
    @PreAuthorize("@ss.hasPermi('bst:fault:edit')")
    @Log(title = "审核待处理故障申报", businessType = BusinessType.UPDATE, bizIdName = "arg0", bizType = LogBizType.FAULT)
    @ApiOperation("审核待处理故障申报")
    @PutMapping("/handle")
    public AjaxResult handle(@RequestBody @Validated FaultVerifyDTO dto) {
        if (!faultValidator.canEdit(dto.getId())){
            return AjaxResult.error("您没有权限处理id为" + dto.getId() + "的故障申报");
        }
        return toAjax(faultService.verify(dto));
    }

    /**
     * 完成维修中故障申报
     * @param dto
     * @return
     */
    @PreAuthorize("@ss.hasPermi('bst:fault:edit')")
    @Log(title = "完成维修中故障申报", businessType = BusinessType.UPDATE, bizIdName = "arg0", bizType = LogBizType.FAULT)
    @ApiOperation("完成维修中故障申报")
    @PutMapping("/complete")
    public AjaxResult complete(@RequestBody @Validated FaultVerifyDTO dto) {
        if (!faultValidator.canEdit(dto.getId())){
            return AjaxResult.error("您没有权限处理id为" + dto.getId() + "的故障申报");
        }
        return toAjax(faultService.complete(dto));
    }
}
