package org.dromara.ems.info.controller;

import org.dromara.common.log.annotation.Log;
import org.dromara.ems.common.web.EmsBaseController;
import org.dromara.common.core.domain.R;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.ems.info.domain.FaultHandlingRecord;
import org.dromara.ems.info.service.IFaultHandlingRecordService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * 故障处置记录Controller
 * 
 * @author Yinq
 * @date 2025-06-05
 */
@RestController
@RequiredArgsConstructor

@RequestMapping("/ems/info/faultHandlingRecord")
public class FaultHandlingRecordController extends EmsBaseController
{
    
    private final IFaultHandlingRecordService faultHandlingRecordService;

    /**
     * 查询故障处置记录列表
     */
    @SaCheckPermission("ems/info:faultHandlingRecord:list")
    @GetMapping("/list")
    public TableDataInfo list(FaultHandlingRecord faultHandlingRecord)
    {
        startPage();
        List<FaultHandlingRecord> list = faultHandlingRecordService.selectFaultHandlingRecordList(faultHandlingRecord);
        return getDataTable(list);
    }

    /**
     * 导出故障处置记录列表
     */
    @SaCheckPermission("ems/info:faultHandlingRecord:export")
    @Log(title = "故障处置记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FaultHandlingRecord faultHandlingRecord)
    {
        List<FaultHandlingRecord> list = faultHandlingRecordService.selectFaultHandlingRecordList(faultHandlingRecord);
        ExcelUtil.exportExcel(list, "故障处置记录数据", FaultHandlingRecord.class, response);
    }

    /**
     * 获取故障处置记录详细信息
     */
    @SaCheckPermission("ems/info:faultHandlingRecord:query")
    @GetMapping(value = "/{id}")
    public R<?> getInfo(@PathVariable("id") Long id)
    {
        return success(faultHandlingRecordService.selectFaultHandlingRecordById(id));
    }

    /**
     * 新增故障处置记录
     */
    @SaCheckPermission("ems/info:faultHandlingRecord:add")
    @Log(title = "故障处置记录", businessType = BusinessType.INSERT)
    @PostMapping
    public R<?> add(@RequestBody FaultHandlingRecord faultHandlingRecord)
    {
        return toAjax(faultHandlingRecordService.insertFaultHandlingRecord(faultHandlingRecord));
    }

    /**
     * 修改故障处置记录
     */
    @SaCheckPermission("ems/info:faultHandlingRecord:edit")
    @Log(title = "故障处置记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<?> edit(@RequestBody FaultHandlingRecord faultHandlingRecord)
    {
        return toAjax(faultHandlingRecordService.updateFaultHandlingRecord(faultHandlingRecord));
    }

    /**
     * 删除故障处置记录
     */
    @SaCheckPermission("ems/info:faultHandlingRecord:remove")
    @Log(title = "故障处置记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public R<?> remove(@PathVariable Long[] ids)
    {
        return toAjax(faultHandlingRecordService.deleteFaultHandlingRecordByIds(ids));
    }

    /**
     * 导入故障处置记录数据
     */
    @SaCheckPermission("ems/info:faultHandlingRecord:add")
    @Log(title = "故障处置记录", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public R<?> importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        List<FaultHandlingRecord> recordList = ExcelUtil.importExcel(file.getInputStream(), FaultHandlingRecord.class);
        String operName = getUsername();
        String message = faultHandlingRecordService.importFaultHandlingRecord(recordList, updateSupport, operName);
        return R.ok(message);
    }

    /**
     * 获取故障处置记录导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil.exportExcel(new ArrayList<>(), "故障处置记录数据", FaultHandlingRecord.class, response);
    }
}
