package com.fms.system.controller;

import com.fms.common.annotation.Log;
import com.fms.common.core.controller.BaseController;
import com.fms.common.core.domain.AjaxResult;
import com.fms.common.core.page.TableDataInfo;
import com.fms.common.enums.BusinessType;
import com.fms.common.utils.poi.ExcelUtil;
import com.fms.system.domain.Failure;
import com.fms.system.service.IFailureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 故障记录Controller
 * 
 * @author Allen
 * @date 2024-06-10
 */
@RestController
@RequestMapping("/system/failure")
public class FailureController extends BaseController
{
    @Autowired
    private IFailureService failureService;

    /**
     * 查询故障记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:failure:list')")
    @GetMapping("/list")
    public TableDataInfo list(Failure failure)
    {
        startPage();
        List<Failure> list = failureService.selectFailureList(failure);
        return getDataTable(list);
    }

    /**
     * 导出故障记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:failure:export')")
    @Log(title = "故障记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Failure failure)
    {
        List<Failure> list = failureService.selectFailureList(failure);
        ExcelUtil<Failure> util = new ExcelUtil<Failure>(Failure.class);
        util.exportExcel(response, list, "故障记录数据");
    }

    /**
     * 获取故障记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:failure:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(failureService.selectFailureById(id));
    }

    /**
     * 新增故障记录
     */
    @PreAuthorize("@ss.hasPermi('system:failure:add')")
    @Log(title = "故障记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Failure failure)
    {
        return toAjax(failureService.insertFailure(failure));
    }

    /**
     * 修改故障记录
     */
    @PreAuthorize("@ss.hasPermi('system:failure:edit')")
    @Log(title = "故障记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Failure failure)
    {
        return toAjax(failureService.updateFailure(failure));
    }

    /**
     * 删除故障记录
     */
    @PreAuthorize("@ss.hasPermi('system:failure:remove')")
    @Log(title = "故障记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(failureService.deleteFailureByIds(ids));
    }

    @Log(title = "故障记录", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:failure:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Failure> util = new ExcelUtil<>(Failure.class);
        List<Failure> failureList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = failureService.importData(failureList, updateSupport, operName);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Failure> util = new ExcelUtil<>(Failure.class);
        util.importTemplateExcel(response, "故障数据");
    }

    @PreAuthorize("@ss.hasPermi('system:failure:export')")
    @Log(title = "故障报告", businessType = BusinessType.EXPORT)
    @GetMapping("/generateReport")
    public AjaxResult generateReport(String reportMonth, String major){
        logger.info("generateReport: reportMonth:{}, major:{}" ,reportMonth, major);
        return AjaxResult.success(failureService.generateReport(reportMonth, major));
    }
    @PreAuthorize("@ss.hasPermi('system:failure:query')")
    @GetMapping("/getMajorOption")
    public AjaxResult getMajorOption(){
        return AjaxResult.success(failureService.getMajorOption());
    }
}
