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.DailyFaultRecord;
import org.dromara.ems.info.service.IDailyFaultRecordService;
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/dailyFaultRecord")
public class DailyFaultRecordController extends EmsBaseController
{
    
    private final IDailyFaultRecordService dailyFaultRecordService;

    /**
     * 查询日常故障记录列表
     */
    @SaCheckPermission("ems/info:dailyFaultRecord:list")
    @GetMapping("/list")
    public TableDataInfo list(DailyFaultRecord dailyFaultRecord)
    {
        startPage();
        List<DailyFaultRecord> list = dailyFaultRecordService.selectDailyFaultRecordList(dailyFaultRecord);
        return getDataTable(list);
    }

    /**
     * 导出日常故障记录列表
     */
    @SaCheckPermission("ems/info:dailyFaultRecord:export")
    @Log(title = "日常故障记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DailyFaultRecord dailyFaultRecord)
    {
        List<DailyFaultRecord> list = dailyFaultRecordService.selectDailyFaultRecordList(dailyFaultRecord);
        ExcelUtil.exportExcel(list, "日常故障记录数据", DailyFaultRecord.class, response);
    }

    /**
     * 获取日常故障记录详细信息
     */
    @SaCheckPermission("ems/info:dailyFaultRecord:query")
    @GetMapping(value = "/{id}")
    public R<?> getInfo(@PathVariable("id") Long id)
    {
        return success(dailyFaultRecordService.selectDailyFaultRecordById(id));
    }

    /**
     * 新增日常故障记录
     */
    @SaCheckPermission("ems/info:dailyFaultRecord:add")
    @Log(title = "日常故障记录", businessType = BusinessType.INSERT)
    @PostMapping
    public R<?> add(@RequestBody DailyFaultRecord dailyFaultRecord)
    {
        return toAjax(dailyFaultRecordService.insertDailyFaultRecord(dailyFaultRecord));
    }

    /**
     * 修改日常故障记录
     */
    @SaCheckPermission("ems/info:dailyFaultRecord:edit")
    @Log(title = "日常故障记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<?> edit(@RequestBody DailyFaultRecord dailyFaultRecord)
    {
        return toAjax(dailyFaultRecordService.updateDailyFaultRecord(dailyFaultRecord));
    }

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

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

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